home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / mtools_3.6.src.lzh / MTOOLS_3.6 / copyfile.c < prev    next >
C/C++ Source or Header  |  1997-11-12  |  807b  |  50 lines

  1. #include "sysincludes.h"
  2. #include "msdos.h"
  3. #include "mtools.h"
  4. #include "file.h"
  5.  
  6. /*
  7.  * Copy the data from source to target
  8.  */
  9.  
  10. int copyfile(Stream_t *Source, Stream_t *Target)
  11. {
  12.     char buffer[16384];
  13.     int pos;
  14.     int ret, retw;
  15.  
  16.     if (!Source){
  17.         fprintf(stderr,"Couldn't open source file\n");
  18.         return -1;
  19.     }
  20.  
  21.     if (!Target){
  22.         fprintf(stderr,"Couldn't open target file\n");
  23.         return -1;
  24.     }
  25.     
  26.     pos = 0;
  27.     while(1){
  28.         ret = READS(Source, buffer, pos, 16384);
  29.         if (ret < 0 ){
  30.             perror("file read");
  31.             return -1;
  32.         }
  33.         if(got_signal)
  34.             return -1;
  35.         if (ret == 0)
  36.             break;
  37.         if ((retw = force_write(Target, buffer, pos, ret)) != ret){
  38.             if(retw < 0 )
  39.                 perror("write");
  40.             else
  41.                 fprintf(stderr,
  42.                     "Short write %d instead of %d\n", retw,
  43.                     ret);
  44.             return ret;
  45.         }
  46.         pos += ret;
  47.     }
  48.     return pos;
  49. }
  50.