home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PROGRAMS / UTILS / COMPRESS / ARC520S.ZIP / ARCADD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-10-23  |  10.5 KB  |  260 lines

  1. /*  ARC - Archive utility - ARCADD
  2.  
  3.     Version 3.40, created on 06/18/86 at 13:10:18
  4.  
  5. (C) COPYRIGHT 1985,86 by System Enhancement Associates; ALL RIGHTS RESERVED
  6.  
  7.     By:  Thom Henderson
  8.  
  9.     Description:
  10.          This file contains the routines used to add files to an archive.
  11.  
  12.     Language:
  13.          Computer Innovations Optimizing C86
  14. */
  15. #include <stdio.h>
  16. #include "arc.h"
  17.  
  18. addarc(num,arg,move,update,fresh)      /* add files to archive */
  19. int num;                               /* number of arguments */
  20. char *arg[];                           /* pointers to arguments */
  21. int move;                              /* true if moving file */
  22. int update;                            /* true if updating */
  23. int fresh;                             /* true if freshening */
  24. {
  25.     char *d, *dir();                   /* directory junk */
  26.     char buf[100];                 /* pathname buffer */
  27.     char **path = NULL;                /* pointer to pointers to paths */
  28.     char **name = NULL;                /* pointer to pointers to names */
  29.     int nfiles = 0;                    /* number of files in lists */
  30.     int notemp;                        /* true until a template works */
  31.     int nowork = 1;                    /* true until files are added */
  32.     char *i, *rindex();                /* string indexing junk */
  33.     char *alloc(), *realloc();         /* memory allocators */
  34.     int m, n;                          /* indices */
  35.     unsigned int coreleft();           /* remaining memory reporter */
  36.  
  37.     if(num<1)                          /* if no files named */
  38.     {    num = 1;                      /* then fake one */
  39.          arg[0] = "*.*";               /* add everything */
  40.     }
  41.  
  42.     for(n=0; n<num; n++)               /* for each template supplied */
  43.     {    strcpy(buf,arg[n]);           /* get ready to fix path */
  44.          if(!(i=rindex(buf,'\\')))
  45.               if(!(i=rindex(buf,'/')))
  46.                    if(!(i=rindex(buf,':')))
  47.                         i = buf-1;
  48.          i++;                          /* pointer to where name goes */
  49.  
  50.          notemp = 1;                   /* reset files flag */
  51.          for(d=dir(arg[n],0); *d; d=dir(NULL,0))
  52.          {    notemp = 0;              /* template is giving results */
  53.               nfiles++;                /* add each matching file */
  54.               path = (char **)realloc(path,nfiles*sizeof(char **));
  55.               name = (char **)realloc(name,nfiles*sizeof(char **));
  56.               strcpy(i,d);             /* put name in path */
  57.               path[nfiles-1] = alloc(strlen(buf)+1);
  58.               strcpy(path[nfiles-1],buf);
  59.               name[nfiles-1] = d;      /* save name */
  60.  
  61.               if(coreleft()<5120)
  62.               {    nfiles = addbunch(nfiles,path,name,move,update,fresh);
  63.                    nowork = nowork && !nfiles;
  64.                    while(nfiles)
  65.                    {    free(path[--nfiles]);
  66.                         free(name[nfiles]);
  67.                    }
  68.                    free(path); free(name);
  69.                    path = name = NULL;
  70.               }
  71.          }
  72.          if(notemp && warn)
  73.               printf("No files match: %s\n",arg[n]);
  74.     }
  75.  
  76.     if(nfiles)
  77.     {    nfiles = addbunch(nfiles,path,name,move,update,fresh);
  78.          nowork = nowork && !nfiles;
  79.          while(nfiles)
  80.          {    free(path[--nfiles]);
  81.               free(name[nfiles]);
  82.          }
  83.          free(path); free(name);
  84.     }
  85.  
  86.     if(nowork && warn)
  87.          printf("No files were added.\n");
  88. }
  89.  
  90. int addbunch(nfiles,path,name,move,update,fresh) /* add a bunch of files */
  91. int nfiles;                            /* number of files to add */
  92. char **path;                           /* pointers to pathnames */
  93. char **name;                           /* pointers to filenames */
  94. int move;                              /* true if moving file */
  95. int update;                            /* true if updating */
  96. int fresh;                             /* true if freshening */
  97. {
  98.     char buf[100];                 /* pathname buffer */
  99.     int m, n;                          /* indices */
  100.     char *d;                           /* swap pointer */
  101.     struct heads hdr;                  /* file header data storage */
  102.  
  103.     for(n=0; n<nfiles-1; n++)          /* sort the list of names */
  104.     {    for(m=n+1; m<nfiles; m++)
  105.          {    if(strcmp(name[n],name[m])>0)
  106.               {    d = path[n];
  107.                    path[n] = path[m];
  108.                    path[m] = d;
  109.                    d = name[n];
  110.                    name[n] = name[m];
  111.                    name[m] = d;
  112.               }
  113.          }
  114.     }
  115.  
  116.     for(n=0; n<nfiles-1; )             /* consolidate the list of names */
  117.     {    if(!strcmp(path[n],path[n+1]) /* if duplicate names */
  118.          || !strcmp(path[n],arcname)   /* or this archive */
  119.          || !strcmp(path[n],newname)   /* or the new version */
  120.          || !strcmp(path[n],bakname))  /* or its backup */
  121.          {    free(path[n]);           /* then forget the file */
  122.               free(name[n]);
  123.               for(m=n; m<nfiles-1; m++)
  124.               {    path[m] = path[m+1];
  125.                    name[m] = name[m+1];
  126.               }
  127.               nfiles--;
  128.          }
  129.          else n++;                     /* else test the next one */
  130.     }
  131.  
  132.     if(!strcmp(path[n],arcname)        /* special check for last file */
  133.     || !strcmp(path[n],newname)        /* courtesy of Rick Moore */
  134.     || !strcmp(path[n],bakname))
  135.     {    free(path[n]);
  136.          free(name[n]);
  137.          nfiles--;
  138.     }
  139.  
  140.     if(!nfiles)                        /* make sure we got some */
  141.          return 0;
  142.  
  143.     for(n=0; n<nfiles-1; n++)          /* watch out for duplicate names */
  144.          if(!strcmp(name[n],name[n+1]))
  145.               abort("Duplicate filenames:\n  %s\n  %s",path[n],path[n+1]);
  146.  
  147.     openarc(1);                        /* open archive for changes */
  148.  
  149.     for(n=0; n<nfiles; n++)            /* add each file in the list */
  150.          addfile(path[n],name[n],update,fresh);
  151.  
  152.     /* now we must copy over all files that follow our additions */
  153.  
  154.     while(readhdr(&hdr,arc))           /* while more entries to copy */
  155.     {    writehdr(&hdr,new);
  156.          filecopy(arc,new,hdr.size);
  157.     }
  158.     hdrver = 0;                        /* archive EOF type */
  159.     writehdr(&hdr,new);                /* write out our end marker */
  160.     closearc(1);                       /* close archive after changes */
  161.  
  162.     if(move)                           /* if this was a move */
  163.     {    for(n=0; n<nfiles; n++)       /* then delete each file added */
  164.          {    if(unlink(path[n]) && warn)
  165.               {    printf("Cannot unsave %s\n",path[n]);
  166.                    nerrs++;
  167.               }
  168.          }
  169.     }
  170.  
  171.     return nfiles;                     /* say how many were added */
  172. }
  173.  
  174. static addfile(path,name,update,fresh) /* add named file to archive */
  175. char *path;                            /* path name of file to add */
  176. char *name;                            /* name of file to add */
  177. int update;                            /* true if updating */
  178. int fresh;                             /* true if freshening */
  179. {
  180.     struct heads nhdr;                 /* data regarding the new file */
  181.     struct heads ohdr;                 /* data regarding an old file */
  182.     FILE *f, *fopen();                 /* file to add, opener */
  183.     long starts, ftell();              /* file locations */
  184.     int c;                             /* one char of file */
  185.     int upd = 0;                       /* true if replacing an entry */
  186.  
  187.     if(!(f=fopen(path,"rb")))
  188.     {    if(warn)
  189.          {    printf("Cannot read file: %s\n",path);
  190.               nerrs++;
  191.          }
  192.          return;
  193.     }
  194.  
  195.     strcpy(nhdr.name,name);            /* save name */
  196.     nhdr.size = 0;                     /* clear out size storage */
  197.     nhdr.crc = 0;                      /* clear out CRC check storage */
  198.     getstamp(f,&nhdr.date,&nhdr.time);
  199.  
  200.     /* position archive to spot for new file */
  201.  
  202.     if(arc)                            /* if adding to existing archive */
  203.     {    starts = ftell(arc);          /* where are we? */
  204.          while(readhdr(&ohdr,arc))     /* while more files to check */
  205.          {    if(!strcmp(ohdr.name,nhdr.name))
  206.               {    upd = 1;            /* replace existing entry */
  207.                    if(update || fresh) /* if updating or freshening */
  208.                    {    if(nhdr.date<ohdr.date
  209.                         || (nhdr.date==ohdr.date && nhdr.time<=ohdr.time))
  210.                         {    fseek(arc,starts,0);
  211.                              fclose(f);
  212.                              return;   /* skip if not newer */
  213.                         }
  214.                    }
  215.               }
  216.  
  217.               if(strcmp(ohdr.name,nhdr.name)>=0)
  218.                    break;              /* found our spot */
  219.  
  220.               writehdr(&ohdr,new);     /* entry preceeds update; keep it */
  221.               filecopy(arc,new,ohdr.size);
  222.               starts = ftell(arc);     /* now where are we? */
  223.          }
  224.  
  225.          if(upd)                       /* if an update */
  226.          {    if(note)
  227.                    printf("Updating file: %-12s  ",name);
  228.               fseek(arc,ohdr.size,1);
  229.          }
  230.          else if(fresh)                /* else if freshening */
  231.          {    fseek(arc,starts,0);     /* then do not add files */
  232.               fclose(f);
  233.               return;
  234.          }
  235.          else                          /* else adding a new file */
  236.          {    if(note)
  237.                    printf("Adding file:   %-12s  ",name);
  238.               fseek(arc,starts,0);     /* reset for next time */
  239.          }
  240.     }
  241.  
  242.     else                               /* no existing archive */
  243.     {    if(fresh)                     /* cannot freshen nothing */
  244.          {    fclose(f);
  245.               return;
  246.          }
  247.          else if(note)                 /* else adding a file */
  248.               printf("Adding file:   %-12s  ",name);
  249.     }
  250.  
  251.     starts = ftell(new);               /* note where header goes */
  252.     hdrver = 8;                  /* anything but end marker */
  253.     writehdr(&nhdr,new);               /* write out header skeleton */
  254.     pack(f,new,&nhdr);                 /* pack file into archive */
  255.     fseek(new,starts,0);               /* move back to header skeleton */
  256.     writehdr(&nhdr,new);               /* write out real header */
  257.     fseek(new,nhdr.size,1);            /* skip over data to next header */
  258.     fclose(f);                         /* all done with the file */
  259. }
  260.