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

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