home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / WB_FCOPY.C < prev    next >
Text File  |  1994-04-03  |  2KB  |  64 lines

  1. .I 11 3
  2. **
  3. ** To compile file_copy(), define Afilecopy=1 on the command line
  4. ** To compile file_append(), define Afileappe=1 on the command line
  5. .I 24 17
  6.  int file_copy(char *from, char *to)
  7. #else
  8.  int file_append(char *from, char *to)
  9. #endif
  10. {
  11.       int fdfrom,fdto;
  12.       int bufsiz;
  13.  
  14.       fdfrom = open(from,O_RDONLY|O_BINARY,0);
  15.       if (fdfrom < 0)
  16.             return 1;
  17. #if Afileappe
  18.  
  19.       /* Open R/W by owner, R by everyone else        */
  20.  
  21.       fdto=open(to,O_BINARY|O_CREAT|O_APPEND|O_RDWR,S_IREAD|S_IWRITE);
  22.       if (fdto < 0)
  23. .D 25 15
  24. .I 41 2
  25.       fdto=open(to,O_BINARY|O_CREAT|O_TRUNC|O_RDWR,S_IREAD|S_IWRITE);
  26.       if (fdto < 0)
  27. .D 42 2
  28. .I 46 5
  29.       /* Use the largest buffer we can get    */
  30.  
  31.       for (bufsiz = 0x4000; bufsiz >= 128; bufsiz >>= 1)
  32.       {
  33.             register char *buffer;
  34. .D 47 3
  35. .I 52 27
  36.             {
  37.                   while (1)
  38.                   {
  39.                         register int n;
  40.  
  41.                         n = read(fdfrom,buffer,bufsiz);
  42.                         if (n == -1)                /* if error             */
  43.                               break;
  44.                         if (n == 0)                 /* if end of file       */
  45.                         {
  46.                               free(buffer);
  47.                               close(fdto);
  48.                               close(fdfrom);
  49.                               return 0;             /* success              */
  50.                         }
  51.                         if (n != write(fdto,buffer,(unsigned) n))
  52.                               break;
  53.                   }
  54.                   free(buffer);
  55.                   break;
  56.             }
  57.       }
  58. err2: close(fdto);
  59.       remove(to);                               /* delete any partial file  */
  60. err:  close(fdfrom);
  61.       return 1;
  62. }
  63. .D 53 24
  64.