mescc: Mes C Library: Explode libc.c.

* lib/libc.c: New file, explode from lib/libc.c.
* lib/posix/execv.c: Likewise.
* lib/posix/isatty.c: Likewise.
* lib/posix/wait.c: Likewise.
* lib/stdio/fgetc.c: Likewise.
* lib/stdio/fputc.c: Likewise.
* lib/stdio/fputs.c: Likewise.
* lib/stdio/getc.c: Likewise.
* lib/stdio/getchar.c: Likewise.
* lib/stdio/putc.c: Likewise.
* lib/stdio/putchar.c: Likewise.
* lib/stdio/ungetc.c: Likewise.
* lib/stdlib/free.c: Likewise.
* lib/stdlib/getenv.c: Likewise.
* lib/stdlib/realloc.c: Likewise.
* lib/stdlib/setenv.c: Likewise.
* lib/string/strcmp.c: Likewise.
* lib/string/strcpy.c: Likewise.
* lib/string/strncmp.c: Likewise.
* lib/libc.c: Include explodings.
This commit is contained in:
Jan Nieuwenhuizen 2018-08-16 10:50:53 +02:00
parent 7a849284dc
commit 3e4a56a05b
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
19 changed files with 551 additions and 148 deletions

View File

@ -65,154 +65,24 @@ __mes_debug ()
#include <assert/assert.c>
#endif
int
getchar ()
{
return fdgetc (g_stdin);
}
#include <stdio/getchar.c>
#include <stdio/putchar.c>
#include <stdio/fputc.c>
#include <stdio/fputs.c>
#include <stdio/putc.c>
#include <stdio/getc.c>
#include <stdio/fgetc.c>
#include <stdio/ungetc.c>
int
putchar (int c)
{
write (STDOUT, (char*)&c, 1);
return 0;
}
#include <string/strcmp.c>
#include <string/strcpy.c>
#include <string/strncmp.c>
int
fputc (int c, FILE* stream)
{
return fdputc (c, (int)stream);
}
#include <stdlib/free.c>
#include <stdlib/realloc.c>
#include <stdlib/getenv.c>
#include <stdlib/setenv.c>
int
fputs (char const* s, FILE* stream)
{
return fdputs (s, (int)stream);
}
int
putc (int c, FILE* stream)
{
return fdputc (c, (int)stream);
}
int
getc (FILE *stream)
{
return fdgetc ((int)stream);
}
int
fgetc (FILE *stream)
{
return fdgetc ((int)stream);
}
void
free (void *ptr)
{
}
int
ungetc (int c, FILE *stream)
{
return fdungetc (c, (int)stream);
}
int
strcmp (char const* a, char const* b)
{
while (*a && *b && *a == *b)
{
a++;b++;
}
return *a - *b;
}
char *
strcpy (char *dest, char const *src)
{
char *p = dest;
while (*src) *p++ = *src++;
*p = 0;
return dest;
}
void *
realloc (void *ptr, size_t size)
{
void *new = malloc (size);
if (ptr && new)
{
memcpy (new, ptr, size);
free (ptr);
}
return new;
}
int
strncmp (char const* a, char const* b, size_t size)
{
if (!size)
return 0;
while (*a && *b && *a == *b && --size)
{
a++;
b++;
}
return *a - *b;
}
char *
getenv (char const* s)
{
char **p = environ;
int length = strlen (s);
while (*p)
{
if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
p++;
}
return 0;
}
int
setenv (char const* s, char const* v, int overwrite_p)
{
char **p = environ;
int length = strlen (s);
while (*p)
{
if (!strncmp (s, *p, length) && *(*p + length) == '=')
break;
p++;
}
char *entry = malloc (length + strlen (v) + 2);
int end_p = *p == 0;
*p = entry;
strcpy (entry, s);
strcpy (entry + length, "=");
strcpy (entry + length + 1, v);
*(entry + length + strlen (v) + 2) = 0;
if (end_p)
*++p = 0;
return 0;
}
int
isatty (int fd)
{
return ioctl (fd, TCGETS, 0) & 0xf0;
}
int
wait (int *status_ptr)
{
return waitpid (-1, status_ptr, 0);
}
int
execv (char const *file_name, char *const argv[])
{
return execve (file_name, argv, environ);
}
#include <posix/isatty.c>
#include <posix/wait.c>
#include <posix/execv.c>

27
lib/posix/execv.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 <unistd.h>
int
execv (char const *file_name, char *const argv[])
{
return execve (file_name, argv, environ);
}

27
lib/posix/isatty.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 <unistd.h>
int
isatty (int fd)
{
return ioctl (fd, TCGETS, 0) & 0xf0;
}

27
lib/posix/wait.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 <sys/wait.h>
int
wait (int *status_ptr)
{
return waitpid (-1, status_ptr, 0);
}

27
lib/stdio/fgetc.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 <stdio.h>
int
fgetc (FILE *stream)
{
return fdgetc ((long)stream);
}

27
lib/stdio/fputc.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 <stdio.h>
int
fputc (int c, FILE* stream)
{
return fdputc (c, (long)stream);
}

27
lib/stdio/fputs.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 <stdio.h>
int
fputs (char const* s, FILE* stream)
{
return fdputs (s, (long)stream);
}

27
lib/stdio/getc.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 <stdio.h>
int
getc (FILE *stream)
{
return fdgetc ((long)stream);
}

27
lib/stdio/getchar.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 <stdio.h>
int
getchar ()
{
return fdgetc (g_stdin);
}

27
lib/stdio/putc.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 <stdio.h>
int
putc (int c, FILE* stream)
{
return fdputc (c, (long)stream);
}

28
lib/stdio/putchar.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 <stdio.h>
int
putchar (int c)
{
write (STDOUT, (char*)&c, 1);
return 0;
}

27
lib/stdio/ungetc.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 <stdio.h>
int
ungetc (int c, FILE *stream)
{
return fdungetc (c, (long)stream);
}

26
lib/stdlib/free.c Normal file
View File

@ -0,0 +1,26 @@
/* -*-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 <stdlib.h>
void
free (void *ptr)
{
}

35
lib/stdlib/getenv.c Normal file
View File

@ -0,0 +1,35 @@
/* -*-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 <stdlib.h>
char *
getenv (char const* s)
{
char **p = environ;
int length = strlen (s);
while (*p)
{
if (!strncmp (s, *p, length) && *(*p + length) == '=')
return (*p + length + 1);
p++;
}
return 0;
}

33
lib/stdlib/realloc.c Normal file
View File

@ -0,0 +1,33 @@
/* -*-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 <stdlib.h>
void *
realloc (void *ptr, size_t size)
{
void *new = malloc (size);
if (ptr && new)
{
memcpy (new, ptr, size);
free (ptr);
}
return new;
}

44
lib/stdlib/setenv.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 <stdlib.h>
int
setenv (char const* s, char const* v, int overwrite_p)
{
char **p = environ;
int length = strlen (s);
while (*p)
{
if (!strncmp (s, *p, length) && *(*p + length) == '=')
break;
p++;
}
char *entry = malloc (length + strlen (v) + 2);
int end_p = *p == 0;
*p = entry;
strcpy (entry, s);
strcpy (entry + length, "=");
strcpy (entry + length + 1, v);
*(entry + length + strlen (v) + 2) = 0;
if (end_p)
*++p = 0;
return 0;
}

32
lib/string/strcmp.c Normal file
View File

@ -0,0 +1,32 @@
/* -*-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 <string.h>
int
strcmp (char const* a, char const* b)
{
while (*a && *b && *a == *b)
{
a++;
b++;
}
return *a - *b;
}

31
lib/string/strcpy.c Normal file
View File

@ -0,0 +1,31 @@
/* -*-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 <string.h>
char *
strcpy (char *dest, char const *src)
{
char *p = dest;
while (*src)
*p++ = *src++;
*p = 0;
return dest;
}

34
lib/string/strncmp.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 <string.h>
int
strncmp (char const* a, char const* b, size_t size)
{
if (!size)
return 0;
while (*a && *b && *a == *b && --size)
{
a++;
b++;
}
return *a - *b;
}