home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / directry / filecopy / filecopy.c next >
Encoding:
C/C++ Source or Header  |  1992-03-20  |  1.6 KB  |  88 lines

  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <fcntl.h>
  4. #include <sys\stat.h>
  5. #include <dos.h>
  6.  
  7.  
  8. #define BUFF_SIZE       20480   /* size of buffer for FileCopy() */
  9.  
  10. main(int argc, char *argv[])
  11. {
  12.     if (argc != 3)
  13.     {
  14.         printf("USAGE:  FILECOPY source destination\n\n");
  15.         printf("\tCopies source file to destination file.\n");
  16.         exit(1);
  17.     }
  18.     FileCopy(argv[1],argv[2]);
  19. }
  20.  
  21.  
  22.  
  23. /* returns 1 on error, else returns 0 */
  24. int    FileCopy(char *file1, char *file2)
  25. {
  26.     int        SourceFile, DestFile, BytesRead;
  27.     int        error;
  28.     char        *buffer;
  29.     unsigned    fdate, ftime;
  30.  
  31.  
  32.     buffer = (char *)malloc(BUFF_SIZE);
  33.     if (!buffer)
  34.     {
  35.         printf("Can't get memory!\n");
  36.         return(1);
  37.     }
  38.  
  39.     SourceFile = open(file1, O_RDONLY|O_BINARY);
  40.     if (SourceFile == -1)
  41.     {
  42.         printf("%s could not be opened!\n", file1);
  43.         free(buffer);
  44.         return(1);
  45.     }
  46.     DestFile = open(file2, O_RDWR|O_BINARY|O_CREAT, S_IWRITE);
  47.     if (DestFile == -1)
  48.     {
  49.         printf("%s could not be opened!\n", file2);
  50.         close(SourceFile);
  51.         free(buffer);
  52.         return(1);
  53.     }
  54.  
  55.     while (1)
  56.     {
  57.         BytesRead = read(SourceFile, buffer, BUFF_SIZE);
  58.         if (BytesRead == -1)
  59.         {
  60.             puts("Read Error");
  61.             close(SourceFile);
  62.             close(DestFile);
  63.             free(buffer);
  64.             return(1);
  65.         }
  66.         if (BytesRead == 0)
  67.             break;
  68.  
  69.         error = write(DestFile, buffer, BytesRead);
  70.         if (error == -1)
  71.         {
  72.             puts("Write Error");
  73.             close(SourceFile);
  74.             close(DestFile);
  75.             free(buffer);
  76.             return(1);
  77.         }
  78.     }
  79.  
  80.     dos_getftime(SourceFile, &fdate, &ftime);
  81.     dos_setftime(DestFile, fdate, ftime);
  82.     close(SourceFile);
  83.     close(DestFile);
  84.     free(buffer);
  85.     return(0);
  86. }
  87.  
  88.