home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / cpm68k / arc68k.arc / ARCADD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-11-27  |  7.9 KB  |  228 lines

  1. /* -*-c,save-*-
  2.  *      arcadd.c        1.1
  3.  *
  4.  *      Author: Thom Henderson
  5.  *      Original System V port: Mike Stump
  6.  *      Enhancements, Bug fixes, and cleanup: Chris Seaman
  7.  *      Date: Fri Mar 20 09:57:02 1987
  8.  *      Last Mod.       3/21/87
  9.  *    line 106 fopen changed to fopenb 7-5-87 ja
  10.  *
  11.  */
  12.  
  13. /*
  14.  * ARC - Archive utility - ARCADD
  15.  * 
  16.  * Version 3.39, created on 02/05/86 at 22:21:53
  17.  * 
  18.  * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
  19.  * 
  20.  *     Description:
  21.  *          This file contains the routines used to add files to an archive.
  22.  */
  23.  
  24. #include "arc.h"
  25. #ifndef CPM68K
  26. #include <stat.h>
  27. #endif
  28.  
  29. INT addarc(argc,argv,move,update,fresh)/* add files to archive */
  30. INT argc;                              /* number of arguments */
  31. char *argv[];                          /* pointers to arguments */
  32. INT move;                              /* true if moving file */
  33. INT update;                            /* true if updating */
  34. INT fresh;                             /* true if freshening */
  35. {
  36.     char *buf;                         /* pathname buffer */
  37.     char *i, *strrchr();               /* string indexing junk */
  38.     INT n;                             /* indices */
  39. #ifndef CPM68K
  40.     struct stat fbuf;                  /* file information structure */
  41. #endif
  42.     struct heads hdr;                  /* file header data storage */
  43.     INT addfile();
  44.  
  45.     openarc(1);                        /* open archive for changes */
  46.     for (n=0; n<argc; ++n) {
  47. #ifdef CPM68K
  48.         if (access(argv[n],4) < 0) {         /* find out what kind of file it is */
  49. #else
  50.         if (stat(argv[n],&fbuf) < 0) {         /* find out what kind of file it is */
  51. #endif
  52.             printf("File not found: %s\n",argv[n]);
  53.             ++nerrs;
  54.     } else {        /* if file is a regular file, try to add it */
  55. #ifdef CPM68K
  56.             if (TRUE) {
  57. #else
  58.             if (((fbuf.st_mode >> 12) == 8) || ((fbuf.st_mode >> 12) == 0)) {
  59. #endif
  60.                 buf = argv[n];
  61. #ifdef CPM68K
  62.             if (i = strrchr(buf, ':'))
  63. #else
  64.                 if (i = strrchr(buf, '/')) 
  65. #endif
  66.             buf=i+1;
  67.                 addfile(argv[n],buf,update,fresh);
  68.             } else {
  69.                 printf("Invalid file type: %s\n",argv[n]);
  70.                 ++nerrs;
  71.             }
  72.     }
  73.     }
  74.  
  75.     /* quit if no changes to make */
  76.     if (nerrs == argc) {
  77.     if (arc>0)
  78.             fclose(arc);
  79.     else
  80.         fprintf(stderr,"Newly created archive deleted\n");
  81.     if (new>0) 
  82.             fclose(new);
  83.         abort("I have no work to do!");
  84.     }
  85.     /* now we must copy over all files that follow our additions */
  86.     while (readhdr(&hdr,arc)) {         /* while more entries to copy */
  87.         writehdr(&hdr,new);
  88.         filecopy(arc,new,hdr.size);
  89.     }
  90.  
  91.     hdrver = 0;                        /* archive EOF type */
  92.     writehdr(&hdr,new);                /* write out our end marker */
  93.     closearc(1);                       /* close archive after changes */
  94.  
  95.     if (move) {
  96.         for (n=0; n<argc; ++n) {  /* if this was a move */
  97. #ifdef CPM68K
  98.             if (TRUE) {
  99. #else
  100.             stat(argv[n],&fbuf);           /* make sure file can be removed */
  101.             if (((fbuf.st_mode >> 12) == 8) || ((fbuf.st_mode >> 12) == 0)) {
  102. #endif
  103.                 if (unlink(argv[n]) && warn) {
  104.                     printf("Cannot unsave %s\n",argv[n]);
  105.                     ++nerrs;
  106.                 }
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. static INT addfile(path,name,update,fresh) /* add named file to archive */
  113. char *path;                            /* path name of file to add */
  114. char *name;                            /* name of file to add */
  115. INT update;                            /* true if updating */
  116. INT fresh;                             /* true if freshening */
  117. {
  118.     struct heads nhdr;                 /* data regarding the new file */
  119.     struct heads ohdr;                 /* data regarding an old file */
  120.     FILE *f;                         /* file to add, opener */
  121.     long starts, ftell();              /* file locations */
  122.     INT upd;                           /* true if replacing an entry */
  123.  
  124.     upd = 0;
  125.     if (!(f=fopenb(path,"r"))) {         /* quit if we can't open the file */
  126.         if (warn)
  127.             printf("Cannot read file: %s\n",path);
  128.         nerrs++;
  129.         fclose(f);
  130.         return;
  131.     }
  132.          /* fill the header structure with information about the new file */
  133.          /* save filename, using 14 or 12 char template, based on ibmpc option */
  134. #ifdef CPM68K
  135.     ucstrncpy(nhdr.name,name,FNLEN2-1);
  136. #else
  137.     strncpy(nhdr.name,name,((ibmpc) ? FNLEN2 : FNLEN1)-1);
  138. #endif
  139.     nhdr.size = 0;                     /* clear out size storage */
  140.     nhdr.crc = 0;                      /* clear out CRC check storage */
  141.     getstamp(f,&nhdr.date,&nhdr.time);
  142.             /* position archive to spot for new file */
  143.     if (arc) {                          /* if adding to existing archive */
  144.         starts = ftell(arc);           /* where are we? */
  145.         while (readhdr(&ohdr,arc)) {    /* while more files to check */
  146. #ifdef CPM68K
  147.             if (cfstrcmp(ohdr.name,nhdr.name) == 0) {
  148. #else
  149.             if (strcmp(ohdr.name,nhdr.name) == 0) {
  150. #endif
  151.                 upd = 1;               /* replace existing entry */
  152.                 if (update || fresh) {  /* if updating or freshening */
  153.                     if (nhdr.date < ohdr.date ||
  154.                        (nhdr.date == ohdr.date 
  155.                        && nhdr.time <= ohdr.time)) {
  156.                         fseek(arc,starts,0);
  157.                         fclose(f);
  158.                         return;        /* skip if not newer */
  159.                     }
  160.                 }
  161.             }
  162. #ifdef CPM68K
  163.             if (cfstrcmp(ohdr.name,nhdr.name) >= 0)
  164. #else
  165.             if (strcmp(ohdr.name,nhdr.name) >= 0)
  166. #endif
  167.                 break;                 /* found our spot */
  168.             writehdr(&ohdr,new);       /* entry preceeds update; keep it */
  169.             filecopy(arc,new,ohdr.size);
  170.             starts = ftell(arc);       /* now where are we? */
  171.         }
  172.         if (upd) {                      /* if an update */
  173.             if (note)
  174.                 printf("Updating file: %-14s  ",name);
  175.             fseek(arc,ohdr.size,1);
  176.         } else {
  177.         if (fresh) {               /* else if freshening */
  178.                 fseek(arc,starts,0);       /* then do not add files */
  179.                 fclose(f);
  180.                 return;
  181.             } else {                          /* else adding a new file */
  182.                 if (note)
  183.                     printf("Adding file:   %-14s  ",name);
  184.                 fseek(arc,starts,0);       /* reset for next time */
  185.             }
  186.         }
  187.     } else {                              /* no existing archive */
  188.         if (fresh) {                    /* cannot freshen nothing */
  189.             fclose(f);
  190.             return;
  191.         }
  192.         else
  193.             if (note)                 /* else adding a file */
  194.                 printf("Adding file:   %-14s  ",name);
  195.     }
  196.  
  197.     starts = ftell(new);               /* note where header goes */
  198.     hdrver = ARCVER;                   /* anything but end marker */
  199.     writehdr(&nhdr,new);               /* write out header skeleton */
  200.     pack(f,new,&nhdr);                 /* pack file into archive */
  201.     fseek(new,starts,0);               /* move back to header skeleton */
  202.     writehdr(&nhdr,new);               /* write out real header */
  203.     fseek(new,nhdr.size,1);            /* skip over data to next header */
  204.     fclose(f);                         /* all done with the file */
  205. }
  206.  
  207. #ifdef CPM68K
  208. static ucstrncpy(dest,source,len)
  209. register char *dest, *source;
  210. register int len;
  211. {
  212.     while (len-- > 0 && *source != '\0') {
  213.     *dest = toupper(*source);
  214.     dest++; source++;
  215.     }
  216.     *dest = '\0';
  217.     }
  218.  
  219. cfstrcmp(a,b)
  220. register char *a,*b;
  221. {
  222.     while (*a != '\0' && *b != '\0' && toupper(*a) == toupper(*b)) {
  223.     a++; b++;
  224.     }
  225.     return(toupper(*a) - toupper(*b));
  226.     }
  227. #endif
  228.