diff options
author | Anton Kling <anton@kling.gg> | 2024-04-17 16:20:12 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-04-17 16:20:21 +0200 |
commit | d3f8196eb2cd57fec5f6e2691fdbc802c4d92fc8 (patch) | |
tree | 7fed2fe700dbb496185fa29711ce0e0a4f76a458 | |
parent | efe10908766d96974c008a6a8f124916fdca7a14 (diff) |
LibC: Add memchr()
-rw-r--r-- | LICENSE | 2 | ||||
-rw-r--r-- | userland/libc/Makefile | 2 | ||||
-rw-r--r-- | userland/libc/include/string.h | 1 | ||||
-rw-r--r-- | userland/libc/string/memchr.c | 11 | ||||
-rw-r--r-- | userland/test/test.c | 21 |
5 files changed, 33 insertions, 4 deletions
@@ -1,7 +1,7 @@ DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE Version 2, December 2004 -Copyright (C) 2022-2023 Anton Kling <anton@kling.gg> +Copyright (C) 2022-2024 Anton Kling <anton@kling.gg> DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION diff --git a/userland/libc/Makefile b/userland/libc/Makefile index 29b37d9..e7b9f23 100644 --- a/userland/libc/Makefile +++ b/userland/libc/Makefile @@ -3,7 +3,7 @@ AR="i686-sb-ar" AS="i686-sb-as" #CFLAGS = -ggdb -ffreestanding -O2 -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type CFLAGS = -ggdb -ffreestanding -Wall -Wextra -pedantic -Wimplicit-fallthrough -I./include/ -static -I../../include/ -Wno-int-conversion -Wno-unused-parameter -Wno-return-type -OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o sys/mman/munmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o fcntl/open_process.o tb/sb.o tb/sv.o +OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.o sys/mman/munmap.o memset.o assert.o stdio/snprintf.o stdio/vfprintf.o string/memcpy.o string/memcmp.o string/strcmp.o ubsan.o string/strcpy.o isspace.o stdio/puts.o stdio/putchar.o dirent/opendir.o dirent/readdir.o dirent/closedir.o unistd/getopt.o dirent/scandir.o dirent/alphasort.o stdio/printf.o stdio/vdprintf.o stdio/vprintf.o stdio/dprintf.o stdio/vprintf.o string/strlen.o string/strnlen.o stdio/stdin.o stdio/getchar.o stdio/fgetc.o arpa/inet/htons.o arpa/inet/htonl.o stdio/fread.o stdio/fwrite.o stdio/fopen.o stdio/fclose.o stdio/fseek.o ctype/isascii.o stdio/fprintf.o stdlib/atoi.o stdlib/strtol.o ctype/toupper.o ctype/tolower.o string/strcat.o string/strchr.o string/sscanf.o sys/stat/stat.o stdlib/getenv.o string/strrchr.o stdio/ftell.o stdio/tmpfile.o stdio/fgets.o stdio/feof.o stdio/fscanf.o stdio/ungetc.o string/strncmp.o stdio/fputc.o string/strncpy.o stdio/remove.o stdio/ferror.o stdio/fputs.o stdlib/rand.o stdlib/srand.o unistd/getpid.o stdlib/strtoul.o stdio/fflush.o stdlib/abort.o string/strcspn.o time/localtime.o time/time.o time/clock_gettime.o time/gmtime.o time/strftime.o string/strpbrk.o ctype/isdigit.o ctype/isalpha.o ctype/isxdigit.o ctype/ispunct.o stdio/setvbuf.o stdio/fileno.o stdio/putc.o stdio/sprintf.o stdlib/abs.o string/strspn.o stdlib/qsort.o string/memmove.o setjmp/longjmp.o setjmp/setjmp.o libgen/basename.o string/strdup.o string/strndup.o string/strlcpy.o stdlib/atexit.o stdio/open_memstream.o libgen/dirname.o unistd/unlink.o string/strstr.o string/strcasecmp.o string/strncasecmp.o stdlib/mkstemp.o string/strtok.o unistd/execvp.o unistd/_exit.o ctype/isalnum.o time/ctime_r.o stdlib/strtold.o sys/time/gettimeofday.o stdio/fgetpos.o stdio/fsetpos.o ctype/isprint.o stdlib/system.o stdio/tmpnam.o unistd/msleep.o stdlib/atof.o stdlib/strtod.o stdio/rename.o sys/stat/mkdir.o unistd/uptime.o unistd/ftruncate.o sys/socket/recvfrom.o sys/socket/sendto.o signal/kill.o signal/sigaction.o unistd/chdir.o unistd/getcwd.o stdio/getdelim.o stdio/getline.o unistd/isatty.o sys/socket/listen.o stdlib/realpath.o systemcall.o sys/random/randomfill.o fcntl/open.o unistd/write.o unistd/pwrite.o fcntl/open_process.o tb/sb.o tb/sv.o string/memchr.o all: libc.a %.o: %.c diff --git a/userland/libc/include/string.h b/userland/libc/include/string.h index 65d0796..accc129 100644 --- a/userland/libc/include/string.h +++ b/userland/libc/include/string.h @@ -3,6 +3,7 @@ #include <stddef.h> #include <stdint.h> +void *memchr(const void *s, int c, size_t n); char *strerror(int errnum); void *memset(void *s, int c, size_t n); void *memcpy(void *dest, const void *src, uint32_t n); diff --git a/userland/libc/string/memchr.c b/userland/libc/string/memchr.c new file mode 100644 index 0000000..cef3bc2 --- /dev/null +++ b/userland/libc/string/memchr.c @@ -0,0 +1,11 @@ +#include <string.h> + +void *memchr(const void *s, int c, size_t n) { + const char *p = s; + for (; n > 0; n--, p++) { + if (*p == c) { + return (void*)p; + } + } + return NULL; +} diff --git a/userland/test/test.c b/userland/test/test.c index 940854c..53fbf55 100644 --- a/userland/test/test.c +++ b/userland/test/test.c @@ -9,7 +9,9 @@ #include <string.h> #if 1 -void dbgln(const char *fmt) { printf("%s\n", fmt); } +void dbgln(const char *fmt) { + printf("%s\n", fmt); +} #else void dbgln(const char *fmt, ...) { printf("| "); @@ -623,7 +625,9 @@ void printf_test(void) { dbgln("printf TEST PASSED"); } -int cmpfunc(const void *a, const void *b) { return (*(char *)a - *(char *)b); } +int cmpfunc(const void *a, const void *b) { + return (*(char *)a - *(char *)b); +} void qsort_test(void) { dbgln("qsort TEST"); @@ -735,6 +739,18 @@ void realpath_test(void) { dbgln("realpath TEST PASSED"); } +void memchr_test(void) { + dbgln("memchr TEST"); + { + char buffer[] = "012345"; + assert(NULL == memchr(buffer, '6', sizeof(buffer))); + assert(&buffer[0] == memchr(buffer, '0', sizeof(buffer))); + assert(&buffer[1] == memchr(buffer, '1', sizeof(buffer))); + assert(&buffer[5] == memchr(buffer, '5', sizeof(buffer))); + } + dbgln("memchr TEST PASSED"); +} + void randomfill_test(void) { dbgln("randomfill TEST"); { @@ -794,6 +810,7 @@ int main(void) { dirname_test(); getline_test(); realpath_test(); + memchr_test(); randomfill_test(); // TODO: Add mkstemp return 0; |