home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 157.lha / Arc_Src / arccvt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-27  |  4.0 KB  |  104 lines

  1. /*  ARC - Archive utility - ARCCVT
  2.  
  3. System V Version 1.0 based upon:
  4.     Version 1.16, created on 02/03/86 at 22:53:02
  5.  
  6. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  7.  
  8.     By:  Thom Henderson
  9.  
  10.     Description:
  11.          This file contains the routines used to convert archives to use
  12.          newer file storage methods.
  13. */
  14. #include "arc.h"
  15.  
  16. static char tempname[STRLEN];          /* temp file name */
  17.  
  18. INT cvtarc(num,arg)                    /* convert archive */
  19. INT num;                               /* number of arguments */
  20. char *arg[];                           /* pointers to arguments */
  21. {
  22.     struct heads hdr;                  /* file header */
  23.     INT cvt;                           /* true to convert current file */
  24.     INT did[MAXARG];                   /* true when argument was used */
  25.     INT n;                             /* index */
  26.     char *makefnam();                  /* filename fixer */
  27.     FILE *fopen();                     /* file opener */
  28.     INT cvtfile();
  29.  
  30.     if(arctemp)                   /* use temp area if specified */
  31.          sprintf(tempname,"%s.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 INT 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); fflush(stdout);}
  88.  
  89.     unpack(arc,tmp,hdr);               /* unpack the entry */
  90.     fseek(tmp,0L,0);                   /* reset temp for reading */
  91.     starts = ftell(new);               /* note where header goes */
  92.     hdrver = ARCVER;                   /* anything but end marker */
  93.     writehdr(hdr,new);                 /* write out header skeleton */
  94.     pack(tmp,new,hdr);                 /* pack file into archive */
  95.     fseek(new,starts,0);               /* move back to header skeleton */
  96.     writehdr(hdr,new);                 /* write out real header */
  97.     fseek(new,hdr->size,1);            /* skip over data to next header */
  98.     fclose(tmp);                       /* all done with the file */
  99.     if(unlink(tempname) && warn)
  100.     {    printf("Cannot unsave %s\n",tempname);
  101.          nerrs++;
  102.     }
  103. }
  104.