From 44cd2f132f5b6ba968aa3ba26402546c97236b8b Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Thu, 24 Oct 2019 09:08:47 +0200 Subject: [PATCH] m2 lib c --- include/linux/x86/syscall.h | 3 ++- lib/m2/_getcwd.c | 32 ++++++++++++++++++++++++++++++++ lib/m2/clock_gettime.c | 4 ++-- lib/m2/getcwd.c | 18 +++++++++++++++++- lib/m2/memcmp.c | 2 +- lib/m2/memcpy.c | 4 ++-- lib/m2/strcpy.c | 26 ++++++++++++++++++++------ 7 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 lib/m2/_getcwd.c diff --git a/include/linux/x86/syscall.h b/include/linux/x86/syscall.h index 346548cf..5ef2c003 100644 --- a/include/linux/x86/syscall.h +++ b/include/linux/x86/syscall.h @@ -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 */ diff --git a/lib/m2/_getcwd.c b/lib/m2/_getcwd.c new file mode 100644 index 00000000..3f69ec30 --- /dev/null +++ b/lib/m2/_getcwd.c @@ -0,0 +1,32 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU Mes is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at + * your option) any later version. + * + * GNU Mes is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GNU Mes. If not, see . + */ + +#include +#include +#include + +char * +_getcwd (char *buffer, int size) +{ + int r = _sys_call2 (SYS_getcwd, buffer, size); + if (r >= 0) + return buffer; + return 0; +} diff --git a/lib/m2/clock_gettime.c b/lib/m2/clock_gettime.c index 7aa42398..95890e9c 100644 --- a/lib/m2/clock_gettime.c +++ b/lib/m2/clock_gettime.c @@ -20,8 +20,8 @@ struct timespec { - long tv_sec; - long tv_nsec; + int tv_sec; + int tv_nsec; }; int clock_gettime (int clk_id, struct timespec *tp); diff --git a/lib/m2/getcwd.c b/lib/m2/getcwd.c index ad57b99f..72d431f4 100644 --- a/lib/m2/getcwd.c +++ b/lib/m2/getcwd.c @@ -18,4 +18,20 @@ * along with GNU Mes. If not, see . */ -char *getcwd (char *buffer, size_t size); +#include +#include +#include +#include + +// CONSTANT PATH_MAX 1024 + +char * +getcwd (char *buffer, int size) +{ + if (! __itoa_buf) + __itoa_buf = malloc (20); + char *buf = __itoa_buf; + if (buffer) + return _getcwd (buffer, size); + return _getcwd (buf, PATH_MAX); +} diff --git a/lib/m2/memcmp.c b/lib/m2/memcmp.c index 120ad3eb..3e4839b1 100644 --- a/lib/m2/memcmp.c +++ b/lib/m2/memcmp.c @@ -21,7 +21,7 @@ #include int -memcmp (void *s1, void *s2, size_t size) +memcmp (void *s1, void *s2, int size) { if (size == 0) return 0; diff --git a/lib/m2/memcpy.c b/lib/m2/memcpy.c index 94e76f10..1679b4f7 100644 --- a/lib/m2/memcpy.c +++ b/lib/m2/memcpy.c @@ -22,10 +22,10 @@ #include void * -memcpy (void *dest, void const *src, size_t n) +memcpy (void *dest, void *src, int n) { char *p = dest; - char const *q = src; + char *q = src; while (n != 0) { diff --git a/lib/m2/strcpy.c b/lib/m2/strcpy.c index df601154..33d5dcc2 100644 --- a/lib/m2/strcpy.c +++ b/lib/m2/strcpy.c @@ -1,4 +1,4 @@ -/* -*-comment-start: "//";comment-end:""-*- +/* -*-comment-start: " * GNU Mes --- Maxwell Equations of Software * Copyright © 2016,2017,2018,2019 Jan (janneke) Nieuwenhuizen * @@ -23,15 +23,29 @@ char * strcpy (char *dest, char *src) { + /* eputs ("\nstrcpy: src="); */ + /* eputs (src); */ + /* eputs ("\n"); */ char *p = dest; + char *orig = dest; - while (src[0] != 0) + /* eputs ("dest="); */ + /* eputs (dest); */ + /* eputs ("\n"); */ + + /* eputs ("c:" ); */ + while (0 != src[0]) { - p[0] = src[0]; - p + p + 1; + /* eputc (src[0]); */ + /* eputs (" "); */ + dest[0] = src[0]; + dest = dest + 1; src = src + 1; } - p[0] = 0; + dest[0] = 0; + /* eputs ("\n => orig="); */ + /* eputs (orig); */ + /* eputs ("\n"); */ - return dest; + return p; }