From f1b569c189cc3d6a26aa3cc7f85559006cde0106 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Mon, 28 Oct 2019 18:43:34 +0100 Subject: init: make addint() support 32 bit values It was limited to 8 bits, and is not usable to emit errno. --- init/init.c | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/init/init.c b/init/init.c index 6200abf..22ced7c 100644 --- a/init/init.c +++ b/init/init.c @@ -574,27 +574,24 @@ static char *addchr(char *dest, char chr) return dest; } -/* appends an 8-bit unsigned integer to the end of a string and +/* appends a 32-bit unsigned integer to the end of a string and * returns the pointer to the new end. No size checks are performed. */ -static char *addint(char *dest, uint8_t num) +static char *adduint(char *dest, uint32_t num) { - int div; - char *out = dest; + uint32_t div; - for (div = (1 << 16) + (10 << 8) + 100; div > 0;) { - int q, r, d; - d = (unsigned char)div; - q = num / d; r = num % d; - div >>= 8; + for (div = 1; num / div >= 10; div *= 10) + ; - if (!div || (out != dest) || q) { - *out++ = q + '0'; - } - num = r; + while (div) { + *dest++ = '0' + num / div; + num %= div; + div /= 10; } - *out = '\0'; - return out; + + *dest = '\0'; + return dest; } /* appends an 8-bit unsigned hex integer to the end of a string @@ -1531,11 +1528,11 @@ static void name_and_minor(char *name, uint8_t *minor, int fields) name = addhex(name, var[f].u.num.value); goto recalc_int; case 'i' : - name = addint(name, var[f].u.num.value); + name = adduint(name, var[f].u.num.value); goto recalc_int; case 'I' : if (var[f].u.num.value) - name = addint(name, var[f].u.num.value); + name = adduint(name, var[f].u.num.value); recalc_int: min += (var[f].u.num.value - var[f].u.num.low) * var[f].scale; break; -- 2.20.1