diff --git a/AUTHORS b/AUTHORS index fdad0bd2..dad7087f 100644 --- a/AUTHORS +++ b/AUTHORS @@ -3,7 +3,7 @@ Main author All files except the imported files listed below Jeremiah Orians -lib/libc.c (fopen) +lib/libc+tcc.c (fopen) scaffold/tests/98-fopen.c Han-Wen Nienhuys diff --git a/build-aux/build-cc32.sh b/build-aux/build-cc32.sh index bec1fdd3..3df24aa9 100755 --- a/build-aux/build-cc32.sh +++ b/build-aux/build-cc32.sh @@ -71,10 +71,10 @@ CC32_CFLAGS=${CC32_CFLAGS-" -Wno-incompatible-pointer-types "} -ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/crt0 -ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/crt1 -ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/crti -ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/crtn +ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/linux/crt0 +ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/linux/crt1 +ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/linux/crti +ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/linux/crtn ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/libc-mini ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/libc ARCHDIR=1 NOLINK=1 sh build-aux/cc32-mes.sh lib/libgetopt diff --git a/build-aux/build-mes.sh b/build-aux/build-mes.sh index cca170da..1461fdc1 100755 --- a/build-aux/build-mes.sh +++ b/build-aux/build-mes.sh @@ -105,10 +105,10 @@ if [ ! -d "$MES_SEED" ] \ fi MES_ARENA=100000000 -ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/crt0 -ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/crt1 -ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/crti -ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/crtn +ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/linux/crt0 +ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/linux/crt1 +ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/linux/crti +ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/linux/crtn ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/libc-mini ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/libc ARCHDIR=1 NOLINK=1 bash build-aux/cc-mes.sh lib/libgetopt diff --git a/build-aux/cc-mes.sh b/build-aux/cc-mes.sh index 25537608..079212f1 100755 --- a/build-aux/cc-mes.sh +++ b/build-aux/cc-mes.sh @@ -57,7 +57,7 @@ if [ -z "$ARCHDIR" ]; then p="mes-" else b=${c##*/} - d=${c%/*} + d=${c%%/*} o="$d/x86-mes/$b" mkdir -p $d/x86-mes fi diff --git a/build-aux/cc32-mes.sh b/build-aux/cc32-mes.sh index a6066a13..2ac327d9 100755 --- a/build-aux/cc32-mes.sh +++ b/build-aux/cc32-mes.sh @@ -63,7 +63,7 @@ if [ -z "$ARCHDIR" ]; then p="$a-" else b=${c##*/} - d=${c%/*} + d=${c%%/*} o="$d/$arch/$b" mkdir -p $d/$arch fi diff --git a/include/argz.h b/include/argz.h new file mode 100644 index 00000000..2904ee21 --- /dev/null +++ b/include/argz.h @@ -0,0 +1,42 @@ +/* -*-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 . + */ +#ifndef __MES_ARGZ_H +#define __MES_ARGZ_H 1 + +#if WITH_GLIBC +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#undef __MES_ARGZ_H +#include_next + +#else // ! WITH_GLIBC + +#ifndef libc_hidden_def +#define libc_hidden_def(x) +#define weak_alias(x,y) +#endif + +size_t __argz_count (char const *argz, size_t len); +void __argz_extract (char const *argz, size_t len, char **argv); + +#endif // ! WITH_GLIBC + +#endif // __MES_ARGZ_H diff --git a/include/errno.h b/include/errno.h index 49c515c7..78ceabe7 100644 --- a/include/errno.h +++ b/include/errno.h @@ -27,6 +27,9 @@ #undef __MES_ERRNO_H #include_next #else // ! WITH_GLIBC + +typedef int error_t; + int errno; #define ENOENT 2 #define EINTR 4 diff --git a/lib/assert/assert.c b/lib/assert/assert.c new file mode 100644 index 00000000..c72177bd --- /dev/null +++ b/lib/assert/assert.c @@ -0,0 +1,10 @@ +void +assert_fail (char* s) +{ + eputs ("assert fail: "); + eputs (s); + eputs ("\n"); + char *fail = s; + fail = 0; + *fail = 0; +} diff --git a/lib/binutils.c b/lib/binutils.c index f05869e3..857b2ff6 100644 --- a/lib/binutils.c +++ b/lib/binutils.c @@ -244,7 +244,7 @@ sbrk (intptr_t delta) { if (delta >= 0) return malloc (delta); - return g_brk; + return __brk; } // binutils 2.30 diff --git a/lib/libc+gnu.c b/lib/libc+gnu.c index abdedd34..98160b0b 100644 --- a/lib/libc+gnu.c +++ b/lib/libc+gnu.c @@ -19,9 +19,19 @@ */ #include -#include + +#if __GNU__ +#include +#elif __linux__ +#include +#else +#error both __GNU__ and _linux__ are undefined, choose one +#endif + #include #include #include -#include +#if !__GNU__ +#include +#endif #include diff --git a/lib/libc+tcc.c b/lib/libc+tcc.c index 9ae88558..877fed81 100644 --- a/lib/libc+tcc.c +++ b/lib/libc+tcc.c @@ -36,7 +36,14 @@ #include #include -#include + +#if __GNU__ +#include +#elif __linux__ +#include +#else +#error both __GNU__ and _linux__ are undefined, choose one +#endif #if __MESC__ diff --git a/lib/libc-mini.c b/lib/libc-mini.c index 2e9539ce..aa7a2b5c 100644 --- a/lib/libc-mini.c +++ b/lib/libc-mini.c @@ -20,17 +20,29 @@ #include +#ifndef _SIZE_T +#define _SIZE_T +#ifndef __SIZE_T +#define __SIZE_T #ifndef __MES_SIZE_T #define __MES_SIZE_T #undef size_t typedef unsigned long size_t; #endif +#endif +#endif +#ifndef _SSIZE_T +#define _SSIZE_T +#ifndef __SSIZE_T +#define __SSIZE_T #ifndef __MES_SSIZE_T #define __MES_SSIZE_T #undef ssize_t typedef long ssize_t; #endif +#endif +#endif ssize_t write (int filedes, void const *buffer, size_t size); @@ -65,15 +77,13 @@ puts (char const* s) return oputs ("\n"); } -#if __MESC__ - -#include - -#else // !__MESC__ - -#include - -#endif // !__MESC__ +#if __GNU__ +#include +#elif __linux__ +#include +#else +#error both __GNU__ and _linux__ are undefined, choose one +#endif void (*__call_at_exit) (void); @@ -84,17 +94,3 @@ exit (int code) (*__call_at_exit) (); _exit (code); } - -ssize_t -write (int filedes, void const *buffer, size_t size) -{ - int r = _write (filedes, buffer, size); - if (r < 0) - { - errno = -r; - r = -1; - } - else - errno = 0; - return r; -} diff --git a/lib/libc.c b/lib/libc.c index 4c53ba2c..3efe18e6 100644 --- a/lib/libc.c +++ b/lib/libc.c @@ -29,21 +29,16 @@ #include #include -#if __MESC__ - -#include - -#else // !__MESC__ - -#include - -#include - -#endif // !__MESC__ - #include #include -#include + +#if __GNU__ +#include +#elif __linux__ +#include +#else +#error both __GNU__ and _linux__ are undefined, choose one +#endif int g_stdin = 0; @@ -63,6 +58,13 @@ __mes_debug () return __mes_debug; } + +#if !___GNU__ +#include +#include +#include +#endif + int getchar () { @@ -94,17 +96,6 @@ putc (int c, FILE* stream) return fdputc (c, (int)stream); } -void -assert_fail (char* s) -{ - eputs ("assert fail: "); - eputs (s); - eputs ("\n"); - char *fail = s; - fail = 0; - *fail = 0; -} - int getc (FILE *stream) { @@ -147,29 +138,6 @@ strcpy (char *dest, char const *src) return dest; } -char *g_brk = 0; - -void * -malloc (size_t size) -{ - if (!g_brk) - g_brk = brk (0); - if (brk (g_brk + size) == (void*)-1) - return 0; - char *p = g_brk; - g_brk += size; - return p; -} - -void * -memcpy (void *dest, void const *src, size_t n) -{ - char* p = dest; - char const* q = src; - while (n--) *p++ = *q++; - return dest; -} - void * realloc (void *ptr, size_t size) { diff --git a/lib/libg.c b/lib/libg.c index b892ca06..7628c8b6 100644 --- a/lib/libg.c +++ b/lib/libg.c @@ -37,7 +37,15 @@ #define sscanf _sscanf #include -#include + +#if __GNU__ +#include +#elif __linux__ +#include +#else +#error both __GNU__ and _linux__ are undefined, choose one +#endif + #include #include #include diff --git a/lib/crt0.c b/lib/linux/crt0.c similarity index 100% rename from lib/crt0.c rename to lib/linux/crt0.c diff --git a/lib/crt1.c b/lib/linux/crt1.c similarity index 100% rename from lib/crt1.c rename to lib/linux/crt1.c diff --git a/lib/crti.c b/lib/linux/crti.c similarity index 100% rename from lib/crti.c rename to lib/linux/crti.c diff --git a/lib/crtn.c b/lib/linux/crtn.c similarity index 100% rename from lib/crtn.c rename to lib/linux/crtn.c diff --git a/lib/linux-gcc.c b/lib/linux/gcc.c similarity index 100% rename from lib/linux-gcc.c rename to lib/linux/gcc.c diff --git a/lib/linux+gnu.c b/lib/linux/gnu.c similarity index 100% rename from lib/linux+gnu.c rename to lib/linux/gnu.c diff --git a/lib/linux/libc-mini.c b/lib/linux/libc-mini.c new file mode 100644 index 00000000..cd9e2d88 --- /dev/null +++ b/lib/linux/libc-mini.c @@ -0,0 +1,43 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,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 . + */ + +#if __MESC__ + +#include + +#else // !__MESC__ + +#include + +#endif // !__MESC__ + +ssize_t +write (int filedes, void const *buffer, size_t size) +{ + int r = _write (filedes, buffer, size); + if (r < 0) + { + errno = -r; + r = -1; + } + else + errno = 0; + return r; +} diff --git a/lib/linux.c b/lib/linux/libc.c similarity index 95% rename from lib/linux.c rename to lib/linux/libc.c index 454aa7b2..0b6ad18f 100644 --- a/lib/linux.c +++ b/lib/linux/libc.c @@ -38,6 +38,18 @@ #define SYS_ioctl 0x36 #define SYS_fsync 0x76 +#if __MESC__ + +#include + +#else // !__MESC__ + +#include + +#include + +#endif // !__MESC__ + int fork () { diff --git a/lib/linux-mes.c b/lib/linux/mes.c similarity index 100% rename from lib/linux-mes.c rename to lib/linux/mes.c diff --git a/lib/linux-mini-gcc.c b/lib/linux/mini-gcc.c similarity index 100% rename from lib/linux-mini-gcc.c rename to lib/linux/mini-gcc.c diff --git a/lib/linux-mini-mes.c b/lib/linux/mini-mes.c similarity index 100% rename from lib/linux-mini-mes.c rename to lib/linux/mini-mes.c diff --git a/lib/linux+tcc.c b/lib/linux/tcc.c similarity index 100% rename from lib/linux+tcc.c rename to lib/linux/tcc.c diff --git a/lib/m4.c b/lib/m4.c index b9c79ac0..f29a4c73 100644 --- a/lib/m4.c +++ b/lib/m4.c @@ -20,15 +20,7 @@ #include -int -abort () -{ - static int stub = 0; - if (__mes_debug () && !stub) - eputs ("abort stub\n"); - stub = 1; - return 0; -} +#include "stdlib/abort.c" int atof (int x) diff --git a/lib/stdlib/abort.c b/lib/stdlib/abort.c new file mode 100644 index 00000000..2231c516 --- /dev/null +++ b/lib/stdlib/abort.c @@ -0,0 +1,29 @@ +/* -*-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 . + */ + +void +abort (void) +{ +#if __i386__ + asm ("hlt"); +#else + asm ("break 0"); +#endif +} diff --git a/lib/alloca.c b/lib/stdlib/alloca.c similarity index 100% rename from lib/alloca.c rename to lib/stdlib/alloca.c diff --git a/lib/stdlib/malloc.c b/lib/stdlib/malloc.c new file mode 100644 index 00000000..ca15261d --- /dev/null +++ b/lib/stdlib/malloc.c @@ -0,0 +1,33 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,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 . + */ + +char *__brk = 0; + +void * +malloc (size_t size) +{ + if (!__brk) + __brk = brk (0); + if (brk (__brk + size) == (void*)-1) + return 0; + char *p = __brk; + __brk += size; + return p; +} diff --git a/lib/string/argz-count.c b/lib/string/argz-count.c new file mode 100644 index 00000000..2cd01752 --- /dev/null +++ b/lib/string/argz-count.c @@ -0,0 +1,44 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright (C) 1995-2018 Free Software Foundation, Inc. + * + * 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 . + */ + +/* Taken from GNU C Library + * Routines for dealing with '\0' separated arg vectors. + * Written by Miles Bader +*/ + +#include +#include + +/* Returns the number of strings in ARGZ. */ +size_t +__argz_count (const char *argz, size_t len) +{ + size_t count = 0; + while (len > 0) + { + size_t part_len = strlen(argz); + argz += part_len + 1; + len -= part_len + 1; + count++; + } + return count; +} +libc_hidden_def (__argz_count) +weak_alias (__argz_count, argz_count) diff --git a/lib/string/argz-extract.c b/lib/string/argz-extract.c new file mode 100644 index 00000000..163a2750 --- /dev/null +++ b/lib/string/argz-extract.c @@ -0,0 +1,42 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright (C) 1995-2018 Free Software Foundation, Inc. + * + * 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 . + */ + +/* Taken from GNU C Library + * Routines for dealing with '\0' separated arg vectors. + * Written by Miles Bader +*/ + +#include + +/* Puts pointers to each string in ARGZ, plus a terminating 0 element, into + ARGV, which must be large enough to hold them all. */ +void +__argz_extract (const char *argz, size_t len, char **argv) +{ + while (len > 0) + { + size_t part_len = strlen (argz); + *argv++ = (char *) argz; + argz += part_len + 1; + len -= part_len + 1; + } + *argv = 0; +} +weak_alias (__argz_extract, argz_extract) diff --git a/lib/string/memcpy.c b/lib/string/memcpy.c new file mode 100644 index 00000000..330311f3 --- /dev/null +++ b/lib/string/memcpy.c @@ -0,0 +1,29 @@ +/* -*-comment-start: "//";comment-end:""-*- + * Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,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 . + */ + +void * +memcpy (void *dest, void const *src, size_t n) +{ + char* p = dest; + char const* q = src; + while (n--) + *p++ = *q++; + return dest; +}