home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / arcppc / src / rcs / marc.c,v < prev    next >
Text File  |  1998-04-23  |  12KB  |  483 lines

  1. head     1.5;
  2. branch   ;
  3. access   ;
  4. symbols  patch2:1.5 patch1:1.3;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.5
  10. date     88.08.01.14.19.19;  author hyc;  state Exp;
  11. branches ;
  12. next     1.4;
  13.  
  14. 1.4
  15. date     88.07.31.19.49.35;  author hyc;  state Exp;
  16. branches ;
  17. next     1.3;
  18.  
  19. 1.3
  20. date     88.07.31.19.32.03;  author hyc;  state Exp;
  21. branches ;
  22. next     1.2;
  23.  
  24. 1.2
  25. date     88.06.06.01.05.02;  author hyc;  state Exp;
  26. branches ;
  27. next     1.1;
  28.  
  29. 1.1
  30. date     88.06.06.00.40.07;  author hyc;  state Exp;
  31. branches ;
  32. next     ;
  33.  
  34.  
  35. desc
  36. @Move files from one archive to to another
  37. @
  38.  
  39.  
  40. 1.5
  41. log
  42. @Typos. Dang.
  43. @
  44. text
  45. @/*
  46.  * $Header: marc.c,v 1.4 88/07/31 19:49:35 hyc Exp $
  47.  */
  48.  
  49. /*  MARC - Archive merge utility
  50.  
  51.     Version 5.21, created on 04/22/87 at 15:05:10
  52.  
  53. (C) COPYRIGHT 1985-87 by System Enhancement Associates; ALL RIGHTS RESERVED
  54.  
  55.     By:     Thom Henderson
  56.  
  57.     Description:
  58.      This program is used to "merge" archives.  That is, to move
  59.      files from one archive to another with no data conversion.
  60.      Please refer to the ARC source for a description of archives
  61.      and archive formats.
  62.  
  63.     Instructions:
  64.      Run this program with no arguments for complete instructions.
  65.  
  66.     Language:
  67.      Computer Innovations Optimizing C86
  68. */
  69. #include <stdio.h>
  70. #include "arc.h"
  71.  
  72. #if    UNIX
  73. #include <sys/types.h>
  74. #include <sys/stat.h>
  75. #endif
  76.  
  77. FILE *src;                   /* source archive */
  78. char srcname[STRLEN];               /* source archive name */
  79.  
  80. static char **lst;               /* files list */
  81. static int lnum;               /* length of files list */
  82.  
  83.  
  84. main(nargs,arg)                   /* system entry point */
  85. int nargs;                   /* number of arguments */
  86. char *arg[];                   /* pointers to arguments */
  87. {
  88.     char *makefnam();               /* filename fixup routine */
  89.     char *calloc();               /* memory manager */
  90.     char *envfind();
  91. #if    !MTS
  92.     char *arctemp2, *mktemp();        /* temp file stuff */
  93. #endif
  94. #if    GEMDOS
  95.     void exitpause();
  96. #endif
  97.     int n;                   /* index */
  98. #if    UNIX
  99.     struct    stat    sbuf;
  100. #endif
  101.  
  102.  
  103.     if(nargs<3)
  104.     {     printf("MARC - Archive merger, Version 5.21, created on 04/22/87 at 15:05:10\n");
  105. /*     printf("(C) COPYRIGHT 1985,86,87 by System Enhancement Associates;");
  106.      printf(" ALL RIGHTS RESERVED\n\n");
  107.      printf("Please refer all inquiries to:\n\n");
  108.      printf("    System Enhancement Associates\n");
  109.      printf("    21 New Street, Wayne NJ 07470\n\n");
  110.      printf("You may copy and distribute this program freely,");
  111.      printf(" provided that:\n");
  112.      printf("    1)      No fee is charged for such copying and");
  113.      printf(" distribution, and\n");
  114.      printf("    2)      It is distributed ONLY in its original,");
  115.      printf(" unmodified state.\n\n");
  116.      printf("If you like this program, and find it of use, then your");
  117.      printf(" contribution will\n");
  118.      printf("be appreciated.  You may not use this product in a");
  119.      printf(" commercial environment\n");
  120.      printf("or a governmental organization without paying a license");
  121.      printf(" fee of $35.  Site\n");
  122.      printf("licenses and commercial distribution licenses are");
  123.      printf(" available.  A program\n");
  124.      printf("disk and printed documentation are available for $50.\n");
  125.      printf("\nIf you fail to abide by the terms of this license, ");
  126.      printf(" then your conscience\n");
  127.      printf("will haunt you for the rest of your life.\n\n");
  128. */
  129.      printf("Usage: MARC <tgtarc> <srcarc> [<filename> . . .]\n");
  130.      printf("Where: <tgtarc> is the archive to add files to,\n");
  131.      printf("    <srcarc> is the archive to get files from, and\n");
  132.      printf("    <filename> is zero or more file names to get.\n");
  133.      printf("\nAdapted from MSDOS by Howard Chu\n");
  134. #if    GEMDOS
  135.      exitpause();
  136. #endif
  137.      return 1;
  138.     }
  139.  
  140.     /* see where temp files go */
  141. #if    !MTS
  142.     arctemp = calloc(1, STRLEN);
  143.     if (!(arctemp2 = envfind("ARCTEMP")))
  144.         arctemp2 = envfind("TMPDIR");
  145.     if (arctemp2) {
  146.         strcpy(arctemp, arctemp2);
  147.         n = strlen(arctemp);
  148.         if (arctemp[n - 1] != CUTOFF)
  149.             arctemp[n] = CUTOFF;
  150.     }
  151. #if    UNIX
  152.     else    strcpy(arctemp, "/tmp/");
  153. #endif
  154. #if    !MSDOS
  155.     {
  156.         static char tempname[] = "AXXXXXX";
  157.         strcat(arctemp, mktemp(tempname));
  158.     }
  159. #else
  160.     strcat(arctemp, "$ARCTEMP");
  161. #endif
  162. #else
  163.     guinfo("SHFSEP    ", gotinf);
  164.     sepchr[0] = gotinf[0];
  165.     guinfo("SCRFCHAR", gotinf);
  166.     tmpchr[0] = gotinf[0];
  167.     arctemp = "-$$$";
  168.     arctemp[0] = tmpchr[0];
  169. #endif
  170.  
  171. #if    UNIX
  172.     if (!stat(arg[1],&sbuf))
  173.         strcpy(arcname,arg[1]);
  174.     else
  175.         makefnam(arg[1],".arc",arcname);
  176.     if (!stat(arg[2],&sbuf))
  177.         strcpy(srcname,arg[2]);
  178.     else
  179.         makefnam(arg[2],".arc",srcname);
  180. #else
  181.     makefnam(arg[1],".ARC",arcname);   /* fix up archive names */
  182.     makefnam(arg[2],".ARC",srcname);
  183. /*    makefnam(".$$$",arcname,newname);*/
  184.     sprintf(newname,"%s.arc",arctemp);
  185. #endif
  186.  
  187.     arc = fopen(arcname,OPEN_R);           /* open the archives */
  188.     if(!(src=fopen(srcname,OPEN_R)))
  189.      abort("Cannot read source archive %s",srcname);
  190.     if(!(new=fopen(newname,OPEN_W)))
  191.      abort("Cannot create new archive %s",newname);
  192.  
  193.     if(!arc)
  194.      printf("Creating new archive %s\n",arcname);
  195.  
  196.     /* get the files list set up */
  197.  
  198.     lnum = nargs-3;               /* initial length of list */
  199.     if(lnum<1)                   /* phoney for default case */
  200.     {     lnum = 1;
  201.      lst = (char **) calloc(1,sizeof(char *));
  202.      lst[0] = "*.*";
  203.     }
  204.     else                   /* else use filenames given */
  205.     {     lst = (char **) calloc(lnum,sizeof(char *));
  206.      for(n=3; n<nargs; n++)
  207.           lst[n-3] = arg[n];
  208.  
  209.      for(n=0; n<lnum; )           /* expand indirect references */
  210.      {    if(*lst[n] == '@@')
  211.            expandlst(n);
  212.           else n++;
  213.      }
  214.     }
  215.  
  216.     merge(lnum,lst);               /* merge desired files */
  217.  
  218.     if(arc) fclose(arc);           /* close the archives */
  219.     fclose(src);
  220.     fclose(new);
  221.  
  222.     if(arc)                   /* make the switch */
  223.      if(unlink(arcname))
  224.           abort("Unable to delete old copy of %s",arcname);
  225.     if(move(newname,arcname))
  226.      abort("Unable to rename %s to %s",newname,arcname);
  227.  
  228.     setstamp(arcname,arcdate,arctime);     /* new arc matches newest file */
  229.  
  230. #if    GEMDOS
  231.     exitpause();
  232. #endif
  233.     return nerrs;
  234. }
  235.  
  236. merge(nargs,arg)               /* merge two archives */
  237. int nargs;                   /* number of filename templates */
  238. char *arg[];                   /* pointers to names */
  239. {
  240.     struct heads srch;               /* source archive header */
  241.     struct heads arch;               /* target archive header */
  242.     int gotsrc, gotarc;               /* archive entry versions (0=end) */
  243.     int copy;                   /* true to copy file from source */
  244.     int n;                   /* index */
  245.  
  246.     gotsrc = gethdr(src,&srch);           /* get first source file */
  247.     gotarc = gethdr(arc,&arch);           /* get first target file */
  248.  
  249.     while(gotsrc || gotarc)           /* while more to merge */
  250.     {     if(strcmp(srch.name,arch.name)>0)
  251.      {    copyfile(arc,&arch,gotarc);
  252.           gotarc = gethdr(arc,&arch);
  253.      }
  254.  
  255.      else if(strcmp(srch.name,arch.name)<0)
  256.      {    copy = 0;
  257.           for(n=0; n<nargs; n++)
  258.           {       if(match(srch.name,arg[n]))
  259.            {    copy = 1;
  260.             break;
  261.            }
  262.           }
  263.           if(copy)               /* select source or target */
  264.           {       printf("Adding file:      %s\n",srch.name);
  265.            copyfile(src,&srch,gotsrc);
  266.           }
  267.           else fseek(src,srch.size,1);
  268.           gotsrc = gethdr(src,&srch);
  269.      }
  270.  
  271.      else                   /* duplicate names */
  272.      {    copy = 0;
  273.           {       if((srch.date>arch.date)
  274.            || (srch.date==arch.date && srch.time>arch.time))
  275.            {    for(n=0; n<nargs; n++)
  276.             {    if(match(srch.name,arg[n]))
  277.                  {      copy = 1;
  278.                   break;
  279.                  }
  280.             }
  281.            }
  282.           }
  283.           if(copy)               /* select source or target */
  284.           {       printf("Updating file: %s\n",srch.name);
  285.            copyfile(src,&srch,gotsrc);
  286.            gotsrc = gethdr(src,&srch);
  287.            if(gotarc)
  288.            {    fseek(arc,arch.size,1);
  289.             gotarc = gethdr(arc,&arch);
  290.            }
  291.           }
  292.           else
  293.           {       copyfile(arc,&arch,gotarc);
  294.            gotarc = gethdr(arc,&arch);
  295.            if(gotsrc)
  296.            {    fseek(src,srch.size,1);
  297.             gotsrc = gethdr(src,&srch);
  298.            }
  299.           }
  300.      }
  301.     }
  302.  
  303.     hdrver = 0;                   /* end of archive marker */
  304.     writehdr(&arch,new);           /* mark the end of the archive */
  305. }
  306.  
  307. int gethdr(f,hdr)               /* special read header for merge */
  308. FILE *f;                   /* file to read from */
  309. struct heads *hdr;               /* storage for header */
  310. {
  311.     char *i = hdr->name;           /* string index */
  312.     int n;                   /* index */
  313.  
  314.     for(n=0; n<FNLEN; n++)           /* fill name field */
  315.      *i++ = 0176;               /* impossible high value */
  316.     *--i = '\0';               /* properly end the name */
  317.  
  318.     hdrver = 0;                   /* reset header version */
  319.     if(readhdr(hdr,f))               /* use normal reading logic */
  320.      return hdrver;               /* return the version */
  321.     else return 0;               /* or fake end of archive */
  322. }
  323.  
  324. copyfile(f,hdr,ver)               /* copy a file from an archive */
  325. FILE *f;                   /* archive to copy from */
  326. struct heads *hdr;               /* header data for file */
  327. int ver;                   /* header version */
  328. {
  329.     hdrver = ver;               /* set header version */
  330.     writehdr(hdr,new);               /* write out the header */
  331.     filecopy(f,new,hdr->size);           /* copy over the data */
  332. }
  333.  
  334. static expandlst(n)               /* expand an indirect reference */
  335. int n;                       /* number of entry to expand */
  336. {
  337.     FILE *lf, *fopen();               /* list file, opener */
  338.     char *malloc(), *realloc();           /* memory managers */
  339.     char buf[100];               /* input buffer */
  340.     int x;                   /* index */
  341.     char *p = lst[n]+1;               /* filename pointer */
  342.  
  343.     if(*p)                   /* use name if one was given */
  344.     {     makefnam(p,".CMD",buf);
  345.      upper(buf);
  346.      if(!(lf=fopen(buf,"r")))
  347.           abort("Cannot read list of files in %s",buf);
  348.     }
  349.     else lf = stdin;               /* else use standard input */
  350.  
  351.     for(x=n+1; x<lnum; x++)           /* drop reference from the list */
  352.      lst[x-1] = lst[x];
  353.     lnum--;
  354.  
  355.     while(fscanf(lf,"%99s",buf)>0)     /* read in the list */
  356.     {     if(!(lst=(char **) realloc(lst,(lnum+1)*sizeof(char *))))
  357.           abort("too many file references");
  358.  
  359.      lst[lnum] = malloc(strlen(buf)+1);
  360.      strcpy(lst[lnum],buf);           /* save the name */
  361.      lnum++;
  362.     }
  363.  
  364.     if(lf!=stdin)               /* avoid closing standard input */
  365.      fclose(lf);
  366. }
  367. @
  368.  
  369.  
  370. 1.4
  371. log
  372. @Fix temp file stuff
  373. @
  374. text
  375. @d2 1
  376. a2 1
  377.  * $Header: marc.c,v 1.3 88/07/31 19:32:03 hyc Exp $
  378. d108 1
  379. a108 1
  380.     else    strcpy(arctemp, "/tmp/";
  381. @
  382.  
  383.  
  384. 1.3
  385. log
  386. @Allow arbitrary names for archives on Unix
  387. @
  388. text
  389. @d2 1
  390. a2 1
  391.  * $Header: marc.c,v 1.2 88/06/06 01:05:02 hyc Locked $
  392. d107 3
  393. a112 3
  394. #if     UNIX
  395.         strcpy(arctemp, "/tmp/");
  396. #endif  /*UNIX*/
  397. @
  398.  
  399.  
  400. 1.2
  401. log
  402. @Converted from MSDOS ARC 5.21 sources
  403. @
  404. text
  405. @d2 1
  406. a2 1
  407.  * $Header$
  408. d28 5
  409. d54 3
  410. d58 1
  411. d108 7
  412. a114 1
  413.     strcat(arctemp, mktemp("AXXXXXX"));
  414. d127 12
  415. a138 2
  416.     makefnam(arg[1],".arc",arcname);   /* fix up archive names */
  417.     makefnam(arg[2],".arc",srcname);
  418. a140 3
  419.  
  420. #if    !UNIX
  421.     upper(arcname); upper(srcname); upper(newname);
  422. d143 2
  423. a144 2
  424.     arc = fopen(arcname,"rb");           /* open the archives */
  425.     if(!(src=fopen(srcname,"rb")))
  426. d146 1
  427. a146 1
  428.     if(!(new=fopen(newname,"wb")))
  429. @
  430.  
  431.  
  432. 1.1
  433. log
  434. @Initial revision
  435. @
  436. text
  437. @d1 4
  438. d29 1
  439. a29 1
  440. char srcname[$strlen];               /* source archive name */
  441. d40 8
  442. a47 1
  443.     char *alloc();               /* memory manager */
  444. d52 1
  445. a52 1
  446.      printf("(C) COPYRIGHT 1985,86,87 by System Enhancement Associates;");
  447. d75 1
  448. a75 1
  449.  
  450. d80 4
  451. d87 31
  452. a117 3
  453.     makefnam(arg[1],".ARC",arcname);   /* fix up archive names */
  454.     makefnam(arg[2],".ARC",srcname);
  455.     makefnam(".$$$",arcname,newname);
  456. d119 1
  457. d135 1
  458. a135 1
  459.      lst = alloc(sizeof(char *));
  460. d139 1
  461. a139 1
  462.     {     lst = alloc(lnum*sizeof(char *));
  463. a153 2
  464.  
  465.     setstamp(new,arcdate,arctime);     /* new arc matches newest file */
  466. d159 1
  467. a159 1
  468.     if(rename(newname,arcname))
  469. d162 5
  470. d248 1
  471. a248 1
  472.     for(n=0; n<$fnlen; n++)           /* fill name field */
  473. d272 1
  474. a272 1
  475.     char *alloc(), *realloc();           /* memory managers */
  476. d290 1
  477. a290 1
  478.     {     if(!(lst=realloc(lst,(lnum+1)*sizeof(char *))))
  479. d293 1
  480. a293 1
  481.      lst[lnum] = alloc(strlen(buf)+1);
  482. @
  483.