home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume1 / 8707 / 66 / arcio.c next >
Encoding:
C/C++ Source or Header  |  1990-07-13  |  5.4 KB  |  185 lines

  1. static char *RCSid = "$Header: arcio.c,v 1.2 86/07/15 07:53:11 turner Exp $";
  2.  
  3. /*
  4.  * $Log:    arcio.c,v $
  5.  * Hack-attack 1.3  86/12/20  01:23:45  wilhite@usceast.uucp
  6.  *     Bludgeoned into submission for VAX 11/780 BSD4.2
  7.  *    (ugly code, but fewer core dumps)
  8.  *
  9.  * Revision 1.2  86/07/15  07:53:11  turner
  10.  * 
  11.  * Revision 1.1  86/06/26  15:00:21  turner
  12.  * initial version
  13.  * 
  14.  */
  15.  
  16. /*  ARC - Archive utility - ARCIO
  17.  
  18. $define(tag,$$segment(@1,$$index(@1,=)+1))#
  19. $define(version,Version $tag(
  20. TED_VERSION DB =2.30), created on $tag(
  21. TED_DATE DB =02/03/86) at $tag(
  22. TED_TIME DB =22:56:00))#
  23. $undefine(tag)#
  24.     $version
  25.  
  26. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  27.  
  28.     By:  Thom Henderson
  29.  
  30.     Description:
  31.          This file contains the file I/O routines used to manipulate
  32.          an archive.
  33.  
  34.     Language:
  35.          Computer Innovations Optimizing C86
  36. */
  37. #include <stdio.h>
  38. #include "arc.h"
  39.  
  40. INT readhdr(hdr,f)                     /* read a header from an archive */
  41. struct heads *hdr;                     /* storage for header */
  42. FILE *f;                               /* archive to read header from */
  43. {
  44. #if BSD | ST
  45.         unsigned char dummy[28];
  46.     INT i,j,k;
  47. #endif
  48.     char name[FNLEN];                  /* filename buffer */
  49.     INT try = 0;                       /* retry counter */
  50.     static INT first = 1;              /* true only on first read */
  51.  
  52.     if(!f)                             /* if archive didn't open */
  53.          return 0;                     /* then pretend it's the end */
  54.     if(feof(f))                        /* if no more data */
  55.          return 0;                     /* then signal end of archive */
  56.  
  57.     if(fgetc(f)!=ARCMARK)             /* check archive validity */
  58.     {    if(warn)
  59.          {    printf("An entry in %s has a bad header.\n",arcname);
  60.               nerrs++;
  61.          }
  62.  
  63.          while(!feof(f))
  64.          {    try++;
  65.               if(fgetc(f)==ARCMARK)
  66.               {    ungetc(hdrver=fgetc(f),f);
  67.                    if(hdrver>=0 && hdrver<=ARCVER)
  68.                         break;
  69.               }
  70.          }
  71.  
  72.          if(feof(f) && first)
  73.               abort("%s is not an archive",arcname);
  74.  
  75.          if(warn)
  76.               printf("  %d bytes skipped.\n",try);
  77.  
  78.          if(feof(f))
  79.               return 0;
  80.     }
  81.  
  82.     hdrver = fgetc(f);                 /* get header version */
  83.     if(hdrver<0)
  84.          abort("Invalid header in archive %s",arcname);
  85.     if(hdrver==0)
  86.          return 0;                     /* note our end of archive marker */
  87.     if(hdrver>ARCVER)
  88.     {    fread(name,sizeof(char),FNLEN,f);
  89.          printf("I don't know how to handle file %s in archive %s\n",
  90.               name,arcname);
  91.          printf("I think you need a newer version of ARC.\n");
  92.          exit(1);
  93.     }
  94.  
  95.     /* amount to read depends on header type */
  96.  
  97.     if(hdrver==1)                      /* old style is shorter */
  98.     {    fread(hdr,sizeof(struct heads)-sizeof(long),1,f);
  99.          hdrver = 2;                   /* convert header to new format */
  100.          hdr->length = hdr->size;      /* size is same when not packed */
  101.     }
  102.     else {
  103. #if MSDOS
  104.     fread(hdr,sizeof(struct heads),1,f);
  105. #endif
  106. #if BSD | ST
  107.     fread(dummy,27,1,f);
  108.  
  109.     for(i=0;i<13;hdr->name[i]=dummy[i],i++);
  110.     hdr->size = (long)((dummy[16]<<24) + (dummy[15]<<16) + (dummy[14]<<8)
  111.         + dummy[13]);
  112.     hdr->date = (short)((dummy[18]<<8) + dummy[17]);
  113.     hdr->time = (short)((dummy[20]<<8) + dummy[19]);
  114.     hdr->crc  = (short)((dummy[22]<<8) + dummy[21]);
  115.     hdr->length = (long)((dummy[26]<<24) + (dummy[25]<<16)
  116.         + (dummy[24]<<8) + dummy[23]);
  117. #endif
  118.     }
  119.  
  120.     first = 0; return 1;               /* we read something */
  121. }
  122.  
  123. INT writehdr(hdr,f)                        /* write a header to an archive */
  124. struct heads *hdr;                     /* header to write */
  125. FILE *f;                               /* archive to write to */
  126. {
  127.     unsigned char dummy[28];
  128.  
  129.     fputc(ARCMARK,f);                 /* write out the mark of ARC */
  130.     fputc(hdrver,f);                   /* write out the header version */
  131.     if(!hdrver)                        /* if that's the end */
  132.          return;                       /* then write no more */
  133. #if MSDOS
  134.     fwrite(hdr,sizeof(struct heads),1,f);
  135. #endif
  136. #if BSD | ST
  137. /*
  138.  * put out the hdr in the brain damaged unaligned half back *sswards
  139.  * way HAL does it
  140.  */
  141.     fwrite(hdr->name,1,13,f);
  142.     fwrite(&hdr->size,sizeof(long),1,f);
  143.     fwrite(&hdr->date,sizeof(INT),1,f);
  144.     fwrite(&hdr->time,sizeof(INT),1,f);
  145.     fwrite(&hdr->crc ,sizeof(INT),1,f);
  146.     fwrite(&hdr->length,sizeof(long),1,f);
  147. #endif
  148.  
  149.     /* note the newest file for updating the archive timestamp */
  150.  
  151.     if(hdr->date>arcdate
  152.     ||(hdr->date==arcdate && hdr->time>arctime))
  153.     {    arcdate = hdr->date;
  154.          arctime = hdr->time;
  155.     }
  156. }
  157.  
  158. INT filecopy(f,t,size)                     /* bulk file copier */
  159. FILE *f, *t;                           /* from, to */
  160. long size;                             /* number of bytes */
  161. {
  162.  INT len;                           /* length of a given copy */
  163.  INT putc_tst();
  164.  
  165.     while(size--)                      /* while more bytes to move */
  166.          putc_tst(fgetc(f),t);
  167. }
  168.  
  169. INT putc_tst(c,t)                          /* put a character, with tests */
  170. char c;                                /* character to output */
  171. FILE *t;                               /* file to write to */
  172. {
  173.     if(t)
  174. #if MSODS | ST
  175.          if(fputc(c,t)==EOF)
  176.               abort("Write fail (disk full?)");
  177. #endif
  178. #if BSD
  179. /*
  180.  * for reasons beyond me BSD unix returns EOF 
  181.  */
  182.     fputc(c,t);
  183. #endif
  184. }
  185.