summaryrefslogtreecommitdiff
path: root/userland
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-11-15 21:12:44 +0100
committerAnton Kling <anton@kling.gg>2023-11-15 21:40:13 +0100
commitc9358cdeac4522922df46fb6e3ab6a517203ec99 (patch)
treea033b4386f1aa825c06d549d3a3bd8f4c0156ddd /userland
parent6713b6a6c112f0de92c63e349d100fc4bd89138e (diff)
LibC: Add getdelim and getline
Diffstat (limited to 'userland')
-rw-r--r--userland/libc/Makefile2
-rw-r--r--userland/libc/include/stdio.h4
-rw-r--r--userland/libc/malloc/malloc.h1
-rw-r--r--userland/libc/stdio/getdelim.c28
-rw-r--r--userland/libc/stdio/getline.c5
-rw-r--r--userland/minibox/utilities/ed.c6
-rw-r--r--userland/test/test.c33
7 files changed, 75 insertions, 4 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);
+}
diff --git a/userland/minibox/utilities/ed.c b/userland/minibox/utilities/ed.c
index 53270dc..bcaba36 100644
--- a/userland/minibox/utilities/ed.c
+++ b/userland/minibox/utilities/ed.c
@@ -12,7 +12,7 @@ FILE *fp = NULL;
FILE *mem_fp = NULL;
int line_number = 1;
-int getline(char *buffer, size_t s) {
+int ed_getline(char *buffer, size_t s) {
(void)s;
int i = 0;
char c;
@@ -86,7 +86,7 @@ void delete_line(void) {
void read_command(void) {
char buffer[4096];
char *s = buffer;
- getline(buffer, 4096);
+ ed_getline(buffer, 4096);
int a = -1;
int rc = sscanf(buffer, "%d", &a);
if (0 < rc) {
@@ -107,7 +107,7 @@ void read_command(void) {
void read_input(void) {
char buffer[4096];
- int l = getline(buffer, 4096);
+ int l = ed_getline(buffer, 4096);
if (0 == strcmp(buffer, ".")) {
mode = COMMAND_MODE;
return;
diff --git a/userland/test/test.c b/userland/test/test.c
index ca8c142..2e4c83d 100644
--- a/userland/test/test.c
+++ b/userland/test/test.c
@@ -686,6 +686,38 @@ void dirname_test(void) {
dbgln("dirname TEST PASSED");
}
+void getline_test(void) {
+ dbgln("getline TEST");
+ {
+ char *buffer;
+ size_t size;
+ FILE *fp = open_memstream(&buffer, &size);
+ assert(fp);
+ const char *s = "line1\nline2\nfoo\nbar";
+ size_t line = strlen(s);
+ assert(fwrite(s, 1, line, fp) == line);
+ assert(0 == fseek(fp, 0, SEEK_SET));
+ char *line_buffer = NULL;
+ {
+ size_t n;
+ n = 256;
+ getline(&line_buffer, &n, fp);
+ assert(0 == strcmp("line1", line_buffer));
+ n = 256;
+ getline(&line_buffer, &n, fp);
+ assert(0 == strcmp("line2", line_buffer));
+ n = 256;
+ getline(&line_buffer, &n, fp);
+ assert(0 == strcmp("foo", line_buffer));
+ n = 256;
+ getline(&line_buffer, &n, fp);
+ assert(0 == strcmp("bar", line_buffer));
+ }
+ free(buffer);
+ }
+ dbgln("getline TEST PASSED");
+}
+
int main(void) {
dbgln("START");
malloc_test();
@@ -723,6 +755,7 @@ int main(void) {
qsort_test();
basename_test();
dirname_test();
+ getline_test();
// TODO: Add mkstemp
return 0;
}