Rewrote copyfd to use library functions, terminate, and copy correct data.
authorMatt Kraai <kraai@debian.org>
Fri, 18 May 2001 14:14:55 +0000 (14:14 -0000)
committerMatt Kraai <kraai@debian.org>
Fri, 18 May 2001 14:14:55 +0000 (14:14 -0000)
include/libbb.h
libbb/copyfd.c
libbb/libbb.h

index 29a756b7f30e449612daf3a7be237f764b360a80..4e324bf865f1edac23c7d2e0d74f022688358188 100644 (file)
@@ -133,7 +133,7 @@ extern pid_t* find_pid_by_name( 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 size_t copyfd(int fd1, int fd2);
+extern int copyfd(int fd1, int fd2);
 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 253a8cf6e0d7e58b5147d8aff4ba9137d0c9f466..aa938d1053e17c32bd842ab5443c51eb28fe4d9f 100644 (file)
 #include "libbb.h"
 
 
-extern size_t copyfd(int fd1, int fd2)
+extern int copyfd(int fd1, int fd2)
 {
-       char buf[32768], *writebuf;
-       int status = TRUE;
-       size_t totalread = 0, bytesread, byteswritten;
+       char buf[8192];
+       ssize_t nread, nwrote;
 
-       while(status) {
-               bytesread = read(fd1, &buf, sizeof(buf));
-               if(bytesread == -1) {
-                       error_msg("read: %s", strerror(errno));
-                       status = FALSE;
+       while (1) {
+               nread = safe_read(fd1, buf, sizeof(buf));
+               if (nread == 0)
                        break;
+               if (nread == -1) {
+                       perror_msg("read");
+                       return -1;
                }
-               byteswritten = 0;
-               writebuf = buf;
-               while(bytesread) {
-                       byteswritten = write( fd2, &writebuf, bytesread );
-                       if(byteswritten == -1) {
-                               error_msg("write: %s", strerror(errno));
-                               status = FALSE;
-                               break;
-                       }
-                       bytesread -= byteswritten;
-                       writebuf += byteswritten;
+
+               nwrote = full_write(fd2, buf, nread);
+               if (nwrote == -1) {
+                       perror_msg("write");
+                       return -1;
                }
        }
-       if ( status == TRUE )
-               return totalread;
-       else
-               return -1;
+
+       return 0;
 }
 
 /* END CODE */
index 29a756b7f30e449612daf3a7be237f764b360a80..4e324bf865f1edac23c7d2e0d74f022688358188 100644 (file)
@@ -133,7 +133,7 @@ extern pid_t* find_pid_by_name( 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 size_t copyfd(int fd1, int fd2);
+extern int copyfd(int fd1, int fd2);
 extern int print_file_by_name(char *filename);
 extern char process_escape_sequence(const char **ptr);
 extern char *get_last_path_component(char *path);