diff options
author | Anton Kling <anton@kling.gg> | 2023-11-15 21:12:44 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2023-11-15 21:40:13 +0100 |
commit | c9358cdeac4522922df46fb6e3ab6a517203ec99 (patch) | |
tree | a033b4386f1aa825c06d549d3a3bd8f4c0156ddd /userland/libc | |
parent | 6713b6a6c112f0de92c63e349d100fc4bd89138e (diff) |
LibC: Add getdelim and getline
Diffstat (limited to 'userland/libc')
-rw-r--r-- | userland/libc/Makefile | 2 | ||||
-rw-r--r-- | userland/libc/include/stdio.h | 4 | ||||
-rw-r--r-- | userland/libc/malloc/malloc.h | 1 | ||||
-rw-r--r-- | userland/libc/stdio/getdelim.c | 28 | ||||
-rw-r--r-- | userland/libc/stdio/getline.c | 5 |
5 files changed, 39 insertions, 1 deletions
diff --git a/userland/libc/Makefile b/userland/libc/Makefile index ec1162b..4543a74 100644 --- a/userland/libc/Makefile +++ b/userland/libc/Makefile @@ -2,7 +2,7 @@ CC="i686-sb-gcc" AR="i686-sb-ar" AS="i686-sb-as" CFLAGS = -ggdb -ffreestanding -O2 -Wall -pedantic -Wimplicit-fallthrough -I./include/ -static -OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.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 +OBJ=crt0.o libc.o malloc/malloc.o pty.o sys/mman/mmap.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 all: libc.a %.o: %.c diff --git a/userland/libc/include/stdio.h b/userland/libc/include/stdio.h index 94ac9dc..aafae78 100644 --- a/userland/libc/include/stdio.h +++ b/userland/libc/include/stdio.h @@ -115,4 +115,8 @@ int fsetpos(FILE *stream, const fpos_t *pos); int fgetpos(FILE *restrict stream, fpos_t *restrict pos); char *tmpnam(char *s); int rename(const char *old, const char *new); +size_t getdelim(char ** lineptr, size_t * n, + int delimiter, FILE * stream); +size_t getline(char ** lineptr, size_t * n, + FILE * stream); #endif diff --git a/userland/libc/malloc/malloc.h b/userland/libc/malloc/malloc.h index 082d8ad..596dbe5 100644 --- a/userland/libc/malloc/malloc.h +++ b/userland/libc/malloc/malloc.h @@ -5,5 +5,6 @@ void *malloc(size_t s); void *calloc(size_t nelem, size_t elsize); +void *realloc(void *ptr, size_t size); void free(void *p); #endif diff --git a/userland/libc/stdio/getdelim.c b/userland/libc/stdio/getdelim.c new file mode 100644 index 0000000..3a6f23e --- /dev/null +++ b/userland/libc/stdio/getdelim.c @@ -0,0 +1,28 @@ +#include <stdio.h> +#include <stdlib.h> + +size_t getdelim(char **lineptr, size_t *n, int delimiter, FILE *stream) { + if (NULL == *lineptr) { + *lineptr = malloc(256); + *n = 256; + } + size_t s = 0; + for (;;) { + char c; + if (0 == fread(&c, 1, 1, stream)) { + s++; + break; + } + if (c == delimiter) { + break; + } + if (s + 1 >= *n) { + *n += 256; + *lineptr = realloc(*lineptr, *n); + } + (*lineptr)[s] = c; + s++; + } + (*lineptr)[s] = '\0'; + return s; +} diff --git a/userland/libc/stdio/getline.c b/userland/libc/stdio/getline.c new file mode 100644 index 0000000..5e9671e --- /dev/null +++ b/userland/libc/stdio/getline.c @@ -0,0 +1,5 @@ +#include <stdio.h> + +size_t getline(char **lineptr, size_t *n, FILE *stream) { + return getdelim(lineptr, n, '\n', stream); +} |