diff --git a/include/libmes.h b/include/libmes.h index f13d1f84..44548072 100644 --- a/include/libmes.h +++ b/include/libmes.h @@ -45,11 +45,23 @@ typedef long ssize_t; #endif #endif +#ifndef STDIN +#define STDIN 0 +#endif + +#ifndef STDOUT +#define STDOUT 1 +#endif + +#ifndef STDERR +#define STDERR 2 +#endif + int __mes_debug (); -char const* number_to_ascii (long number, int base, int signed_p); -char const* itoa (long number); +char const* ntoab (long number, int base, int signed_p); +char const* itoa (int number); char const* utoa (unsigned long number); -char const* itoab (long x, int base); +char const* ltoab (long x, int base); int _atoi (char const**, int base); int atoi (char const *s); int eputc (int c); @@ -58,6 +70,7 @@ int fdgetc (int fd); int fdputc (int c, int fd); int fdputs (char const* s, int fd); int fdungetc (int c, int fd); +int _fdungetc_p (int fd); int isdigit (int c); int isspace (int c); int isxdigit (int c); diff --git a/lib/abtol.c b/lib/abtol.c new file mode 100644 index 00000000..25b3afa6 --- /dev/null +++ b/lib/abtol.c @@ -0,0 +1,44 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +long +abtol (char const **p, int base) +{ + char const *s = *p; + long i = 0; + int sign = 1; + if (!base) base = 10; + if (*s && *s == '-') + { + sign = -1; + s++; + } + while (isnumber (*s, base)) + { + i *= base; + long m = *s > '9' ? 'a' - 10 : '0'; + i += *s - m; + s++; + } + *p = s; + return i * sign; +} diff --git a/lib/ctype/isdigit.c b/lib/ctype/isdigit.c new file mode 100644 index 00000000..b29b1728 --- /dev/null +++ b/lib/ctype/isdigit.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +isdigit (int c) +{ + return c >= '0' && c <= '9'; +} diff --git a/lib/ctype/isnumber.c b/lib/ctype/isnumber.c new file mode 100644 index 00000000..023e9015 --- /dev/null +++ b/lib/ctype/isnumber.c @@ -0,0 +1,34 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +isnumber (int c, int base) +{ + if (base == 2) + return (c >= '0') && (c <= '1'); + if (base == 8) + return (c >= '0') && (c <= '7'); + if (base == 10) + return isdigit (c); + if (base == 16) + return isxdigit (c); +} diff --git a/lib/ctype/isspace.c b/lib/ctype/isspace.c new file mode 100644 index 00000000..2ac7a47c --- /dev/null +++ b/lib/ctype/isspace.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +isspace (int c) +{ + return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' '); +} diff --git a/lib/ctype/isxdigit.c b/lib/ctype/isxdigit.c new file mode 100644 index 00000000..9a3bfe6a --- /dev/null +++ b/lib/ctype/isxdigit.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +isxdigit (int c) +{ + return isdigit (c) || (c >= 'a' && c <= 'f'); +} diff --git a/lib/libc+tcc.c b/lib/libc+tcc.c index d19d7363..a9ed2751 100644 --- a/lib/libc+tcc.c +++ b/lib/libc+tcc.c @@ -573,10 +573,10 @@ strtol (char const *string, char **tailptr, int base) if (tailptr) { *tailptr = string; - return abtoi (tailptr, base); + return abtol (tailptr, base); } char **p = &string; - return abtoi (p, base); + return abtol (p, base); } long long int @@ -707,7 +707,7 @@ vfprintf (FILE* f, char const* format, va_list ap) } if (c >= '0' && c <= '9') { - width = abtoi (&p, 10); + width = abtol (&p, 10); c = *p; } else if (c == '*') @@ -720,7 +720,7 @@ vfprintf (FILE* f, char const* format, va_list ap) c = *++p; if (c >= '0' && c <= '9') { - precision = abtoi (&p, 10); + precision = abtol (&p, 10); c = *p; } else if (c == '*') @@ -751,7 +751,7 @@ vfprintf (FILE* f, char const* format, va_list ap) int base = c == 'o' ? 8 : c == 'x' || c == 'X' ? 16 : 10; - char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X'); + char const *s = ntoab (d, base, c != 'u' && c != 'x' && c != 'X'); if (c == 'X') strupr (s); int length = strlen (s); @@ -884,7 +884,7 @@ vsscanf (char const *s, char const *template, va_list ap) case 'u': { int *d = va_arg (ap, int*); - *d = abtoi (&p, 10); + *d = abtol (&p, 10); count++; break; } @@ -934,7 +934,7 @@ vsprintf (char *str, char const* format, va_list ap) } if (c >= '0' && c <= '9') { - width = abtoi (&p, 10); + width = abtol (&p, 10); c = *p; } else if (c == '*') @@ -947,7 +947,7 @@ vsprintf (char *str, char const* format, va_list ap) c = *++p; if (c >= '0' && c <= '9') { - precision = abtoi (&p, 10); + precision = abtol (&p, 10); c = *p; } else if (c == '*') @@ -980,7 +980,7 @@ vsprintf (char *str, char const* format, va_list ap) int base = c == 'o' ? 8 : c == 'x' || c == 'X' ? 16 : 10; - char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X'); + char const *s = ntoab (d, base, c != 'u' && c != 'x' && c != 'X'); if (c == 'X') strupr (s); int length = strlen (s); diff --git a/lib/libmes.c b/lib/libmes.c index 2e90ef91..e8ca8af3 100644 --- a/lib/libmes.c +++ b/lib/libmes.c @@ -23,226 +23,25 @@ #include #include -int -isdigit (int c) -{ - return c >= '0' && c <= '9'; -} +#include +#include +#include +#include -int -isxdigit (int c) -{ - return isdigit (c) || (c >= 'a' && c <= 'f'); -} - -int -isspace (int c) -{ - return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' '); -} - -int -isnumber (int c, int base) -{ - if (base == 2) - return (c >= '0') && (c <= '1'); - if (base == 8) - return (c >= '0') && (c <= '7'); - if (base == 10) - return isdigit (c); - if (base == 16) - return isxdigit (c); -} - -int -abtoi (char const **p, int base) -{ - char const *s = *p; - int i = 0; - int sign = 1; - if (!base) base = 10; - if (*s && *s == '-') - { - sign = -1; - s++; - } - while (isnumber (*s, base)) - { - i *= base; - int m = *s > '9' ? 'a' - 10 : '0'; - i += *s - m; - s++; - } - *p = s; - return i * sign; -} - -int -atoi (char const *s) -{ - char const *p = s; - return abtoi (&p, 0); -} - -char const* -number_to_ascii (int x, int base, int signed_p) -{ - static char itoa_buf[12]; - char *p = itoa_buf + 11; - *p-- = 0; - - int sign = 0; - unsigned u = x; - if (signed_p && x < 0) - { - sign = 1; - u = -x; - } - - do - { - int i = u % base; - *p-- = i > 9 ? 'a' + i - 10 : '0' + i; - u = u / base; - } while (u); - - if (sign && *(p + 1) != '0') - *p-- = '-'; - - return p+1; -} - -char const* -itoab (int x, int base) -{ - return number_to_ascii (x, base, 1); -} - -char const* -itoa (int x) -{ - return number_to_ascii (x, 10, 1); -} - -char const* -utoa (unsigned x) -{ - return number_to_ascii (x, 10, 0); -} - -int _ungetc_pos = -1; -int _ungetc_fd = -1; -char _ungetc_buf[10]; - -int -fdgetc (int fd) -{ - char c; - int i; - if (_ungetc_pos == -1) - { - int r = read (fd, &c, 1); - if (r < 1) - return -1; - i = c; - } - else - { - i = _ungetc_buf[_ungetc_pos]; - if (_ungetc_fd != fd && i == 10) - { - // FIXME: Nyacc's ungetc exposes harmless libmec.c bug - // we need one unget position per FD - _ungetc_pos = -1; - _ungetc_fd = -1; - return fdgetc (fd); - } - else if (_ungetc_fd != fd) - { - eputs (" ***MES LIB C*** fdgetc ungetc conflict unget-fd="); - eputs (itoa (_ungetc_fd)); - eputs (", fdgetc-fd="); - eputs (itoa (fd)); - eputs (", c="); - eputs (itoa ( _ungetc_buf[_ungetc_pos])); - eputs ("\n"); - exit (1); - } - i = _ungetc_buf[_ungetc_pos]; - _ungetc_pos -= 1; - if (_ungetc_pos == -1) - _ungetc_fd = -1; - } - if (i < 0) - i += 256; - - return i; -} - -int -fdputc (int c, int fd) -{ - write (fd, (char*)&c, 1); - return 0; -} - -int -fdputs (char const* s, int fd) -{ - int i = strlen (s); - write (fd, s, i); - return 0; -} - -int -fdungetc (int c, int fd) -{ - if (c == -1) - return c; - if (_ungetc_pos == -1) - _ungetc_fd = fd; - else if (_ungetc_fd != fd) - { - eputs (" ***MES LIB C*** fdungetc ungetc conflict unget-fd="); - eputs (itoa (_ungetc_fd)); - eputs (", fdungetc-fd="); - eputs (itoa (fd)); - eputs ("\n"); - exit (1); - } - _ungetc_pos++; - _ungetc_buf[_ungetc_pos] = c; - return c; -} - -int -_fdungetc_p (int fd) -{ - return _ungetc_pos > -1; -} +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #if POSIX -#define STDERR 2 -int -eputs (char const* s) -{ - int i = strlen (s); - write (STDERR, s, i); - return 0; -} +#include +#include +#endif // POSIX -#define STDOUT 1 -int -oputs (char const* s) -{ - int i = strlen (s); - write (STDOUT, s, i); - return 0; -} -#endif - -int -eputc (int c) -{ - return fdputc (c, STDERR); -} +#include diff --git a/lib/linux/libc-mini.c b/lib/linux/libc-mini.c index 0bcfcb0e..05cc339f 100644 --- a/lib/linux/libc-mini.c +++ b/lib/linux/libc-mini.c @@ -18,6 +18,8 @@ * along with GNU Mes. If not, see . */ +#include + #if __MESC__ #include diff --git a/lib/mes/abtol.c b/lib/mes/abtol.c new file mode 100644 index 00000000..bf0ba9c1 --- /dev/null +++ b/lib/mes/abtol.c @@ -0,0 +1,44 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +long +abtol (char const **p, int base) +{ + char const *s = *p; + int i = 0; + int sign = 1; + if (!base) base = 10; + if (*s && *s == '-') + { + sign = -1; + s++; + } + while (isnumber (*s, base)) + { + i *= base; + int m = *s > '9' ? 'a' - 10 : '0'; + i += *s - m; + s++; + } + *p = s; + return i * sign; +} diff --git a/lib/mes/eputc.c b/lib/mes/eputc.c new file mode 100644 index 00000000..e9f7dc05 --- /dev/null +++ b/lib/mes/eputc.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +eputc (int c) +{ + return fdputc (c, STDERR); +} diff --git a/lib/mes/eputs.c b/lib/mes/eputs.c index a22b145d..3b0ece0b 100644 --- a/lib/mes/eputs.c +++ b/lib/mes/eputs.c @@ -24,6 +24,6 @@ int eputs (char const* s) { int i = strlen (s); - write (2, s, i); + write (STDERR, s, i); return 0; } diff --git a/lib/mes/fdgetc.c b/lib/mes/fdgetc.c new file mode 100644 index 00000000..bf1240a9 --- /dev/null +++ b/lib/mes/fdgetc.c @@ -0,0 +1,70 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int _ungetc_pos = -1; +int _ungetc_fd = -1; +char _ungetc_buf[10]; + +int +fdgetc (int fd) +{ + char c; + int i; + if (_ungetc_pos == -1) + { + int r = read (fd, &c, 1); + if (r < 1) + return -1; + i = c; + } + else + { + i = _ungetc_buf[_ungetc_pos]; + if (_ungetc_fd != fd && i == 10) + { + // FIXME: Nyacc's ungetc exposes harmless libmec.c bug + // we need one unget position per FD + _ungetc_pos = -1; + _ungetc_fd = -1; + return fdgetc (fd); + } + else if (_ungetc_fd != fd) + { + eputs (" ***MES C LIB*** fdgetc ungetc conflict unget-fd="); + eputs (itoa (_ungetc_fd)); + eputs (", fdgetc-fd="); + eputs (itoa (fd)); + eputs (", c="); + eputs (itoa ( _ungetc_buf[_ungetc_pos])); + eputs ("\n"); + exit (1); + } + i = _ungetc_buf[_ungetc_pos]; + _ungetc_pos -= 1; + if (_ungetc_pos == -1) + _ungetc_fd = -1; + } + if (i < 0) + i += 256; + + return i; +} diff --git a/lib/mes/fdputc.c b/lib/mes/fdputc.c new file mode 100644 index 00000000..14f4f6f6 --- /dev/null +++ b/lib/mes/fdputc.c @@ -0,0 +1,28 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +fdputc (int c, int fd) +{ + write (fd, (char*)&c, 1); + return 0; +} diff --git a/lib/mes/fdputs.c b/lib/mes/fdputs.c new file mode 100644 index 00000000..472799cd --- /dev/null +++ b/lib/mes/fdputs.c @@ -0,0 +1,29 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +fdputs (char const* s, int fd) +{ + int i = strlen (s); + write (fd, s, i); + return 0; +} diff --git a/lib/mes/fdungetc.c b/lib/mes/fdungetc.c new file mode 100644 index 00000000..ddf134b2 --- /dev/null +++ b/lib/mes/fdungetc.c @@ -0,0 +1,48 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +fdungetc (int c, int fd) +{ + if (c == -1) + return c; + if (_ungetc_pos == -1) + _ungetc_fd = fd; + else if (_ungetc_fd != fd) + { + eputs (" ***MES LIB C*** fdungetc ungetc conflict unget-fd="); + eputs (itoa (_ungetc_fd)); + eputs (", fdungetc-fd="); + eputs (itoa (fd)); + eputs ("\n"); + exit (1); + } + _ungetc_pos++; + _ungetc_buf[_ungetc_pos] = c; + return c; +} + +int +_fdungetc_p (int fd) +{ + return _ungetc_pos > -1; +} diff --git a/lib/mes/itoa.c b/lib/mes/itoa.c new file mode 100644 index 00000000..cd330e88 --- /dev/null +++ b/lib/mes/itoa.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +char const* +itoa (int x) +{ + return ntoab (x, 10, 1); +} diff --git a/lib/mes/ltoab.c b/lib/mes/ltoab.c new file mode 100644 index 00000000..090d3ca2 --- /dev/null +++ b/lib/mes/ltoab.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +char const* +ltoab (long x, int base) +{ + return ntoab (x, base, 1); +} diff --git a/lib/mes/ntoab.c b/lib/mes/ntoab.c new file mode 100644 index 00000000..52990854 --- /dev/null +++ b/lib/mes/ntoab.c @@ -0,0 +1,49 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +char const* +ntoab (long x, int base, int signed_p) +{ + static char itoa_buf[20]; + char *p = itoa_buf + 11; + *p-- = 0; + + int sign = 0; + unsigned long u = x; + if (signed_p && x < 0) + { + sign = 1; + u = -x; + } + + do + { + long i = u % base; + *p-- = i > 9 ? 'a' + i - 10 : '0' + i; + u = u / base; + } while (u); + + if (sign && *(p + 1) != '0') + *p-- = '-'; + + return p+1; +} diff --git a/lib/mes/utoa.c b/lib/mes/utoa.c new file mode 100644 index 00000000..d67c8d64 --- /dev/null +++ b/lib/mes/utoa.c @@ -0,0 +1,27 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +char const* +utoa (unsigned long x) +{ + return ntoab (x, 10, 0); +} diff --git a/lib/stdlib/atoi.c b/lib/stdlib/atoi.c new file mode 100644 index 00000000..cc02dca4 --- /dev/null +++ b/lib/stdlib/atoi.c @@ -0,0 +1,28 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018 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 + +int +atoi (char const *s) +{ + char const *p = s; + return abtol (&p, 0); +} diff --git a/scaffold/tests/7j-strtoull.c b/scaffold/tests/7j-strtoull.c index b500cb0f..20b98645 100644 --- a/scaffold/tests/7j-strtoull.c +++ b/scaffold/tests/7j-strtoull.c @@ -28,7 +28,7 @@ int main () { char *p = "42foo\n"; - int n = abtoi (&p, 0); + int n = abtol (&p, 0); if (n != 42) return 1; eputs (p); if (strcmp (p, "foo\n")) return 2;