diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index d19bbdf6..4999cb42 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -192,6 +192,7 @@ scaffold/tests/84-struct-field-list scaffold/tests/85-sizeof lib/tests/dirent/90-readdir lib/tests/io/90-stat +lib/tests/mes/90-abtod lib/tests/posix/90-execlp lib/tests/posix/90-unsetenv lib/tests/signal/90-signal diff --git a/include/libmes.h b/include/libmes.h index 579f4709..f9763698 100644 --- a/include/libmes.h +++ b/include/libmes.h @@ -24,6 +24,7 @@ #include int __mes_debug (); +double abtod (char const** p, int base); long abtol (char const** p, int base); char const* ntoab (long number, int base, int signed_p); char const* itoa (int number); diff --git a/lib/libc+gnu.c b/lib/libc+gnu.c index 873228c4..4a86d5a3 100644 --- a/lib/libc+gnu.c +++ b/lib/libc+gnu.c @@ -83,7 +83,6 @@ #include // gcc - #include #include #include @@ -122,6 +121,7 @@ // gawk #include +// #include in libc+tcc for strtod,strtof (could be stubbed) // tar #include diff --git a/lib/libc+tcc.c b/lib/libc+tcc.c index 177da71b..a579d991 100644 --- a/lib/libc+tcc.c +++ b/lib/libc+tcc.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -61,6 +61,7 @@ int errno; #include #include #include +#include // implementation instead of stub for GNU gawk #include #include #include @@ -85,6 +86,7 @@ int errno; #include #include #include +#include #include #include #include @@ -105,5 +107,4 @@ int errno; #include #include #include -#include #include diff --git a/lib/mes/abtod.c b/lib/mes/abtod.c new file mode 100644 index 00000000..6cb0aca2 --- /dev/null +++ b/lib/mes/abtod.c @@ -0,0 +1,53 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU 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. + * + * GNU 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 GNU Mes. If not, see . + */ + +#include + +double +abtod (char const **p, int base) +{ + char const *s = *p; + double d = 0; + int sign_p = 0; + if (!base) + base = 10; + double dbase = base; + long i = abtol (&s, base); + long f = 0; + long e = 0; + if (*s == '.') + { + s++; + f = abtol (&s, base); + } + if (*s == 'e') + { + s++; + e = abtol (&s, base); + } + d = i + f / dbase; + if (e < 0) + while (e++) + d = d / dbase; + while (e--) + d = d * dbase; + *p = s; + return sign_p ? -d : d; +} diff --git a/lib/mes/abtol.c b/lib/mes/abtol.c index 69ec50a6..64dc5c93 100644 --- a/lib/mes/abtol.c +++ b/lib/mes/abtol.c @@ -25,12 +25,16 @@ abtol (char const **p, int base) { char const *s = *p; int i = 0; - int sign = 1; + int sign_p = 0; if (!base) base = 10; + while (isspace (*s)) + s++; + if (*s && *s == '+') + s++; if (*s && *s == '-') { - sign = -1; + sign_p = 1; s++; } while (isnumber (*s, base)) @@ -41,5 +45,5 @@ abtol (char const **p, int base) s++; } *p = s; - return i * sign; + return sign_p ? -i : i; } diff --git a/lib/stdlib/strtod.c b/lib/stdlib/strtod.c new file mode 100644 index 00000000..b22851e4 --- /dev/null +++ b/lib/stdlib/strtod.c @@ -0,0 +1,40 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU 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. + * + * GNU 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 GNU Mes. If not, see . + */ + +#include +#include + +double +strtod (char const *string, char **tailptr) +{ + int base = 10; + if (!strncmp (string, "0x", 2)) + { + string += 2; + base = 16; + } + if (tailptr) + { + *tailptr = string; + return abtod (tailptr, base); + } + char **p = &string; + return abtod (p, base); +} diff --git a/lib/stub/strtod.c b/lib/tests/mes/90-abtod.c similarity index 79% rename from lib/stub/strtod.c rename to lib/tests/mes/90-abtod.c index 9a74fe0f..dc7c3814 100644 --- a/lib/stub/strtod.c +++ b/lib/tests/mes/90-abtod.c @@ -1,6 +1,6 @@ /* -*-comment-start: "//";comment-end:""-*- * GNU Mes --- Maxwell Equations of Software - * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen + * Copyright © 2019 Jan (janneke) Nieuwenhuizen * * This file is part of GNU Mes. * @@ -21,12 +21,13 @@ #include #include -double -strtod (char const *string, char **tailptr) +int +main () { - static int stub = 0; - if (__mes_debug () && !stub) - eputs ("strtod stub\n"); - stub = 1; + char *s = "1.2e3"; + char *p = s; + double d = abtod (&p, 0); + printf ("%f\n", d); + return 0; } diff --git a/lib/tests/mes/90-abtod.stdout b/lib/tests/mes/90-abtod.stdout new file mode 100644 index 00000000..84402f49 --- /dev/null +++ b/lib/tests/mes/90-abtod.stdout @@ -0,0 +1 @@ +1200.000000