home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / ARC521-2.ZIP / ARCADD.C < prev    next >
C/C++ Source or Header  |  1989-12-29  |  10KB  |  373 lines

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