diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-13 11:20:44 +0300 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-12-13 11:20:44 +0300 |
commit | 25fe94fd32f39a76294456a7bc7b8dd0595afb0d (patch) | |
tree | 5f41636eb6f772de78a17e90189da6aeee3f32a7 /libbb | |
parent | a67dffe186eefb6cf75c08422dbfa0e4a752c692 (diff) |
Merge copyfd and copy_file_chunk
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/copy_file.c | 2 | ||||
-rw-r--r-- | libbb/copyfd.c | 57 | ||||
-rw-r--r-- | libbb/print_file.c | 5 |
3 files changed, 47 insertions, 17 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c index 5f667cf4f..23a2d75a3 100644 --- a/libbb/copy_file.c +++ b/libbb/copy_file.c @@ -183,7 +183,7 @@ int copy_file(const char *source, const char *dest, int flags) } } - if (copy_file_chunk(sfp, dfp, -1) < 0) + if (copyfd(fileno(sfp), fileno(dfp), 0) == -1) status = -1; if (fclose(dfp) < 0) { diff --git a/libbb/copyfd.c b/libbb/copyfd.c index 22d8c3996..4df1fd084 100644 --- a/libbb/copyfd.c +++ b/libbb/copyfd.c @@ -24,27 +24,54 @@ #include <errno.h> #include "libbb.h" - -extern int copyfd(int fd1, int fd2) +/* If chunksize is 0 copy untill EOF */ +extern int copyfd(int fd1, int fd2, const off_t chunksize) { - char buf[8192]; - ssize_t nread, nwrote; + size_t nread; + size_t nwritten; + size_t size; + size_t remaining; + char buffer[BUFSIZ]; + + if (chunksize) { + remaining = chunksize; + } else { + remaining = -1; + } + + do { + if ((chunksize > BUFSIZ) || (chunksize == 0)) { + size = BUFSIZ; + } else { + size = chunksize; + } + + nread = safe_read(fd1, buffer, size); - while (1) { - nread = safe_read(fd1, buf, sizeof(buf)); - if (nread == 0) - break; if (nread == -1) { - perror_msg("read"); - return -1; + perror_msg("read failure"); + return(-1); } + else if (nread == 0) { + if (chunksize) { + error_msg("Unable to read all data"); + return(-1); + } else { + return(0); + } + } + + nwritten = full_write(fd2, buffer, nread); - nwrote = full_write(fd2, buf, nread); - if (nwrote == -1) { - perror_msg("write"); - return -1; + if (nwritten != nread) { + error_msg("Unable to write all data"); + return(-1); } - } + + if (chunksize) { + remaining -= nwritten; + } + } while (remaining != 0); return 0; } diff --git a/libbb/print_file.c b/libbb/print_file.c index a6df14ed9..cdd60e7a0 100644 --- a/libbb/print_file.c +++ b/libbb/print_file.c @@ -20,6 +20,7 @@ */ #include <stdio.h> +#include <stdlib.h> #include <sys/stat.h> #include "libbb.h" @@ -27,7 +28,9 @@ extern void print_file(FILE *file) { fflush(stdout); - copyfd(fileno(file), fileno(stdout)); + if (copyfd(fileno(file), fileno(stdout), 0) == -1) { + exit(EXIT_FAILURE); + } fclose(file); } |