From c9358cdeac4522922df46fb6e3ab6a517203ec99 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Wed, 15 Nov 2023 21:12:44 +0100 Subject: LibC: Add getdelim and getline --- userland/libc/stdio/getdelim.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 userland/libc/stdio/getdelim.c (limited to 'userland/libc/stdio/getdelim.c') 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 +#include + +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; +} -- cgit v1.2.3