Merge copyfd and copy_file_chunk
authorGlenn L McGrath <bug1@ihug.co.nz>
Fri, 13 Dec 2002 08:20:44 +0000 (08:20 -0000)
committerGlenn L McGrath <bug1@ihug.co.nz>
Fri, 13 Dec 2002 08:20:44 +0000 (08:20 -0000)
include/libbb.h
libbb/copy_file.c
libbb/copyfd.c
libbb/print_file.c
networking/ftpgetput.c

index 1e95a903e7c96d3d3bd7ea2082cd9ca231cc428d..5f437a95f277eeaedd9088d5ed6b25777f457cfc 100644 (file)
@@ -131,7 +131,7 @@ extern long* find_pid_by_name( const char* pidName);
 extern char *find_real_root_device_name(const char* name);
 extern char *get_line_from_file(FILE *file);
 extern void print_file(FILE *file);
-extern int copyfd(int fd1, int fd2);
+extern int copyfd(int fd1, int fd2, const off_t chunksize);
 extern int print_file_by_name(char *filename);
 extern char process_escape_sequence(const char **ptr);
 extern char *get_last_path_component(char *path);
index 5f667cf4f79d9683c33c2509faf3d99152b9e05a..23a2d75a3dde4b3f697781cf6f79e772932c9f28 100644 (file)
@@ -183,7 +183,7 @@ int copy_file(const char *source, const char *dest, int flags)
                        }
                }
 
-               if (copy_file_chunk(sfp, dfp, -1) < 0)
+               if (copyfd(fileno(sfp), fileno(dfp), 0) == -1)
                        status = -1;
 
                if (fclose(dfp) < 0) {
index 22d8c3996831510739facc567275f37ed89693fc..4df1fd084af856eb19590ad5736346edf5853ce9 100644 (file)
 #include <errno.h>
 #include "libbb.h"
 
-
-extern int copyfd(int fd1, int fd2)
+/* If chunksize is 0 copy untill EOF */
+extern int copyfd(int fd1, int fd2, const off_t chunksize)
 {
-       char buf[8192];
-       ssize_t nread, nwrote;
+       size_t nread;
+       size_t nwritten;
+       size_t size;
+       size_t remaining;
+       char buffer[BUFSIZ];
+
+       if (chunksize) {
+               remaining = chunksize;
+       } else {
+               remaining = -1;
+       }
+
+       do {
+               if ((chunksize > BUFSIZ) || (chunksize == 0)) {
+                       size = BUFSIZ;
+               } else {
+                       size = chunksize;
+               }
+
+               nread = safe_read(fd1, buffer, size);
 
-       while (1) {
-               nread = safe_read(fd1, buf, sizeof(buf));
-               if (nread == 0)
-                       break;
                if (nread == -1) {
-                       perror_msg("read");
-                       return -1;
+                       perror_msg("read failure");
+                       return(-1);
                }
+               else if (nread == 0) {
+                       if (chunksize) {
+                               error_msg("Unable to read all data");
+                               return(-1);
+                       } else {
+                               return(0);
+                       }
+               }
+
+               nwritten = full_write(fd2, buffer, nread);
 
-               nwrote = full_write(fd2, buf, nread);
-               if (nwrote == -1) {
-                       perror_msg("write");
-                       return -1;
+               if (nwritten != nread) {
+                       error_msg("Unable to write all data");
+                       return(-1);
                }
-       }
+
+               if (chunksize) {
+                       remaining -= nwritten;
+               }
+       } while (remaining != 0);
 
        return 0;
 }
index a6df14ed9558f8394eb1ca664298e65c46e5c62a..cdd60e7a04ab1500731764fedaf1f4f8ad00b519 100644 (file)
@@ -20,6 +20,7 @@
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <sys/stat.h>
 #include "libbb.h"
 
@@ -27,7 +28,9 @@
 extern void print_file(FILE *file)
 {
        fflush(stdout);
-       copyfd(fileno(file), fileno(stdout));
+       if (copyfd(fileno(file), fileno(stdout), 0) == -1) {
+               exit(EXIT_FAILURE);
+       }
        fclose(file);
 }
 
index 909f3b117fe6639092aa98664249bb9040e0587e..a23c64af1427ed9e7d032b0c3d4bcec3c4396517 100644 (file)
@@ -56,51 +56,6 @@ typedef struct ftp_host_info_s {
 static char verbose_flag;
 static char do_continue = 0;
 
-static int copyfd_chunk(int fd1, int fd2, const off_t chunksize)
-{
-       size_t nread;
-       size_t nwritten;
-       size_t size;
-       size_t remaining;
-       char buffer[BUFSIZ];
-
-       if (chunksize) {
-               remaining = chunksize;
-       } else {
-               remaining = -1;
-       }
-
-       do {
-               if ((chunksize > BUFSIZ) || (chunksize == 0)) {
-                       size = BUFSIZ;
-               } else {
-                       size = chunksize;
-               }
-
-               nread = safe_read(fd1, buffer, size);
-
-               if (nread <= 0) {
-                       if (chunksize) {
-                               perror_msg_and_die("read error");
-                       } else {
-                               return(0);
-                       }
-               }
-
-               nwritten = full_write(fd2, buffer, nread);
-
-               if (nwritten != nread) {
-                       error_msg_and_die("Unable to write all data");
-               }
-
-               if (chunksize) {
-                       remaining -= nwritten;
-               }
-       } while (remaining != 0);
-
-       return 0;
-}
-
 static ftp_host_info_t *ftp_init(void)
 {
        ftp_host_info_t *host;
@@ -252,7 +207,9 @@ static int ftp_recieve(FILE *control_stream, const char *host, const char *local
        }
 
        /* Copy the file */
-       copyfd_chunk(fd_data, fd_local, filesize);
+       if (copyfd(fd_data, fd_local, filesize) == -1) {
+               exit(EXIT_FAILURE);
+       }
 
        /* close it all down */
        close(fd_data);
@@ -311,7 +268,9 @@ static int ftp_send(FILE *control_stream, const char *host, const char *server_p
        }
 
        /* transfer the file  */
-       copyfd_chunk(fd_local, fd_data, 0);
+       if (copyfd(fd_local, fd_data, 0) == -1) {
+               exit(EXIT_FAILURE);
+       }
 
        /* close it all down */
        close(fd_data);