home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / g / gtak212.zip / 1.10 / tar.c < prev    next >
C/C++ Source or Header  |  1993-02-02  |  41KB  |  1,631 lines

  1. /*****************************************************************************
  2.  * $Id: tar.c,v 1.20 1993/01/27 08:17:02 ak Exp $
  3.  *****************************************************************************
  4.  * $Log: tar.c,v $
  5.  * Revision 1.20  1993/01/27  08:17:02  ak
  6.  * --archive statt --modified.
  7.  *
  8.  * Revision 1.19  1993/01/14  21:50:32  ak
  9.  * EMX 0.8f fixes and enhancements by Kai Uwe Rommel.
  10.  *
  11.  * Revision 1.18  1993/01/10  11:19:40  ak
  12.  * J.R.: Bugfix: f_gnudump='f' sicherte doppelt.
  13.  *
  14.  * Revision 1.17  1992/12/13  09:57:13  ak
  15.  * *** empty log message ***
  16.  *
  17.  * Revision 1.16  1992/12/13  09:49:58  ak
  18.  * *** empty log message ***
  19.  *
  20.  * Revision 1.15  1992/12/13  09:39:33  ak
  21.  * K.U.R: f_archive/f_reset_archive added to support the "archived" bit.
  22.  *
  23.  * Revision 1.14  1992/10/31  17:42:01  ak
  24.  * *** empty log message ***
  25.  *
  26.  * Revision 1.13  1992/10/31  08:16:12  ak
  27.  * Added long option --all-files as alias for --same-perm. Rearranged the
  28.  * corresponding usage text.
  29.  *
  30.  * Revision 1.12  1992/10/31  06:55:04  ak
  31.  * Modified -G (again) to specify the generation of increment.
  32.  *
  33.  * Revision 1.11  1992/10/29  09:59:58  ak
  34.  * -G for incremental backups like -g, but w/o writing the dumpfile.
  35.  *
  36.  * Revision 1.10  1992/10/25  10:20:41  ak
  37.  * Don't archive/extract ctime+atime if a single -p or --same-perm is
  38.  * specified. It make trouble on incremental backups and file transfers.
  39.  * Added an option --all-timestamps or twice -p to archive/extract
  40.  * ctime+atime.
  41.  *
  42.  * Revision 1.9  1992/09/29  09:56:13  ak
  43.  * K.U.R., once again :-)
  44.  * - removed a dup() in buffer.c
  45.  * - EMX opendir with hidden/system test in gnu.c (...dotdot)
  46.  *
  47.  * Revision 1.8  1992/09/26  08:31:59  ak
  48.  * *** empty log message ***
  49.  *
  50.  * Revision 1.7  1992/09/20  07:46:56  ak
  51.  * Fixes from Kai Uwe Rommel
  52.  *   --checkpoints instead of --semi-verbose (1.11)
  53.  *   -g filenames
  54.  *
  55.  * Revision 1.6  1992/09/16  20:59:57  ak
  56.  * EMX 0.8e
  57.  *
  58.  * Revision 1.5  1992/09/12  17:06:03  ak
  59.  * Added usage of -E --semi-verbose
  60.  *
  61.  * Revision 1.4  1992/09/12  16:07:10  ak
  62.  * Added entry for --fat to usage
  63.  *
  64.  * Revision 1.3  1992/09/12  15:57:18  ak
  65.  * - Usenet patches for GNU TAR 1.10
  66.  * - Bugfixes and patches of Kai Uwe Rommel:
  67.  *         filename conversion for FAT
  68.  *         EMX 0.8e
  69.  *         -0..1 alias for a: b:
  70.  *         -2..7 alias for +++TAPE$x
  71.  *
  72.  * Revision 1.2  1992/09/02  20:08:47  ak
  73.  * Version AK200
  74.  * - Tape access
  75.  * - Quick file access
  76.  * - OS/2 extended attributes
  77.  * - Some OS/2 fixes
  78.  * - Some fixes of Kai Uwe Rommel
  79.  *
  80.  * Revision 1.1.1.1  1992/09/02  19:22:47  ak
  81.  * Original GNU Tar 1.10 with some filenames changed for FAT compatibility.
  82.  *
  83.  * Revision 1.1  1992/09/02  19:22:45  ak
  84.  * Initial revision
  85.  *
  86.  *****************************************************************************/
  87.  
  88. static char *rcsid = "$Id: tar.c,v 1.20 1993/01/27 08:17:02 ak Exp $";
  89.  
  90. /*
  91.  * Modified by Andreas Kaiser July 92.
  92.  * See CHANGES.AK for info.
  93.  */
  94.  
  95. /* Tar -- a tape archiver.
  96.  
  97.     Copyright (C) 1988 Free Software Foundation
  98.  
  99. GNU tar is distributed in the hope that it will be useful, but WITHOUT ANY
  100. WARRANTY.  No author or distributor accepts responsibility to anyone
  101. for the consequences of using it or for whether it serves any
  102. particular purpose or works at all, unless he says so in writing.
  103. Refer to the GNU tar General Public License for full details.
  104.  
  105. Everyone is granted permission to copy, modify and redistribute GNU tar,
  106. but only under the conditions described in the GNU tar General Public
  107. License.  A copy of this license is supposed to have been given to you
  108. along with GNU tar so you can know your rights and responsibilities.  It
  109. should be in a file named COPYING.  Among other things, the copyright
  110. notice and this notice must be preserved on all copies.
  111.  
  112. In other words, go ahead and share GNU tar, but don't try to stop
  113. anyone else from sharing it farther.  Help stamp out software hoarding!
  114. */
  115.  
  116. /*
  117.  * A tar (tape archiver) program.
  118.  *
  119.  * Written by John Gilmore, ihnp4!hoptoad!gnu, starting 25 Aug 85.
  120.  *
  121.  * @(#)tar.c 1.34 11/6/87 - gnu
  122.  */
  123.  
  124. #include <stdio.h>
  125. #include <ctype.h>
  126. #include <sys/types.h>        /* Needed for typedefs in tar.h */
  127. #include <sys/stat.h>        /* JF */
  128. #include "getopt.h"
  129. #include "regex.h"
  130.  
  131. #ifdef USG
  132. #define rindex strrchr
  133. #endif
  134.  
  135. #ifdef BSD42
  136.  #include <sys/dir.h>
  137. #else
  138.  #ifdef __MSDOS__
  139.   #include "msd_dir.h"
  140.  #else
  141.   #ifdef USG
  142.    #ifdef NDIR
  143.     #include <ndir.h>
  144.    #else
  145.     #include <dirent.h>
  146.    #endif
  147.    #ifndef DIRECT
  148.     #define direct dirent
  149.    #endif
  150.    #define DP_NAMELEN(x) strlen((x)->d_name)
  151.   #else
  152.    /*
  153.     * FIXME: On other systems there is no standard place for the header file
  154.     * for the portable directory access routines.  Change the #include line
  155.     * below to bring it in from wherever it is.
  156.     */
  157.    #ifdef OS2
  158.     #include "dirent.h"
  159.     #define direct dirent
  160.     #define DP_NAMELEN(x) strlen((x)->d_name)
  161.    #else
  162.     #include "ndir.h"
  163.    #endif
  164.   #endif
  165.  #endif
  166. #endif
  167.  
  168. #ifndef DP_NAMELEN
  169. #define DP_NAMELEN(x)    (x)->d_namlen
  170. #endif
  171.  
  172. extern char     *malloc();
  173. extern char     *getenv();
  174. extern char    *strncpy();
  175. extern char    *index();
  176. extern char    *strcpy();    /* JF */
  177. extern char    *strcat();    /* JF */
  178.  
  179. extern char    *optarg;    /* Pointer to argument */
  180. extern int    optind;        /* Global argv index from getopt */
  181.  
  182. extern char     *ck_malloc();
  183. extern char     *ck_realloc();
  184. /*
  185.  * The following causes "tar.h" to produce definitions of all the
  186.  * global variables, rather than just "extern" declarations of them.
  187.  */
  188. #define TAR_EXTERN /**/
  189. #include "tar.h"
  190. #include "port.h"
  191. #include "rmt.h"
  192.  
  193. #if 0
  194. /*
  195.  * We should use a conversion routine that does reasonable error
  196.  * checking -- atoi doesn't.  For now, punt.  FIXME.
  197.  */
  198. #define intconv    atoi
  199. #else
  200. /*
  201.  * AK920724 -- I did add the error checking. And a bit more.
  202.  * Allowed to append 'B' for multiples of the block size, 'K'
  203.  * for KB (the default), 'R' for records (512 bytes) and 'M'
  204.  * for MB.
  205.  *
  206.  * Second argument is the default factor. The result is divided
  207.  * by this number.
  208.  */
  209. long intconv();
  210. #endif
  211.  
  212. extern int    getoldopt();
  213. extern void    read_and();
  214. extern void    list_archive();
  215. extern void    extract_archive();
  216. extern void    diff_archive();
  217. extern void    create_archive();
  218. extern void    update_archive();
  219. extern void    junk_archive();
  220.  
  221. /* JF */
  222. extern time_t    get_date();
  223.  
  224. time_t new_time;
  225.  
  226. static FILE    *namef;        /* File to read names from */
  227. static char    **n_argv;    /* Argv used by name routines */
  228. static int    n_argc;        /* Argc used by name routines */
  229. static char    **n_ind;    /* Store an array of names */
  230. static int    n_indalloc;    /* How big is the array? */
  231. static int    n_indused;    /* How many entries does it have? */
  232. static int    n_indscan;    /* How many of the entries have we scanned? */
  233.  
  234.  
  235. extern FILE *msg_file;
  236.  
  237. FILE    *map_file;
  238. int    map_read;
  239.  
  240. void    describe();
  241. void    options();
  242.  
  243. #ifndef S_IFLNK
  244. #define lstat stat
  245. #endif
  246.  
  247. #ifndef DEFBLOCKING
  248. #define DEFBLOCKING 20
  249. #endif
  250.  
  251. #ifndef DEF_AR_FILE
  252. #if defined(OS2) && defined(TAPE_IO)
  253. #define DEF_AR_FILE (getenv("TAPE") ? getenv("TAPE") : "tape.out")
  254. #else
  255. #define DEF_AR_FILE "tar.out"
  256. #endif
  257. #endif
  258.  
  259. /* For long options that unconditionally set a single flag, we have getopt
  260.    do it.  For the others, we share the code for the equivalent short
  261.    named option, the name of which is stored in the otherwise-unused `val'
  262.    field of the `struct option'; for long options that have no equivalent
  263.    short option, we use nongraphic characters as pseudo short option
  264.    characters, starting (for no particular reason) with character 10. */
  265.  
  266. struct option long_options[] =
  267. {
  268.     {"create",        0,    0,            'c'},
  269.     {"append",        0,    0,            'r'},
  270.     {"extract",        0,    0,            'x'},
  271.     {"get",            0,    0,            'x'},
  272.     {"list",        0,    0,            't'},
  273.     {"update",        0,    0,            'u'},
  274.     {"catenate",        0,    0,            'A'},
  275.     {"concatenate",        0,    0,            'A'},
  276.     {"compare",        0,    0,            'd'},
  277.     {"diff",        0,    0,            'd'},
  278.     {"delete",        0,    0,            14},
  279.     {"help",        0,    0,            12},
  280.  
  281.     {"directory",        1,    0,            'C'},
  282.     {"record-number",    0,    &f_sayblock,        1},
  283.     {"files-from",        1,    0,            'T'},
  284.     {"label",        1,    0,            'V'},
  285.     {"exclude-from",    1,    0,            'X'},
  286.     {"exclude",        1,    0,            15},
  287.     {"show-omitted-dirs",    0,    0,            16},
  288.     {"file",        1,    0,            'f'},
  289.     {"block-size",        1,    0,            'b'},
  290.     {"version",        0,    0,            11},
  291.     {"verbose",         0,    0,            'v'},
  292.     {"checkpoints",        0,    0,            'E'},
  293.     {"totals",        0,    &f_totals,        1},
  294.       
  295.     {"read-full-blocks",    0,    &f_reblock,        1},
  296.     {"starting-file",    1,    0,            'K'},
  297.     {"to-stdout",        0,    &f_exstdout,        1},
  298.     {"ignore-zeros",    0,    &f_ignorez,        1},
  299.     {"keep-old-files",    0,    0,            'k'},
  300.     {"uncompress",        0,    &f_compress,        1},
  301.     {"same-permissions",    0,    &f_use_protection,    1},
  302.     {"preserve-permissions",0,    &f_use_protection,    1},
  303.     {"modification-time",    0,    &f_modified,        1},
  304.     {"preserve",        0,    0,            10},
  305.     {"same-order",        0,    &f_sorted_names,    1},
  306.     {"same-owner",        0,    &f_do_chown,        1},
  307.     {"preserve-order",    0,    &f_sorted_names,    1},
  308.  
  309.     {"newer",        1,    0,            'N'},
  310.     {"after-date",        1,    0,            'N'},
  311.     {"newer-mtime",        1,    0,            13},
  312.     {"generation",        1,    0,            'G'},
  313.     {"incremental",        1,    0,            'g'},
  314.     {"multi-volume",    0,    &f_multivol,        1},
  315.     {"info-script",        1,    0,            'F'},
  316.     {"absolute-paths",    0,    &f_absolute_paths,    1},
  317.     {"interactive",        0,    &f_confirm,        1},
  318.     {"confirmation",    0,    &f_confirm,        1},
  319.  
  320.     {"verify",        0,    &f_verify,        1},
  321.     {"dereference",        0,    &f_follow_links,    1},
  322.     {"one-file-system",    0,    &f_local_filesys,     1},
  323.     {"old-archive",        0,    0,            'o'},
  324.     {"portability",        0,    0,            'o'},
  325.     {"compress",        0,    &f_compress,        1},
  326.     {"compress-block",    0,    &f_compress,        2},
  327.     {"sparse",        0,    &f_sparse_files,    1},
  328.     {"tape-length",        1,    0,            'L'},
  329.  
  330. #ifdef HOST
  331.     {"host",        0,    &f_host,        1},
  332. #endif
  333.     {"tape-directory",    1,    0,            'D'},
  334.     {"no-recursion",    0,    &f_no_recursion,    'Y'},
  335. #ifdef MSDOS
  336.     {"fat",         0,    &f_fat,         1},
  337.     {"all-files",        0,    &f_use_protection,    1},
  338.     {"all-timestamps",    0,    &f_all_timestamps,    1},
  339.      {"archive",         0,    &f_archive,         1},
  340.      {"reset-archive",    0,    &f_reset_archive,    1},
  341. #endif
  342.  
  343.     {0, 0, 0, 0}
  344. };
  345.  
  346. /*
  347.  * Main routine for tar.
  348.  */
  349. main(argc, argv)
  350.     int    argc;
  351.     char    **argv;
  352. {
  353.     extern char version_string[];
  354.  
  355. #if defined(__EMX__)
  356.     _response(&argc, &argv);
  357.     _wildcard(&argc, &argv);
  358.     tzset();
  359. #endif
  360.  
  361.     tar = argv[0];        /* JF: was "tar" Set program name */
  362. #ifdef MSDOS
  363.     {    char *p; int i;
  364.  
  365.         for (i = 0; i < argc; ++i) {
  366.             p = argv[i];
  367.             if (*p != '\'' && *p != '"')
  368.                 for (; *p; ++p)
  369.                     if (*p == '\\')
  370.                         *p = '/';
  371.         }
  372.         for (p = tar + strlen(tar); --p != tar; )
  373.             if (*p == ':' || *p == '\\') {
  374.                 tar = p + 1;
  375.                 break;
  376.             }
  377.         for (p = tar; *p && *p != '.'; ++p)
  378.             ;
  379.         *p = 0;
  380.         strlwr(tar);
  381.  
  382.     }
  383. #endif
  384.     errors = 0;
  385.  
  386.     options(argc, argv);
  387.  
  388.     if (f_map_file) {
  389.         switch(cmd_mode) {
  390.         default:
  391.             map_file = fopen(f_map_file, "a");
  392.             break;
  393.         case CMD_EXTRACT:
  394.         case CMD_DIFF:
  395.             map_read = 1;
  396.             map_file = fopen(f_map_file, "r");
  397.         }
  398.         if (map_file == NULL) {
  399.             fprintf(stderr, "Cannot open map file %s\n", f_map_file);
  400.             exit(EX_ARGSBAD);
  401.         }
  402.     }
  403.  
  404.     if(!n_argv)
  405.         name_init(argc, argv);
  406.  
  407.     switch(cmd_mode) {
  408.     case CMD_CAT:
  409.     case CMD_UPDATE:
  410.     case CMD_APPEND:
  411.         update_archive();
  412.         break;
  413.     case CMD_DELETE:
  414.         junk_archive();
  415.         break;
  416.     case CMD_CREATE:
  417.         create_archive();
  418.         if (f_totals)
  419.             fprintf (stderr, "Total bytes written: %d\n", tot_written);
  420.         break;
  421.     case CMD_EXTRACT:
  422. #ifdef REGEX
  423.         if (f_volhdr) {
  424.             char *err;
  425.             label_pattern = (struct re_pattern_buffer *)
  426.               ck_malloc (sizeof *label_pattern);
  427.              err = re_compile_pattern (f_volhdr, strlen (f_volhdr),
  428.                           label_pattern);
  429.             if (err) {
  430.                 fprintf (stderr,"Bad regular expression: %s\n",
  431.                      err);
  432.                 errors++;
  433.                 break;
  434.             }
  435.            
  436.         }          
  437. #endif
  438.         extr_init();
  439.         if (f_map_file)
  440.             seek_and(extract_archive);
  441.         else
  442.             read_and(extract_archive);
  443.         break;
  444.     case CMD_LIST:
  445. #ifdef REGEX
  446.         if (f_volhdr) {
  447.             char *err;
  448.             label_pattern = (struct re_pattern_buffer *)
  449.               ck_malloc (sizeof *label_pattern);
  450.              err = re_compile_pattern (f_volhdr, strlen (f_volhdr),
  451.                           label_pattern);
  452.             if (err) {
  453.                 fprintf (stderr,"Bad regular expression: %s\n",
  454.                      err);
  455.                 errors++;
  456.                 break;
  457.             }
  458.         }
  459. #endif
  460.         read_and(list_archive);
  461. #if 0
  462.         if (!errors)
  463.             errors = different;
  464. #endif
  465.         break;
  466.     case CMD_DIFF:
  467.         diff_init();
  468.         if (f_map_file)
  469.             seek_and(diff_archive);
  470.         else
  471.             read_and(diff_archive);
  472.         break;
  473.     case CMD_VERSION:
  474.         fprintf(stderr,"%s\n",version_string);
  475.         break;
  476.     case CMD_NONE:
  477.         fprintf(stderr,"\nThis is %s, a tape archiving program.\n\n",
  478.             version_string);
  479.         fprintf(stderr,"You must specify exactly one of the r, c, t, x, or d options.\n");
  480.          fprintf(stderr,"For more information, type ``%s --help''.\n",tar);
  481.         exit(EX_ARGSBAD);
  482.     }
  483.     if (f_map_file)
  484.         fclose(map_file);
  485.     exit(errors);
  486.     /* NOTREACHED */
  487. }
  488.  
  489.  
  490. /*
  491.  * Parse the options for tar.
  492.  */
  493. void
  494. options(argc, argv)
  495.     int    argc;
  496.     char    **argv;
  497. {
  498.     register int    c;        /* Option letter */
  499.     int        ind = -1;
  500.  
  501.     /* Set default option values */
  502.     blocking = DEFBLOCKING;        /* From Makefile */
  503.     ar_file = getenv("TAPE");    /* From environment, or */
  504.     if (ar_file == 0)
  505.         ar_file = DEF_AR_FILE;    /* From Makefile */
  506.  
  507.     /* Parse options */
  508.     while ((c = getoldopt(argc, argv,
  509.                   "-01234567Ab:BcC:dD:Ef:F:g:G:hHikK:lL:mMN:oOpPrRsStT:uvV:wWxX:YzZ",
  510.                   long_options, &ind)) != EOF) {
  511.         switch (c) {
  512.         case 0:        /* long options that set a single flag */
  513.               break;
  514.         case 1:
  515.             /* File name or non-parsed option */
  516.             name_add(optarg);
  517.             break;
  518.         case 'C':
  519.             name_add("-C");
  520.             name_add(optarg);
  521.             break;
  522.         case 10:    /* preserve */
  523.             f_use_protection = f_sorted_names = 1;
  524.             break;
  525.         case 11:
  526.             if(cmd_mode!=CMD_NONE)
  527.                 goto badopt;
  528.             cmd_mode=CMD_VERSION;
  529.             break;
  530.         case 12:    /* help */
  531.             fprintf(stderr,"This is GNU tar, the tape archiving program.\n");
  532.             describe();
  533.             exit(1);
  534.         case 13:
  535.             f_new_files++;
  536.             goto get_newer;
  537.  
  538.         case 14:            /* Delete in the archive */
  539.             if(cmd_mode!=CMD_NONE)
  540.                 goto badopt;
  541.             cmd_mode=CMD_DELETE;
  542.             break;
  543.  
  544.         case 15:
  545.             f_exclude++;
  546.             add_exclude(optarg);
  547.             break;
  548.  
  549.         case 16:    /* show-omitted-dirs */
  550.                     f_show_omitted_dirs++;
  551.                         break;
  552.                         
  553.         case '0':
  554.         case '1':
  555. #ifdef MSDOS
  556.             {
  557.                           static char buf[8];
  558.                           sprintf(buf,"%c:",c-'0'+'A');
  559.                           ar_file=buf;
  560.                         }
  561.                         break;
  562. #endif
  563.  
  564.         case '2':
  565.         case '3':
  566.         case '4':
  567.         case '5':
  568.         case '6':
  569.         case '7':
  570.             {
  571.                 /* JF this'll have to be modified for other
  572.                    systems, of course! */
  573.                 int d,add;
  574.                 static char buf[50];
  575.  
  576. #ifdef MSDOS
  577.                 sprintf(buf, "+++TAPE$%c", c);
  578. #else
  579.                 d=getoldopt(argc,argv,"lmh");
  580. #ifdef MAYBEDEF
  581.                 sprintf(buf,"/dev/rmt/%d%c",c,d);
  582. #else
  583. #ifndef LOW_NUM
  584. #define LOW_NUM 0
  585. #define MID_NUM 8
  586. #define HGH_NUM 16
  587. #endif
  588.                 if(d=='l') add=LOW_NUM;
  589.                 else if(d=='m') add=MID_NUM;
  590.                 else if(d=='h') add=HGH_NUM;
  591.                 else goto badopt;
  592.  
  593.                 sprintf(buf,"/dev/rmt%d",add+c-'0');
  594. #endif
  595. #endif
  596.                 ar_file=buf;
  597.             }
  598.             break;
  599.  
  600.         case 'A':            /* Arguments are tar files,
  601.                            just cat them onto the end
  602.                            of the archive.  */
  603.             if(cmd_mode!=CMD_NONE)
  604.                 goto badopt;
  605.             cmd_mode=CMD_CAT;
  606.             break;
  607.  
  608.         case 'b':            /* Set blocking factor */
  609.             blocking = intconv(optarg, RECORDSIZE);
  610.             break;
  611.  
  612.         case 'B':            /* Try to reblock input */
  613.             f_reblock++;        /* For reading 4.2BSD pipes */
  614.             break;
  615.  
  616.         case 'c':            /* Create an archive */
  617.             if(cmd_mode!=CMD_NONE)
  618.                 goto badopt;
  619.             cmd_mode=CMD_CREATE;
  620.             break;
  621.  
  622. /*        case 'C':
  623.             if(chdir(optarg)<0)
  624.                 msg_perror("Can't change directory to %d",optarg);
  625.             break; */
  626.  
  627.         case 'd':            /* Find difference tape/disk */
  628.             if(cmd_mode!=CMD_NONE)
  629.                 goto badopt;
  630.             cmd_mode=CMD_DIFF;
  631.             break;
  632.  
  633. #ifdef TAPE_IO
  634.         case 'D':
  635.             f_map_file = optarg;    /* Maintain/use map file */
  636.             break;
  637. #endif
  638.  
  639.         case 'f':            /* Use ar_file for the archive */
  640.             ar_file = optarg;
  641.             break;
  642.  
  643.         case 'F':
  644.             /* Since -F is only useful with -M , make it implied */
  645.             f_run_script_at_end++;  /* run this script at the end */
  646.             info_script = optarg;    /* of each tape */
  647.             f_multivol++;
  648.             break;
  649.  
  650.         case 'g':            /* We are making a GNU dump; save
  651.                            directories at the beginning of
  652.                            the archive, and include in each
  653.                            directory its contents */
  654.             if(f_oldarch)
  655.                 goto badopt;
  656.             f_gnudump = 'i';
  657. #ifdef MSDOS
  658.             f_use_protection = 1;
  659. #endif
  660.             gnu_dumpfile = optarg;
  661.             break;
  662.  
  663.         case 'G':            /* We are making a GNU dump; save
  664.                            directories at the beginning of
  665.                            the archive, and include in each
  666.                            directory its contents */
  667.             if(f_oldarch)
  668.                 goto badopt;
  669.             c = strlen(optarg);
  670.             if (strncmp(optarg, "first", c)
  671.              && strncmp(optarg, "second", c)
  672.              && strncmp(optarg, "incremental", c)) {
  673.                 msg("don't understand generation option %s", optarg);
  674.                 exit(EX_ARGSBAD);
  675.             }
  676.             f_gnudump = optarg[0];
  677.             break;
  678.  
  679.         case 'h':
  680.             f_follow_links++;    /* follow symbolic links */
  681.             break;
  682.  
  683.         case 'i':
  684.             f_ignorez++;        /* Ignore zero records (eofs) */
  685.             /*
  686.              * This can't be the default, because Unix tar
  687.              * writes two records of zeros, then pads out the
  688.              * block with garbage.
  689.              */
  690.             break;
  691.  
  692.         case 'k':            /* Don't overwrite files */
  693. #ifdef NO_OPEN3
  694.             msg("can't do -k option on this system");
  695.             exit(EX_ARGSBAD);
  696. #else
  697.             f_keep++;
  698. #endif
  699.             break;
  700.  
  701.         case 'K':
  702.             f_startfile++;
  703.             addname(optarg);
  704.             break;
  705.  
  706.         case 'l':            /* When dumping directories, don't
  707.                            dump files/subdirectories that are
  708.                            on other filesystems. */
  709.             f_local_filesys++;
  710.             break;
  711.  
  712.         case 'L':
  713.             tape_length = intconv (optarg, 1024);
  714.             f_multivol++;
  715.             break;
  716.         case 'm':
  717.             f_modified++;
  718.             break;
  719.  
  720.         case 'M':            /* Make Multivolume archive:
  721.                            When we can't write any more
  722.                            into the archive, re-open it,
  723.                            and continue writing */
  724.             f_multivol++;
  725.             break;
  726.  
  727.         case 'N':            /* Only write files newer than X */
  728.         get_newer:
  729.             f_new_files++;
  730.             new_time=get_date(optarg,(struct timeb *)0);
  731.             break;
  732.  
  733.         case 'o':            /* Generate old archive */
  734.             if(f_gnudump /* || f_dironly */)
  735.                 goto badopt;
  736.             f_oldarch++;
  737.             break;
  738.  
  739.         case 'O':
  740.             f_exstdout++;
  741.             break;
  742.  
  743.         case 'p':
  744.             if (++f_use_protection >= 2)
  745.                 ++f_all_timestamps;
  746.             break;
  747.  
  748.         case 'P':
  749.             f_absolute_paths++;
  750.             break;
  751.  
  752.         case 'r':            /* Append files to the archive */
  753.             if(cmd_mode!=CMD_NONE)
  754.                 goto badopt;
  755.             cmd_mode=CMD_APPEND;
  756.             break;
  757.  
  758.         case 'R':
  759.             f_sayblock++;        /* Print block #s for debug */
  760.             break;            /* of bad tar archives */
  761.  
  762.         case 's':
  763.             f_sorted_names++;    /* Names to extr are sorted */
  764.             break;
  765.  
  766.         case 'S':            /* deal with sparse files */
  767.             f_sparse_files++;
  768.             break;
  769.         case 't':
  770.             if(cmd_mode!=CMD_NONE)
  771.                 goto badopt;
  772.             cmd_mode=CMD_LIST;
  773.             f_verbose++;        /* "t" output == "cv" or "xv" */
  774.             break;
  775.  
  776.         case 'T':
  777.             name_file = optarg;
  778.             f_namefile++;
  779.             break;
  780.  
  781.         case 'u':            /* Append files to the archive that
  782.                            aren't there, or are newer than the
  783.                            copy in the archive */
  784.             if(cmd_mode!=CMD_NONE)
  785.                 goto badopt;
  786.             cmd_mode=CMD_UPDATE;
  787.             break;
  788.  
  789.         case 'E' :
  790.                 f_checkpoints = 1;
  791.         case 'v':
  792.             f_verbose++;
  793.             break;
  794.  
  795.         case 'V':
  796.             f_volhdr=optarg;
  797.             break;
  798.  
  799.         case 'w':
  800.             f_confirm++;
  801.             break;
  802.  
  803.         case 'W':
  804.             f_verify++;
  805.             break;
  806.  
  807.         case 'x':            /* Extract files from the archive */
  808.             if(cmd_mode!=CMD_NONE)
  809.                 goto badopt;
  810.             cmd_mode=CMD_EXTRACT;
  811.             break;
  812.  
  813.         case 'X':
  814.             f_exclude++;
  815.             add_exclude_file(optarg);
  816.             break;
  817.  
  818.         case 'Z':        /* Like the filename extension .Z */
  819.             f_buffered++;
  820.         case 'z':        /* Easy to type */
  821.             f_compress++;
  822.             break;
  823.  
  824.         case '?':
  825.         badopt:
  826.             msg("Unknown option.  Use '%s --help' for a complete list of options.", tar);
  827.             exit(EX_ARGSBAD);
  828.  
  829.         }
  830.     }
  831.  
  832.     blocksize = blocking * RECORDSIZE;
  833.  
  834.     if (f_gnudump && !gnu_dumpfile) {
  835.         msg("you have not specified a dump file (-g)");
  836.         exit(EX_ARGSBAD);
  837.     }
  838. #ifdef HOST
  839.     if (f_host)
  840.         host_init(ar_file);
  841. #endif
  842. #ifdef S_IFCHR
  843.     { struct stat st;
  844.       is_device = stat(ar_file, &st) == 0
  845.            && (st.st_mode & S_IFMT) == S_IFCHR;
  846.     }
  847. #endif
  848. #ifdef TAPE_IO
  849. # ifndef NO_REMOTE
  850.     is_device |= _remdev(ar_file);
  851. # endif
  852. #endif
  853. }
  854.  
  855.  
  856. /*
  857.  * Print as much help as the user's gonna get.
  858.  *
  859.  * We have to sprinkle in the KLUDGE lines because too many compilers
  860.  * cannot handle character strings longer than about 512 bytes.  Yuk!
  861.  * In particular, MSDOS and Xenix MSC and PDP-11 V7 Unix have this
  862.  * problem.
  863.  */
  864. void
  865. describe()
  866. {
  867.     msg("Choose one of the following:");
  868.     fprintf(stderr,
  869. "-A    --catenate,\n"
  870. "    --concatenate        append tar files to an archive\n"
  871. "-c    --create        create a new archive\n"
  872. "-d    --diff,\n"
  873. "    --compare        find differences between archive and file system\n"
  874. "    --delete        delete from the archive (not for use on mag tapes!)\n"
  875. "-r    --append        append files to the end of an archive\n"
  876. "-t    --list            list the contents of an archive\n"
  877. "-u    --update        only append files that are newer than copy in archive\n"
  878. "-x    --extract,\n"
  879. "    --get            extract files from an archive\n"
  880. "\n"
  881.     );
  882.     fprintf(stderr,
  883. "Other options:\n"
  884. "-b    --block-size N        block size of Nx512 bytes (default N=%d)\n"
  885. "-B    --read-full-blocks    reblock as we read (for reading 4.2BSD pipes)\n"
  886. "-C    --directory DIR        change to directory DIR\n"
  887. "-D    --tape-directory F    use/maintain tape directory in file F\n"
  888. "-E    --checkpoints        list directories as they are processed\n"
  889. "-f    --file [HOSTNAME:]F    use archive file/device F (default %s)\n"
  890. "-F    --info-script F        run script at end of each tape (implies -M)\n"
  891. "-g    --incremental F     create/list/extract new GNU-format incremental backup\n"
  892. "-G    --generation O        specifies the incremental backup generation\n"
  893. "            O=f[irst]    first full backup, F erased before use\n"
  894. "            O=s[econd]    second backup, F used but not written\n"
  895. "            O=i[ncremental]    true incremental: F used and written\n"
  896. "                if -G is not given, -g assumes O=i\n"
  897. "-h    --dereference        don't dump symlinks; dump the files they point to\n"
  898. #ifdef HOST
  899. "-H    --host            send/receive archive to/from VM host\n"
  900. #endif
  901. "-i    --ignore-zeros        ignore blocks of zeros in archive (normally mean EOF)\n"
  902. "-k    --keep-old-files    keep existing files; don't overwrite them from archive\n"
  903. "-K    --starting-file FILE    begin at FILE in the archive\n"
  904. "-l    --one-file-system    stay in local file system creating an archive\n"
  905. "-L    --tape-length LENGTH    change tapes after writing LENGTH\n"
  906. "-m    --modification-time    don't extract file modified time\n"
  907. "-M    --multi-volume        create/list/extract multi-volume archive\n"
  908. "-N    --after-date DATE,\n"
  909. "    --newer DATE        only store files newer than DATE\n"
  910. "-o    --old-archive,\n"
  911. "    --portability        write a V7 rather than ANSI format archive\n"
  912. "-O    --to-stdout        extract files to standard output\n"
  913.     );
  914.     fprintf(stderr,
  915. #ifdef MSDOS
  916. "-p    --all-files        archive/extract extended attributes,\n"
  917. "                hidden and system files\n"
  918. "-pp    --all-timestamps    archive/extract access and creation times too\n"
  919. #else
  920. "-p    --same-permissions,\n"
  921. "    --preserve-permissions    extract all protection information\n"
  922. #endif
  923. "-P    --absolute-paths    don't strip leading `/'s from file names\n"
  924. "    --preserve        like -p -s\n"
  925. "-R    --record-number        show record number within archive with each message\n"
  926. "-s    --same-order,\n"
  927. "    --preserve-order    list of names to extract is sorted to match archive\n"
  928. "    --same-order        create extracted files with the same ownership \n"
  929. "-S    --sparse        handle sparse files efficiently\n"
  930. "-T    --files-from F        get names to extract or create from file F\n"
  931. "    --totals        print total bytes written with --create\n"
  932. "-v    --verbose        verbosely list files processed\n"
  933. "-V    --label NAME        create archive with volume name NAME\n"
  934. "    --version        print tar program version number\n"
  935. "-w    --interactive,\n"
  936. "    --confirmation        ask for confirmation for every action\n"
  937. "-W    --verify        attempt to verify the archive after writing it\n"
  938. "    --exclude FILE        exclude file FILE\n"
  939. "-X    --exclude-from FILE    exclude files listed in FILE\n"
  940. "-Y    --no-recursion        archive only files specified in command line\n"
  941. #ifdef MSDOS
  942. "    --fat            convert filenames to FAT 8.3 format\n"
  943. "    --archive        incremental backup using the 'archive' bit\n"
  944. "    --reset-archive        reset 'archive' bit in files archived\n"
  945. #endif
  946. "-z -Z    --compress,\n"
  947. "    --uncompress          filter the archive through compress\n"
  948. "-[0-7][lmh]            specify drive and density\n"
  949.         , DEFBLOCKING, DEF_AR_FILE);
  950. }
  951.  
  952. name_add(name)
  953. char *name;
  954. {
  955.     if(n_indalloc==n_indused) {
  956.         n_indalloc+=10;
  957.         n_ind=(char **)(n_indused ? ck_realloc(n_ind,n_indalloc*sizeof(char *)) : ck_malloc(n_indalloc*sizeof(char *)));
  958.     }
  959.     n_ind[n_indused++]=name;
  960. }
  961.         
  962. /*
  963.  * Set up to gather file names for tar.
  964.  *
  965.  * They can either come from stdin or from argv.
  966.  */
  967. name_init(argc, argv)
  968.     int    argc;
  969.     char    **argv;
  970. {
  971.  
  972.     if (f_namefile) {
  973.         if (optind < argc) {
  974.             msg("too many args with -T option");
  975.             exit(EX_ARGSBAD);
  976.         }
  977.         if (!strcmp(name_file, "-")) {
  978.             namef = stdin;
  979.         } else {
  980.             namef = fopen(name_file, "r");
  981.             if (namef == NULL) {
  982.                 msg_perror("can't open file %s",name_file);
  983.                 exit(EX_BADFILE);
  984.             }
  985.         }
  986.     } else {
  987.         /* Get file names from argv, after options. */
  988.         n_argc = argc;
  989.         n_argv = argv;
  990.     }
  991. }
  992.  
  993. /*
  994.  * Get the next name from argv or the name file.
  995.  *
  996.  * Result is in static storage and can't be relied upon across two calls.
  997.  */
  998.  
  999. /* C is non-zero if we should deal with -C */
  1000. char *
  1001. name_next(c)
  1002. {
  1003.     static char    *buffer;    /* Holding pattern */
  1004.     static buffer_siz;
  1005.     register char    *p;
  1006.     register char    *q = 0;
  1007.     register char    *q2 = 0;
  1008.     extern char *un_quote_string();
  1009.  
  1010.     if(buffer_siz==0) {
  1011.         buffer=ck_malloc(NAMSIZ+2);
  1012.         buffer_siz=NAMSIZ;
  1013.     }
  1014.  tryagain:
  1015.     if (namef == NULL) {
  1016.         if(n_indscan<n_indused)
  1017.             p=n_ind[n_indscan++];
  1018.         else if (optind < n_argc)        
  1019.             /* Names come from argv, after options */
  1020.             p=n_argv[optind++];
  1021.         else {
  1022.             if(q)
  1023.                 msg("Missing filename after -C");
  1024.             return NULL;
  1025.         }
  1026.  
  1027.         /* JF trivial support for -C option.  I don't know if
  1028.            chdir'ing at this point is dangerous or not.
  1029.            It seems to work, which is all I ask. */
  1030.         if(c && !q && p[0]=='-' && p[1]=='C' && p[2]=='\0') {
  1031.             q=p;
  1032.             goto tryagain;
  1033.         }
  1034.         if(q) {
  1035.             if(chdir(p)<0)
  1036.                 msg_perror("Can't chdir to %s",p);
  1037.             q=0;
  1038.             goto tryagain;
  1039.         }
  1040.         /* End of JF quick -C hack */
  1041.  
  1042.         if(f_exclude && check_exclude(p))
  1043.             goto tryagain;
  1044.         return un_quote_string(p);
  1045.     }
  1046.     while(p = fgets(buffer, buffer_siz+1 /*nl*/, namef)) {
  1047.         q = p+strlen(p)-1;        /* Find the newline */
  1048.         if (q <= p)            /* Ignore empty lines */
  1049.             continue;
  1050.         while(q==p+buffer_siz && *q!='\n') {
  1051.             buffer=ck_realloc(buffer,buffer_siz+NAMSIZ+2);
  1052.             p=buffer;
  1053.             q=buffer+buffer_siz;
  1054.             buffer_siz+=NAMSIZ;
  1055.             fgets(q+1,NAMSIZ,namef);
  1056.             q=p+strlen(p)-1;
  1057.         }
  1058.         *q-- = '\0';            /* Zap the newline */
  1059.         while (q > p && *q == '/')    /* Zap trailing /s */
  1060.             *q-- = '\0';
  1061.         if (c && !q2 && p[0] == '-' && p[1] == 'C' && p[2] == '\0') {
  1062.             q2 = p;
  1063.             goto tryagain;
  1064.         }
  1065.         if (q2) {
  1066.             if (chdir (p) < 0)
  1067.                 msg_perror ("Can't chdir to %s", p);
  1068.             q2 = 0;
  1069.             goto tryagain;
  1070.         }
  1071.         if(f_exclude && check_exclude(p))
  1072.             goto tryagain;
  1073.         return un_quote_string(p);
  1074.     }
  1075.     return NULL;
  1076. }
  1077.  
  1078.  
  1079. /*
  1080.  * Close the name file, if any.
  1081.  */
  1082. name_close()
  1083. {
  1084.  
  1085.     if (namef != NULL && namef != stdin) fclose(namef);
  1086. }
  1087.  
  1088.  
  1089. /*
  1090.  * Gather names in a list for scanning.
  1091.  * Could hash them later if we really care.
  1092.  *
  1093.  * If the names are already sorted to match the archive, we just
  1094.  * read them one by one.  name_gather reads the first one, and it
  1095.  * is called by name_match as appropriate to read the next ones.
  1096.  * At EOF, the last name read is just left in the buffer.
  1097.  * This option lets users of small machines extract an arbitrary
  1098.  * number of files by doing "tar t" and editing down the list of files.
  1099.  */
  1100. name_gather()
  1101. {
  1102.     register char *p;
  1103.     static struct name *namebuf;    /* One-name buffer */
  1104.     static namelen;
  1105.     static char *chdir_name;
  1106.  
  1107.     if (f_sorted_names) {
  1108.         if(!namelen) {
  1109.             namelen=NAMSIZ;
  1110.             namebuf=(struct name *)ck_malloc(sizeof(struct name)+NAMSIZ);
  1111.         }
  1112.         p = name_next(0);
  1113.         if (p) {
  1114.             if(*p=='-' && p[1]=='C' && p[2]=='\0') {
  1115.                 chdir_name=name_next(0);
  1116.                 p=name_next(0);
  1117.                 if(!p) {
  1118.                     msg("Missing file name after -C");
  1119.                     exit(EX_ARGSBAD);
  1120.                 }
  1121.                 namebuf->change_dir=chdir_name;
  1122.             }
  1123.             namebuf->length = strlen(p);
  1124.             if (namebuf->length >= namelen) {
  1125.                 namebuf=(struct name *)ck_realloc(namebuf,sizeof(struct name)+namebuf->length);
  1126.                 namelen=namebuf->length;
  1127.             }
  1128.             strncpy(namebuf->name, p, namebuf->length);
  1129.             namebuf->name[ namebuf->length ] = 0;
  1130.             namebuf->next = (struct name *)NULL;
  1131.             namebuf->found = 0;
  1132.             namelist = namebuf;
  1133.             namelast = namelist;
  1134.         }
  1135.         return;
  1136.     }
  1137.  
  1138.     /* Non sorted names -- read them all in */
  1139.     while (p = name_next(0))
  1140.         addname(p);
  1141. }
  1142.  
  1143. /*
  1144.  * Add a name to the namelist.
  1145.  */
  1146. addname(name)
  1147.     char    *name;            /* pointer to name */
  1148. {
  1149.     register int    i;        /* Length of string */
  1150.     register struct name    *p;    /* Current struct pointer */
  1151.     static char *chdir_name;
  1152.     char *new_name();
  1153. #ifndef MAXPATHLEN
  1154. #define MAXPATHLEN 1024
  1155. #endif
  1156.  
  1157.     if(name[0]=='-' && name[1]=='C' && name[2]=='\0') {
  1158.         chdir_name=name_next(0);
  1159.         name=name_next(0);
  1160.         if(!chdir_name) {
  1161.             msg("Missing file name after -C");
  1162.             exit(EX_ARGSBAD);
  1163.         }
  1164. #ifdef MSDOS
  1165.         if(chdir_name[0]!='/' && (!isalpha(chdir_name[0]) || 
  1166.                       chdir_name[1]!=':' || 
  1167.                       chdir_name[2]!='/')) {
  1168.             char path[MAXPATHLEN];
  1169.               _fullpath(path,chdir_name,MAXPATHLEN);
  1170.             chdir_name=new_name(path,"");
  1171.         }
  1172. #else
  1173.         if(chdir_name[0]!='/') {
  1174.             char path[MAXPATHLEN];
  1175. #ifdef USG
  1176.             int getcwd();
  1177.             if(!getcwd(path,MAXPATHLEN))
  1178.                 msg("Couldn't get current directory.");
  1179.                 exit(EX_SYSTEM);
  1180. #else
  1181.             char *getwd();
  1182.  
  1183.             if(!getwd(path)) {
  1184.                 msg("Couldn't get current directory: %s",path);
  1185.                 exit(EX_SYSTEM);
  1186.             }
  1187. #endif
  1188.             chdir_name=new_name(path,chdir_name);
  1189.         }
  1190. #endif
  1191.     }
  1192.  
  1193.     if (name)
  1194.       {
  1195.         i = strlen(name);
  1196.         /*NOSTRICT*/
  1197.         p = (struct name *)malloc((unsigned)(sizeof(struct name) + i));
  1198.       }
  1199.     else
  1200.       p = (struct name *)malloc ((unsigned)(sizeof (struct name)));
  1201.     if (!p) {
  1202.       if (name)
  1203.         msg("cannot allocate mem for name '%s'.",name);
  1204.       else
  1205.         msg("cannot allocate mem for chdir record.");
  1206.       exit(EX_SYSTEM);
  1207.     }
  1208.     p->next = (struct name *)NULL;
  1209.     if (name)
  1210.       {
  1211.         p->fake = 0;
  1212.         p->length = i;
  1213.         strncpy(p->name, name, i);
  1214.         p->name[i] = '\0';    /* Null term */
  1215.       }
  1216.     else
  1217.       p->fake = 1;
  1218.     p->found = 0;
  1219.     p->regexp = 0;        /* Assume not a regular expression */
  1220.     p->firstch = 1;        /* Assume first char is literal */
  1221.     p->change_dir=chdir_name;
  1222.     p->dir_contents = 0;    /* JF */
  1223.     if (name)
  1224.       {
  1225.         if (index(name, '*') || index(name, '[') || index(name, '?')) {
  1226.           p->regexp = 1;    /* No, it's a regexp */
  1227.           if (name[0] == '*' || name[0] == '[' || name[0] == '?')
  1228.         p->firstch = 0;        /* Not even 1st char literal */
  1229.         }
  1230.       }
  1231.  
  1232.     if (namelast) namelast->next = p;
  1233.     namelast = p;
  1234.     if (!namelist) namelist = p;
  1235. }
  1236. /*
  1237.  * Match a name from an archive, p, with a name from the namelist.
  1238.  */
  1239. name_match(p)
  1240.     register char *p;
  1241. {
  1242.     register struct name    *nlp;
  1243.     register int        len;
  1244.  
  1245. again:
  1246.     if (0 == (nlp = namelist))    /* Empty namelist is easy */
  1247.         return 1;
  1248.     if (nlp->fake)
  1249.       {
  1250.         if (nlp->change_dir && chdir (nlp->change_dir))
  1251.           msg_perror ("Can't change to directory %d", nlp->change_dir);
  1252.         namelist = 0;
  1253.         return 1;
  1254.       }
  1255.     len = strlen(p);
  1256.     for (; nlp != 0; nlp = nlp->next) {
  1257.         /* If first chars don't match, quick skip */
  1258.         if (nlp->firstch && nlp->name[0] != p[0])
  1259.             continue;
  1260.  
  1261.         /* Regular expressions */
  1262.         if (nlp->regexp) {
  1263.             if (wildmat(p, nlp->name)) {
  1264.                 nlp->found = 1;    /* Remember it matched */
  1265.                 if(f_startfile) {
  1266.                     free((void *)namelist);
  1267.                     namelist=0;
  1268.                 }
  1269.                 if(nlp->change_dir && chdir(nlp->change_dir))
  1270.                     msg_perror("Can't change to directory %s",nlp->change_dir);
  1271.                 return 1;    /* We got a match */
  1272.             }
  1273.             continue;
  1274.         }
  1275.  
  1276.         /* Plain Old Strings */
  1277.         if (nlp->length <= len        /* Archive len >= specified */
  1278.          && (p[nlp->length] == '\0' || p[nlp->length] == '/')
  1279.                         /* Full match on file/dirname */
  1280.          && strncmp(p, nlp->name, nlp->length) == 0) /* Name compare */
  1281.         {
  1282.             nlp->found = 1;        /* Remember it matched */
  1283.             if(f_startfile) {
  1284.                 free((void *)namelist);
  1285.                 namelist = 0;
  1286.             }
  1287.             if(nlp->change_dir && chdir(nlp->change_dir))
  1288.                 msg_perror("Can't change to directory %s",nlp->change_dir);
  1289.             return 1;        /* We got a match */
  1290.         }
  1291.     }
  1292.  
  1293.     /*
  1294.      * Filename from archive not found in namelist.
  1295.      * If we have the whole namelist here, just return 0.
  1296.      * Otherwise, read the next name in and compare it.
  1297.      * If this was the last name, namelist->found will remain on.
  1298.      * If not, we loop to compare the newly read name.
  1299.      */
  1300.     if (f_sorted_names && namelist->found) {
  1301.         name_gather();        /* Read one more */
  1302.         if (!namelist->found) goto again;
  1303.     }
  1304.     return 0;
  1305. }
  1306.  
  1307.  
  1308. /*
  1309.  * Print the names of things in the namelist that were not matched.
  1310.  */
  1311. names_notfound()
  1312. {
  1313.     register struct name    *nlp,*next;
  1314.     register char        *p;
  1315.  
  1316.     for (nlp = namelist; nlp != 0; nlp = next) {
  1317.         next=nlp->next;
  1318.         if (!nlp->found)
  1319.             msg("%s not found in archive",nlp->name);
  1320.  
  1321.         /*
  1322.          * We could free() the list, but the process is about
  1323.          * to die anyway, so save some CPU time.  Amigas and
  1324.          * other similarly broken software will need to waste
  1325.          * the time, though.
  1326.          */
  1327. /*    THIS IS JUST PLAIN WRONG, N. Becker */
  1328. #if 0
  1329. #ifndef unix
  1330.         if (!f_sorted_names)
  1331.             free(nlp);
  1332. #endif
  1333. #endif
  1334.     }
  1335.     namelist = (struct name *)NULL;
  1336.     namelast = (struct name *)NULL;
  1337.  
  1338.     if (f_sorted_names) {
  1339.         while (0 != (p = name_next(1)))
  1340.             msg("%s not found in archive", p);
  1341.     }
  1342. }
  1343.  
  1344. /* These next routines were created by JF */
  1345.  
  1346. name_expand()
  1347. {
  1348. ;
  1349. }
  1350.  
  1351. /* This is like name_match(), except that it returns a pointer to the name
  1352.    it matched, and doesn't set ->found  The caller will have to do that
  1353.    if it wants to.  Oh, and if the namelist is empty, it returns 0, unlike
  1354.    name_match(), which returns TRUE */
  1355.  
  1356. struct name *
  1357. name_scan(p)
  1358. register char *p;
  1359. {
  1360.     register struct name    *nlp;
  1361.     register int        len;
  1362.  
  1363. again:
  1364.     if (0 == (nlp = namelist))    /* Empty namelist is easy */
  1365.         return 0;
  1366.     len = strlen(p);
  1367.     for (; nlp != 0; nlp = nlp->next) {
  1368.         /* If first chars don't match, quick skip */
  1369.         if (nlp->firstch && nlp->name[0] != p[0])
  1370.             continue;
  1371.  
  1372.         /* Regular expressions */
  1373.         if (nlp->regexp) {
  1374.             if (wildmat(p, nlp->name))
  1375.                 return nlp;    /* We got a match */
  1376.             continue;
  1377.         }
  1378.  
  1379.         /* Plain Old Strings */
  1380.         if (nlp->length <= len        /* Archive len >= specified */
  1381.          && (p[nlp->length] == '\0' || p[nlp->length] == '/')
  1382.                         /* Full match on file/dirname */
  1383.          && strncmp(p, nlp->name, nlp->length) == 0) /* Name compare */
  1384.             return nlp;        /* We got a match */
  1385.     }
  1386.  
  1387.     /*
  1388.      * Filename from archive not found in namelist.
  1389.      * If we have the whole namelist here, just return 0.
  1390.      * Otherwise, read the next name in and compare it.
  1391.      * If this was the last name, namelist->found will remain on.
  1392.      * If not, we loop to compare the newly read name.
  1393.      */
  1394.     if (f_sorted_names && namelist->found) {
  1395.         name_gather();        /* Read one more */
  1396.         if (!namelist->found) goto again;
  1397.     }
  1398.     return (struct name *) 0;
  1399. }
  1400.  
  1401. /* This returns a name from the namelist which doesn't have ->found set.
  1402.    It sets ->found before returning, so successive calls will find and return
  1403.    all the non-found names in the namelist */
  1404.  
  1405. struct name *gnu_list_name;
  1406.  
  1407. char *
  1408. name_from_list()
  1409. {
  1410.     if(!gnu_list_name)
  1411.         gnu_list_name = namelist;
  1412.     while(gnu_list_name && gnu_list_name->found)
  1413.         gnu_list_name=gnu_list_name->next;
  1414.     if(gnu_list_name) {
  1415.         gnu_list_name->found++;
  1416.         if(gnu_list_name->change_dir)
  1417.             if(chdir(gnu_list_name->change_dir)<0)
  1418.                 msg_perror("can't chdir to %s",gnu_list_name->change_dir);
  1419.         return gnu_list_name->name;
  1420.     }
  1421.     return (char *)0;
  1422. }
  1423.  
  1424. blank_name_list()
  1425. {
  1426.     struct name *n;
  1427.  
  1428.     gnu_list_name = 0;
  1429.     for(n=namelist;n;n=n->next)
  1430.         n->found = 0;
  1431. }
  1432.  
  1433. char *
  1434. new_name(path,name)
  1435. char *path,*name;
  1436. {
  1437.     char *path_buf;
  1438.  
  1439.     path_buf=(char *)malloc(strlen(path)+strlen(name)+2);
  1440.     if(path_buf==0) {
  1441.         msg("Can't allocate memory for name '%s/%s",path,name);
  1442.         exit(EX_SYSTEM);
  1443.     }
  1444.     (void) sprintf(path_buf,"%s%s%s",path,(*name?"/":""),name);
  1445.     return path_buf;
  1446. }
  1447.  
  1448. /* returns non-zero if the luser typed 'y' or 'Y', zero otherwise. */
  1449.  
  1450. int
  1451. confirm(action,file)
  1452. char *action, *file;
  1453. {
  1454.     int    c,nl;
  1455.     static FILE *confirm_file = 0;
  1456.     extern FILE *msg_file;
  1457.     extern char TTY_NAME[];
  1458.  
  1459.     fprintf(msg_file,"%s %s?", action, file);
  1460.     fflush(msg_file);
  1461.     if(!confirm_file) {
  1462.         confirm_file = (archive == 0) ? fopen(TTY_NAME, "r") : stdin;
  1463.         if(!confirm_file) {
  1464.             msg("Can't read confirmation from user");
  1465.             exit(EX_SYSTEM);
  1466.         }
  1467.     }
  1468.     c=getc(confirm_file);
  1469.     for(nl = c; nl != '\n' && nl != EOF; nl = getc(confirm_file))
  1470.         ;
  1471.     return (c=='y' || c=='Y');
  1472. }
  1473.  
  1474. char *x_buffer = 0;
  1475. int size_x_buffer;
  1476. int free_x_buffer;
  1477.  
  1478. char **exclude = 0;
  1479. int size_exclude = 0;
  1480. int free_exclude = 0;
  1481.  
  1482. char **re_exclude = 0;
  1483. int size_re_exclude = 0;
  1484. int free_re_exclude = 0;
  1485.  
  1486. add_exclude(name)
  1487. char *name;
  1488. {
  1489.     char *rname;
  1490.     char **tmp_ptr;
  1491.     int size_buf;
  1492.     extern char *un_quote_string();
  1493.  
  1494.     un_quote_string(name);
  1495.     size_buf = strlen(name);
  1496.  
  1497.     if(x_buffer==0) {
  1498.         x_buffer = (char *)ck_malloc(size_buf+1024);
  1499.         free_x_buffer=1024;
  1500.     } else if(free_x_buffer<=size_buf) {
  1501.         char *old_x_buffer;
  1502.         char **tmp_ptr;
  1503.  
  1504.         old_x_buffer = x_buffer;
  1505.         x_buffer = (char *)ck_realloc(x_buffer,size_x_buffer+1024);
  1506.         free_x_buffer = 1024;
  1507.         for(tmp_ptr=exclude;tmp_ptr<exclude+size_exclude;tmp_ptr++)
  1508.             *tmp_ptr= x_buffer + ((*tmp_ptr) - old_x_buffer);
  1509.         for(tmp_ptr=re_exclude;tmp_ptr<re_exclude+size_re_exclude;tmp_ptr++)
  1510.             *tmp_ptr= x_buffer + ((*tmp_ptr) - old_x_buffer);
  1511.     }
  1512.  
  1513.     if(is_regex(name)) {
  1514.         if(free_re_exclude==0) {
  1515.             re_exclude= (char **)(re_exclude ? ck_realloc(re_exclude,(size_re_exclude+32)*sizeof(char *)) : ck_malloc(sizeof(char *)*32));
  1516.             free_re_exclude+=32;
  1517.         }
  1518.         re_exclude[size_re_exclude]=x_buffer+size_x_buffer;
  1519.         size_re_exclude++;
  1520.         free_re_exclude--;
  1521.     } else {
  1522.         if(free_exclude==0) {
  1523.             exclude=(char **)(exclude ? ck_realloc(exclude,(size_exclude+32)*sizeof(char *)) : ck_malloc(sizeof(char *)*32));
  1524.             free_exclude+=32;
  1525.         }
  1526.         exclude[size_exclude]=x_buffer+size_x_buffer;
  1527.         size_exclude++;
  1528.         free_exclude--;
  1529.     }
  1530.     strcpy(x_buffer+size_x_buffer,name);
  1531.     size_x_buffer+=size_buf+1;
  1532.     free_x_buffer-=size_buf+1;
  1533. }
  1534.  
  1535. add_exclude_file(file)
  1536. char *file;
  1537. {
  1538.     FILE *fp;
  1539.     char buf[1024];
  1540.     extern char *rindex();
  1541.  
  1542.     if(strcmp(file, "-"))
  1543.         fp=fopen(file,"r");
  1544.     else
  1545.         /* Let's hope the person knows what they're doing. */
  1546.         /* Using -X - -T - -f - will get you *REALLY* strange
  1547.            results. . . */
  1548.         fp=stdin;
  1549.  
  1550.     if(!fp) {
  1551.         msg_perror("can't open %s",file);
  1552.         exit(2);
  1553.     }
  1554.     while(fgets(buf,1024,fp)) {
  1555.         int size_buf;
  1556.         char *end_str;
  1557.  
  1558.         end_str=rindex(buf,'\n');
  1559.         if(end_str)
  1560.             *end_str='\0';
  1561.         add_exclude(buf);
  1562.  
  1563.     }
  1564.     fclose(fp);
  1565. }
  1566.  
  1567. int
  1568. is_regex(str)
  1569. char *str;
  1570. {
  1571.     return index(str,'*') || index(str,'[') || index(str,'?');
  1572. }
  1573.  
  1574. /* Returns non-zero if the file 'name' should not be added/extracted */
  1575. int
  1576. check_exclude(name)
  1577. char *name;
  1578. {
  1579.     int n;
  1580.     char *str;
  1581.     extern char *strstr();
  1582.  
  1583.     for(n=0;n<size_re_exclude;n++) {
  1584.         if(wildmat(name,re_exclude[n]))
  1585.             return 1;
  1586.     }
  1587.     for(n=0;n<size_exclude;n++) {
  1588.         /* Accept the output from strstr only if it is the last
  1589.            part of the string.  There is certainly a faster way to
  1590.            do this. . . */
  1591.         if(   (str=strstr(name,exclude[n]))
  1592.             && (str==name || str[-1]=='/')
  1593.            && str[strlen(exclude[n])]=='\0')
  1594.             return 1;
  1595.     }
  1596.     return 0;
  1597. }
  1598.  
  1599. long
  1600. intconv(char *s, long unit)
  1601. {
  1602.     long value, factor = unit;
  1603.     char *q;
  1604.     value = strtol(s, &q, 0);
  1605.     if (q == s) {
  1606. bad:        fprintf(stderr, "Invalid number '%s' specified\n", s);
  1607.         exit(EX_ARGSBAD);
  1608.     }
  1609.     if (q && *q) {
  1610.         switch (*q++) {
  1611.         case 'b': case 'B':
  1612.             factor = blocking * RECORDSIZE;
  1613.             break;
  1614.         case 'k': case 'K':
  1615.             factor = 1024;
  1616.             break;
  1617.         case 'm': case 'M':
  1618.             factor = 1024L * 1024L;
  1619.             break;
  1620.         case 'r': case 'R':
  1621.             factor = RECORDSIZE;
  1622.             break;
  1623.         default:
  1624.             goto bad;
  1625.         }
  1626.         if (*q)
  1627.             goto bad;
  1628.     }
  1629.     return (value * factor) / unit;
  1630. }
  1631.