aboutsummaryrefslogtreecommitdiff
path: root/libbb/copyfd.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/copyfd.c')
-rw-r--r--libbb/copyfd.c76
1 files changed, 31 insertions, 45 deletions
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index 4df1fd084..41b78c7d6 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -22,65 +22,51 @@
22#include <unistd.h> 22#include <unistd.h>
23#include <string.h> 23#include <string.h>
24#include <errno.h> 24#include <errno.h>
25#include "libbb.h" 25#include "busybox.h"
26 26
27/* If chunksize is 0 copy untill EOF */ 27#if BUFSIZ < 4096
28extern int copyfd(int fd1, int fd2, const off_t chunksize) 28#undef BUFSIZ
29#define BUFSIZ 4096
30#endif
31
32/* If chunksize is 0 copy until EOF */
33extern int bb_copyfd(int fd1, int fd2, const off_t chunksize)
29{ 34{
30 size_t nread; 35 ssize_t nread;
31 size_t nwritten;
32 size_t size; 36 size_t size;
33 size_t remaining; 37 off_t remaining;
34 char buffer[BUFSIZ]; 38 RESERVE_CONFIG_BUFFER(buffer,BUFSIZ);
35 39
40 remaining = size = BUFSIZ;
36 if (chunksize) { 41 if (chunksize) {
37 remaining = chunksize; 42 remaining = chunksize;
38 } else {
39 remaining = -1;
40 } 43 }
41 44
42 do { 45 do {
43 if ((chunksize > BUFSIZ) || (chunksize == 0)) { 46 if (size > remaining) {
44 size = BUFSIZ; 47 size = remaining;
45 } else {
46 size = chunksize;
47 } 48 }
48 49
49 nread = safe_read(fd1, buffer, size); 50 if ((nread = safe_read(fd1, buffer, size)) > 0) {
50 51 if (bb_full_write(fd2, buffer, nread) < 0) {
51 if (nread == -1) { 52 bb_perror_msg(bb_msg_write_error); /* match Read error below */
52 perror_msg("read failure"); 53 break;
53 return(-1); 54 }
54 } 55 if (chunksize && ((remaining -= nread) == 0)) {
55 else if (nread == 0) { 56 return 0;
57 }
58 } else if (!nread) {
56 if (chunksize) { 59 if (chunksize) {
57 error_msg("Unable to read all data"); 60 bb_error_msg("Unable to read all data");
58 return(-1); 61 break;
59 } else {
60 return(0);
61 } 62 }
63 return 0;
64 } else { /* nread < 0 */
65 bb_perror_msg("Read error"); /* match bb_msg_write_error above */
66 break;
62 } 67 }
63 68
64 nwritten = full_write(fd2, buffer, nread); 69 } while (1);
65 70
66 if (nwritten != nread) { 71 return -1;
67 error_msg("Unable to write all data");
68 return(-1);
69 }
70
71 if (chunksize) {
72 remaining -= nwritten;
73 }
74 } while (remaining != 0);
75
76 return 0;
77} 72}
78
79/* END CODE */
80/*
81Local Variables:
82c-file-style: "linux"
83c-basic-offset: 4
84tab-width: 4
85End:
86*/