diff options
Diffstat (limited to 'src/user')
-rw-r--r-- | src/user/app/shell/shell.c | 13 | ||||
-rw-r--r-- | src/user/lib/file.c | 12 | ||||
-rw-r--r-- | src/user/lib/include/stdio.h | 1 |
3 files changed, 15 insertions, 11 deletions
diff --git a/src/user/app/shell/shell.c b/src/user/app/shell/shell.c index 995ce53..7367a4b 100644 --- a/src/user/app/shell/shell.c +++ b/src/user/app/shell/shell.c @@ -10,15 +10,6 @@ int main(); -static int readline(char *buf, size_t max, FILE *f) { - char c = '\0'; - size_t pos = 0; - while (pos < (max-1) && c != '\n' && fread(&c, 1, 1, f)) - buf[pos++] = c; - buf[pos++] = '\0'; - return pos; -} - static void execp(char **argv) { if (!argv || !*argv) return; if (argv[0][0] == '/') { @@ -125,8 +116,8 @@ int main(int argc, char **argv) { for (;;) { if (f == stdin) printf("$ "); - readline(buf, 256, f); - if (feof(f)) return 0; + if (!fgets(buf, 256, f)) + return 0; run(buf); } } diff --git a/src/user/lib/file.c b/src/user/lib/file.c index bd6bc56..a26c8bb 100644 --- a/src/user/lib/file.c +++ b/src/user/lib/file.c @@ -143,6 +143,18 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nitems, FILE *restri return nitems; } +char *fgets(char *buf, int size, FILE *f) { + char c = '\0'; + size_t pos = 0; + while (pos < (size-1) && c != '\n' && fread(&c, 1, 1, f)) + buf[pos++] = c; + buf[pos++] = '\0'; + + if (f->eof && pos == 1) return NULL; + if (f->error) return NULL; + return buf; +} + int fseek(FILE *f, long offset, int whence) { if (fflush(f)) return -1; diff --git a/src/user/lib/include/stdio.h b/src/user/lib/include/stdio.h index eb59793..bf9e09e 100644 --- a/src/user/lib/include/stdio.h +++ b/src/user/lib/include/stdio.h @@ -25,6 +25,7 @@ int fflush(FILE *f); 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); +char *fgets(char *buf, int size, FILE *f); int fseek(FILE *f, long offset, int whence); int feof(FILE *); |