From: Thierry FOURNIER Date: Sat, 6 Jun 2015 17:14:59 +0000 (+0200) Subject: MINOR: standard: add function that converts signed int to a string X-Git-Tag: v1.6-dev2~17 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1480bd8dd27dc5bc638acda68b4822fddbfce0d5;p=thirdparty%2Fhaproxy.git MINOR: standard: add function that converts signed int to a string This function is the same as "ultoa_r", but it takes a signed value as input. --- diff --git a/include/common/standard.h b/include/common/standard.h index 759d0a11ca..df2444d335 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -105,6 +105,7 @@ extern int strlcpy2(char *dst, const char *src, int size); */ extern char itoa_str[][171]; extern char *ultoa_r(unsigned long n, char *buffer, int size); +extern char *sltoa_r(long n, char *buffer, int size); extern const char *ulltoh_r(unsigned long long n, char *buffer, int size); static inline const char *ultoa(unsigned long n) { diff --git a/src/standard.c b/src/standard.c index 4e458c25c1..fb24f7d37c 100644 --- a/src/standard.c +++ b/src/standard.c @@ -406,6 +406,22 @@ char *ultoa_r(unsigned long n, char *buffer, int size) return pos + 1; } +/* + * This function simply returns a locally allocated string containing + * the ascii representation for signed number 'n' in decimal. + */ +char *sltoa_r(long n, char *buffer, int size) +{ + char *pos; + + if (n >= 0) + return ultoa_r(n, buffer, size); + + pos = ultoa_r(-n, buffer + 1, size - 1) - 1; + *pos = '-'; + return pos; +} + /* * This function simply returns a locally allocated string containing * the ascii representation for number 'n' in decimal, formatted for