Added, fixed, and type-safed a bunch of stringlib functions. Fixed some
authorDavid Given <dg@cowlark.com>
Sun, 15 Mar 2015 01:01:21 +0000 (02:01 +0100)
committerDavid Given <dg@cowlark.com>
Sun, 15 Mar 2015 01:01:21 +0000 (02:01 +0100)
incorrect prototypes in the headers.

--HG--
extra : source : 3aa32af0f4d668e3adf3ede31e9e015c2a4e0b4b

18 files changed:
Library/include/string.h
Library/include/syscalls.h
Library/libs/memchr.c
Library/libs/memcmp.c
Library/libs/memcpy.c
Library/libs/memmove.c [new file with mode: 0644]
Library/libs/memset.c
Library/libs/putchar.c [new file with mode: 0644]
Library/libs/strcat.c
Library/libs/strchr.c
Library/libs/strcmp.c
Library/libs/strcpy.c
Library/libs/strcspn.c
Library/libs/strlen.c
Library/libs/strncat.c
Library/libs/strncmp.c [new file with mode: 0644]
Library/libs/strncpy.c
Library/libs/strrchr.c

index ed8c044..9448bcf 100644 (file)
@@ -12,8 +12,8 @@ extern char * strcat __P ((char*, const char*));
 extern char * strcpy __P ((char*, const char*));
 extern int strcmp __P ((const char*, const char*));
 
-extern char * strncat __P ((const char*, const char*, size_t));
-extern char * strncpy __P ((const char*, const char*, size_t));
+extern char * strncat __P ((char*, const char*, size_t));
+extern char * strncpy __P ((char*, const char*, size_t));
 extern int strncmp __P((const char*, const char*, size_t));
 
 extern int stricmp __P((const char*, const char*));
index 6c097a9..b464046 100644 (file)
@@ -73,7 +73,11 @@ struct hd_geometry {
 #define HDIO_GETGEO            0x0101
 #define HDIO_GET_IDENTITY      0x0102  /* Not yet implemented anywhere */
 
-extern int _exit(int code);
+struct times;
+struct utsname;
+struct tms;
+
+extern void _exit(int code);
 extern int open(const char *path, int flags, ...);
 extern int close(int fd);
 extern int creat(const char *path, mode_t mode);
@@ -146,7 +150,7 @@ extern int alarm(uint16_t seconds);
 extern time_t time(time_t *t);
 extern int stime(const time_t *t);
 extern int times(struct tms *tms);
-extern int utime(const char *filename, const struct utimbuf *utim);
+//extern int utime(const char *filename, const struct utimbuf *utim);
 extern int uname(struct utsname *buf);
 extern int profil(unsigned short *bufbase, size_t bufsize, unsigned long offset,
                   unsigned int scale);
index c18f224..ec1460e 100644 (file)
@@ -7,13 +7,13 @@
 #include <string.h>\r
     \r
 /********************** Function memchr ************************************/ \r
-void *memchr(void *str, int c, size_t l) \r
+void *memchr(const void *str, int c, size_t l) \r
 {
-       register char *p = (char *) str;
+       register const char *p = str;
 \r
        while (l-- != 0) {
                if (*p == c)
-                       return p;
+                       return (void*) p;
                p++;
        }
        return NULL;
index 75b2ce0..338acc8 100644 (file)
@@ -1,61 +1,21 @@
-/* memcmp.c\r
- * Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>\r
- * This file is part of the Linux-8086 C library and is distributed\r
- * under the GNU Library General Public License.\r
- *\r
- * Z80 rewrite from UMZIX\r
+/*\r
+ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.\r
+ * This file is licensed under the terms of the MIT open source license.\r
  */\r
\r
-#include <stdlib.h>\r
 \r
-/********************** Function memcmp ************************************/\r
-int memcmp(void *s, void *d, size_t l) __naked\r
-{\r
-__asm\r
-\r
-        push    ix\r
-        ld      ix,#0\r
-        add     ix,sp\r
+#include       <string.h>\r
 \r
-        ; d = BC, s=HL, l=DE\r
-       \r
-        ld      l, 4(ix)\r
-        ld      h, 5(ix)\r
-        ld      c, 6(ix)\r
-        ld      b, 7(ix)\r
-        ld      e, 8(ix)\r
-        ld      d, 9(ix)\r
-        push    bc\r
-        pop     iy      ; IY=d\r
-        ld      bc,#0x0000    ; char1, char2\r
-l_1:      \r
-       ld      a,(hl)\r
-        ld      b,a\r
-        ld      a,(iy)  ; char1 != char 2 ?\r
-        ld      c,a\r
-        cp      b\r
-        jr      nz,l_2\r
-        inc     hl     ; s++\r
-        inc     iy     ; d++\r
-        dec     de     ; l--\r
-        ld      a,d\r
-        or      e\r
-        jp      nz,l_1 ; l != 0, continue\r
-l_2:      \r
-       ld      a,c     ; char1 - char2\r
-        ld      e,a\r
-       rla\r
-       sbc     a,a\r
-       ld      d,a\r
-        ld      a,b\r
-        ld      l,b\r
-       rla\r
-       sbc     a,a\r
-       ld      h,a\r
-       or      a\r
-       sbc     hl,de\r
+int\r
+memcmp(const void *s1, const void *s2, size_t n)\r
+{\r
+       register const unsigned char *p1 = s1, *p2 = s2;\r
 \r
-       pop ix\r
-       ret\r
-__endasm;\r
+       if (n) {\r
+               n++;\r
+               while (--n > 0) {\r
+                       if (*p1++ == *p2++) continue;\r
+                       return *--p1 - *--p2;\r
+               }\r
+       }\r
+       return 0;\r
 }\r
index d56b275..dfddc51 100644 (file)
@@ -1,31 +1,22 @@
-#include <string.h>
+/*
+ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ * This file is licensed under the terms of the MIT open source license.
+ */
 
-/* Z80 rewrite from UMZIX */
+#include       <string.h>
 
-void *memcpy(void *dst, void *src, size_t count) __naked
+void *
+memcpy(void *s1, const void *s2, register size_t n)
 {
-__asm
-        push    ix
-        ld      ix,#0
-        add     ix,sp
-       
-        ld e, 4(ix)
-        ld d, 5(ix)
-        ld l, 6(ix)
-        ld h, 7(ix)
-        ld c, 8(ix)
-        ld b, 9(ix)
-       push de
-        ld      a,b
-        or      c
-        jr      z,_skip
+       register char *p1 = s1;
+       register const char *p2 = s2;
 
-        ldir
 
-_skip:
-       pop hl
-
-       pop ix
-       ret
-__endasm;
+       if (n) {
+               n++;
+               while (--n > 0) {
+                       *p1++ = *p2++;
+               }
+       }
+       return s1;
 }
diff --git a/Library/libs/memmove.c b/Library/libs/memmove.c
new file mode 100644 (file)
index 0000000..d891b13
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ * This file is licensed under the terms of the MIT open source license.
+ */
+
+#include       <string.h>
+
+void *
+memmove(void *s1, const void *s2, register size_t n)
+{
+       register char *p1 = s1;
+       register const char *p2 = s2;
+
+       if (n>0) {
+               if (p2 <= p1 && p2 + n > p1) {
+                       /* overlap, copy backwards */
+                       p1 += n;
+                       p2 += n;
+                       n++;
+                       while (--n > 0) {
+                               *--p1 = *--p2;
+                       }
+               } else {
+                       n++;
+                       while (--n > 0) {
+                               *p1++ = *p2++;
+                       }
+               }
+       }
+       return s1;
+}
index 909d989..c60018c 100644 (file)
@@ -1,44 +1,19 @@
-/* memset.c\r
- * Copyright (C) 1995,1996 Robert de Bath <rdebath@cix.compulink.co.uk>\r
- * This file is part of the Linux-8086 C library and is distributed\r
- * under the GNU Library General Public License.\r
- *\r
- * Z80 rewrite from UMZIX\r
+/*\r
+ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.\r
+ * This file is licensed under the terms of the MIT open source license.\r
  */\r
+#include       <string.h>\r
 \r
-#include <stdlib.h>\r
-\r
-/********************** Function memset ************************************/\r
-void *memset(void *str, int c, size_t l) __naked\r
+void *\r
+memset(void *s, register int c, register size_t n)\r
 {\r
-__asm\r
-        push    ix\r
-        ld      ix,#0\r
-        add     ix,sp\r
+       register char *s1 = s;\r
 \r
-       ld l, 4(ix)\r
-       ld h, 5(ix)\r
-       ld d, 6(ix)\r
-       ld c, 8(ix)\r
-       ld b, 9(ix)\r
-       ld a,b\r
-       or c    ; l=0? so return\r
-       jr z,_retw\r
-       ld a,d\r
-       ld (hl),a       ; fill first byte\r
-       ld d,h\r
-       ld e,l\r
-       inc de  ; DE=str+1\r
-       dec bc\r
-       ld a,b  ; l=1? so return\r
-       or c\r
-       jr z,_retw\r
-       push hl\r
-       ldir\r
-       pop hl\r
-       \r
-_retw:\r
-       pop ix\r
-       ret\r
-__endasm;\r
+       if (n>0) {\r
+               n++;\r
+               while (--n > 0) {\r
+                       *s1++ = c;\r
+               }\r
+       }\r
+       return s;\r
 }\r
diff --git a/Library/libs/putchar.c b/Library/libs/putchar.c
new file mode 100644 (file)
index 0000000..dbe91dd
--- /dev/null
@@ -0,0 +1,10 @@
+#include <stdio.h>
+
+/* Some compilers will emit calls to this function, even though it's strictly
+ * a macro; so we have to have a real function version of it. */
+
+int (putchar)(int c)
+{
+       return putchar(c);
+}
+
index f87818a..3262742 100644 (file)
@@ -7,7 +7,7 @@
 #include <string.h>\r
     \r
 /********************** Function strcat ************************************/ \r
-char *strcat(char *d, char *s) \r
+char *strcat(char *d, const char *s) \r
 {
        strcpy(d + strlen(d), s);
        return d;
index fa8c69e..0b0d215 100644 (file)
@@ -8,7 +8,7 @@
 \r
 /* FIXME: asm version ?? */    \r
 /********************** Function strchr ************************************/ \r
-char *strchr(char *s, int c) \r
+char *strchr(const char *s, int c) \r
 {
        register char ch;
        \r
index 870384d..a689aab 100644 (file)
@@ -7,7 +7,7 @@
 #include <string.h>\r
     \r
 /********************** Function strcmp ************************************/ \r
-int strcmp(char *d, char *s) \r
+int strcmp(const char *d, const char *s) \r
 {
        register char *s1 = (char *) d, *s2 = (char *) s, c1, c2;
 \r
index eab28a7..5b15a2c 100644 (file)
@@ -7,7 +7,7 @@
 #include <string.h>\r
     \r
 /********************** Function strcpy ************************************/ \r
-char *strcpy(char *d, char *s) \r
+char *strcpy(char *d, const char *s) \r
 {
        return memcpy(d, s, strlen(s) + 1);
 }
index ab2efbc..69ab8ae 100644 (file)
@@ -4,7 +4,7 @@
 
 #include <string.h>
 
-size_t strcspn(char *string, char *set)
+size_t strcspn(const char *string, const char *set)
 /*
  *     Return the length of the sub-string of <string> that consists
  *     entirely of characters not found in <set>.  The terminating '\0'
@@ -12,7 +12,7 @@ size_t strcspn(char *string, char *set)
  *     character if <string> is in <set>, 0 is returned.
  */
 {
-    register char *setptr;
+    register const char *setptr;
     char *start;
 
     start = string;
index a643115..98c1e27 100644 (file)
@@ -7,19 +7,10 @@
 #include <string.h>
 
 /********************** Function strlen ************************************/
-size_t strlen(char *str) __naked
+size_t strlen(const char *str)
 {
-__asm
-        pop     bc
-        pop     hl
-        push    hl
-        push    bc
-        xor     a, a
-        ld      b, a
-        ld      c, a
-        cpir
-        ld      hl, #-1
-        sbc     hl, bc  ; C flag still cleared from xor above.
-        ret
-__endasm;
+       const char* start = str;
+       while (*str++)
+               ;
+       return str - start;
 }
index 9db164f..e5db378 100644 (file)
@@ -7,7 +7,7 @@
 #include <string.h>\r
     \r
 /********************** Function strncat ************************************/ \r
-char *strncat(char *d, char *s, size_t l) \r
+char *strncat(char *d, const char *s, size_t l) \r
 {
        register char *s1 = d + strlen(d), *s2 = memchr(s, 0, l);
 \r
diff --git a/Library/libs/strncmp.c b/Library/libs/strncmp.c
new file mode 100644 (file)
index 0000000..b748aa0
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
+ * This file is licensed under the terms of the MIT open source license.
+ */
+
+#include       <string.h>
+
+int
+strncmp(register const char *s1, register const char *s2, register size_t n)
+{
+       if (n) {
+               do {
+                       if (*s1 != *s2++)
+                               break;
+                       if (*s1++ == '\0')
+                               return 0;
+               } while (--n > 0);
+               if (n > 0) {
+                       if (*s1 == '\0') return -1;
+                       if (*--s2 == '\0') return 1;
+                       return (unsigned char) *s1 - (unsigned char) *s2;
+               }
+       }
+       return 0;
+}
index 81dd2db..0742b6a 100644 (file)
@@ -7,9 +7,10 @@
 #include <string.h>\r
     \r
 /********************** Function strncpy ************************************/ \r
-char *strncpy(char *d, char *s, size_t l) \r
+char *strncpy(char *d, const char *s, size_t l) \r
 {
-       register char *s1 = d, *s2 = s;\r
+       register char *s1 = d;
+       register const char *s2 = s;\r
 \r
        while (l) {
                l--;
index a58ee8b..a8c3823 100644 (file)
@@ -7,9 +7,9 @@
 #include <string.h>\r
     \r
 /********************** Function strrchr ************************************/ \r
-char *strrchr(char *s, int c) \r
+char *strrchr(const char *s, int c) \r
 {
-       register char *p = s + strlen(s);
+       register const char *p = s + strlen(s);
         \r
        /* For null it's just like strlen */ \r
        if (c == '\0')