From: David Given Date: Sun, 15 Mar 2015 01:01:21 +0000 (+0100) Subject: Added, fixed, and type-safed a bunch of stringlib functions. Fixed some X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=49bdc22f0c80e29b278056e702a572518fe38f06;p=FUZIX.git Added, fixed, and type-safed a bunch of stringlib functions. Fixed some incorrect prototypes in the headers. --HG-- extra : source : 3aa32af0f4d668e3adf3ede31e9e015c2a4e0b4b --- diff --git a/Library/include/string.h b/Library/include/string.h index ed8c044d..9448bcfa 100644 --- a/Library/include/string.h +++ b/Library/include/string.h @@ -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*)); diff --git a/Library/include/syscalls.h b/Library/include/syscalls.h index 6c097a9f..b4640461 100644 --- a/Library/include/syscalls.h +++ b/Library/include/syscalls.h @@ -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); diff --git a/Library/libs/memchr.c b/Library/libs/memchr.c index c18f224e..ec1460e4 100644 --- a/Library/libs/memchr.c +++ b/Library/libs/memchr.c @@ -7,13 +7,13 @@ #include /********************** Function memchr ************************************/ -void *memchr(void *str, int c, size_t l) +void *memchr(const void *str, int c, size_t l) { - register char *p = (char *) str; + register const char *p = str; while (l-- != 0) { if (*p == c) - return p; + return (void*) p; p++; } return NULL; diff --git a/Library/libs/memcmp.c b/Library/libs/memcmp.c index 75b2ce01..338acc86 100644 --- a/Library/libs/memcmp.c +++ b/Library/libs/memcmp.c @@ -1,61 +1,21 @@ -/* memcmp.c - * Copyright (C) 1995,1996 Robert de Bath - * This file is part of the Linux-8086 C library and is distributed - * under the GNU Library General Public License. - * - * Z80 rewrite from UMZIX +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * This file is licensed under the terms of the MIT open source license. */ - -#include -/********************** Function memcmp ************************************/ -int memcmp(void *s, void *d, size_t l) __naked -{ -__asm - - push ix - ld ix,#0 - add ix,sp +#include - ; d = BC, s=HL, l=DE - - ld l, 4(ix) - ld h, 5(ix) - ld c, 6(ix) - ld b, 7(ix) - ld e, 8(ix) - ld d, 9(ix) - push bc - pop iy ; IY=d - ld bc,#0x0000 ; char1, char2 -l_1: - ld a,(hl) - ld b,a - ld a,(iy) ; char1 != char 2 ? - ld c,a - cp b - jr nz,l_2 - inc hl ; s++ - inc iy ; d++ - dec de ; l-- - ld a,d - or e - jp nz,l_1 ; l != 0, continue -l_2: - ld a,c ; char1 - char2 - ld e,a - rla - sbc a,a - ld d,a - ld a,b - ld l,b - rla - sbc a,a - ld h,a - or a - sbc hl,de +int +memcmp(const void *s1, const void *s2, size_t n) +{ + register const unsigned char *p1 = s1, *p2 = s2; - pop ix - ret -__endasm; + if (n) { + n++; + while (--n > 0) { + if (*p1++ == *p2++) continue; + return *--p1 - *--p2; + } + } + return 0; } diff --git a/Library/libs/memcpy.c b/Library/libs/memcpy.c index d56b2757..dfddc518 100644 --- a/Library/libs/memcpy.c +++ b/Library/libs/memcpy.c @@ -1,31 +1,22 @@ -#include +/* + * (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 -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 index 00000000..d891b13e --- /dev/null +++ b/Library/libs/memmove.c @@ -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 + +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; +} diff --git a/Library/libs/memset.c b/Library/libs/memset.c index 909d989b..c60018c8 100644 --- a/Library/libs/memset.c +++ b/Library/libs/memset.c @@ -1,44 +1,19 @@ -/* memset.c - * Copyright (C) 1995,1996 Robert de Bath - * This file is part of the Linux-8086 C library and is distributed - * under the GNU Library General Public License. - * - * Z80 rewrite from UMZIX +/* + * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. + * This file is licensed under the terms of the MIT open source license. */ +#include -#include - -/********************** Function memset ************************************/ -void *memset(void *str, int c, size_t l) __naked +void * +memset(void *s, register int c, register size_t n) { -__asm - push ix - ld ix,#0 - add ix,sp + register char *s1 = s; - ld l, 4(ix) - ld h, 5(ix) - ld d, 6(ix) - ld c, 8(ix) - ld b, 9(ix) - ld a,b - or c ; l=0? so return - jr z,_retw - ld a,d - ld (hl),a ; fill first byte - ld d,h - ld e,l - inc de ; DE=str+1 - dec bc - ld a,b ; l=1? so return - or c - jr z,_retw - push hl - ldir - pop hl - -_retw: - pop ix - ret -__endasm; + if (n>0) { + n++; + while (--n > 0) { + *s1++ = c; + } + } + return s; } diff --git a/Library/libs/putchar.c b/Library/libs/putchar.c new file mode 100644 index 00000000..dbe91ddb --- /dev/null +++ b/Library/libs/putchar.c @@ -0,0 +1,10 @@ +#include + +/* 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); +} + diff --git a/Library/libs/strcat.c b/Library/libs/strcat.c index f87818aa..3262742a 100644 --- a/Library/libs/strcat.c +++ b/Library/libs/strcat.c @@ -7,7 +7,7 @@ #include /********************** Function strcat ************************************/ -char *strcat(char *d, char *s) +char *strcat(char *d, const char *s) { strcpy(d + strlen(d), s); return d; diff --git a/Library/libs/strchr.c b/Library/libs/strchr.c index fa8c69e0..0b0d2156 100644 --- a/Library/libs/strchr.c +++ b/Library/libs/strchr.c @@ -8,7 +8,7 @@ /* FIXME: asm version ?? */ /********************** Function strchr ************************************/ -char *strchr(char *s, int c) +char *strchr(const char *s, int c) { register char ch; diff --git a/Library/libs/strcmp.c b/Library/libs/strcmp.c index 870384d0..a689aaba 100644 --- a/Library/libs/strcmp.c +++ b/Library/libs/strcmp.c @@ -7,7 +7,7 @@ #include /********************** Function strcmp ************************************/ -int strcmp(char *d, char *s) +int strcmp(const char *d, const char *s) { register char *s1 = (char *) d, *s2 = (char *) s, c1, c2; diff --git a/Library/libs/strcpy.c b/Library/libs/strcpy.c index eab28a73..5b15a2ce 100644 --- a/Library/libs/strcpy.c +++ b/Library/libs/strcpy.c @@ -7,7 +7,7 @@ #include /********************** Function strcpy ************************************/ -char *strcpy(char *d, char *s) +char *strcpy(char *d, const char *s) { return memcpy(d, s, strlen(s) + 1); } diff --git a/Library/libs/strcspn.c b/Library/libs/strcspn.c index ab2efbca..69ab8ae4 100644 --- a/Library/libs/strcspn.c +++ b/Library/libs/strcspn.c @@ -4,7 +4,7 @@ #include -size_t strcspn(char *string, char *set) +size_t strcspn(const char *string, const char *set) /* * Return the length of the sub-string of that consists * entirely of characters not found in . The terminating '\0' @@ -12,7 +12,7 @@ size_t strcspn(char *string, char *set) * character if is in , 0 is returned. */ { - register char *setptr; + register const char *setptr; char *start; start = string; diff --git a/Library/libs/strlen.c b/Library/libs/strlen.c index a6431156..98c1e274 100644 --- a/Library/libs/strlen.c +++ b/Library/libs/strlen.c @@ -7,19 +7,10 @@ #include /********************** 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; } diff --git a/Library/libs/strncat.c b/Library/libs/strncat.c index 9db164f2..e5db3782 100644 --- a/Library/libs/strncat.c +++ b/Library/libs/strncat.c @@ -7,7 +7,7 @@ #include /********************** Function strncat ************************************/ -char *strncat(char *d, char *s, size_t l) +char *strncat(char *d, const char *s, size_t l) { register char *s1 = d + strlen(d), *s2 = memchr(s, 0, l); diff --git a/Library/libs/strncmp.c b/Library/libs/strncmp.c new file mode 100644 index 00000000..b748aa00 --- /dev/null +++ b/Library/libs/strncmp.c @@ -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 + +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; +} diff --git a/Library/libs/strncpy.c b/Library/libs/strncpy.c index 81dd2db1..0742b6a6 100644 --- a/Library/libs/strncpy.c +++ b/Library/libs/strncpy.c @@ -7,9 +7,10 @@ #include /********************** Function strncpy ************************************/ -char *strncpy(char *d, char *s, size_t l) +char *strncpy(char *d, const char *s, size_t l) { - register char *s1 = d, *s2 = s; + register char *s1 = d; + register const char *s2 = s; while (l) { l--; diff --git a/Library/libs/strrchr.c b/Library/libs/strrchr.c index a58ee8b9..a8c3823e 100644 --- a/Library/libs/strrchr.c +++ b/Library/libs/strrchr.c @@ -7,9 +7,9 @@ #include /********************** Function strrchr ************************************/ -char *strrchr(char *s, int c) +char *strrchr(const char *s, int c) { - register char *p = s + strlen(s); + register const char *p = s + strlen(s); /* For null it's just like strlen */ if (c == '\0')