aboutsummaryrefslogtreecommitdiff
path: root/libbb/copyfd.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/copyfd.c')
-rw-r--r--libbb/copyfd.c40
1 files changed, 16 insertions, 24 deletions
diff --git a/libbb/copyfd.c b/libbb/copyfd.c
index 253a8cf6e..aa938d105 100644
--- a/libbb/copyfd.c
+++ b/libbb/copyfd.c
@@ -25,36 +25,28 @@
25#include "libbb.h" 25#include "libbb.h"
26 26
27 27
28extern size_t copyfd(int fd1, int fd2) 28extern int copyfd(int fd1, int fd2)
29{ 29{
30 char buf[32768], *writebuf; 30 char buf[8192];
31 int status = TRUE; 31 ssize_t nread, nwrote;
32 size_t totalread = 0, bytesread, byteswritten;
33 32
34 while(status) { 33 while (1) {
35 bytesread = read(fd1, &buf, sizeof(buf)); 34 nread = safe_read(fd1, buf, sizeof(buf));
36 if(bytesread == -1) { 35 if (nread == 0)
37 error_msg("read: %s", strerror(errno));
38 status = FALSE;
39 break; 36 break;
37 if (nread == -1) {
38 perror_msg("read");
39 return -1;
40 } 40 }
41 byteswritten = 0; 41
42 writebuf = buf; 42 nwrote = full_write(fd2, buf, nread);
43 while(bytesread) { 43 if (nwrote == -1) {
44 byteswritten = write( fd2, &writebuf, bytesread ); 44 perror_msg("write");
45 if(byteswritten == -1) { 45 return -1;
46 error_msg("write: %s", strerror(errno));
47 status = FALSE;
48 break;
49 }
50 bytesread -= byteswritten;
51 writebuf += byteswritten;
52 } 46 }
53 } 47 }
54 if ( status == TRUE ) 48
55 return totalread; 49 return 0;
56 else
57 return -1;
58} 50}
59 51
60/* END CODE */ 52/* END CODE */