aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/copy_file.c2
-rw-r--r--libbb/copyfd.c57
-rw-r--r--libbb/print_file.c5
-rw-r--r--networking/ftpgetput.c53
5 files changed, 54 insertions, 65 deletions
diff --git a/include/libbb.h b/include/libbb.h
index 1e95a903e..5f437a95f 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -131,7 +131,7 @@ extern long* find_pid_by_name( const char* pidName);
131extern char *find_real_root_device_name(const char* name); 131extern char *find_real_root_device_name(const char* name);
132extern char *get_line_from_file(FILE *file); 132extern char *get_line_from_file(FILE *file);
133extern void print_file(FILE *file); 133extern void print_file(FILE *file);
134extern int copyfd(int fd1, int fd2); 134extern int copyfd(int fd1, int fd2, const off_t chunksize);
135extern int print_file_by_name(char *filename); 135extern int print_file_by_name(char *filename);
136extern char process_escape_sequence(const char **ptr); 136extern char process_escape_sequence(const char **ptr);
137extern char *get_last_path_component(char *path); 137extern char *get_last_path_component(char *path);
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 */
28extern int copyfd(int fd1, int fd2) 28extern 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 @@
27extern void print_file(FILE *file) 28extern 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
diff --git a/networking/ftpgetput.c b/networking/ftpgetput.c
index 909f3b117..a23c64af1 100644
--- a/networking/ftpgetput.c
+++ b/networking/ftpgetput.c
@@ -56,51 +56,6 @@ typedef struct ftp_host_info_s {
56static char verbose_flag; 56static char verbose_flag;
57static char do_continue = 0; 57static char do_continue = 0;
58 58
59static int copyfd_chunk(int fd1, int fd2, const off_t chunksize)
60{
61 size_t nread;
62 size_t nwritten;
63 size_t size;
64 size_t remaining;
65 char buffer[BUFSIZ];
66
67 if (chunksize) {
68 remaining = chunksize;
69 } else {
70 remaining = -1;
71 }
72
73 do {
74 if ((chunksize > BUFSIZ) || (chunksize == 0)) {
75 size = BUFSIZ;
76 } else {
77 size = chunksize;
78 }
79
80 nread = safe_read(fd1, buffer, size);
81
82 if (nread <= 0) {
83 if (chunksize) {
84 perror_msg_and_die("read error");
85 } else {
86 return(0);
87 }
88 }
89
90 nwritten = full_write(fd2, buffer, nread);
91
92 if (nwritten != nread) {
93 error_msg_and_die("Unable to write all data");
94 }
95
96 if (chunksize) {
97 remaining -= nwritten;
98 }
99 } while (remaining != 0);
100
101 return 0;
102}
103
104static ftp_host_info_t *ftp_init(void) 59static ftp_host_info_t *ftp_init(void)
105{ 60{
106 ftp_host_info_t *host; 61 ftp_host_info_t *host;
@@ -252,7 +207,9 @@ static int ftp_recieve(FILE *control_stream, const char *host, const char *local
252 } 207 }
253 208
254 /* Copy the file */ 209 /* Copy the file */
255 copyfd_chunk(fd_data, fd_local, filesize); 210 if (copyfd(fd_data, fd_local, filesize) == -1) {
211 exit(EXIT_FAILURE);
212 }
256 213
257 /* close it all down */ 214 /* close it all down */
258 close(fd_data); 215 close(fd_data);
@@ -311,7 +268,9 @@ static int ftp_send(FILE *control_stream, const char *host, const char *server_p
311 } 268 }
312 269
313 /* transfer the file */ 270 /* transfer the file */
314 copyfd_chunk(fd_local, fd_data, 0); 271 if (copyfd(fd_local, fd_data, 0) == -1) {
272 exit(EXIT_FAILURE);
273 }
315 274
316 /* close it all down */ 275 /* close it all down */
317 close(fd_data); 276 close(fd_data);