home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / WB_FCOPY.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  2KB  |  88 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** by: Walter Bright via Usenet C newsgroup
  5. **
  6. ** modified by: Bob Stout, Ray Gardner, and David Gersic
  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.  
  14. #include <stdlib.h>
  15. #include <io.h>
  16. #include <fcntl.h>
  17. #include "snipfile.h"
  18.  
  19. #if !defined(__ZTC__) && !defined(__TURBOC__)
  20.  #include <sys\types.h>
  21. #endif
  22.  
  23. #include <sys\stat.h>
  24.  
  25. int file_copy(char *from, char *to)
  26. {
  27.       int fdfrom,fdto;
  28.  
  29.       fdfrom = open(from,O_RDONLY|O_BINARY,0);
  30.       if (fdfrom < 0)
  31.             return Error_;
  32.  
  33.       /* Open R/W by owner, R by everyone else        */
  34.  
  35.       fdto=open(to,O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IREAD|S_IWRITE);
  36.       if (fdto >= 0)
  37.       {
  38.             if (Success_ == fdcopy(fdfrom, fdto))
  39.             {
  40.                   close(fdto);
  41.                   close(fdfrom);
  42.                   return Success_;
  43.             }
  44.             else
  45.             {
  46.                   close(fdto);
  47.                   remove(to);             /* delete any partial file  */
  48.             }
  49.       }
  50.       close(fdfrom);
  51.       return Error_;
  52. }
  53.  
  54. int fdcopy(int fdfrom, int fdto)
  55. {
  56.       int bufsiz, retval = Error_;
  57.  
  58.       /* Use the largest buffer we can get    */
  59.  
  60.       for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
  61.       {
  62.             register char *buffer;
  63.  
  64.             buffer = (char *) malloc(bufsiz);
  65.             if (buffer)
  66.             {
  67.                   while (1)
  68.                   {
  69.                         register int n;
  70.  
  71.                         n = read(fdfrom,buffer,bufsiz);
  72.                         if (n == -1)            /* if error             */
  73.                               break;
  74.                         if (n == 0)             /* if end of file       */
  75.                         {
  76.                               retval = Success_;
  77.                               break;
  78.                         }
  79.                         if (n != write(fdto,buffer,(unsigned) n))
  80.                               break;
  81.                   }
  82.                   free(buffer);
  83.                   break;
  84.             }
  85.       }
  86.       return retval;
  87. }
  88.