summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Eitzenberger <heitzenberger@astaro.com>2009-10-08 17:18:57 +0200
committerHarald Welte <laforge@gnumonks.org>2010-10-21 19:15:36 +0200
commita085cb4369d12d1dbce2654c932f5d322ae5074a (patch)
tree0887055f59e2fe3f622fa3efb8ee6af8d0f23e01
parent86fd0c448af9e2474b7abc260a50fa26d5644aa1 (diff)
Fix utoa() return value
The return value for utoa() is wrong for numbers with an even number of digits. This patch fixes it. Signed-off-by: Holger Eitzenberger <holger@eitzenberger.org>
-rw-r--r--src/common.c26
1 files changed, 12 insertions, 14 deletions
diff --git a/src/common.c b/src/common.c
index 8a3e38b..32ebb05 100644
--- a/src/common.c
+++ b/src/common.c
@@ -96,31 +96,29 @@ strntr(char *str, char from, char to)
int
utoa(unsigned v, char *str, size_t len)
{
- char *end = str + len;
- int i;
+ char *pch = str;
+ int i, written;
do {
unsigned mod = v % 10;
v /= 10;
+ *pch++ = '0' + mod;
+ } while (v > 0 && pch < str + len - 1);
- *str++ = '0' + mod;
- } while (v > 0 && str < end - 1);
-
- *str = '\0';
+ *pch = '\0';
+ written = pch - str;
/* characters are in reverse order, therefore swap */
- end -= len;
-
- len = str - end;
- for (i = 0, str--; i < (len / 2); i++) {
- char tmp = str[-i];
+ len = pch - str;
+ for (i = 0, pch--; i < (len / 2); i++) {
+ char tmp = pch[-i];
- str[-i] = end[i];
- end[i] = tmp;
+ pch[-i] = str[i];
+ str[i] = tmp;
}
- return len - (end - str);
+ return written;
}
static inline void