From cc466662b914274a1fdf43fd0104525779bbf230 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Wed, 6 Jun 2018 22:39:57 +0200 Subject: [PATCH] mescc: Support gcc-3.2: Implement integer strto*. * lib/libc+tcc.c (strtoll, strtoul): Call strtoul. --- build-aux/check-mescc.sh | 1 + include/stdlib.h | 14 ++++---- lib/libc+tcc.c | 69 +++++++++++++++++++++++++++++---------- scaffold/tests/96-strto.c | 57 ++++++++++++++++++++++++++++++++ 4 files changed, 117 insertions(+), 24 deletions(-) create mode 100644 scaffold/tests/96-strto.c diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index a78f13e7..e23ce511 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -140,6 +140,7 @@ t 93-fread-fwrite 94-unsetenv 95-signal +96-strto " # 90: needs GNU, fails for mescc, passes for tcc diff --git a/include/stdlib.h b/include/stdlib.h index 230291f2..1c747e4b 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -49,13 +49,13 @@ void unsetenv (char const *name); void *malloc (size_t); void qsort (void *base, size_t nmemb, size_t size, int (*compar)(void const *, void const *)); void *realloc (void *p, size_t size); -double strtod (char const *nptr, char **endptr); -float strtof (char const *nptr, char **endptr); -long double strtold (char const *nptr, char **endptr); -long strtol (char const *nptr, char **endptr, int base); -long long strtoll (char const *nptr, char **endptr, int base); -unsigned long strtoul (char const *nptr, char **endptr, int base); -unsigned long long strtoull (char const *nptr, char **endptr, int base); +double strtod (char const *string, char **tailptr); +float strtof (char const *string, char **tailptr); +long double strtold (char const *string, char **tailptr); +long strtol (char const *string, char **tailptr, int base); +long long strtoll (char const *string, char **tailptr, int base); +unsigned long strtoul (char const *string, char **tailptr, int base); +unsigned long long strtoull (char const *string, char **tailptr, int base); #define EXIT_FAILURE 1 #define EXIT_SUCCESS 0 diff --git a/lib/libc+tcc.c b/lib/libc+tcc.c index 6b66eabb..b7bdd0f2 100644 --- a/lib/libc+tcc.c +++ b/lib/libc+tcc.c @@ -218,6 +218,7 @@ fseek (FILE *stream, long offset, int whence) int gettimeofday (struct timeval *tv, struct timezone *tz) { + eputs ("gettimeofday stub\n"); return 0; } @@ -450,57 +451,91 @@ strstr (char const *haystack, char const *needle) } double -strtod (char const *nptr, char **endptr) +strtod (char const *string, char **tailptr) { eputs ("strtod stub\n"); } float -strtof (char const *nptr, char **endptr) +strtof (char const *string, char **tailptr) { - return strtod (nptr, endptr); + return strtod (string, tailptr); } long double -strtold (char const *nptr, char **endptr) +strtold (char const *string, char **tailptr) { - return strtod (nptr, endptr); + return strtod (string, tailptr); } long -strtol (char const *nptr, char **endptr, int base) +strtol (char const *string, char **tailptr, int base) { - if (!strncmp (nptr, "0x", 2)) + if (!strncmp (string, "0x", 2)) { - char const *p = nptr + 2; - return abtoi (&p, 16); + string += 2; + base = 16; } - return abtoi (&nptr, base); + if (tailptr) + { + *tailptr = string; + return abtoi (tailptr, base); + } + char **p = &string; + return abtoi (p, base); +} + +#if 1 + +long long int +strtoll (char const *string, char **tailptr, int base) +{ + return strtol (string, tailptr, base); +} + +unsigned long +strtoul (char const *string, char **tailptr, int base) +{ + return strtol (string, tailptr, base); +} + +unsigned long long +strtoull (char const *string, char **tailptr, int base) +{ + return strtol (string, tailptr, base); } +#else + long long int -strtoll (char const *nptr, char **endptr, int base) +strtoll (char const *string, char **tailptr, int base) { eputs ("strtoll stub\n"); return 0; } unsigned long -strtoul (char const *nptr, char **endptr, int base) +strtoul (char const *string, char **tailptr, int base) { eputs ("strtoul stub\n"); return 0; } unsigned long long -strtoull (char const *p, char **endptr, int base) +strtoull (char const *string, char **tailptr, int base) { - *endptr = p; - return abtoi (endptr, base); + // *endptr = p; + // return abtoi (endptr, base); + eputs ("strtoull stub\n"); + return 0; } -time_t time (time_t *tloc) +#endif + +time_t +time (time_t *tloc) { + eputs ("time stub\n"); return 0; } @@ -519,12 +554,12 @@ calloc (size_t nmemb, size_t size) return p; } - int islower (int c) { return c >= 'a' && c <= 'z'; } + int isupper (int c) { diff --git a/scaffold/tests/96-strto.c b/scaffold/tests/96-strto.c new file mode 100644 index 00000000..5fd7c481 --- /dev/null +++ b/scaffold/tests/96-strto.c @@ -0,0 +1,57 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2018 Jan (janneke) Nieuwenhuizen + * + * This file is part of Mes. + * + * Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Mes. If not, see . + */ + +#include +#include + +int +main () +{ + eputs ("0x12\n"); + if (strtol ("0x12", 0, 0) != 18) + 1; + + eputs ("012\n"); + if (strtol ("012", 0, 0) != 10) + 2; + + eputs ("-1\n"); + if (strtol ("-1", 0, 0) != -1) + 3; + + eputs ("-1\n"); + if (strtoul ("-1", 0, 0) != -1) + 4; + + char *p = "16"; + int n = strtol (p, (char **)&p, 0); + eputs ("p="); eputs (p); eputs ("\n"); + if (*p != 0) + return 5; + + p = "0x12"; + n = strtol (p, (char **)&p, 0); + eputs ("p="); eputs (p); eputs ("\n"); + if (*p != 0) + return 5; + + + return 0; +} -- 2.28.0