home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / COMPRESS / ARC520S.ZIP / MARC.C < prev   
Encoding:
C/C++ Source or Header  |  1986-10-23  |  7.9 KB  |  198 lines

  1. /*  MARC - Archive merge utility
  2.  
  3.     Version 5.20, created on 10/23/86 at 17:51:27
  4.  
  5. (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  6.  
  7.     By:  Thom Henderson
  8.  
  9.     Description:
  10.          This program is used to "merge" archives.  That is, to move
  11.          files from one archive to another with no data conversion.
  12.          Please refer to the ARC source for a description of archives
  13.          and archive formats.
  14.  
  15.     Instructions:
  16.          Run this program with no arguments for complete instructions.
  17.  
  18.     Language:
  19.          Computer Innovations Optimizing C86
  20. */
  21. #include <stdio.h>
  22. #include "arc.h"
  23.  
  24. FILE *src;                             /* source archive */
  25.  
  26. char srcname[100];                 /* source archive name */
  27.  
  28. main(nargs,arg)                        /* system entry point */
  29. int nargs;                             /* number of arguments */
  30. char *arg[];                           /* pointers to arguments */
  31. {
  32.     char *makefnam();                  /* filename fixup routine */
  33.     static char *allf[] = {"*.*"};     /* phony arg to merge all files */
  34.  
  35.     if(nargs<3)
  36.     {    printf("MARC - Archive merger, Version 5.20, created on 10/23/86 at 17:51:27\n");
  37.          printf("(C) COPYRIGHT 1985 by System Enhancement Associates;");
  38.          printf(" ALL RIGHTS RESERVED\n\n");
  39.          printf("Please refer all inquiries to:\n\n");
  40.          printf("       System Enhancement Associates\n");
  41.          printf("       21 New Street, Wayne NJ 07470\n\n");
  42.          printf("You may copy and distribute this program freely,");
  43.          printf(" provided that:\n");
  44.          printf("    1)   No fee is charged for such copying and");
  45.          printf(" distribution, and\n");
  46.          printf("    2)   It is distributed ONLY in its original,");
  47.          printf(" unmodified state.\n\n");
  48.          printf("If you like this program, and find it of use, then your");
  49.          printf(" contribution will\n");
  50.          printf("be appreciated.  You may not use this product in a");
  51.          printf(" commercial environment\n");
  52.          printf("or a governmental organization without paying a license");
  53.          printf(" fee of $35.  Site\n");
  54.          printf("licenses and commercial distribution licenses are");
  55.          printf(" available.  A program\n");
  56.          printf("disk and printed documentation are available for $50.\n");
  57.          printf("\nIf you fail to abide by the terms of this license, ");
  58.          printf(" then your conscience\n");
  59.          printf("will haunt you for the rest of your life.\n\n");
  60.  
  61.          printf("Usage: MARC <tgtarc> <srcarc> [<filename> . . .]\n");
  62.          printf("Where: <tgtarc> is the archive to add files to,\n");
  63.          printf("       <srcarc> is the archive to get files from, and\n");
  64.          printf("       <filename> is zero or more file names to get.\n");
  65.          return 1;
  66.     }
  67.  
  68.     makefnam(arg[1],".ARC",arcname);   /* fix up archive names */
  69.     makefnam(arg[2],".ARC",srcname);
  70.     makefnam(".$$$",arcname,newname);
  71.     upper(arcname); upper(srcname); upper(newname);
  72.  
  73.     arc = fopen(arcname,"rb");         /* open the archives */
  74.     if(!(src=fopen(srcname,"rb")))
  75.          abort("Cannot read source archive %s",srcname);
  76.     if(!(new=fopen(newname,"wb")))
  77.          abort("Cannot create new archive %s",newname);
  78.  
  79.     if(!arc)
  80.          printf("Creating new archive %s\n",arcname);
  81.  
  82.     if(nargs==3)
  83.          merge(1,allf);                /* merge all files */
  84.     else merge(nargs-3,&arg[3]);       /* merge selected files */
  85.  
  86.     if(arc) fclose(arc);               /* close the archives */
  87.     fclose(src);
  88.  
  89.     setstamp(new,arcdate,arctime);     /* new arc matches newest file */
  90.     fclose(new);
  91.  
  92.     if(arc)                            /* make the switch */
  93.          if(unlink(arcname))
  94.               abort("Unable to delete old copy of %s",arcname);
  95.     if(rename(newname,arcname))
  96.          abort("Unable to rename %s to %s",newname,arcname);
  97.  
  98.     return nerrs;
  99. }
  100.  
  101. merge(nargs,arg)                       /* merge two archives */
  102. int nargs;                             /* number of filename templates */
  103. char *arg[];                           /* pointers to names */
  104. {
  105.     struct heads srch;                 /* source archive header */
  106.     struct heads arch;                 /* target archive header */
  107.     int gotsrc, gotarc;                /* archive entry versions (0=end) */
  108.     int copy;                          /* true to copy file from source */
  109.     int n;                             /* index */
  110.  
  111.     gotsrc = gethdr(src,&srch);        /* get first source file */
  112.     gotarc = gethdr(arc,&arch);        /* get first target file */
  113.  
  114.     while(gotsrc || gotarc)            /* while more to merge */
  115.     {    if(strcmp(srch.name,arch.name)>0)
  116.          {    copyfile(arc,&arch,gotarc);
  117.               gotarc = gethdr(arc,&arch);
  118.          }
  119.  
  120.          else if(strcmp(srch.name,arch.name)<0)
  121.          {    copy = 0;
  122.               for(n=0; n<nargs; n++)
  123.               {    if(match(srch.name,arg[n]))
  124.                    {    copy = 1;
  125.                         break;
  126.                    }
  127.               }
  128.               if(copy)                 /* select source or target */
  129.               {    printf("Adding file:   %s\n",srch.name);
  130.                    copyfile(src,&srch,gotsrc);
  131.               }
  132.               else fseek(src,srch.size,1);
  133.               gotsrc = gethdr(src,&srch);
  134.          }
  135.  
  136.          else                          /* duplicate names */
  137.          {    copy = 0;
  138.               {    if((srch.date>arch.date)
  139.                    || (srch.date==arch.date && srch.time>arch.time))
  140.                    {    for(n=0; n<nargs; n++)
  141.                         {    if(match(srch.name,arg[n]))
  142.                              {    copy = 1;
  143.                                   break;
  144.                              }
  145.                         }
  146.                    }
  147.               }
  148.               if(copy)                 /* select source or target */
  149.               {    printf("Updating file: %s\n",srch.name);
  150.                    copyfile(src,&srch,gotsrc);
  151.                    gotsrc = gethdr(src,&srch);
  152.                    if(gotarc)
  153.                    {    fseek(arc,arch.size,1);
  154.                         gotarc = gethdr(arc,&arch);
  155.                    }
  156.               }
  157.               else
  158.               {    copyfile(arc,&arch,gotarc);
  159.                    gotarc = gethdr(arc,&arch);
  160.                    if(gotsrc)
  161.                    {    fseek(src,srch.size,1);
  162.                         gotsrc = gethdr(src,&srch);
  163.                    }
  164.               }
  165.          }
  166.     }
  167.  
  168.     hdrver = 0;                        /* end of archive marker */
  169.     writehdr(&arch,new);               /* mark the end of the archive */
  170. }
  171.  
  172. int gethdr(f,hdr)                      /* special read header for merge */
  173. FILE *f;                               /* file to read from */
  174. struct heads *hdr;                     /* storage for header */
  175. {
  176.     char *i = hdr->name;               /* string index */
  177.     int n;                             /* index */
  178.  
  179.     for(n=0; n<13; n++)            /* fill name field */
  180.          *i++ = 0176;                  /* impossible high value */
  181.     *--i = '\0';                       /* properly end the name */
  182.  
  183.     hdrver = 0;                        /* reset header version */
  184.     if(readhdr(hdr,f))                 /* use normal reading logic */
  185.          return hdrver;                /* return the version */
  186.     else return 0;                     /* or fake end of archive */
  187. }
  188.  
  189. copyfile(f,hdr,ver)                    /* copy a file from an archive */
  190. FILE *f;                               /* archive to copy from */
  191. struct heads *hdr;                     /* header data for file */
  192. int ver;                               /* header version */
  193. {
  194.     hdrver = ver;                      /* set header version */
  195.     writehdr(hdr,new);                 /* write out the header */
  196.     filecopy(f,new,hdr->size);         /* copy over the data */
  197. }
  198.