home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d050 / unixarc.lha / UnixArc / arcio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-17  |  4.2 KB  |  136 lines

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