summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2023-10-24 13:50:36 +0200
committerAnton Kling <anton@kling.gg>2023-10-24 13:50:36 +0200
commit3ae251a204748a2f1c4c2af6c55e73d5e5159d0f (patch)
tree07d02eb1b25173e3af41284a9def4cdb9191bb97
parente91a4f1e31ee100b2a13cefedb182e9d69ef04ad (diff)
LibC: Allocate stdin, stdout, stderr on the heap so they can be closed with fclose
-rw-r--r--userland/libc/Makefile5
-rw-r--r--userland/libc/crt0.s1
-rw-r--r--userland/libc/include/stdio.h13
-rw-r--r--userland/libc/libc.c47
-rw-r--r--userland/libc/stdio/fclose.c5
-rw-r--r--userland/libc/stdio/stderr.c11
-rw-r--r--userland/libc/stdio/stdin.c10
-rw-r--r--userland/libc/stdio/stdout.c13
8 files changed, 52 insertions, 53 deletions
diff --git a/userland/libc/Makefile b/userland/libc/Makefile
index 18c80fa..0681eda 100644
--- a/userland/libc/Makefile
+++ b/userland/libc/Makefile
@@ -1,11 +1,8 @@
CC="i686-sb-gcc"
AR="i686-sb-ar"
AS="i686-sb-as"
-#CFLAGS = -ggdb -ffreestanding -O0 -Wall -Wextra -pedantic -mgeneral-regs-only -Wimplicit-fallthrough -I./include/ -static -fsanitize=shift,signed-integer-overflow,bounds
CFLAGS = -ggdb -ffreestanding -O2 -Wall -pedantic -Wimplicit-fallthrough -I./include/ -static
-#BINS=c.a libc.o crt0.o malloc.o pty.o mmap.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/stdout.o stdio/stderr.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
-#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/stdout.o stdio/stderr.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
+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
all: libc.a
%.o: %.c
diff --git a/userland/libc/crt0.s b/userland/libc/crt0.s
index 05144d4..c950949 100644
--- a/userland/libc/crt0.s
+++ b/userland/libc/crt0.s
@@ -1,6 +1,7 @@
.global _start
.extern main
_start:
+ call _libc_setup
call main
mov %eax, %ebx
mov $8, %eax
diff --git a/userland/libc/include/stdio.h b/userland/libc/include/stdio.h
index 22afbb2..94ac9dc 100644
--- a/userland/libc/include/stdio.h
+++ b/userland/libc/include/stdio.h
@@ -48,14 +48,13 @@ typedef struct {
int fd;
} FILE_FD_COOKIE;
-extern FILE __stdin_FILE;
-extern FILE __stdout_FILE;
-extern FILE __stderr_FILE;
+extern FILE *__stdin_FILE;
+extern FILE *__stdout_FILE;
+extern FILE *__stderr_FILE;
-#define stdin (&__stdin_FILE)
-#define stdout (&__stdout_FILE)
-//#define stderr (&__stderr_FILE)
-#define stderr (&__stdout_FILE)
+#define stdin __stdin_FILE
+#define stdout __stdout_FILE
+#define stderr __stderr_FILE
typedef int mode_t;
diff --git a/userland/libc/libc.c b/userland/libc/libc.c
index 259ef0c..90c0323 100644
--- a/userland/libc/libc.c
+++ b/userland/libc/libc.c
@@ -96,6 +96,41 @@ char *errno_strings[] = {
#define TAB_SIZE 8
+FILE *__stdin_FILE;
+FILE *__stdout_FILE;
+FILE *__stderr_FILE;
+#define stdin __stdin_FILE
+#define stdout __stdout_FILE
+#define stderr __stderr_FILE
+
+void _libc_setup(void) {
+ __stdin_FILE = malloc(sizeof(FILE));
+ __stdin_FILE->write = write_fd;
+ __stdin_FILE->read = read_fd;
+ __stdin_FILE->seek = NULL;
+ __stdin_FILE->is_eof = 0;
+ __stdin_FILE->has_error = 0;
+ __stdin_FILE->cookie = NULL;
+ __stdin_FILE->fd = 0;
+
+ __stdout_FILE = malloc(sizeof(FILE));
+ __stdout_FILE->write = write_fd;
+ __stdout_FILE->read = read_fd;
+ __stdout_FILE->is_eof = 0;
+ __stdout_FILE->has_error = 0;
+ __stdout_FILE->seek = NULL;
+ __stdout_FILE->cookie = NULL;
+ __stdout_FILE->fd = 1;
+
+ __stderr_FILE = malloc(sizeof(FILE));
+ __stderr_FILE->write = write_fd;
+ __stderr_FILE->read = read_fd;
+ __stderr_FILE->is_eof = 0;
+ __stderr_FILE->has_error = 0;
+ __stderr_FILE->cookie = NULL;
+ __stderr_FILE->fd = 2;
+}
+
// Functions preserve the registers ebx, esi, edi, ebp, and esp; while
// eax, ecx, edx are scratch registers.
@@ -176,12 +211,12 @@ int s_syscall(int sys) {
}
int write(int fd, const char *buf, size_t count) {
- /*
- struct SYS_WRITE_PARAMS args = {
- .fd = fd,
- .buf = buf,
- .count = count,
- };*/
+ /*
+struct SYS_WRITE_PARAMS args = {
+.fd = fd,
+.buf = buf,
+.count = count,
+};*/
return syscall(SYS_WRITE, fd, buf, count, 0, 0);
}
diff --git a/userland/libc/stdio/fclose.c b/userland/libc/stdio/fclose.c
index 02d93ae..01bdb22 100644
--- a/userland/libc/stdio/fclose.c
+++ b/userland/libc/stdio/fclose.c
@@ -4,7 +4,8 @@
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html
// FIXME: Do some actual error checking.
int fclose(FILE *stream) {
+ if (stream)
free(stream->cookie);
- free(stream);
- return 0;
+ free(stream);
+ return 0;
}
diff --git a/userland/libc/stdio/stderr.c b/userland/libc/stdio/stderr.c
deleted file mode 100644
index 76597e2..0000000
--- a/userland/libc/stdio/stderr.c
+++ /dev/null
@@ -1,11 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-
-FILE __stderr_FILE = {
- .write = write_fd,
- .read = read_fd,
- .is_eof = 0,
- .has_error = 0,
- .cookie = NULL,
- .fd = 2,
-};
diff --git a/userland/libc/stdio/stdin.c b/userland/libc/stdio/stdin.c
index 2e1e50f..e84ea7e 100644
--- a/userland/libc/stdio/stdin.c
+++ b/userland/libc/stdio/stdin.c
@@ -71,13 +71,3 @@ int seek_fd(FILE *stream, long offset, int whence) {
// FIXME: Error checking
return 0;
}
-
-FILE __stdin_FILE = {
- .write = write_fd,
- .read = read_fd,
- .seek = NULL,
- .is_eof = 0,
- .has_error = 0,
- .cookie = NULL,
- .fd = 0,
-};
diff --git a/userland/libc/stdio/stdout.c b/userland/libc/stdio/stdout.c
deleted file mode 100644
index 7f4edf0..0000000
--- a/userland/libc/stdio/stdout.c
+++ /dev/null
@@ -1,13 +0,0 @@
-#include <stdio.h>
-#include <unistd.h>
-
-FILE __stdout_FILE = {
- .write = write_fd,
- .read = read_fd,
- .is_eof = 0,
- .has_error = 0,
- .seek = NULL,
- .cookie = NULL,
- .fd = 1,
-};
-FILE __stderr_FILE;