diff options
Diffstat (limited to 'lib/io/src/unix/istream.c')
-rw-r--r-- | lib/io/src/unix/istream.c | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/lib/io/src/unix/istream.c b/lib/io/src/unix/istream.c index 60abff1..e96bc6c 100644 --- a/lib/io/src/unix/istream.c +++ b/lib/io/src/unix/istream.c @@ -105,7 +105,7 @@ static void file_destroy(sqfs_object_t *obj) free(file); } -istream_t *istream_open_file(const char *path) +istream_t *istream_open_handle(const char *path, int fd) { file_istream_t *file = calloc(1, sizeof(*file)); istream_t *strm = (istream_t *)file; @@ -123,11 +123,12 @@ istream_t *istream_open_file(const char *path) goto fail_free; } - file->fd = open(path, O_RDONLY); + file->fd = dup(fd); if (file->fd < 0) { perror(path); goto fail_path; } + close(fd); strm->get_buffered_data = file_get_buffered_data; strm->advance_buffer = file_advance_buffer; @@ -140,27 +141,25 @@ fail_free: return NULL; } -istream_t *istream_open_stdin(void) +istream_t *istream_open_file(const char *path) { - file_istream_t *file = calloc(1, sizeof(*file)); - istream_t *strm = (istream_t *)file; + istream_t *out; + int fd; - if (file == NULL) - goto fail; + fd = open(path, O_RDONLY); + if (fd < 0) { + perror(path); + return NULL; + } - sqfs_object_init(file, file_destroy, NULL); + out = istream_open_handle(path, fd); + if (out == NULL) + close(fd); - file->path = strdup("stdin"); - if (file->path == NULL) - goto fail; + return out; +} - file->fd = STDIN_FILENO; - strm->get_buffered_data = file_get_buffered_data; - strm->advance_buffer = file_advance_buffer; - strm->get_filename = file_get_filename; - return strm; -fail: - perror("creating file wrapper for stdin"); - free(file); - return NULL; +istream_t *istream_open_stdin(void) +{ + return istream_open_handle("stdin", STDIN_FILENO); } |