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

  1. /*  ARC - Archive utility - ARCCVT
  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 routines used to convert archives to use
  9.          newer file storage methods.
  10.  
  11.     Language:
  12.          Computer Innovations Optimizing C86
  13. */
  14. #include <stdio.h>
  15. #include "arc.h"
  16.  
  17. static char tempname[STRLEN];         /* temp file name */
  18.  
  19. cvtarc(num,arg)                        /* convert archive */
  20. int num;                               /* number of arguments */
  21. char *arg[];                           /* pointers to arguments */
  22. {
  23.     struct heads hdr;                  /* file header */
  24.     int cvt;                           /* true to convert current file */
  25.     int did[MAXARG];                  /* true when argument was used */
  26.     int n;                             /* index */
  27.     char *makefnam();                  /* filename fixer */
  28.     FILE *fopen();                     /* file opener */
  29.  
  30.     if(arctemp)                   /* use temp area if specified */
  31.          sprintf(tempname,"%s/$ARCTEMP.CVT",arctemp);
  32.     else makefnam("$ARCTEMP.CVT",arcname,tempname);
  33.  
  34.     openarc(1);                        /* open archive for changes */
  35.  
  36.     for(n=0; n<num; n++)               /* for each argument */
  37.          did[n] = 0;                   /* reset usage flag */
  38.     rempath(num,arg);                  /* strip off paths */
  39.  
  40.     if(num)                            /* if files were named */
  41.     {    while(readhdr(&hdr,arc))      /* while more files to check */
  42.          {    cvt = 0;                 /* reset convert flag */
  43.               for(n=0; n<num; n++)     /* for each template given */
  44.               {    if(match(hdr.name,arg[n]))
  45.                    {    cvt = 1;       /* turn on convert flag */
  46.                         did[n] = 1;    /* turn on usage flag */
  47.                         break;         /* stop looking */
  48.                    }
  49.               }
  50.  
  51.               if(cvt)                  /* if converting this one */
  52.                    cvtfile(&hdr);      /* then do it */
  53.               else                     /* else just copy it */
  54.               {    writehdr(&hdr,new);
  55.                    filecopy(arc,new,hdr.size);
  56.               }
  57.          }
  58.     }
  59.  
  60.     else while(readhdr(&hdr,arc))      /* else convert all files */
  61.          cvtfile(&hdr);
  62.  
  63.     hdrver = 0;                        /* archive EOF type */
  64.     writehdr(&hdr,new);                /* write out our end marker */
  65.     closearc(1);                       /* close archive after changes */
  66.  
  67.     if(note)
  68.     {    for(n=0; n<num; n++)          /* report unused args */
  69.          {    if(!did[n])
  70.               {    printf("File not found: %s\n",arg[n]);
  71.                    nerrs++;
  72.               }
  73.          }
  74.     }
  75. }
  76.  
  77. static cvtfile(hdr)                    /* convert a file */
  78. struct heads *hdr;                     /* pointer to header data */
  79. {
  80.     long starts, ftell();              /* where the file goes */
  81.     FILE *tmp, *fopen();               /* temporary file */
  82.  
  83.     if(!(tmp=fopen(tempname,"w+")))
  84.          abort("Unable to create temporary file %s",tempname);
  85.  
  86.     if(note)
  87.          printf("Converting file: %-12s   reading, ",hdr->name);
  88.  
  89.     unpack(arc,tmp,hdr);               /* unpack the entry */
  90.     fseek(tmp,0L,0);                   /* reset temp for reading */
  91.  
  92.     starts = ftell(new);               /* note where header goes */
  93.     hdrver = ARCVER;                  /* anything but end marker */
  94.     writehdr(hdr,new);                 /* write out header skeleton */
  95.     pack(tmp,new,hdr);                 /* pack file into archive */
  96.     fseek(new,starts,0);               /* move back to header skeleton */
  97.     writehdr(hdr,new);                 /* write out real header */
  98.     fseek(new,hdr->size,1);            /* skip over data to next header */
  99.     fclose(tmp);                       /* all done with the file */
  100.     if(unlink(tempname) && warn)
  101.     {    printf("Cannot unsave %s\n",tempname);
  102.          nerrs++;
  103.     }
  104. }
  105.