home *** CD-ROM | disk | FTP | other *** search
- /* -*-c,save-*-
- * arcadd.c 1.1
- *
- * Author: Thom Henderson
- * Original System V port: Mike Stump
- * Enhancements, Bug fixes, and cleanup: Chris Seaman
- * Date: Fri Mar 20 09:57:02 1987
- * Last Mod. 3/21/87
- * line 106 fopen changed to fopenb 7-5-87 ja
- *
- */
-
- /*
- * ARC - Archive utility - ARCADD
- *
- * Version 3.39, created on 02/05/86 at 22:21:53
- *
- * (C) COPYRIGHT 1985 by System Enhancement Associates; ALL RIGHTS RESERVED
- *
- * Description:
- * This file contains the routines used to add files to an archive.
- */
-
- #include "arc.h"
- #ifndef CPM68K
- #include <stat.h>
- #endif
-
- INT addarc(argc,argv,move,update,fresh)/* add files to archive */
- INT argc; /* number of arguments */
- char *argv[]; /* pointers to arguments */
- INT move; /* true if moving file */
- INT update; /* true if updating */
- INT fresh; /* true if freshening */
- {
- char *buf; /* pathname buffer */
- char *i, *strrchr(); /* string indexing junk */
- INT n; /* indices */
- #ifndef CPM68K
- struct stat fbuf; /* file information structure */
- #endif
- struct heads hdr; /* file header data storage */
- INT addfile();
-
- openarc(1); /* open archive for changes */
- for (n=0; n<argc; ++n) {
- #ifdef CPM68K
- if (access(argv[n],4) < 0) { /* find out what kind of file it is */
- #else
- if (stat(argv[n],&fbuf) < 0) { /* find out what kind of file it is */
- #endif
- printf("File not found: %s\n",argv[n]);
- ++nerrs;
- } else { /* if file is a regular file, try to add it */
- #ifdef CPM68K
- if (TRUE) {
- #else
- if (((fbuf.st_mode >> 12) == 8) || ((fbuf.st_mode >> 12) == 0)) {
- #endif
- buf = argv[n];
- #ifdef CPM68K
- if (i = strrchr(buf, ':'))
- #else
- if (i = strrchr(buf, '/'))
- #endif
- buf=i+1;
- addfile(argv[n],buf,update,fresh);
- } else {
- printf("Invalid file type: %s\n",argv[n]);
- ++nerrs;
- }
- }
- }
-
- /* quit if no changes to make */
- if (nerrs == argc) {
- if (arc>0)
- fclose(arc);
- else
- fprintf(stderr,"Newly created archive deleted\n");
- if (new>0)
- fclose(new);
- abort("I have no work to do!");
- }
- /* now we must copy over all files that follow our additions */
- while (readhdr(&hdr,arc)) { /* while more entries to copy */
- writehdr(&hdr,new);
- filecopy(arc,new,hdr.size);
- }
-
- hdrver = 0; /* archive EOF type */
- writehdr(&hdr,new); /* write out our end marker */
- closearc(1); /* close archive after changes */
-
- if (move) {
- for (n=0; n<argc; ++n) { /* if this was a move */
- #ifdef CPM68K
- if (TRUE) {
- #else
- stat(argv[n],&fbuf); /* make sure file can be removed */
- if (((fbuf.st_mode >> 12) == 8) || ((fbuf.st_mode >> 12) == 0)) {
- #endif
- if (unlink(argv[n]) && warn) {
- printf("Cannot unsave %s\n",argv[n]);
- ++nerrs;
- }
- }
- }
- }
- }
-
- static INT addfile(path,name,update,fresh) /* add named file to archive */
- char *path; /* path name of file to add */
- char *name; /* name of file to add */
- INT update; /* true if updating */
- INT fresh; /* true if freshening */
- {
- struct heads nhdr; /* data regarding the new file */
- struct heads ohdr; /* data regarding an old file */
- FILE *f; /* file to add, opener */
- long starts, ftell(); /* file locations */
- INT upd; /* true if replacing an entry */
-
- upd = 0;
- if (!(f=fopenb(path,"r"))) { /* quit if we can't open the file */
- if (warn)
- printf("Cannot read file: %s\n",path);
- nerrs++;
- fclose(f);
- return;
- }
- /* fill the header structure with information about the new file */
- /* save filename, using 14 or 12 char template, based on ibmpc option */
- #ifdef CPM68K
- ucstrncpy(nhdr.name,name,FNLEN2-1);
- #else
- strncpy(nhdr.name,name,((ibmpc) ? FNLEN2 : FNLEN1)-1);
- #endif
- nhdr.size = 0; /* clear out size storage */
- nhdr.crc = 0; /* clear out CRC check storage */
- getstamp(f,&nhdr.date,&nhdr.time);
- /* position archive to spot for new file */
- if (arc) { /* if adding to existing archive */
- starts = ftell(arc); /* where are we? */
- while (readhdr(&ohdr,arc)) { /* while more files to check */
- #ifdef CPM68K
- if (cfstrcmp(ohdr.name,nhdr.name) == 0) {
- #else
- if (strcmp(ohdr.name,nhdr.name) == 0) {
- #endif
- upd = 1; /* replace existing entry */
- if (update || fresh) { /* if updating or freshening */
- if (nhdr.date < ohdr.date ||
- (nhdr.date == ohdr.date
- && nhdr.time <= ohdr.time)) {
- fseek(arc,starts,0);
- fclose(f);
- return; /* skip if not newer */
- }
- }
- }
- #ifdef CPM68K
- if (cfstrcmp(ohdr.name,nhdr.name) >= 0)
- #else
- if (strcmp(ohdr.name,nhdr.name) >= 0)
- #endif
- break; /* found our spot */
- writehdr(&ohdr,new); /* entry preceeds update; keep it */
- filecopy(arc,new,ohdr.size);
- starts = ftell(arc); /* now where are we? */
- }
- if (upd) { /* if an update */
- if (note)
- printf("Updating file: %-14s ",name);
- fseek(arc,ohdr.size,1);
- } else {
- if (fresh) { /* else if freshening */
- fseek(arc,starts,0); /* then do not add files */
- fclose(f);
- return;
- } else { /* else adding a new file */
- if (note)
- printf("Adding file: %-14s ",name);
- fseek(arc,starts,0); /* reset for next time */
- }
- }
- } else { /* no existing archive */
- if (fresh) { /* cannot freshen nothing */
- fclose(f);
- return;
- }
- else
- if (note) /* else adding a file */
- printf("Adding file: %-14s ",name);
- }
-
- starts = ftell(new); /* note where header goes */
- hdrver = ARCVER; /* anything but end marker */
- writehdr(&nhdr,new); /* write out header skeleton */
- pack(f,new,&nhdr); /* pack file into archive */
- fseek(new,starts,0); /* move back to header skeleton */
- writehdr(&nhdr,new); /* write out real header */
- fseek(new,nhdr.size,1); /* skip over data to next header */
- fclose(f); /* all done with the file */
- }
-
- #ifdef CPM68K
- static ucstrncpy(dest,source,len)
- register char *dest, *source;
- register int len;
- {
- while (len-- > 0 && *source != '\0') {
- *dest = toupper(*source);
- dest++; source++;
- }
- *dest = '\0';
- }
-
- cfstrcmp(a,b)
- register char *a,*b;
- {
- while (*a != '\0' && *b != '\0' && toupper(*a) == toupper(*b)) {
- a++; b++;
- }
- return(toupper(*a) - toupper(*b));
- }
- #endif
-