mescc: Mes C Library: Explode libmes.c.

* include/libmes.h: Add declarations.
* lib/abtol.c: New file, explode from lib/libmes.c.
* lib/ctype/isdigit.c: Likewise.
* lib/ctype/isnumber.c: Likewise.
* lib/ctype/isspace.c: Likewise.
* lib/ctype/isxdigit.c: Likewise.
* lib/mes/abtol.c: Likewise.
* lib/mes/eputc.c: Likewise.
* lib/mes/eputs.c: Likewise.
* lib/mes/fdgetc.c: Likewise.
* lib/mes/fdputc.c: Likewise.
* lib/mes/fdputs.c: Likewise.
* lib/mes/fdungetc.c: Likewise.
* lib/mes/itoa.c: Likewise.
* lib/mes/ltoab.c: Likewise.
* lib/mes/ntoab.c: Likewise.
* lib/mes/utoa.c: Likewise.
* lib/stdlib/atoi.c: Likewise.
* lib/libmes.c: Include explodings.
This commit is contained in:
Jan Nieuwenhuizen 2018-08-16 11:43:27 +02:00
parent 38a7077b72
commit a08baf2d48
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
22 changed files with 610 additions and 233 deletions

View file

@ -45,11 +45,23 @@ typedef long ssize_t;
#endif #endif
#endif #endif
#ifndef STDIN
#define STDIN 0
#endif
#ifndef STDOUT
#define STDOUT 1
#endif
#ifndef STDERR
#define STDERR 2
#endif
int __mes_debug (); int __mes_debug ();
char const* number_to_ascii (long number, int base, int signed_p); char const* ntoab (long number, int base, int signed_p);
char const* itoa (long number); char const* itoa (int number);
char const* utoa (unsigned long 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**, int base);
int atoi (char const *s); int atoi (char const *s);
int eputc (int c); int eputc (int c);
@ -58,6 +70,7 @@ int fdgetc (int fd);
int fdputc (int c, int fd); int fdputc (int c, int fd);
int fdputs (char const* s, int fd); int fdputs (char const* s, int fd);
int fdungetc (int c, int fd); int fdungetc (int c, int fd);
int _fdungetc_p (int fd);
int isdigit (int c); int isdigit (int c);
int isspace (int c); int isspace (int c);
int isxdigit (int c); int isxdigit (int c);

44
lib/abtol.c Normal file
View file

@ -0,0 +1,44 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
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;
}

27
lib/ctype/isdigit.c Normal file
View file

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
int
isdigit (int c)
{
return c >= '0' && c <= '9';
}

34
lib/ctype/isnumber.c Normal file
View file

@ -0,0 +1,34 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
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);
}

27
lib/ctype/isspace.c Normal file
View file

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
int
isspace (int c)
{
return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ');
}

27
lib/ctype/isxdigit.c Normal file
View file

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
int
isxdigit (int c)
{
return isdigit (c) || (c >= 'a' && c <= 'f');
}

View file

@ -573,10 +573,10 @@ strtol (char const *string, char **tailptr, int base)
if (tailptr) if (tailptr)
{ {
*tailptr = string; *tailptr = string;
return abtoi (tailptr, base); return abtol (tailptr, base);
} }
char **p = &string; char **p = &string;
return abtoi (p, base); return abtol (p, base);
} }
long long int long long int
@ -707,7 +707,7 @@ vfprintf (FILE* f, char const* format, va_list ap)
} }
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
{ {
width = abtoi (&p, 10); width = abtol (&p, 10);
c = *p; c = *p;
} }
else if (c == '*') else if (c == '*')
@ -720,7 +720,7 @@ vfprintf (FILE* f, char const* format, va_list ap)
c = *++p; c = *++p;
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
{ {
precision = abtoi (&p, 10); precision = abtol (&p, 10);
c = *p; c = *p;
} }
else if (c == '*') else if (c == '*')
@ -751,7 +751,7 @@ vfprintf (FILE* f, char const* format, va_list ap)
int base = c == 'o' ? 8 int base = c == 'o' ? 8
: c == 'x' || c == 'X' ? 16 : c == 'x' || c == 'X' ? 16
: 10; : 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') if (c == 'X')
strupr (s); strupr (s);
int length = strlen (s); int length = strlen (s);
@ -884,7 +884,7 @@ vsscanf (char const *s, char const *template, va_list ap)
case 'u': case 'u':
{ {
int *d = va_arg (ap, int*); int *d = va_arg (ap, int*);
*d = abtoi (&p, 10); *d = abtol (&p, 10);
count++; count++;
break; break;
} }
@ -934,7 +934,7 @@ vsprintf (char *str, char const* format, va_list ap)
} }
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
{ {
width = abtoi (&p, 10); width = abtol (&p, 10);
c = *p; c = *p;
} }
else if (c == '*') else if (c == '*')
@ -947,7 +947,7 @@ vsprintf (char *str, char const* format, va_list ap)
c = *++p; c = *++p;
if (c >= '0' && c <= '9') if (c >= '0' && c <= '9')
{ {
precision = abtoi (&p, 10); precision = abtol (&p, 10);
c = *p; c = *p;
} }
else if (c == '*') else if (c == '*')
@ -980,7 +980,7 @@ vsprintf (char *str, char const* format, va_list ap)
int base = c == 'o' ? 8 int base = c == 'o' ? 8
: c == 'x' || c == 'X' ? 16 : c == 'x' || c == 'X' ? 16
: 10; : 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') if (c == 'X')
strupr (s); strupr (s);
int length = strlen (s); int length = strlen (s);

View file

@ -23,226 +23,25 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
int #include <ctype/isdigit.c>
isdigit (int c) #include <ctype/isxdigit.c>
{ #include <ctype/isspace.c>
return c >= '0' && c <= '9'; #include <ctype/isnumber.c>
}
int #include <mes/abtol.c>
isxdigit (int c) #include <stdlib/atoi.c>
{ #include <mes/ntoab.c>
return isdigit (c) || (c >= 'a' && c <= 'f'); #include <mes/ltoab.c>
} #include <mes/itoa.c>
#include <mes/utoa.c>
int #include <mes/fdgetc.c>
isspace (int c) #include <mes/fdputc.c>
{ #include <mes/fdputs.c>
return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' '); #include <mes/fdungetc.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;
}
#if POSIX #if POSIX
#define STDERR 2 #include <mes/eputs.c>
int #include <mes/oputs.c>
eputs (char const* s) #endif // POSIX
{
int i = strlen (s);
write (STDERR, s, i);
return 0;
}
#define STDOUT 1 #include <mes/eputc.c>
int
oputs (char const* s)
{
int i = strlen (s);
write (STDOUT, s, i);
return 0;
}
#endif
int
eputc (int c)
{
return fdputc (c, STDERR);
}

View file

@ -18,6 +18,8 @@
* 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 <errno.h>
#if __MESC__ #if __MESC__
#include <linux/x86-mes/mini.c> #include <linux/x86-mes/mini.c>

44
lib/mes/abtol.c Normal file
View file

@ -0,0 +1,44 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
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;
}

27
lib/mes/eputc.c Normal file
View file

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
int
eputc (int c)
{
return fdputc (c, STDERR);
}

View file

@ -24,6 +24,6 @@ int
eputs (char const* s) eputs (char const* s)
{ {
int i = strlen (s); int i = strlen (s);
write (2, s, i); write (STDERR, s, i);
return 0; return 0;
} }

70
lib/mes/fdgetc.c Normal file
View file

@ -0,0 +1,70 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
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;
}

28
lib/mes/fdputc.c Normal file
View file

@ -0,0 +1,28 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
int
fdputc (int c, int fd)
{
write (fd, (char*)&c, 1);
return 0;
}

29
lib/mes/fdputs.c Normal file
View file

@ -0,0 +1,29 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
int
fdputs (char const* s, int fd)
{
int i = strlen (s);
write (fd, s, i);
return 0;
}

48
lib/mes/fdungetc.c Normal file
View file

@ -0,0 +1,48 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
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;
}

27
lib/mes/itoa.c Normal file
View file

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
char const*
itoa (int x)
{
return ntoab (x, 10, 1);
}

27
lib/mes/ltoab.c Normal file
View file

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
char const*
ltoab (long x, int base)
{
return ntoab (x, base, 1);
}

49
lib/mes/ntoab.c Normal file
View file

@ -0,0 +1,49 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
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;
}

27
lib/mes/utoa.c Normal file
View file

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
char const*
utoa (unsigned long x)
{
return ntoab (x, 10, 0);
}

28
lib/stdlib/atoi.c Normal file
View file

@ -0,0 +1,28 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include <libmes.h>
int
atoi (char const *s)
{
char const *p = s;
return abtol (&p, 0);
}

View file

@ -28,7 +28,7 @@ int
main () main ()
{ {
char *p = "42foo\n"; char *p = "42foo\n";
int n = abtoi (&p, 0); int n = abtol (&p, 0);
if (n != 42) return 1; if (n != 42) return 1;
eputs (p); eputs (p);
if (strcmp (p, "foo\n")) return 2; if (strcmp (p, "foo\n")) return 2;