home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ARC521-2.ZIP / ARCCVT.C < prev    next >
C/C++ Source or Header  |  1989-12-29  |  4KB  |  121 lines

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