diff --git a/libguac/include/guacio.h b/libguac/include/guacio.h index e82061ed..ff3a1639 100644 --- a/libguac/include/guacio.h +++ b/libguac/include/guacio.h @@ -123,7 +123,7 @@ GUACIO* guac_open(int fd); * @param i The unsigned int to write. * @return Zero on success, or non-zero if an error occurs while writing. */ -ssize_t guac_write_int(GUACIO* io, unsigned long i); +ssize_t guac_write_int(GUACIO* io, long i); /** * Writes the given string to the given GUACIO object. The data diff --git a/libguac/src/guacio.c b/libguac/src/guacio.c index e984c4fc..7c04054c 100644 --- a/libguac/src/guacio.c +++ b/libguac/src/guacio.c @@ -98,21 +98,31 @@ ssize_t __guac_write(GUACIO* io, const char* buf, int count) { return retval; } -ssize_t guac_write_int(GUACIO* io, unsigned long i) { +ssize_t guac_write_int(GUACIO* io, long i) { char buffer[128]; char* ptr = &(buffer[127]); + int nonneg; + + /* Obtain non-negative value */ + if (i < 0) nonneg = -i; + else nonneg = i; + + /* Generate numeric string */ *ptr = 0; do { ptr--; - *ptr = '0' + (i % 10); + *ptr = '0' + (nonneg % 10); - i /= 10; + nonneg /= 10; - } while (i > 0 && ptr >= buffer); + } while (nonneg > 0 && ptr >= buffer); + + /* Prepend with dash if negative */ + if (i < 0 && ptr >= buffer) *(--ptr) = '-'; return guac_write_string(io, ptr);