home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / CUJJUN93.ZIP / 1106129A < prev    next >
Text File  |  1993-04-01  |  971b  |  44 lines

  1. /* filecopy.c:  Low-level file copy */
  2.  
  3. #include <io.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7.  
  8. #define BUFSIZ 512
  9. #define INPUT_MODE  (O_RDONLY | O_BINARY)
  10. #define OUTPUT_MODE (O_WRONLY | O_BINARY | O_CREAT)
  11.  
  12. int filecopy(char *from, char *to)
  13. {
  14.     int nbytes;
  15.     int status = -1;
  16.     int fd1 = open(from,INPUT_MODE);
  17.     int fd2 = open(to,OUTPUT_MODE,S_IWRITE);
  18.     static char buffer[BUFSIZ];
  19.  
  20.     if (fd1 >= 0 && fd2 >= 0)
  21.     {
  22.         status = 0;
  23.         while ((nbytes = read(fd1,buffer,BUFSIZ)) > 0)
  24.             if (write(fd2,buffer,nbytes) != nbytes)
  25.             {
  26.                 /* Write error */
  27.                 status = -1;
  28.                 break;      /* Write error */
  29.             }
  30.         
  31.         /* Was there a read error? */
  32.         if (nbytes == -1)
  33.             status = -1;
  34.     }
  35.  
  36.     if (fd1 >= 0)
  37.         close(fd1);
  38.     if (fd2 >= 0)
  39.         close(fd2);
  40.     return status;
  41. }
  42.  
  43.  
  44.