mescc: Mes C Library: Prepare for M2-Planet: ntoab.

* lib/mes/ntoab.c: Rewrite C-constructs not supported by M2-Planet.
This commit is contained in:
Jan (janneke) Nieuwenhuizen 2020-05-17 19:28:37 +02:00
parent 5dbae4a29b
commit d49ea35044
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273

View file

@ -18,24 +18,32 @@
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>. * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <assert.h>
#include <mes/lib.h> #include <mes/lib.h>
#include <stdint.h> #include <assert.h>
#include <stdlib.h>
#include <string.h>
#define STR(x) #x char *__itoa_buf;
#define XSTR(s) STR(s)
char * char *
ntoab (long x, unsigned base, int signed_p) ntoab (long x, unsigned base, int signed_p)
{ {
static char itoa_buf[20]; #if 0
char *p = itoa_buf + 11; if (! __itoa_buf)
*p-- = 0; __itoa_buf = malloc (20);
assert(base > 1); 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; int sign_p = 0;
unsigned long u; unsigned long u;
if (signed_p && x < 0) if (signed_p != 0 && x < 0)
{ {
sign_p = 1; sign_p = 1;
/* Avoid LONG_MIN */ /* Avoid LONG_MIN */
@ -54,12 +62,19 @@ ntoab (long x, unsigned base, int signed_p)
i = u % base; i = u % base;
u = u / base; u = u / base;
#endif #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') if (sign_p && p[1] != '0')
*p-- = '-'; {
p[0] = '-';
p = p - 1;
}
return p + 1; return p + 1;
} }