home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d050 / unixarc.lha / UnixArc / marc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-01-17  |  7.9 KB  |  207 lines

  1. #define MAIN
  2. /*  MARC - Archive merge utility
  3.  
  4. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  5.  
  6.     By:  Thom Henderson
  7.  
  8.     Description:
  9.          This program is used to "merge" archives.  That is, to move
  10.          files from one archive to another with no data conversion.
  11.          Please refer to the ARC source for a description of archives
  12.          and archive formats.
  13.  
  14.     Instructions:
  15.          Run this program with no arguments for complete instructions.
  16.  
  17.     Language:
  18.          Computer Innovations Optimizing C86
  19. */
  20. #include <stdio.h>
  21. #include "arc.h"
  22.  
  23. FILE *src;                             /* source archive */
  24.  
  25. char srcname[STRLEN];                 /* source archive name */
  26.  
  27. main(nargs,arg)                        /* system entry point */
  28. int nargs;                             /* number of arguments */
  29. char *arg[];                           /* pointers to arguments */
  30. {
  31.     char *makefnam();                  /* filename fixup routine */
  32.     static char *allf[] = {"*.*"};     /* phony arg to merge all files */
  33.  
  34.     if(nargs<3)
  35.     {    printf("MARC - Archive merger, %s\n",VERSION);
  36.          printf("(C) COPYRIGHT 1985 by System Enhancement Associates;");
  37.          printf(" ALL RIGHTS RESERVED\n\n");
  38. #if 0
  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. #endif 0
  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. #if unix
  71.     makefnam(arg[1],".$$$",newname);
  72. #else
  73.     makefnam(arg[1],".$$$$",newname);
  74.     upper(arcname); upper(srcname); upper(newname);
  75. #endif
  76.  
  77.     arc = fopen(arcname,"r");         /* open the archives */
  78.     if(!(src=fopen(srcname,"r")))
  79.          abort("Cannot read source archive %s",srcname);
  80.     if(!(new=fopen(newname,"w")))
  81.          abort("Cannot create new archive %s",newname);
  82.  
  83.     if(!arc)
  84.          printf("Creating new archive %s\n",arcname);
  85.  
  86.     if(nargs==3)
  87.          merge(1,allf);                /* merge all files */
  88.     else merge(nargs-3,&arg[3]);       /* merge selected files */
  89.  
  90.     if(arc) fclose(arc);               /* close the archives */
  91.     fclose(src);
  92.  
  93. #if unix
  94.     fclose(new);
  95.     setstamp(newname,arcdate,arctime);     /* new arc matches newest file */
  96. #else
  97.     setstamp(new,arcdate,arctime);     /* new arc matches newest file */
  98.     fclose(new);
  99. #endif
  100.  
  101.     if(arc)                            /* make the switch */
  102.          if(unlink(arcname))
  103.               abort("Unable to delete old copy of %s",arcname);
  104.     if(rename(newname,arcname))
  105.          abort("Unable to rename %s to %s",newname,arcname);
  106.  
  107.     return nerrs;
  108. }
  109.  
  110. merge(nargs,arg)                       /* merge two archives */
  111. int nargs;                             /* number of filename templates */
  112. char *arg[];                           /* pointers to names */
  113. {
  114.     struct heads srch;                 /* source archive header */
  115.     struct heads arch;                 /* target archive header */
  116.     int gotsrc, gotarc;                /* archive entry versions (0=end) */
  117.     int copy;                          /* true to copy file from source */
  118.     int n;                             /* index */
  119.  
  120.     gotsrc = gethdr(src,&srch);        /* get first source file */
  121.     gotarc = gethdr(arc,&arch);        /* get first target file */
  122.  
  123.     while(gotsrc || gotarc)            /* while more to merge */
  124.     {    if(strcmp(srch.name,arch.name)>0)
  125.          {    copyfile(arc,&arch,gotarc);
  126.               gotarc = gethdr(arc,&arch);
  127.          }
  128.  
  129.          else if(strcmp(srch.name,arch.name)<0)
  130.          {    copy = 0;
  131.               for(n=0; n<nargs; n++)
  132.               {    if(match(srch.name,arg[n]))
  133.                    {    copy = 1;
  134.                         break;
  135.                    }
  136.               }
  137.               if(copy)                 /* select source or target */
  138.               {    printf("Adding file:   %s\n",srch.name);
  139.                    copyfile(src,&srch,gotsrc);
  140.               }
  141.               else fseek(src,srch.size,1);
  142.               gotsrc = gethdr(src,&srch);
  143.          }
  144.  
  145.          else                          /* duplicate names */
  146.          {    copy = 0;
  147.               {    if((srch.date>arch.date)
  148.                    || (srch.date==arch.date && srch.time>arch.time))
  149.                    {    for(n=0; n<nargs; n++)
  150.                         {    if(match(srch.name,arg[n]))
  151.                              {    copy = 1;
  152.                                   break;
  153.                              }
  154.                         }
  155.                    }
  156.               }
  157.               if(copy)                 /* select source or target */
  158.               {    printf("Updating file: %s\n",srch.name);
  159.                    copyfile(src,&srch,gotsrc);
  160.                    gotsrc = gethdr(src,&srch);
  161.                    if(gotarc)
  162.                    {    fseek(arc,arch.size,1);
  163.                         gotarc = gethdr(arc,&arch);
  164.                    }
  165.               }
  166.               else
  167.               {    copyfile(arc,&arch,gotarc);
  168.                    gotarc = gethdr(arc,&arch);
  169.                    if(gotsrc)
  170.                    {    fseek(src,srch.size,1);
  171.                         gotsrc = gethdr(src,&srch);
  172.                    }
  173.               }
  174.          }
  175.     }
  176.  
  177.     hdrver = 0;                        /* end of archive marker */
  178.     writehdr(&arch,new);               /* mark the end of the archive */
  179. }
  180.  
  181. int gethdr(f,hdr)                      /* special read header for merge */
  182. FILE *f;                               /* file to read from */
  183. struct heads *hdr;                     /* storage for header */
  184. {
  185.     char *i = hdr->name;               /* string index */
  186.     int n;                             /* index */
  187.  
  188.     for(n=0; n<FNLEN; n++)            /* fill name field */
  189.          *i++ = 0176;                  /* impossible high value */
  190.     *--i = '\0';                       /* properly end the name */
  191.  
  192.     hdrver = 0;                        /* reset header version */
  193.     if(readhdr(hdr,f))                 /* use normal reading logic */
  194.          return hdrver;                /* return the version */
  195.     else return 0;                     /* or fake end of archive */
  196. }
  197.  
  198. copyfile(f,hdr,ver)                    /* copy a file from an archive */
  199. FILE *f;                               /* archive to copy from */
  200. struct heads *hdr;                     /* header data for file */
  201. int ver;                               /* header version */
  202. {
  203.     hdrver = ver;                      /* set header version */
  204.     writehdr(hdr,new);                 /* write out the header */
  205.     filecopy(f,new,hdr->size);         /* copy over the data */
  206. }
  207.