home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 157.lha / Arc_Src / arcadd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-04-27  |  5.4 KB  |  137 lines

  1. /*  ARC - Archive utility - ARCADD
  2.  
  3. System V Version 1.0 based upon:
  4.     Version 3.39, created on 02/05/86 at 22:21:53
  5.  
  6. (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  7.  
  8.     By:  Thom Henderson
  9.  
  10.     Description:
  11.          This file contains the routines used to add files to an archive.
  12. */
  13. #include "arc.h"
  14.  
  15. INT addarc(num,arg,move,update,fresh)  /* add files to archive */
  16. INT num;                               /* number of arguments */
  17. char *arg[];                           /* pointers to arguments */
  18. INT move;                              /* true if moving file */
  19. INT update;                            /* true if updating */
  20. INT fresh;                             /* true if freshening */
  21. {
  22.     char *buf;                         /* pathname buffer */
  23.     char *i, *rindex();                /* string indexing junk */
  24.     INT n;                             /* indices */
  25.     struct heads hdr;                  /* file header data storage */
  26.     INT addfile();
  27.  
  28.     openarc(1); /* open archive for changes */
  29.  
  30.     for (n=0; n<num; ++n) {
  31.         if (i=rindex(buf=arg[n], '/')) buf=i+1;
  32.         addfile(arg[n],buf,update,fresh);
  33.     }
  34.  
  35.     /* now we must copy over all files that follow our additions */
  36.  
  37.     while (readhdr(&hdr,arc))           /* while more entries to copy */
  38.     {    writehdr(&hdr,new);
  39.          filecopy(arc,new,hdr.size);
  40.     }
  41.     hdrver = 0;                        /* archive EOF type */
  42.     writehdr(&hdr,new);                /* write out our end marker */
  43.     closearc(1);                       /* close archive after changes */
  44.  
  45.     if (move) for (n=0; n<num; ++n)    /* if this was a move */
  46.         if (unlink(arg[n]) && warn) {
  47.             printf("Cannot unsave %s\n",arg[n]);
  48.             ++nerrs;
  49.          }
  50. }
  51.  
  52. static INT addfile(path,name,update,fresh) /* add named file to archive */
  53. char *path;                                /* path name of file to add */
  54. char *name;                                /* name of file to add */
  55. INT update;                                /* true if updating */
  56. INT fresh;                                 /* true if freshening */
  57. {
  58.     struct heads nhdr;                 /* data regarding the new file */
  59.     struct heads ohdr;                 /* data regarding an old file */
  60.     FILE *f, *fopen();                 /* file to add, opener */
  61.     long starts, ftell();              /* file locations */
  62.     INT c;                             /* one char of file */
  63.     INT upd = 0;                       /* true if replacing an entry */
  64.  
  65.     if (!(f=fopen(path,"r"))) {
  66.         if (warn)
  67.          {    printf("Cannot read file: %s\n",path);
  68.               nerrs++;
  69.          }
  70.     }
  71.  
  72.     strcpy(nhdr.name,name);            /* save name */
  73.     nhdr.size = 0;                     /* clear out size storage */
  74.     nhdr.crc = 0;                      /* clear out CRC check storage */
  75.     getstamp(f,&nhdr.date,&nhdr.time);
  76.  
  77.     /* position archive to spot for new file */
  78.  
  79.     if (arc)                           /* if adding to existing archive */
  80.     {    starts = ftell(arc);          /* where are we? */
  81.          while (readhdr(&ohdr,arc))    /* while more files to check */
  82.          {    if (!strcmp(ohdr.name,nhdr.name))
  83.               {    upd = 1;            /* replace existing entry */
  84.                    if (update || fresh) /* if updating or freshening */
  85.                    {    if (nhdr.date<ohdr.date
  86.                         || (nhdr.date==ohdr.date && nhdr.time<=ohdr.time))
  87.                         {    fseek(arc,starts,0);
  88.                              fclose(f);
  89.                              return;   /* skip if not newer */
  90.                         }
  91.                    }
  92.               }
  93.  
  94.               if (strcmp(ohdr.name,nhdr.name)>=0)
  95.                    break;              /* found our spot */
  96.  
  97.               writehdr(&ohdr,new);     /* entry preceeds update; keep it */
  98.               filecopy(arc,new,ohdr.size);
  99.               starts = ftell(arc);     /* now where are we? */
  100.          }
  101.  
  102.          if (upd)                      /* if an update */
  103.          {    if (note)
  104.                  { printf("Updating file: %-12s  ",name); fflush(stdout);}
  105.               fseek(arc,ohdr.size,1);
  106.          }
  107.          else if (fresh)               /* else if freshening */
  108.          {    fseek(arc,starts,0);     /* then do not add files */
  109.               fclose(f);
  110.               return;
  111.          }
  112.          else                          /* else adding a new file */
  113.          {    if (note)
  114.                  { printf("Adding file:   %-12s  ",name); fflush(stdout);}
  115.               fseek(arc,starts,0);     /* reset for next time */
  116.          }
  117.     }
  118.  
  119.     else                               /* no existing archive */
  120.     {    if (fresh)                    /* cannot freshen nothing */
  121.          {    fclose(f);
  122.               return;
  123.          }
  124.          else if (note)                 /* else adding a file */
  125.             { printf("Adding file:   %-12s  ",name); fflush(stdout);}
  126.     }
  127.  
  128.     starts = ftell(new);               /* note where header goes */
  129.     hdrver = ARCVER;                   /* anything but end marker */
  130.     writehdr(&nhdr,new);               /* write out header skeleton */
  131.     pack(f,new,&nhdr);                 /* pack file into archive */
  132.     fseek(new,starts,0);               /* move back to header skeleton */
  133.     writehdr(&nhdr,new);               /* write out real header */
  134.     fseek(new,nhdr.size,1);            /* skip over data to next header */
  135.     fclose(f);                         /* all done with the file */
  136. }
  137.