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

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** by: Walter Bright via Usenet C newsgroup
  5. **
  6. ** modified by: Bob Stout based on a recommendation by Ray Gardner
  7. **
  8. ** modified by: David Gersic to deal with binary files
  9. **
  10. ** There is no point in going to asm to get high speed file copies. Since it
  11. ** is inherently disk-bound, there is no sense (unless tiny code size is
  12. ** the goal). Here's a C version that you'll find is as fast as any asm code
  13. ** for files larger than a few bytes (the trick is to use large disk buffers):
  14. */
  15.  
  16. #include <stdlib.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include "snipfile.h"               /* Contains prototype for fdcopy()  */
  20.  
  21. #if !defined(__ZTC__) && !defined(__TURBOC__)
  22.  #include <sys\types.h>
  23. #endif
  24.  
  25. #include <sys\stat.h>
  26.  
  27. int file_append(char *from, char *to)
  28. {
  29.       int fdfrom,fdto;
  30.  
  31.       fdfrom = open(from,O_RDONLY|O_BINARY,0);
  32.       if (fdfrom < 0)
  33.             return 1;
  34.  
  35.       /* Open R/W by owner, R by everyone else        */
  36.  
  37.       fdto=open(to,O_BINARY|O_CREAT|O_APPEND|O_RDWR,S_IREAD|S_IWRITE);
  38.       if (fdto >= 0)
  39.       {
  40.             if (Success_ == fdcopy(fdfrom, fdto))
  41.             {
  42.                   close(fdto);
  43.                   close(fdfrom);
  44.                   return Success_;
  45.             }
  46.             else
  47.             {
  48.                   close(fdto);
  49.                   remove(to);             /* delete any partial file  */
  50.             }
  51.       }
  52.       close(fdfrom);
  53.       return Error_;
  54. }
  55.