home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / compress / zoosrc20.zoo / zooadd2.c < prev    next >
C/C++ Source or Header  |  1989-07-25  |  9KB  |  274 lines

  1. #ifndef LINT
  2. /* @(#) zooadd2.c 2.14 88/01/27 10:40:32 */
  3. static char sccsid[]="@(#) zooadd2.c 2.14 88/01/27 10:40:32";
  4. #endif /* LINT */
  5.  
  6. /*
  7. Copyright (C) 1986, 1987 Rahul Dhesi -- All rights reserved
  8. (C) Copyright 1988 Rahul Dhesi -- All rights reserved
  9. */
  10. #include "options.h"
  11. #include "zoo.h"
  12. #ifndef    OK_STDIO
  13. #include <stdio.h>
  14. #define    OK_STDIO
  15. #endif
  16. #include "various.h"
  17. #include "zooio.h"
  18. #include "zoofns.h"
  19. #include "errors.i"
  20. #include "assert.h"
  21. #include "debug.h"
  22. #include "parse.h"
  23.  
  24. /*
  25. Miscellaneous routines to support zooadd().
  26. */
  27.  
  28. /****************
  29. This function is called with zoo_file positioned to the first
  30. directory entry in an archive.  It skips past all existing files,
  31. counts the number of deleted files, saves the latest data and time
  32. encountered, and adds all filenames encountered to a global list. The
  33. long filename is added if available, else the MSDOS filename is
  34. added.  
  35. */
  36.  
  37. void skip_files (zoo_file, latest_date, latest_time, delcount, 
  38.                   latest_name, latest_pos)
  39. ZOOFILE zoo_file;
  40. unsigned int *latest_date, *latest_time;
  41. int *delcount;
  42. char latest_name[];
  43. long *latest_pos;
  44. {
  45.    long save_offset, next_ptr;
  46.    struct direntry direntry;
  47.    struct direntry *drp = &direntry;
  48.  
  49.    *latest_pos = 0L;
  50.    do {
  51.       /* read a directory entry */
  52.       save_offset = zootell (zoo_file);     /* save pos'n of this dir entry */
  53.       readdir (&direntry, zoo_file, 1);   /* read directory entry */
  54.       if (drp->next == 0L) {                 /* END OF CHAIN */
  55.          zooseek (zoo_file, save_offset, 0);      /* back up */
  56.          break;                                 /* EXIT on end of chain */
  57.       } else
  58.          *latest_pos = save_offset;
  59.       /* remember most recent date and time, for files not marked deleted */
  60.       if (!drp->deleted)
  61.          if (drp->date > *latest_date ||
  62.             (drp->date == *latest_date && drp->time > *latest_time)) {
  63.                *latest_date = drp->date;
  64.                *latest_time = drp->time;
  65.          }
  66.       next_ptr = drp->next;            /* ptr to next dir entry */
  67.       if (drp->deleted)
  68.          ++(*delcount);                      /* count deleted entries */
  69.       /* add name of file and position of direntry into global list */
  70.       /* but only if the entry is not deleted */
  71.       if (!drp->deleted) {
  72. #ifdef FOLD
  73.             /* IS THIS REALLY NEEDED?  IF SO, WHAT ABOUT drp->lfname? */
  74.          strlwr(drp->fname);
  75. #endif
  76.             /* add full pathname to global list */
  77.             strcpy (latest_name, fullpath (drp));
  78.          addfname (latest_name, save_offset, drp->date, drp->time,
  79.                             drp->vflag, drp->version_no);
  80.       }
  81.       zooseek (zoo_file, next_ptr, 0);   /* ..seek to next dir entry */
  82.    } while (next_ptr != 0L);              /* loop terminates on null ptr */
  83. }
  84.  
  85. /*******************/
  86. /* kill_files() deletes all files in the supplied list of pointers to
  87. filenames */
  88.  
  89. int kill_files (flist, pathlength)
  90. char *flist[];                      /* list of ptrs to input fnames */
  91. int pathlength;                     /* length of longest pathname */
  92. {
  93.    int status = 0;
  94.    int fptr;
  95.    prterror ('M', "-----\nErasing added files...\n");
  96.    for (fptr = 0;  flist[fptr] != NULL; fptr++) {
  97. #ifdef CHEKUDIR
  98.                 if (isuadir(flist[fptr]))
  99.                     continue;
  100. #else /* CHEKUDIR */
  101. # ifdef CHEKDIR
  102.                 if (isfdir(flist[fptr]))
  103.                     continue;
  104. # endif /* CHEKDIR */
  105. #endif /* CHEKUDIR */
  106.       prterror ('m', "%-*s -- ", pathlength, flist[fptr]);
  107.       if (unlink (flist[fptr]) == 0) {
  108.          prterror ('M', "erased\n");
  109.       } else {
  110.          prterror ('w', "Could not erase %s.\n", flist[fptr]);
  111.          status = 1;
  112.       }
  113.    }
  114.    return (status);
  115. }
  116.  
  117. #ifndef PORTABLE
  118. /*******************/
  119. void copyfields (drp, thp)
  120. struct direntry *drp;
  121. struct tiny_header *thp;
  122. {
  123.    drp->org_size = thp->org_size;
  124.    drp->file_crc = thp->file_crc;
  125.    drp->size_now = thp->size_now;
  126.    drp->major_ver = thp->major_ver;
  127.    drp->minor_ver = thp->minor_ver;
  128. }
  129. #endif
  130.  
  131. /*******************/
  132. /* processes option switches for zooadd() */
  133. void opts_add (option, zootime, quiet, suppress, move, new, pack,
  134.           update, add_comment, z_fmt, need_dir, inargs, genson)
  135. char *option;
  136. int *zootime, *quiet, *suppress, *move, *new, *pack,
  137.    *update, *add_comment, *z_fmt, *need_dir, *inargs,
  138.     *genson;
  139.  
  140. {
  141.    if (*option == 'T') {
  142.       (*zootime)++;
  143.       option++;
  144.       while (*option) {
  145.          switch (*option) {
  146.             case 'q': (*quiet)++; break;
  147.             default:
  148.                prterror ('f', inv_option, *option);
  149.          }
  150.       option++;
  151.       }
  152.    }
  153.    
  154.    while (*option) {
  155.       switch (*option) {
  156.          case 'a': break;  
  157.          case 'f': (*suppress)++; break;        /* suppress compression */
  158.          case 'M': (*move)++; break;            /* delete files after adding them */
  159.          case 'n': (*new)++; break;             /* add only files not in archive */
  160.          case 'P': (*pack)++; break;            /* pack after adding */
  161.          case 'u': (*update)++;   break;        /* add only files already in archive */
  162.          case 'q': (*quiet)++; break;           /* be quiet */
  163.          case 'c': (*add_comment)++; break;     /* add comment */
  164.          case ':': *need_dir = 0; break;        /* don't store directories */
  165.          case 'I': (*inargs)++; break;            /* get filenames from stdin */
  166. /* #ifdef PORTABLE */ /* avoid Turbo C warning about unused param */
  167.          case 'z': (*z_fmt)++; break;           /* look for Z format files */
  168. /* #endif */
  169.             case '+': 
  170.                         *genson = 1;  break;
  171.             case '-': 
  172.                         *genson = 0;  break;
  173.          default:
  174.             prterror ('f', inv_option, *option);
  175.       }
  176.       option++;
  177.    } /* end while */
  178. }
  179.  
  180.  
  181. /* 
  182. Stores long filename into direntry.lfname iff it is different from MSDOS
  183. filename.  Also stores directory name if need_dir is true.  Moved out of 
  184. zooadd() so zooadd() doesn't get too big for optimization.
  185. */
  186. void storefname (direntry, this_path, need_dir)
  187. struct direntry *direntry;
  188. char *this_path;
  189. int need_dir;
  190. {
  191.    struct path_st path_st;
  192.    parse (&path_st, this_path);
  193.    direntry->lfname[0] = '\0';
  194.    direntry->namlen = 0;
  195. #ifdef SPECMOD
  196.    specfname (path_st.lfname);
  197.    specdir (path_st.dir);
  198. #endif
  199.    if (strcmp(path_st.lfname,direntry->fname) != 0) {
  200.          strcpy (direntry->lfname, path_st.lfname); /* full filename */
  201.          direntry->namlen = strlen(direntry->lfname) + 1;
  202.    }
  203.    if (need_dir) {
  204.       strcpy (direntry->dirname, path_st.dir);   /* directory name */
  205.       direntry->dirlen = strlen(direntry->dirname) + 1;
  206.       if (direntry->dirlen == 1) /* don't store trailing null alone */
  207.          direntry->dirlen = 0;
  208.    } else {
  209.       direntry->dirname[0] = '\0';
  210.       direntry->dirlen = 0;
  211.    }
  212. }
  213.  
  214. /* 
  215. Function getsdtin() gets a pathname from standard input, cleans
  216. it if necessary by removing any following blanks/tabs and other
  217. junk, and returns it in a static area that is overwritten by each
  218. call.
  219. */
  220. char *getstdin()
  221. {
  222.     char *chptr;                                    /* temp pointer */
  223.     static char tempname[PATHSIZE];
  224.     do {
  225.         if (fgets (tempname, PATHSIZE, stdin) == NULL)
  226.             return (NULL);
  227.         /* remove trailing blank, tab, newline */
  228.         for (chptr = tempname; *chptr != '\0';  chptr++) {
  229.             if (
  230.     /* PURIFY means remove trailing blanks/tabs and all subsequent chars */
  231. #ifdef PURIFY
  232.                     *chptr == '\t' || *chptr == ' ' ||
  233. #endif
  234.                     *chptr == '\n'                        /* always remove trailing \n */
  235.                 )
  236.             {
  237.     
  238.                 *chptr = '\0';
  239.                 break;
  240.             }
  241.         }
  242.     } while (*tempname == '\0');                /* get a nonempty line */
  243. #ifdef FOLD
  244.     strlwr (tempname);
  245. #endif
  246.     return (tempname);
  247. }
  248.  
  249. /* 
  250. Function newdir() adds some default information to a directory entry.
  251. This will be a new directory entry added to an archive.
  252. */
  253. void newdir (direntry)
  254. register struct direntry *direntry;
  255. {
  256. #ifdef GETTZ
  257.     long gettz();
  258. #endif
  259.    direntry->zoo_tag = ZOO_TAG;
  260.    direntry->type = 2;                  /* type is now 2 */
  261. #ifdef GETTZ
  262.     direntry->tz = (uchar) (gettz() / (15 * 60)); /* seconds => 15-min units */
  263. #else
  264.    direntry->tz = NO_TZ;                /* timezone unknown */
  265. #endif
  266.    direntry->struc = 0;                 /* unstructured file */
  267.    direntry->system_id = SYSID_NIX;     /* identify **IX filesystem */
  268.     direntry->vflag = VFL_ON|VFL_LAST;     /* latest version */
  269.     direntry->version_no = 1;                    /* begin with version 1 */
  270.     /* 1 for namlen, 1 for dirlen, 2 for system id, 3 for attributes,
  271.         1 for version flag and 2 for version number */
  272.    direntry->var_dir_len = direntry->dirlen + direntry->namlen + 10;
  273. }
  274.