home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNIP9404.ZIP / WB_FCOPY.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  3KB  |  87 lines

  1. /* 
  2. ** by: Walter Bright via Usenet C newsgroup
  3. **
  4. ** modified by: Bob Stout based on a recommendation by Ray Gardner
  5. **
  6. ** modified by: David Gersic to deal with binary files
  7. **
  8. ** There is no point in going to asm to get high speed file copies. Since it
  9. ** is inherently disk-bound, there is no sense (unless tiny code size is
  10. ** the goal). Here's a C version that you'll find is as fast as any asm code
  11. ** for files larger than a few bytes (the trick is to use large disk buffers):
  12. **
  13. ** To compile file_copy(), define Afilecopy=1 on the command line
  14. ** To compile file_append(), define Afileappe=1 on the command line
  15. */
  16.  
  17. #include <stdlib.h>
  18. #include <io.h>
  19. #include <fcntl.h>
  20.  
  21. #if !defined(__ZTC__) && !defined(__TURBOC__)
  22.  #include <sys\types.h>
  23. #endif
  24.  
  25. #include <sys\stat.h>
  26.  
  27. #if Afilecopy
  28.  int file_copy(char *from, char *to)
  29. #else
  30.  int file_append(char *from, char *to)
  31. #endif
  32. {
  33.       int fdfrom,fdto;
  34.       int bufsiz;
  35.  
  36.       fdfrom = open(from,O_RDONLY|O_BINARY,0);
  37.       if (fdfrom < 0)
  38.             return 1;
  39. #if Afileappe
  40.  
  41.       /* Open R/W by owner, R by everyone else        */
  42.  
  43.       fdto=open(to,O_BINARY|O_CREAT|O_APPEND|O_RDWR,S_IREAD|S_IWRITE);
  44.       if (fdto < 0)
  45.             goto err;
  46. #else
  47.       fdto=open(to,O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IREAD|S_IWRITE);
  48.       if (fdto < 0)
  49.             goto err;
  50. #endif
  51.  
  52.       /* Use the largest buffer we can get    */
  53.  
  54.       for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
  55.       {
  56.             register char *buffer;
  57.  
  58.             buffer = (char *) malloc(bufsiz);
  59.             if (buffer)
  60.             {
  61.                   while (1)
  62.                   {
  63.                         register int n;
  64.  
  65.                         n = read(fdfrom,buffer,bufsiz);
  66.                         if (n == -1)                /* if error             */
  67.                               break;
  68.                         if (n == 0)                 /* if end of file       */
  69.                         {
  70.                               free(buffer);
  71.                               close(fdto);
  72.                               close(fdfrom);
  73.                               return 0;             /* success              */
  74.                         }
  75.                         if (n != write(fdto,buffer,(unsigned) n))
  76.                               break;
  77.                   }
  78.                   free(buffer);
  79.                   break;
  80.             }
  81.       }
  82. err2: close(fdto);
  83.       remove(to);                               /* delete any partial file  */
  84. err:  close(fdfrom);
  85.       return 1;
  86. }
  87.