home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / OS2ARC_S.ZIP / ARCIO.C < prev    next >
Text File  |  1987-10-14  |  4KB  |  125 lines

  1. /*  ARC - Archive utility - ARCIO
  2.  
  3. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  4. $define(version,Version $tag(
  5. TED_VERSION DB =2.32), created on $tag(
  6. TED_DATE DB =02/13/86) at $tag(
  7. TED_TIME DB =10:19:36))#
  8. $undefine(tag)#
  9.     $version
  10.  
  11. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  12.  
  13.     By:  Thom Henderson
  14.  
  15.     Description:
  16.          This file contains the file I/O routines used to manipulate
  17.          an archive.
  18.  
  19.     Language:
  20.          Computer Innovations Optimizing C86
  21. */
  22. #include <stdio.h>
  23. #include "arc.h"
  24.  
  25. int readhdr(hdr,f)                     /* read a header from an archive */
  26. struct heads *hdr;                     /* storage for header */
  27. FILE *f;                               /* archive to read header from */
  28. {
  29.     char name[_FNLEN];                 /* filename buffer */
  30.     int try = 0;                       /* retry counter */
  31.     static int first = 1;              /* true only on first read */
  32.  
  33.     if(!f)                             /* if archive didn't open */
  34.          return 0;                     /* then pretend it's the end */
  35.     if(feof(f))                        /* if no more data */
  36.          return 0;                     /* then signal end of archive */
  37.  
  38.     if(fgetc(f)!=_ARCMARK)             /* check archive validity */
  39.     {    if(warn)
  40.          {    printf("An entry in %s has a bad header.",arcname);
  41.               nerrs++;
  42.          }
  43.  
  44.          while(!feof(f))
  45.          {    try++;
  46.               if(fgetc(f)==_ARCMARK)
  47.               {    ungetc(hdrver=fgetc(f),f);
  48.                    if(hdrver>=0 && hdrver<=_ARCVER)
  49.                         break;
  50.               }
  51.          }
  52.  
  53.          if(feof(f) && first)
  54.               abort("%s is not an archive",arcname);
  55.  
  56.          if(warn)
  57.               printf("  %d bytes skipped.\n",try);
  58.  
  59.          if(feof(f))
  60.               return 0;
  61.     }
  62.  
  63.     hdrver = fgetc(f);                 /* get header version */
  64.     if(hdrver<0)
  65.          abort("Invalid header in archive %s",arcname);
  66.     if(hdrver==0)
  67.          return 0;                     /* note our end of archive marker */
  68.     if(hdrver>_ARCVER)
  69.     {    fread(name,sizeof(char),_FNLEN,f);
  70.          printf("I don't know how to handle file %s in archive %s\n",
  71.               name,arcname);
  72.          printf("I think you need a newer version of ARC.\n");
  73.          exit(1);
  74.     }
  75.  
  76.     /* amount to read depends on header type */
  77.  
  78.     if(hdrver==1)                      /* old style is shorter */
  79.     {    fread(hdr,sizeof(struct heads)-sizeof(long int),1,f);
  80.          hdrver = 2;                   /* convert header to new format */
  81.          hdr->length = hdr->size;      /* size is same when not packed */
  82.     }
  83.     else fread(hdr,sizeof(struct heads),1,f);
  84.  
  85.     first = 0; return 1;               /* we read something */
  86. }
  87.  
  88. writehdr(hdr,f)                        /* write a header to an archive */
  89. struct heads *hdr;                     /* header to write */
  90. FILE *f;                               /* archive to write to */
  91. {
  92.     fputc(_ARCMARK,f);                 /* write out the mark of ARC */
  93.     fputc(hdrver,f);                   /* write out the header version */
  94.     if(!hdrver)                        /* if that's the end */
  95.          return;                       /* then write no more */
  96.     fwrite(hdr,sizeof(struct heads),1,f);
  97.  
  98.     /* note the newest file for updating the archive timestamp */
  99.  
  100.     if(hdr->date>arcdate
  101.     ||(hdr->date==arcdate && hdr->time>arctime))
  102.     {    arcdate = hdr->date;
  103.          arctime = hdr->time;
  104.     }
  105. }
  106.  
  107. filecopy(f,t,size)                     /* bulk file copier */
  108. FILE *f, *t;                           /* from, to */
  109. long size;                             /* number of bytes */
  110. {
  111.     int len;                           /* length of a given copy */
  112.  
  113.     while(size--)                      /* while more bytes to move */
  114.          putc_tst(fgetc(f),t);
  115. }
  116.  
  117. putc_tst(c,t)                          /* put a character, with tests */
  118. char c;                                /* character to output */
  119. FILE *t;                               /* file to write to */
  120. {
  121.     if(t)
  122.          if(fputc(c,t)==EOF)
  123.               abort("Write fail (disk full?)");
  124. }
  125.