From d49ea35044f24cc04f8be38be7b07f8d5a6ab797 Mon Sep 17 00:00:00 2001 From: "Jan (janneke) Nieuwenhuizen" Date: Sun, 17 May 2020 19:28:37 +0200 Subject: [PATCH] mescc: Mes C Library: Prepare for M2-Planet: ntoab. * lib/mes/ntoab.c: Rewrite C-constructs not supported by M2-Planet. --- lib/mes/ntoab.c | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/mes/ntoab.c b/lib/mes/ntoab.c index f2f0e89d..988a0b25 100644 --- a/lib/mes/ntoab.c +++ b/lib/mes/ntoab.c @@ -18,24 +18,32 @@ * along with GNU Mes. If not, see . */ -#include #include -#include +#include +#include +#include -#define STR(x) #x -#define XSTR(s) STR(s) +char *__itoa_buf; char * ntoab (long x, unsigned base, int signed_p) { - static char itoa_buf[20]; - char *p = itoa_buf + 11; - *p-- = 0; - assert(base > 1); +#if 0 + if (! __itoa_buf) + __itoa_buf = malloc (20); + p = __itoa_buf + 11; +#else + static char buf[20]; + char *p = buf + 19; +#endif + + p[0] = 0; + p = p - 1; + assert_msg (base > 0, "base > 0"); int sign_p = 0; unsigned long u; - if (signed_p && x < 0) + if (signed_p != 0 && x < 0) { sign_p = 1; /* Avoid LONG_MIN */ @@ -54,12 +62,19 @@ ntoab (long x, unsigned base, int signed_p) i = u % base; u = u / base; #endif - *p-- = i > 9 ? 'a' + i - 10 : '0' + i; + if (i > 9) + p[0] = 'a' + i - 10; + else + p[0] = '0' + i; + p = p - 1; } - while (u); + while (u != 0); - if (sign_p && *(p + 1) != '0') - *p-- = '-'; + if (sign_p && p[1] != '0') + { + p[0] = '-'; + p = p - 1; + } return p + 1; }