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

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