home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d01xx / d0128.lha / MRBackup / CopyFile.c < prev    next >
C/C++ Source or Header  |  1988-01-02  |  2KB  |  110 lines

  1. /* MRBackup: File Copy Routine.
  2.  * Date:        09/04/87
  3.  * Notes:
  4.  *        To enhance the performance of MRBackup, this package was copied
  5.  * from my Misc library and modified to couple it more tightly with
  6.  * MRBackup.  It now uses a global buffer area allocated during
  7.  * initialization.
  8.  *
  9.  * History:        (most recent change first)
  10.  *
  11.  * 09/04/87 -MRR- CopyFile, upon failure, now returns the error status
  12.  *                code from IoErr().
  13.  */
  14.  
  15. #include "MRBackup.h"
  16.  
  17. /* 
  18.  * Copy file, preserving date.  
  19.  * Depends upon file date routines in FileMisc.c 
  20.  * Original author: Jeff Lydiatt, Vancouver, Canada
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <exec/types.h>
  25. #include <libraries/dos.h>
  26. #include <exec/memory.h>
  27. #include <functions.h>
  28.  
  29. #define MAXSTR 127
  30.  
  31. extern long Chk_Abort();
  32. extern BOOL GetFileDate(), SetFileDate();
  33. extern long IoErr();
  34.  
  35. /* Copy the last modified date from one file to another.
  36.  * Called with:
  37.  *        from:        name of source file
  38.  *        to:            name of destination file
  39.  * Returns:
  40.  *        0 => success, 1 => failure
  41.  * Note:
  42.  *        Dynamic memory allocation of the DateStamp struction is
  43.  *        necessary to insure longword alignment.
  44.  */
  45.  
  46. BOOL 
  47. CopyFileDate(from,to)
  48.     char *from, *to;
  49. {
  50.     struct DateStamp *date;
  51.     int status = 1;                /* default is fail code */
  52.  
  53.     if (date = (struct DateStamp *) 
  54.         AllocMem((long) sizeof(struct DateStamp), MEMF_PUBLIC)) {
  55.         if (GetFileDate(from,date))
  56.             if (SetFileDate(to,date))
  57.                 status = 0;
  58.         FreeMem(date, (long) sizeof(struct DateStamp));
  59.     }
  60.     return status;
  61. }
  62.  
  63. int     
  64. CopyFile(from,to)
  65.     char *from,*to;
  66. {
  67.     long status, count;
  68.     struct FileHandle *fin,*fout;
  69.  
  70. #ifdef DEBUG
  71.     char    errmsg[256];
  72.     static char *errfmt = "I/O error %ld, file %s";
  73. #endif
  74.  
  75.     if ((fin = Open(from, MODE_OLDFILE ))== NULL ){
  76. badcopy:
  77.         status = IoErr();
  78. #ifdef DEBUG
  79.         sprintf(errmsg, errfmt, status, from);
  80. print_err:
  81.         puts(errmsg);
  82. #endif
  83.         if (fin) Close(fin);
  84.         if (fout) {
  85.             Close(fout);
  86.             unlink(to);                /* delete the bad copy */
  87.         }
  88.         return (int) status;
  89.     }
  90.  
  91.     if ( !(fout = Open(to, MODE_NEWFILE)) ) goto badcopy;
  92.  
  93.     status = 0;
  94.     while ( !status && (count = Read( fin, buffer, bufsize )) == bufsize )
  95.         if ( Write(fout, buffer, count) != count)
  96.                 status = IoErr();
  97.  
  98.     if (!status && count > 0 ) {
  99.         if (Write(fout, buffer, count) != count)
  100.             status = IoErr();
  101.     }
  102.  
  103.     if (status) goto badcopy;
  104.  
  105.     Close(fin);
  106.     Close(fout);
  107.  
  108.     return CopyFileDate(from, to);
  109. }
  110.