mescc: Mes C Library: Add M2-Planet support. WIP

* lib/m2/abtol.c: New file.
* lib/m2/access.c: New file.
* lib/m2/atoi.c: New file.
* lib/m2/chmod.c: New file.
* lib/m2/clock_gettime.c: New file.
* lib/m2/dup.c: New file.
* lib/m2/dup2.c: New file.
* lib/m2/eputs.c: New file.
* lib/m2/execv.c: New file.
* lib/m2/execve.c: New file.
* lib/m2/exit.c: New file.
* lib/m2/fdputc.c: New file.
* lib/m2/fork.c: New file.
* lib/m2/getcwd.c: New file.
* lib/m2/getenv.c: New file.
* lib/m2/isatty.c: New file.
* lib/m2/memchr.c: New file.
* lib/m2/memcmp.c: New file.
* lib/m2/memcpy.c: New file.
* lib/m2/memset.c: New file.
* lib/m2/mes_open.c: New file.
* lib/m2/ntoab.c: New file.
* lib/m2/open.c: New file.
* lib/m2/read.c: New file.
* lib/m2/setenv.c: New file.
* lib/m2/strcmp.c: New file.
* lib/m2/strcpy.c: New file.
* lib/m2/strlen.c: New file.
* lib/m2/strncmp.c: New file.
* lib/m2/time.c: New file.
* lib/m2/unlink.c: New file.
* lib/m2/waitpid.c: New file.
* lib/m2/write.c: New file.
This commit is contained in:
Jan Nieuwenhuizen 2019-10-20 19:25:25 +02:00
parent ce6e927af5
commit 78c4757ec3
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
36 changed files with 1151 additions and 65 deletions

View File

@ -53,6 +53,8 @@
#define SYS_ioctl 0x36
// CONSTANT SYS_fsync 0x76
#define SYS_fsync 0x76
// CONSTANT SYS_getcwd 0xb7
#define SYS_getcwd 0xb7
/* libc+tcc */
#define SYS_close 0x06
@ -62,7 +64,6 @@
#define SYS_rmdir 0x28
#define SYS_gettimeofday 0x4e
#define SYS_stat 0x6a
#define SYS_getcwd 0xb7
/* libc+gnu */

32
lib/m2/_getcwd.c Normal file
View File

@ -0,0 +1,32 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 <mes/lib-mini.h>
#include <linux/syscall.h>
#include <syscall.h>
char *
_getcwd (char *buffer, int size)
{
int r = _sys_call2 (SYS_getcwd, buffer, size);
if (r >= 0)
return buffer;
return 0;
}

55
lib/m2/abtol.c Normal file
View File

@ -0,0 +1,55 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 <mes/lib.h>
#include <ctype.h>
int
abtol (char **p, int base)
{
char *s = p[0];
int i = 0;
int sign_p = 0;
if (base == 0)
base = 10;
while (isspace (s[0]) != 0)
s = s + 1;
if (s[0] != 0 && s[0] == '+')
s = s + 1;
if (s[0] != 0 && s[0] == '-')
{
sign_p = 1;
s = s + 1;
}
while (isnumber (s[0], base) != 0)
{
i = i * base;
int m = '0';
if (s[0] > '9')
m = 'a' - 10;
i = i + s[0] - m;
s = s + 1;
}
p[0] = s;
if (sign_p != 0)
return -i;
return i;
}

21
lib/m2/access.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int access (char *file_name, int how);

26
lib/m2/atoi.c Normal file
View File

@ -0,0 +1,26 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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/>.
*/
int
atoi (char *string)
{
char *p = string;
return abtol (&p, 0);
}

21
lib/m2/chmod.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int chmod (char *file_name, int mode);

27
lib/m2/clock_gettime.c Normal file
View File

@ -0,0 +1,27 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
struct timespec
{
int tv_sec;
int tv_nsec;
};
int clock_gettime (int clk_id, struct timespec *tp);

21
lib/m2/dup.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int dup (int old);

21
lib/m2/dup2.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int dup2 (int old, int new);

25
lib/m2/execv.c Normal file
View File

@ -0,0 +1,25 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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/>.
*/
int
execv (char *file_name, char **argv)
{
return execve (file_name, argv, environ);
}

21
lib/m2/execve.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int execve (char *file_name, char **argv, char **envp);

29
lib/m2/exit.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 <mes/lib.h>
void _exit (int code);
void
exit (int code)
{
_exit (code);
}

28
lib/m2/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 <mes/lib.h>
int
fdputc (int c, int fd)
{
write (fd, &c, 1);
return 0;
}

21
lib/m2/fork.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int fork ();

37
lib/m2/getcwd.c Normal file
View File

@ -0,0 +1,37 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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 <mes/lib.h>
#include <limits.h>
#include <sys/types.h>
#include <mes/lib.h>
// CONSTANT PATH_MAX 1024
char *
getcwd (char *buffer, int size)
{
if (__getcwd_buf == 0)
__getcwd_buf = malloc (PATH_MAX);
char *buf = __itoa_buf;
if (buffer != 0)
return _getcwd (buffer, size);
return _getcwd (buf, PATH_MAX);
}

47
lib/m2/getenv.c Normal file
View File

@ -0,0 +1,47 @@
/* -*-comment-start: "
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 <mes/lib.h>
#include <string.h>
#include <stdlib.h>
char *
getenv (char *s)
{
/* eputs ("\ngetenv s="); eputs (s); eputs ("\n"); */
char **p = environ;
int length = strlen (s);
while (p[0] != 0)
{
/* eputs ("getenv p[0]="); eputs (p[0]); eputs ("\n"); */
if (strncmp (s, p[0], length) == 0)
{
/* eputs ("found!\n"); */
char *q = p[0] + length;
if (q[0] == '=')
return q + 1;
}
/* else */
/* eputs ("not found!\n"); */
p = p + sizeof (char *); /* FIXME! */
}
return 0;
}

21
lib/m2/isatty.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int isatty (int filedes);

36
lib/m2/malloc.c Normal file
View File

@ -0,0 +1,36 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 <mes/lib.h>
#include <string.h>
char *__brk = 0;
void *
malloc (int size)
{
if (__brk == 0)
__brk = brk (0);
if (brk (__brk + size) == -1)
return 0;
char *p = __brk;
__brk = __brk + size;
return p;
}

37
lib/m2/memchr.c Normal file
View File

@ -0,0 +1,37 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 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>
void *
memchr (void *block, int c, size_t size)
{
char *p = block;
while (size != 0)
{
size = size - 1;
if (c == p[0])
return p;
p = p + 1;
}
return 0;
}

39
lib/m2/memcmp.c Normal file
View File

@ -0,0 +1,39 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 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
memcmp (void *s1, void *s2, int size)
{
if (size == 0)
return 0;
char *a = s1;
char *b = s2;
while (a[0] == b[0] && size > 0)
{
size = size - 1;
a = a + 1;
b = b + 1;
}
return a[0] - b[0];
}

38
lib/m2/memcpy.c Normal file
View File

@ -0,0 +1,38 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 <mes/lib.h>
#include <string.h>
void *
memcpy (void *dest, void *src, int n)
{
char *p = dest;
while (n != 0)
{
n = n - 1;
dest[0] = src[0];
dest = dest + 1;
src = src + 1;
}
return p;
}

34
lib/m2/memset.c Normal file
View File

@ -0,0 +1,34 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2017,2018,2019 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>
void *
memset (void *s, int c, int n)
{
char *p = s;
while (n != 0)
{
n = n - 1;
s[0] = c;
s = s + 1;
}
return p;
}

24
lib/m2/missing.hex2 Normal file
View File

@ -0,0 +1,24 @@
:FUNCTION_execve
C3
:FUNCTION_access
C3
:FUNCTION_chmod
C3
:FUNCTION_isatty
C3
:FUNCTION_fork
C3
:FUNCTION_waitpid
C3
:FUNCTION_clock_gettime
C3
:FUNCTION_time
C3
:FUNCTION_getcwd
C3
:FUNCTION_dup
C3
:FUNCTION_dup2
C3
:FUNCTION_unlink
C3

69
lib/m2/ntoab.c Normal file
View File

@ -0,0 +1,69 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 <mes/lib.h>
#include <assert.h>
#include <string.h>
char *__itoa_buf;
char *
ntoab (int x, int base, int signed_p)
{
if (__itoa_buf == 0)
__itoa_buf = malloc (20);
char *buf = __itoa_buf;
char *p = buf + 11;
p[0] = 0;
p = p - 1;
assert_msg (base > 0, "base > 0");
int sign_p = 0;
unsigned u;
if (signed_p != 0 && x < 0)
{
sign_p = 1;
u = -x;
}
else
u = x;
do
{
unsigned i;
i = u % base;
u = u / base;
if (i > 9)
p[0] = 'a' + i - 10;
else
p[0] = '0' + i;
p = p - 1;
}
while (u != 0);
if (sign_p && p[1] != '0')
{
p[0] = '-';
p = p - 1;
}
return p + 1;
}

52
lib/m2/setenv.c Normal file
View File

@ -0,0 +1,52 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 <mes/lib.h>
#include <string.h>
#include <stdlib.h>
int
setenv (char *s, char *v, int overwrite_p)
{
char **p = environ;
int length = strlen (s);
while (p[0] != 0)
{
if (strncmp (s, p[0], length) == 0)
{
char *q = p[0] + length;
if (q[0] == '=')
break;
}
p = p + 1;
}
char *entry = malloc (length + strlen (v) + 2);
int end_p = p[0] == 0;
p[0] = entry;
strcpy (entry, s);
strcpy (entry + length, "=");
strcpy (entry + length + 1, v);
entry[length + strlen (v) + 2] = 0;
if (end_p != 0)
p[1] = 0;
return 0;
}

33
lib/m2/strcmp.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 <string.h>
int
strcmp (char *a, char *b)
{
while (a[0] != 0 && b[0] != 0 && a[0] == b[0])
{
a = a + 1;
b = b + 1;
}
return a[0] - b[0];
}

51
lib/m2/strcpy.c Normal file
View File

@ -0,0 +1,51 @@
/* -*-comment-start: "
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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 *src)
{
/* eputs ("\nstrcpy: src="); */
/* eputs (src); */
/* eputs ("\n"); */
char *p = dest;
char *orig = dest;
/* eputs ("dest="); */
/* eputs (dest); */
/* eputs ("\n"); */
/* eputs ("c:" ); */
while (0 != src[0])
{
/* eputc (src[0]); */
/* eputs (" "); */
dest[0] = src[0];
dest = dest + 1;
src = src + 1;
}
dest[0] = 0;
/* eputs ("\n => orig="); */
/* eputs (orig); */
/* eputs ("\n"); */
return p;
}

30
lib/m2/strlen.c Normal file
View File

@ -0,0 +1,30 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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/>.
*/
int
strlen (char *s)
{
int i = 0;
while (s[i] != 0)
i = i + 1;
return i;
}

35
lib/m2/strncmp.c Normal file
View File

@ -0,0 +1,35 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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/>.
*/
int
strncmp (char *a, char *b, int size)
{
if (size == 0)
return 0;
while (a[0] != 0 && b[0] != 0 && a[0] == b[0] && size > 1)
{
size = size - 1;
a = a + 1;
b = b + 1;
}
return a[0] - b[0];
}

21
lib/m2/time.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int time (int *result);

21
lib/m2/unlink.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2016,2017,2018,2019 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/>.
*/
int unlink (char *file_name);

21
lib/m2/waitpid.c Normal file
View File

@ -0,0 +1,21 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 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/>.
*/
int waitpid (int pid, int *status_ptr, int options);

View File

@ -26,6 +26,7 @@
#include <sys/resource.h>
#include <unistd.h>
int errno;
int *__ungetc_buf;
int

View File

@ -23,8 +23,9 @@
int
memcmp (void const *s1, void const *s2, size_t size)
{
if (!size)
if (size == 0)
return 0;
char const *a = s1;
char const *b = s2;

View File

@ -25,7 +25,6 @@ void *
memcpy (void *dest, void const *src, size_t n)
{
char *p = dest;
char const *q = src;
while (n--)
*p++ = *q++;

View File

@ -26,7 +26,12 @@ MES = bin/mes-gcc
#MES_CPU = x86
M2_PLANET = M2-Planet
M2_PLANET_FLAGS = --architecture amd64
M2_PLANET_ARCH = x86
M2_PLANET_FUBAR = i386
#M2_PLANET_ARCH = amd64
#M2_PLANET_FUBAR = amd64
M2_PLANET_FLAGS = --debug --architecture $(M2_PLANET_ARCH)
CFLAGS:= \
$(CFLAGS) \
@ -61,12 +66,64 @@ MES_SOURCES = \
src/symbol.c \
src/vector.c
LIB_SOURCES = \
lib/mes/eputs.c \
lib/mes/assert_msg.c \
lib/mes/itoa.c
M2_SOURCES = \
lib/linux/x86-mes-m2/crt1.c \
lib/linux/x86-mes-m2/mini.c \
lib/m2/exit.c \
lib/m2/write.c \
lib/linux/x86-mes-m2/syscall.c \
lib/m2/brk.c \
lib/m2/malloc.c \
lib/m2/memset.c \
lib/m2/read.c \
lib/m2/fdgetc.c \
lib/stdio/getchar.c \
lib/m2/putchar.c \
lib/m2/open.c \
lib/m2/mes_open.c \
lib/m2/strlen.c \
lib/m2/eputs.c \
lib/m2/fdputc.c \
lib/mes/eputc.c \
\
lib/mes/__assert_fail.c \
lib/mes/assert_msg.c \
\
lib/m2/fdputc.c \
lib/m2/strncmp.c \
lib/m2/getenv.c \
lib/mes/fdputs.c \
lib/m2/ntoab.c \
lib/ctype/isdigit.c \
lib/ctype/isxdigit.c \
lib/ctype/isspace.c \
lib/ctype/isnumber.c \
lib/m2/abtol.c \
lib/m2/atoi.c \
lib/m2/memcpy.c \
lib/stdlib/free.c \
lib/stdlib/realloc.c \
lib/m2/strcpy.c \
lib/mes/itoa.c \
lib/mes/fdungetc.c \
lib/m2/setenv.c \
lib/m2/access.c \
lib/m2/chmod.c \
lib/m2/isatty.c \
lib/m2/fork.c \
lib/m2/execve.c \
lib/m2/execv.c \
lib/m2/waitpid.c \
lib/m2/clock_gettime.c \
lib/m2/time.c \
lib/m2/_getcwd.c \
lib/m2/getcwd.c \
lib/m2/dup.c \
lib/m2/dup2.c \
lib/m2/unlink.c \
lib/m2/strcmp.c \
lib/m2/memcmp.c
M2_TODO = \
lib/m2/file_print.c \
@ -74,7 +131,6 @@ M2_TODO = \
lib/mes/fdgetc.c \
lib/mes/fdungetc.c
SOURCES = $(M2_SOURCES) $(LIB_SOURCES) $(MES_SOURCES)
INCLUDES = \
include/mes/builtins.h \
include/mes/constants.h \
@ -91,18 +147,21 @@ MES_LIBC = \
GCC_SOURCES = \
lib/mes/__mes_debug.c \
lib/mes/eputc.c \
lib/mes/eputs.c \
lib/mes/fdgetc.c \
lib/mes/fdputc.c \
lib/mes/fdputs.c \
lib/mes/fdungetc.c \
lib/mes/mes_open.c \
lib/mes/ntoab.c \
$(SOURCES)
lib/mes/itoa.c \
lib/mes/assert_msg.c \
$(MES_SOURCES)
mes-gcc: bin/mes-gcc
mes-m2: bin/mes-m2
bin/mes-gcc: $(MAKEFILES) $(GCC_SOURCES) $(INCLUDES) | bin
bin/mes-gcc: simple.make $(GCC_SOURCES) $(INCLUDES) | bin
$(CC) $(CFLAGS) $(GCC_SOURCES) -o $@
M2_PLANET_INCLUDES = \
@ -110,65 +169,96 @@ M2_PLANET_INCLUDES = \
include/mes/m2.h \
include/mes/builtins.h \
include/mes/constants.h \
include/mes/symbols.h
include/mes/symbols.h \
include/linux/$(M2_PLANET_ARCH)/syscall.h
M2_PLANET_PREFIX = ../M2-Planet
M2_PLANET_SOURCES = \
$(M2_PLANET_PREFIX)/test/common_amd64/functions/exit.c \
$(M2_PLANET_PREFIX)/test/common_amd64/functions/malloc.c \
$(M2_PLANET_PREFIX)/functions/calloc.c \
$(M2_PLANET_INCLUDES:%.h=%.h.m2) \
$(SOURCES:%.c=%.c.m2)
M2_PLANET_SOURCES = \
$(M2_PLANET_INCLUDES:%.h=%.h.m2) \
$(M2_SOURCES) \
$(MES_SOURCES:%.c=%.c.m2)
%.h.m2: %.h $(MAKEFILES)
@sed -r \
-e 's,^//,@@,' \
-e 's@^(#include.*)@/* \1 */@' \
$< \
| $(CC) -E -I include \
-D __M2_PLANET__=1 \
-D FUNCTION0=FUNCTION \
-D FUNCTION1=FUNCTION \
-D FUNCTION2=FUNCTION \
-D FUNCTION3=FUNCTION \
-D FUNCTIONN=FUNCTION \
-D const= \
-D long=SCM \
-D size_t=SCM \
-D ssize_t=SCM \
-D unsigned=SCM \
-include mes/m2.h \
-x c - \
| sed -r \
-e 's,^@@,//,' \
> $@ \
%.c.m2: %.c $(MAKEFILES)
@sed -r \
-e 's,^//,@@,' \
-e 's@^(#include.*)@/* \1 */@' \
$< \
| $(CC) -E -I include \
-D __M2_PLANET__=1 \
-D FUNCTION0=FUNCTION \
-D FUNCTION1=FUNCTION \
-D FUNCTION2=FUNCTION \
-D FUNCTION3=FUNCTION \
-D FUNCTIONN=FUNCTION \
-D const= \
-D long=SCM \
-D size_t=SCM \
-D ssize_t=SCM \
-D unsigned=SCM \
-include mes/m2.h \
-x c - \
| sed -r \
-e 's,^@@,//,' \
%.h.m2: %.h simple.make
@sed -r \
-e 's,^//,@@,' \
-e 's@^(#include.*)@/* \1 */@' \
$< \
| $(CC) -E -I include \
-D POINTER_CELLS=0 \
-D __M2_PLANET__=1 \
-D 'MES_VERSION="git"' \
-D 'MES_PKGDATADIR="/usr/local/share/mes"' \
-D FUNCTION0=FUNCTION \
-D FUNCTION1=FUNCTION \
-D FUNCTION2=FUNCTION \
-D FUNCTION3=FUNCTION \
-D FUNCTIONN=FUNCTION \
-D const= \
-D long=SCM \
-D size_t=SCM \
-D ssize_t=SCM \
-D unsigned=SCM \
-include mes/m2.h \
-x c - \
| sed -r \
-e 's,^@@,//,' \
> $@
bin/mes-m2: $(MAKEFILES) $(M2_PLANET_SOURCES) $(M2_PLANET_INCLUDES) | bin
%.c.m2: %.c simple.make
@sed -r \
-e 's,^//,@@,' \
-e 's@^(#include.*)@/* \1 */@' \
$< \
| $(CC) -E -I include \
-D POINTER_CELLS=0 \
-D __M2_PLANET__=1 \
-D 'MES_VERSION="git"' \
-D 'MES_PKGDATADIR="/usr/local/share/mes"' \
-D FUNCTION0=FUNCTION \
-D FUNCTION1=FUNCTION \
-D FUNCTION2=FUNCTION \
-D FUNCTION3=FUNCTION \
-D FUNCTIONN=FUNCTION \
-D EOF=-1 \
-D const= \
-D long=SCM \
-D size_t=SCM \
-D ssize_t=SCM \
-D unsigned=SCM \
-include mes/m2.h \
-x c - \
| sed -r \
-e 's,^@@,//,' \
> $@
bin/mes-m2.M1: simple.make $(M2_PLANET_SOURCES) $(M2_PLANET_INCLUDES) | bin
$(M2_PLANET) $(M2_PLANET_FLAGS) $(M2_PLANET_SOURCES:%=-f %) -o $@ || rm -f $@
bin/mes-m2.blood-elf.M1: bin/mes-m2.M1
# blood-elf --32 -f $< -o $@
blood-elf -f $< -o $@
bin/mes-m2.hex2: bin/mes-m2.blood-elf.M1
M1 \
--LittleEndian \
--architecture $(M2_PLANET_ARCH) \
-f lib/m2/x86/x86_defs.M1 \
-f lib/x86-mes/x86.M1 \
-f lib/linux/x86-mes-m2/crt1.M1 \
-f bin/mes-m2.M1 \
-f bin/mes-m2.blood-elf.M1 \
-o $@
bin/mes-m2: bin/mes-m2.hex2
hex2 \
--LittleEndian \
--architecture $(M2_PLANET_ARCH) \
--BaseAddress 0x1000000 \
--exec_enable \
-f lib/x86-mes/elf32-header.hex2 \
-f bin/mes-m2.hex2 \
-f lib/m2/missing.hex2 \
-o $@
# Clean up after ourselves
.PHONY: clean
clean: