home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / arcppc / src / arcadd.c < prev    next >
C/C++ Source or Header  |  1998-04-23  |  10KB  |  362 lines

  1. /*
  2.  * $Header: arcadd.c,v 1.9 88/07/31 18:45:14 hyc Exp $
  3.  */
  4.  
  5. /*
  6.  * ARC - Archive utility - ARCADD
  7.  * 
  8.  * Version 3.40, created on 06/18/86 at 13:10:18
  9.  * 
  10.  * (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  11.  * 
  12.  * By:  Thom Henderson
  13.  * 
  14.  * Description: This file contains the routines used to add files to an archive.
  15.  * 
  16.  * Language: Computer Innovations Optimizing C86
  17.  */
  18. #include <stdio.h>
  19. #include "arc.h"
  20. #if    MTS
  21. #include <mts.h>
  22. #endif
  23.  
  24. static    void    addfile();
  25. char    *strcpy();
  26. int    strcmp(), strlen(), free(), readhdr(), unlink();
  27. #if    UNIX
  28. int    izadir();
  29. #endif
  30. void    writehdr(), filecopy(), getstamp();
  31. void    pack(), closearc(), openarc(), abort();
  32.  
  33. void
  34. addarc(num, arg, move, update, fresh)        /* add files to archive */
  35.     int             num;    /* number of arguments */
  36.     char           *arg[];    /* pointers to arguments */
  37. int             move;        /* true if moving file */
  38. int             update;        /* true if updating */
  39. int             fresh;        /* true if freshening */
  40. {
  41.     char           *d, *dir();    /* directory junk */
  42.     char            buf[STRLEN];    /* pathname buffer */
  43.     char          **path;    /* pointer to pointers to paths */
  44.     char          **name;    /* pointer to pointers to names */
  45.     int             nfiles = 0;    /* number of files in lists */
  46.     int             notemp;    /* true until a template works */
  47.     int             nowork = 1;    /* true until files are added */
  48.     char           *i, *rindex();    /* string indexing junk */
  49.     char           *malloc(), *realloc();    /* memory allocators */
  50.     int             n;    /* index */
  51. #if    MSDOS
  52.     unsigned int    coreleft();    /* remaining memory reporter */
  53. #endif
  54.     int        addbunch();
  55.  
  56.     if (num < 1) {        /* if no files named */
  57.         num = 1;    /* then fake one */
  58. #if    DOS
  59.         arg[0] = "*.*";    /* add everything */
  60. #endif
  61. #if    UNIX
  62.         arg[0] = "*";
  63. #endif
  64. #if    MTS
  65.         arg[0] = "?";
  66. #endif
  67.     }
  68.     path = (char **) malloc(sizeof(char **));
  69.     name = (char **) malloc(sizeof(char **));
  70.  
  71.  
  72.     for (n = 0; n < num; n++) {    /* for each template supplied */
  73.         strcpy(buf, arg[n]);    /* get ready to fix path */
  74. #if    !MTS
  75.         if (!(i = rindex(buf, '\\')))
  76.             if (!(i = rindex(buf, '/')))
  77.                 if (!(i = rindex(buf, ':')))
  78.                     i = buf - 1;
  79. #else
  80.         if (!(i = rindex(buf, sepchr[0])))
  81.             if (buf[0] != tmpchr[0])
  82.                 i = buf - 1;
  83.             else
  84.                 i = buf;
  85. #endif
  86.         i++;        /* pointer to where name goes */
  87.  
  88.         notemp = 1;    /* reset files flag */
  89.         for (d = dir(arg[n]); d; d = dir(NULL)) {
  90.             notemp = 0;    /* template is giving results */
  91.             nfiles++;    /* add each matching file */
  92.             path = (char **) realloc(path, nfiles * sizeof(char **));
  93.             name = (char **) realloc(name, nfiles * sizeof(char **));
  94.             strcpy(i, d);    /* put name in path */
  95.             path[nfiles - 1] = malloc(strlen(buf) + 1);
  96.             strcpy(path[nfiles - 1], buf);
  97.             name[nfiles - 1] = d;    /* save name */
  98. #if    MSDOS
  99.             if (coreleft() < 5120) {
  100.                 nfiles = addbunch(nfiles, path, name, move, update, fresh);
  101.                 nowork = nowork && !nfiles;
  102.                 while (nfiles) {
  103.                     free(path[--nfiles]);
  104.                     free(name[nfiles]);
  105.                 }
  106.                 free(path);
  107.                 free(name);
  108.                 path = name = NULL;
  109.             }
  110. #endif
  111.         }
  112.         if (notemp && warn)
  113.             printf("No files match: %s\n", arg[n]);
  114.     }
  115.  
  116.     if (nfiles) {
  117.         nfiles = addbunch(nfiles, path, name, move, update, fresh);
  118.         nowork = nowork && !nfiles;
  119.         while (nfiles) {
  120.             free(path[--nfiles]);
  121.             free(name[nfiles]);
  122.         }
  123.         free(path);
  124.         free(name);
  125.     }
  126.     if (nowork && warn)
  127.         printf("No files were added.\n");
  128. }
  129.  
  130. int
  131. addbunch(nfiles, path, name, move, update, fresh)    /* add a bunch of files */
  132.     int             nfiles;    /* number of files to add */
  133.     char          **path;    /* pointers to pathnames */
  134.     char          **name;    /* pointers to filenames */
  135.     int             move;    /* true if moving file */
  136.     int             update;    /* true if updating */
  137.     int             fresh;    /* true if freshening */
  138. {
  139.     int             m, n;    /* indices */
  140.     char           *d;    /* swap pointer */
  141.     struct heads    hdr;    /* file header data storage */
  142.  
  143.     for (n = 0; n < nfiles - 1; n++) {    /* sort the list of names */
  144.         for (m = n + 1; m < nfiles; m++) {
  145.             if (strcmp(name[n], name[m]) > 0) {
  146.                 d = path[n];
  147.                 path[n] = path[m];
  148.                 path[m] = d;
  149.                 d = name[n];
  150.                 name[n] = name[m];
  151.                 name[m] = d;
  152.             }
  153.         }
  154.     }
  155.  
  156.     for (n = 0; n < nfiles - 1;) {    /* consolidate the list of names */
  157.         if (!strcmp(path[n], path[n + 1])    /* if duplicate names */
  158.             ||!strcmp(path[n], arcname)    /* or this archive */
  159. #if    UNIX
  160.             ||izadir(path[n])    /* or a directory */
  161. #endif
  162.             ||!strcmp(path[n], newname)    /* or the new version */
  163.             ||!strcmp(path[n], bakname)) {    /* or its backup */
  164.             free(path[n]);    /* then forget the file */
  165.             free(name[n]);
  166.             for (m = n; m < nfiles - 1; m++) {
  167.                 path[m] = path[m + 1];
  168.                 name[m] = name[m + 1];
  169.             }
  170.             nfiles--;
  171.         } else
  172.             n++;    /* else test the next one */
  173.     }
  174.  
  175.     if (!strcmp(path[n], arcname)    /* special check for last file */
  176.         ||!strcmp(path[n], newname)    /* courtesy of Rick Moore */
  177. #if    UNIX
  178.         ||izadir(path[n])
  179. #endif
  180.         || !strcmp(path[n], bakname)) {
  181.         free(path[n]);
  182.         free(name[n]);
  183.         nfiles--;
  184.     }
  185.     if (!nfiles)        /* make sure we got some */
  186.         return 0;
  187.  
  188.     for (n = 0; n < nfiles - 1; n++) {    /* watch out for duplicate
  189.                          * names */
  190.         if (!strcmp(name[n], name[n + 1]))
  191.             abort("Duplicate filenames:\n  %s\n  %s", path[n], path[n + 1]);
  192.     }
  193.     openarc(1);        /* open archive for changes */
  194.  
  195.     for (n = 0; n < nfiles; n++)    /* add each file in the list */
  196.         addfile(path[n], name[n], update, fresh);
  197.  
  198.     /* now we must copy over all files that follow our additions */
  199.  
  200.     while (readhdr(&hdr, arc)) {    /* while more entries to copy */
  201.         writehdr(&hdr, new);
  202.         filecopy(arc, new, hdr.size);
  203.     }
  204.     hdrver = 0;        /* archive EOF type */
  205.     writehdr(&hdr, new);    /* write out our end marker */
  206.     closearc(1);        /* close archive after changes */
  207.  
  208.     if (move) {        /* if this was a move */
  209.         for (n = 0; n < nfiles; n++) {    /* then delete each file
  210.                          * added */
  211.             if (unlink(path[n]) && warn) {
  212.                 printf("Cannot unsave %s\n", path[n]);
  213.                 nerrs++;
  214.             }
  215.         }
  216.     }
  217.     return nfiles;        /* say how many were added */
  218. }
  219.  
  220. static          void
  221. addfile(path, name, update, fresh)    /* add named file to archive */
  222.     char           *path;    /* path name of file to add */
  223.     char           *name;    /* name of file to add */
  224.     int             update;    /* true if updating */
  225.     int             fresh;    /* true if freshening */
  226. {
  227.     struct heads    nhdr;    /* data regarding the new file */
  228.     struct heads    ohdr;    /* data regarding an old file */
  229.     FILE           *f, *fopen();    /* file to add, opener */
  230.     long            starts, ftell();    /* file locations */
  231.     int             upd = 0;/* true if replacing an entry */
  232.  
  233. #if    !MTS
  234.     if (!(f = fopen(path, OPEN_R)))
  235. #else
  236.     if (image)
  237.         f = fopen(path, "rb");
  238.     else
  239.         f = fopen(path, "r");
  240.     if (!f)
  241. #endif
  242.     {
  243.         if (warn) {
  244.             printf("Cannot read file: %s\n", path);
  245.             nerrs++;
  246.         }
  247.         return;
  248.     }
  249. #if    !DOS
  250.     if (strlen(name) >= FNLEN) {
  251.         if (warn) {
  252.             char    buf[STRLEN];
  253.             printf("WARNING: File %s name too long!\n", name);
  254.             name[FNLEN-1]='\0';
  255.             while(1) {
  256.                 printf("  Truncate to %s (y/n)? ", name);
  257.                 fflush(stdout);
  258.                 fgets(buf, STRLEN, stdin);
  259.                 *buf = toupper(*buf);
  260.                 if (*buf == 'Y' || *buf == 'N')
  261.                     break;
  262.             }
  263.             if (*buf == 'N') {
  264.                 printf("Skipping...\n");
  265.                 fclose(f);
  266.                 return;
  267.             }
  268.         }
  269.         else {
  270.             if (note)
  271.                 printf("Skipping file: %s - name too long.\n",
  272.                     name);
  273.             fclose(f);
  274.             return;
  275.         }
  276.     }
  277. #endif
  278.     strcpy(nhdr.name, name);/* save name */
  279.     nhdr.size = 0;        /* clear out size storage */
  280.     nhdr.crc = 0;        /* clear out CRC check storage */
  281. #if    !MTS
  282.     getstamp(f, &nhdr.date, &nhdr.time);
  283. #else
  284.     {
  285.     char *buffer, *malloc();
  286.     int    inlen;
  287.     struct    GDDSECT    *region;
  288.  
  289.     region=gdinfo(f->_fd._fdub);
  290.     inlen=region->GDINLEN;
  291.     buffer=malloc(inlen);    /* maximum line length */
  292.     setbuf(f,buffer);        
  293.     f->_bufsiz=inlen;        
  294.     f->_mods|=_NOIC;    /* Don't do "$continue with" */
  295.     f->_mods&=~_IC;        /* turn it off, if set... */
  296.     }
  297.     getstamp(path, &nhdr.date, &nhdr.time);
  298. #endif
  299.  
  300.     /* position archive to spot for new file */
  301.  
  302.     if (arc) {        /* if adding to existing archive */
  303.         starts = ftell(arc);    /* where are we? */
  304.         while (readhdr(&ohdr, arc)) {    /* while more files to check */
  305.             if (!strcmp(ohdr.name, nhdr.name)) {
  306.                 upd = 1;    /* replace existing entry */
  307.                 if (update || fresh) {    /* if updating or
  308.                              * freshening */
  309.                     if (nhdr.date < ohdr.date
  310.                         || (nhdr.date == ohdr.date && nhdr.time <= ohdr.time)) {
  311.                         fseek(arc, starts, 0);
  312.                         fclose(f);
  313.                         return;    /* skip if not newer */
  314.                     }
  315.                 }
  316.             }
  317.             if (strcmp(ohdr.name, nhdr.name) >= 0)
  318.                 break;    /* found our spot */
  319.  
  320.             writehdr(&ohdr, new);    /* entry preceeds update;
  321.                          * keep it */
  322.             filecopy(arc, new, ohdr.size);
  323.             starts = ftell(arc);    /* now where are we? */
  324.         }
  325.  
  326.         if (upd) {    /* if an update */
  327.             if (note) {
  328.                 printf("Updating file: %-12s  ", name);
  329.                 fflush(stdout);
  330.             }
  331.             fseek(arc, ohdr.size, 1);
  332.         } else if (fresh) {    /* else if freshening */
  333.             fseek(arc, starts, 0);    /* then do not add files */
  334.             fclose(f);
  335.             return;
  336.         } else {    /* else adding a new file */
  337.             if (note) {
  338.                 printf("Adding file:   %-12s  ", name);
  339.                 fflush(stdout);
  340.             }
  341.             fseek(arc, starts, 0);    /* reset for next time */
  342.         }
  343.     } else {        /* no existing archive */
  344.         if (fresh) {    /* cannot freshen nothing */
  345.             fclose(f);
  346.             return;
  347.         } else if (note) {    /* else adding a file */
  348.             printf("Adding file:   %-12s  ", name);
  349.             fflush(stdout);
  350.         }
  351.     }
  352.  
  353.     starts = ftell(new);    /* note where header goes */
  354.     hdrver = ARCVER;        /* anything but end marker */
  355.     writehdr(&nhdr, new);    /* write out header skeleton */
  356.     pack(f, new, &nhdr);    /* pack file into archive */
  357.     fseek(new, starts, 0);    /* move back to header skeleton */
  358.     writehdr(&nhdr, new);    /* write out real header */
  359.     fseek(new, nhdr.size, 1);    /* skip over data to next header */
  360.     fclose(f);        /* all done with the file */
  361. }
  362.