From a533b4bc4948c6b8f143eb9607ea4b0bf1b1197f Mon Sep 17 00:00:00 2001 From: Avi Kivity <avi@cloudius-systems.com> Date: Thu, 10 Jan 2013 14:59:04 +0200 Subject: [PATCH] file: implement fgets() --- libc/file.cc | 21 +++++++++++++++++++++ runtime.cc | 5 ----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libc/file.cc b/libc/file.cc index 97d0b90e7..d0c27c9f2 100644 --- a/libc/file.cc +++ b/libc/file.cc @@ -61,6 +61,27 @@ FILE* fopen(const char* fname, const char* fmode) return new std_file(fd); } +char *fgets(char *s, int size, FILE *stream) +{ + char* orig = s; + while (size > 1) { + int r = ::read(stream->_fileno, s, 1); + assert(r != -1); + if (r == 0) { + break; + } + ++s; + --size; + if (s[-1] == '\n') { + break; + } + } + if (size) { + *s = '\0'; + } + return s == orig ? nullptr : orig; +} + DIR* opendir(const char* fname) { auto f = rootfs->open(fname); diff --git a/runtime.cc b/runtime.cc index 631db5d01..93132cabe 100644 --- a/runtime.cc +++ b/runtime.cc @@ -402,11 +402,6 @@ int fgetc(FILE *stream) UNIMPLEMENTED("fgetc"); } -char *fgets(char *s, int size, FILE *stream) -{ - UNIMPLEMENTED("fgets"); -} - #undef getc int getc(FILE *stream) { -- GitLab