From c5ad54a55123582b09f9d9d8046623916c2dec4a Mon Sep 17 00:00:00 2001 From: dzwdz Date: Thu, 14 Jul 2022 17:23:50 +0200 Subject: user/shell/cat: support reading from stdin until eof --- src/user/lib/stdlib.c | 17 +++++++++++++++++ src/user/lib/stdlib.h | 1 + 2 files changed, 18 insertions(+) (limited to 'src/user/lib') diff --git a/src/user/lib/stdlib.c b/src/user/lib/stdlib.c index 32b4f06..56b8bb4 100644 --- a/src/user/lib/stdlib.c +++ b/src/user/lib/stdlib.c @@ -94,6 +94,23 @@ fail: return NULL; } +libc_file *file_clone(const libc_file *f) { + handle_t h = _syscall_dup(f->fd, -1, 0); + libc_file *f2; + if (h < 0) return NULL; + + // TODO file_wrapfd + f2 = malloc(sizeof *f2); + if (!f2) { + close(h); + return NULL; + } + f2->pos = f->pos; + f2->eof = f->eof; + f2->fd = h; + return f2; +} + int file_read(libc_file *f, char *buf, size_t len) { if (f->fd < 0) return -1; diff --git a/src/user/lib/stdlib.h b/src/user/lib/stdlib.h index 22227e3..1982e85 100644 --- a/src/user/lib/stdlib.h +++ b/src/user/lib/stdlib.h @@ -17,6 +17,7 @@ typedef struct { } libc_file; libc_file *file_open(const char *path, int flags); libc_file *file_reopen(libc_file*, const char *path, int flags); +libc_file *file_clone(const libc_file*); int file_read(libc_file*, char *buf, size_t len); int file_write(libc_file*, const char *buf, size_t len); void file_close(libc_file*); -- cgit v1.2.3