diff options
author | dzwdz | 2022-07-27 21:32:39 +0200 |
---|---|---|
committer | dzwdz | 2022-07-27 21:32:39 +0200 |
commit | c8998127732765aabba020ece5e42befc6f1e305 (patch) | |
tree | 75e1c51ec57054e2f438cd3e88eadebfc2f83f2d /src/user/lib/file.c | |
parent | 004a6fcd1a7d40564310c8348813d12ac0ea9de9 (diff) |
user/libc: a shitty fgets()
Diffstat (limited to 'src/user/lib/file.c')
-rw-r--r-- | src/user/lib/file.c | 12 |
1 files changed, 12 insertions, 0 deletions
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; |