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/stdio | |
parent | 6713b6a6c112f0de92c63e349d100fc4bd89138e (diff) |
LibC: Add getdelim and getline
Diffstat (limited to 'userland/libc/stdio')
-rw-r--r-- | userland/libc/stdio/getdelim.c | 28 | ||||
-rw-r--r-- | userland/libc/stdio/getline.c | 5 |
2 files changed, 33 insertions, 0 deletions
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); +} |