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

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