summaryrefslogtreecommitdiff
path: root/userland/libc/stdio/getdelim.c
diff options
context:
space:
mode:
Diffstat (limited to 'userland/libc/stdio/getdelim.c')
-rw-r--r--userland/libc/stdio/getdelim.c28
1 files changed, 28 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;
+}