From 8af714e59d16f186fd9173edba4fb2699690947a Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 26 Jul 2022 22:10:20 +0200 Subject: user/libc: make FILE an opaque type --- src/user/app/init/shell.c | 4 +++- src/user/lib/elfload.c | 1 + src/user/lib/file.h | 9 +++++++++ src/user/lib/include/bits/file.h | 10 +--------- src/user/lib/include/stdio.h | 6 +++++- src/user/lib/stdlib.c | 9 +++++++++ 6 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/user/lib/file.h (limited to 'src') diff --git a/src/user/app/init/shell.c b/src/user/app/init/shell.c index 253acce..e53d7d9 100644 --- a/src/user/app/init/shell.c +++ b/src/user/app/init/shell.c @@ -72,7 +72,7 @@ static void cmd_cat_ls(const char *args, bool ls) { return; } - while (!file->eof) { + while (!feof(file)) { int len = fread(buf, 1, sizeof buf, file); if (len <= 0) break; @@ -135,6 +135,8 @@ void shell_loop(void) { printf("%x$ ", level); readline(buf, 256); + if (feof(stdin)) + _syscall_exit(0); redir = strtrim(strsplit(buf, '>')); cmd = strtrim(buf); args = strtrim(strsplit(cmd, 0)); diff --git a/src/user/lib/elfload.c b/src/user/lib/elfload.c index b73dfe5..a471513 100644 --- a/src/user/lib/elfload.c +++ b/src/user/lib/elfload.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include diff --git a/src/user/lib/file.h b/src/user/lib/file.h new file mode 100644 index 0000000..9630b6d --- /dev/null +++ b/src/user/lib/file.h @@ -0,0 +1,9 @@ +#pragma once +#include + +struct _LIBC_FILE { + int fd; + int pos; + bool eof; + bool error; +}; diff --git a/src/user/lib/include/bits/file.h b/src/user/lib/include/bits/file.h index e50d402..63a31c4 100644 --- a/src/user/lib/include/bits/file.h +++ b/src/user/lib/include/bits/file.h @@ -1,10 +1,2 @@ #pragma once -#include -// TODO make opaque -struct FILE { - int fd; - int pos; - bool eof; - bool error; -}; -typedef struct FILE FILE; +typedef struct _LIBC_FILE FILE; diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index 2f0746f..b8de85d 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -16,6 +16,10 @@ FILE *fopen(const char *path, const char *mode); FILE *freopen(const char *path, const char *mode, FILE *); FILE *fdopen(int fd, const char *mode); FILE *file_clone(const FILE *); +int fclose(FILE *); + size_t fread(void *restrict ptr, size_t size, size_t nitems, FILE *restrict); size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restrict); -int fclose(FILE *); + +int feof(FILE *); +int ferror(FILE *); diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c index cd2ec90..0f49ccb 100644 --- a/src/user/lib/stdlib.c +++ b/src/user/lib/stdlib.c @@ -1,3 +1,4 @@ +#include "file.h" #include #include #include @@ -200,6 +201,14 @@ int fclose(FILE *f) { return 0; } +int feof(FILE *f) { + return f->eof; +} + +int ferror(FILE *f) { + return f->error; +} + int fork(void) { return _syscall_fork(0, NULL); -- cgit v1.2.3