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 | |
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')
-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) | |||
183 | } | 183 | } |
184 | } | 184 | } |
185 | 185 | ||
186 | if (copy_file_chunk(sfp, dfp, -1) < 0) | 186 | if (copyfd(fileno(sfp), fileno(dfp), 0) == -1) |
187 | status = -1; | 187 | status = -1; |
188 | 188 | ||
189 | if (fclose(dfp) < 0) { | 189 | 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 @@ | |||
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 | } |
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 @@ | |||
20 | */ | 20 | */ |
21 | 21 | ||
22 | #include <stdio.h> | 22 | #include <stdio.h> |
23 | #include <stdlib.h> | ||
23 | #include <sys/stat.h> | 24 | #include <sys/stat.h> |
24 | #include "libbb.h" | 25 | #include "libbb.h" |
25 | 26 | ||
@@ -27,7 +28,9 @@ | |||
27 | extern void print_file(FILE *file) | 28 | extern void print_file(FILE *file) |
28 | { | 29 | { |
29 | fflush(stdout); | 30 | fflush(stdout); |
30 | copyfd(fileno(file), fileno(stdout)); | 31 | if (copyfd(fileno(file), fileno(stdout), 0) == -1) { |
32 | exit(EXIT_FAILURE); | ||
33 | } | ||
31 | fclose(file); | 34 | fclose(file); |
32 | } | 35 | } |
33 | 36 | ||