diff options
author | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-12-13 08:20:44 +0000 |
---|---|---|
committer | bug1 <bug1@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2002-12-13 08:20:44 +0000 |
commit | a133b7ee337539f354e2f3038381e80cf50ef1d2 (patch) | |
tree | 5f41636eb6f772de78a17e90189da6aeee3f32a7 /libbb/copyfd.c | |
parent | ef0da010322212433a63de124b475bc56ca604d9 (diff) | |
download | busybox-w32-a133b7ee337539f354e2f3038381e80cf50ef1d2.tar.gz busybox-w32-a133b7ee337539f354e2f3038381e80cf50ef1d2.tar.bz2 busybox-w32-a133b7ee337539f354e2f3038381e80cf50ef1d2.zip |
Merge copyfd and copy_file_chunk
git-svn-id: svn://busybox.net/trunk/busybox@6208 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'libbb/copyfd.c')
-rw-r--r-- | libbb/copyfd.c | 57 |
1 files changed, 42 insertions, 15 deletions
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 @@ | |||
24 | #include <errno.h> | 24 | #include <errno.h> |
25 | #include "libbb.h" | 25 | #include "libbb.h" |
26 | 26 | ||
27 | 27 | /* If chunksize is 0 copy untill EOF */ | |
28 | extern int copyfd(int fd1, int fd2) | 28 | extern int copyfd(int fd1, int fd2, const off_t chunksize) |
29 | { | 29 | { |
30 | char buf[8192]; | 30 | size_t nread; |
31 | ssize_t nread, nwrote; | 31 | size_t nwritten; |
32 | size_t size; | ||
33 | size_t remaining; | ||
34 | char buffer[BUFSIZ]; | ||
35 | |||
36 | if (chunksize) { | ||
37 | remaining = chunksize; | ||
38 | } else { | ||
39 | remaining = -1; | ||
40 | } | ||
41 | |||
42 | do { | ||
43 | if ((chunksize > BUFSIZ) || (chunksize == 0)) { | ||
44 | size = BUFSIZ; | ||
45 | } else { | ||
46 | size = chunksize; | ||
47 | } | ||
48 | |||
49 | nread = safe_read(fd1, buffer, size); | ||
32 | 50 | ||
33 | while (1) { | ||
34 | nread = safe_read(fd1, buf, sizeof(buf)); | ||
35 | if (nread == 0) | ||
36 | break; | ||
37 | if (nread == -1) { | 51 | if (nread == -1) { |
38 | perror_msg("read"); | 52 | perror_msg("read failure"); |
39 | return -1; | 53 | return(-1); |
40 | } | 54 | } |
55 | else if (nread == 0) { | ||
56 | if (chunksize) { | ||
57 | error_msg("Unable to read all data"); | ||
58 | return(-1); | ||
59 | } else { | ||
60 | return(0); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | nwritten = full_write(fd2, buffer, nread); | ||
41 | 65 | ||
42 | nwrote = full_write(fd2, buf, nread); | 66 | if (nwritten != nread) { |
43 | if (nwrote == -1) { | 67 | error_msg("Unable to write all data"); |
44 | perror_msg("write"); | 68 | return(-1); |
45 | return -1; | ||
46 | } | 69 | } |
47 | } | 70 | |
71 | if (chunksize) { | ||
72 | remaining -= nwritten; | ||
73 | } | ||
74 | } while (remaining != 0); | ||
48 | 75 | ||
49 | return 0; | 76 | return 0; |
50 | } | 77 | } |