home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / RES.ZIP / RES.C next >
Encoding:
C/C++ Source or Header  |  1988-02-26  |  2.6 KB  |  93 lines

  1. /* Res.c    Drops the 128 byte file header that Dos 3.2's backup places
  2.              on each file.  By doing this you may be able to fix a file that
  3.              dos won't restore.  This should work with other versions of
  4.              dos.   The source code is for Turbo C.  For best results compile
  5.              in the compact, large, or huge models.*/
  6.  
  7. #include <io.h>
  8. #include <alloc.h>
  9. #include <stdio.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <process.h>
  13. #include <stat.h>
  14. #define MAXBUFSIZE 65534L       /*can't be > 65534 due to read fcn*/
  15. main(int argc, char *argv[])
  16. {
  17.     int in_han, out_han;
  18.     char *databuffer;
  19.     unsigned long filesize, bufsize;
  20.     unsigned int amt_to_transfer;
  21.  
  22.     printf("RES version 2.0 by S. Ostrander\n\n");
  23.  
  24.     bufsize = coreleft();            /* allocate memory for data buffer*/
  25.     if (bufsize > MAXBUFSIZE)
  26.         bufsize = MAXBUFSIZE;
  27.     bufsize -= 100;
  28.     if ((databuffer = malloc(bufsize)) == NULL)
  29.         {
  30.         printf("Not enough memory to run\n");
  31.         exit (1);
  32.         }
  33.  
  34.     if (argc != 3)
  35.         {
  36.         printf("Usage is RES infile outfile\n  Res removes the 128 byte header created by dos's Backup\n  A restore is not needed after using Res.\n");
  37.         printf("  WARNING don't use on backuped files that extend accross more than one disk\n  without consulting the operating instruction.\n");
  38.         exit(2);
  39.         }
  40.     if ((stricmp(argv[1],argv[2])) == 0)
  41.         {
  42.         printf("Input and output files with the same name are not allowed\n");
  43.         exit(3);
  44.         }
  45.  
  46.     _fmode = O_BINARY;                               /* open files*/
  47.     if ((in_han = open(argv[1], O_RDONLY)) == -1)
  48.         {
  49.         printf("Error opening input file\n");
  50.            exit(4);
  51.         }
  52.     if ((out_han = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE)) == -1)
  53.         {
  54.         printf("Error opening output file\n");
  55.         exit (5);
  56.         }
  57.  
  58.     filesize = filelength(in_han);
  59.     amt_to_transfer = (filesize < bufsize) ? filesize : bufsize;
  60.  
  61. /* read and write first buffer and drop restore header */
  62.  
  63.     if ((read(in_han, databuffer, amt_to_transfer)) == -1)
  64.         {
  65.         printf("error reading file");
  66.         exit (6);
  67.         }
  68.     if ((write(out_han, databuffer + 128, amt_to_transfer - 128)) == -1)
  69.         {
  70.         printf("Error writing file");
  71.         exit (7);
  72.         }
  73.  
  74. /* do the rest of the file */
  75.  
  76.     while ((filesize -= amt_to_transfer) != 0)
  77.         {
  78.           amt_to_transfer = (filesize < bufsize) ? filesize : bufsize;
  79.         if ((read(in_han, databuffer, amt_to_transfer)) == -1)
  80.             {
  81.             printf("error reading file");
  82.             exit (8);
  83.             }
  84.         if ((write(out_han, databuffer, amt_to_transfer)) == -1)
  85.             {
  86.             printf("Error writing file");
  87.             exit (9);
  88.             }
  89.         }
  90.     printf("Restoration is completed.\n");
  91.     exit (0);
  92.  
  93. }