Home | History | Annotate | Line # | Download | only in pax
options.c revision 1.95
      1 /*	$NetBSD: options.c,v 1.95 2006/03/19 19:06:39 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992 Keith Muller.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Keith Muller of the University of California, San Diego.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #if HAVE_NBTOOL_CONFIG_H
     37 #include "nbtool_config.h"
     38 #endif
     39 
     40 #include <sys/cdefs.h>
     41 #if !defined(lint)
     42 #if 0
     43 static char sccsid[] = "@(#)options.c	8.2 (Berkeley) 4/18/94";
     44 #else
     45 __RCSID("$NetBSD: options.c,v 1.95 2006/03/19 19:06:39 christos Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/time.h>
     51 #include <sys/stat.h>
     52 #include <sys/param.h>
     53 #include <ctype.h>
     54 #include <errno.h>
     55 #if HAVE_NBTOOL_CONFIG_H
     56 #include "compat_getopt.h"
     57 #else
     58 #include <getopt.h>
     59 #endif
     60 #include <limits.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 #include <paths.h>
     66 #include "pax.h"
     67 #include "options.h"
     68 #include "cpio.h"
     69 #include "tar.h"
     70 #include "extern.h"
     71 #ifndef SMALL
     72 #include "mtree.h"
     73 #endif	/* SMALL */
     74 
     75 /*
     76  * Routines which handle command line options
     77  */
     78 
     79 static int nopids;		/* tar mode: suppress "pids" for -p option */
     80 static char flgch[] = FLGCH;	/* list of all possible flags (pax) */
     81 static OPLIST *ophead = NULL;	/* head for format specific options -x */
     82 static OPLIST *optail = NULL;	/* option tail */
     83 
     84 static int no_op(void);
     85 static void printflg(unsigned int);
     86 static int c_frmt(const void *, const void *);
     87 static off_t str_offt(char *);
     88 static char *getline(FILE *fp);
     89 static void pax_options(int, char **);
     90 static void pax_usage(void);
     91 static void tar_options(int, char **);
     92 static void tar_usage(void);
     93 #ifndef NO_CPIO
     94 static void cpio_options(int, char **);
     95 static void cpio_usage(void);
     96 #endif
     97 
     98 /* errors from getline */
     99 #define GETLINE_FILE_CORRUPT 1
    100 #define GETLINE_OUT_OF_MEM 2
    101 static int getline_error;
    102 
    103 #define BZIP2_CMD	"bzip2"		/* command to run as bzip2 */
    104 #define GZIP_CMD	"gzip"		/* command to run as gzip */
    105 #define COMPRESS_CMD	"compress"	/* command to run as compress */
    106 
    107 /*
    108  * Long options.
    109  */
    110 #define	OPT_USE_COMPRESS_PROGRAM	0
    111 #define	OPT_CHECKPOINT			1
    112 #define	OPT_UNLINK			2
    113 #define	OPT_HELP			3
    114 #define	OPT_ATIME_PRESERVE		4
    115 #define	OPT_IGNORE_FAILED_READ		5
    116 #define	OPT_REMOVE_FILES		6
    117 #define	OPT_NULL			7
    118 #define	OPT_TOTALS			8
    119 #define	OPT_VERSION			9
    120 #define	OPT_EXCLUDE			10
    121 #define	OPT_BLOCK_COMPRESS		11
    122 #define	OPT_NORECURSE			12
    123 #define	OPT_FORCE_LOCAL			13
    124 #define	OPT_INSECURE			14
    125 #define	OPT_STRICT			15
    126 #define	OPT_SPARSE			16
    127 #if !HAVE_NBTOOL_CONFIG_H
    128 #define	OPT_CHROOT			17
    129 #endif
    130 
    131 /*
    132  *	Format specific routine table - MUST BE IN SORTED ORDER BY NAME
    133  *	(see pax.h for description of each function)
    134  *
    135  *	name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
    136  *	read, end_read, st_write, write, end_write, trail,
    137  *	subtrail, rd_data, wr_data, options
    138  */
    139 
    140 FSUB fsub[] = {
    141 #ifndef NO_CPIO
    142 /* 0: OLD BINARY CPIO */
    143 	{ "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
    144 	bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
    145 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    146 
    147 /* 1: OLD OCTAL CHARACTER CPIO */
    148 	{ "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
    149 	cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
    150 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    151 
    152 /* 2: SVR4 HEX CPIO */
    153 	{ "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
    154 	vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
    155 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    156 
    157 /* 3: SVR4 HEX CPIO WITH CRC */
    158 	{ "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
    159 	vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
    160 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    161 #endif
    162 /* 4: OLD TAR */
    163 	{ "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
    164 	tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
    165 	NULL, rd_wrfile, wr_rdfile, tar_opt },
    166 
    167 /* 5: POSIX USTAR */
    168 	{ "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
    169 	ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
    170 	NULL, rd_wrfile, wr_rdfile, bad_opt }
    171 };
    172 #ifndef NO_CPIO
    173 #define F_BCPIO		0	/* old binary cpio format */
    174 #define F_CPIO		1	/* old octal character cpio format */
    175 #define F_SV4CPIO	2	/* SVR4 hex cpio format */
    176 #define F_SV4CRC	3	/* SVR4 hex with crc cpio format */
    177 #define F_TAR		4	/* old V7 UNIX tar format */
    178 #define F_USTAR		5	/* ustar format */
    179 #else
    180 #define F_TAR		0	/* old V7 UNIX tar format */
    181 #define F_USTAR		1	/* ustar format */
    182 #endif
    183 #define DEFLT		F_USTAR	/* default write format from list above */
    184 
    185 /*
    186  * ford is the archive search order used by get_arc() to determine what kind
    187  * of archive we are dealing with. This helps to properly id archive formats
    188  * some formats may be subsets of others....
    189  */
    190 int ford[] = {F_USTAR, F_TAR,
    191 #ifndef NO_CPIO
    192     F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO,
    193 #endif
    194     -1};
    195 
    196 /*
    197  * filename record separator
    198  */
    199 int sep = '\n';
    200 
    201 /*
    202  * options()
    203  *	figure out if we are pax, tar or cpio. Call the appropriate options
    204  *	parser
    205  */
    206 
    207 void
    208 options(int argc, char **argv)
    209 {
    210 
    211 	/*
    212 	 * Are we acting like pax, tar or cpio (based on argv[0])
    213 	 */
    214 	if ((argv0 = strrchr(argv[0], '/')) != NULL)
    215 		argv0++;
    216 	else
    217 		argv0 = argv[0];
    218 
    219 	if (strstr(argv0, NM_TAR)) {
    220 		argv0 = NM_TAR;
    221 		tar_options(argc, argv);
    222 #ifndef NO_CPIO
    223 	} else if (strstr(argv0, NM_CPIO)) {
    224 		argv0 = NM_CPIO;
    225 		cpio_options(argc, argv);
    226 #endif
    227 	} else {
    228 		argv0 = NM_PAX;
    229 		pax_options(argc, argv);
    230 	}
    231 }
    232 
    233 struct option pax_longopts[] = {
    234 	{ "insecure",		no_argument,		0,
    235 						OPT_INSECURE },
    236 	{ "force-local",	no_argument,		0,
    237 						OPT_FORCE_LOCAL },
    238 	{ 0,			0,			0 },
    239 };
    240 
    241 /*
    242  * pax_options()
    243  *	look at the user specified flags. set globals as required and check if
    244  *	the user specified a legal set of flags. If not, complain and exit
    245  */
    246 
    247 static void
    248 pax_options(int argc, char **argv)
    249 {
    250 	int c;
    251 	int i;
    252 	unsigned int flg = 0;
    253 	unsigned int bflg = 0;
    254 	char *pt;
    255 	FSUB tmp;
    256 
    257 	/*
    258 	 * process option flags
    259 	 */
    260 	while ((c = getopt_long(argc, argv,
    261 	    "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:XYZ",
    262 	    pax_longopts, NULL)) != -1) {
    263 		switch (c) {
    264 		case '0':
    265 			sep = '\0';
    266 			break;
    267 		case 'a':
    268 			/*
    269 			 * append
    270 			 */
    271 			flg |= AF;
    272 			break;
    273 		case 'b':
    274 			/*
    275 			 * specify blocksize
    276 			 */
    277 			flg |= BF;
    278 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
    279 				tty_warn(1, "Invalid block size %s", optarg);
    280 				pax_usage();
    281 			}
    282 			break;
    283 		case 'c':
    284 			/*
    285 			 * inverse match on patterns
    286 			 */
    287 			cflag = 1;
    288 			flg |= CF;
    289 			break;
    290 		case 'd':
    291 			/*
    292 			 * match only dir on extract, not the subtree at dir
    293 			 */
    294 			dflag = 1;
    295 			flg |= DF;
    296 			break;
    297 		case 'f':
    298 			/*
    299 			 * filename where the archive is stored
    300 			 */
    301 			arcname = optarg;
    302 			flg |= FF;
    303 			break;
    304 		case 'i':
    305 			/*
    306 			 * interactive file rename
    307 			 */
    308 			iflag = 1;
    309 			flg |= IF;
    310 			break;
    311 		case 'j':
    312 			/*
    313 			 * pass through bzip2
    314 			 */
    315 			jflag = 1;
    316 			gzip_program = BZIP2_CMD;
    317 			break;
    318 		case 'k':
    319 			/*
    320 			 * do not clobber files that exist
    321 			 */
    322 			kflag = 1;
    323 			flg |= KF;
    324 			break;
    325 		case 'l':
    326 			/*
    327 			 * try to link src to dest with copy (-rw)
    328 			 */
    329 			lflag = 1;
    330 			flg |= LF;
    331 			break;
    332 		case 'n':
    333 			/*
    334 			 * select first match for a pattern only
    335 			 */
    336 			nflag = 1;
    337 			flg |= NF;
    338 			break;
    339 		case 'o':
    340 			/*
    341 			 * pass format specific options
    342 			 */
    343 			flg |= OF;
    344 			if (opt_add(optarg) < 0)
    345 				pax_usage();
    346 			break;
    347 		case 'p':
    348 			/*
    349 			 * specify file characteristic options
    350 			 */
    351 			for (pt = optarg; *pt != '\0'; ++pt) {
    352 				switch(*pt) {
    353 				case 'a':
    354 					/*
    355 					 * do not preserve access time
    356 					 */
    357 					patime = 0;
    358 					break;
    359 				case 'e':
    360 					/*
    361 					 * preserve user id, group id, file
    362 					 * mode, access/modification times
    363 					 * and file flags.
    364 					 */
    365 					pids = 1;
    366 					pmode = 1;
    367 					patime = 1;
    368 					pmtime = 1;
    369 					pfflags = 1;
    370 					break;
    371 #if 0
    372 				case 'f':
    373 					/*
    374 					 * do not preserve file flags
    375 					 */
    376 					pfflags = 0;
    377 					break;
    378 #endif
    379 				case 'm':
    380 					/*
    381 					 * do not preserve modification time
    382 					 */
    383 					pmtime = 0;
    384 					break;
    385 				case 'o':
    386 					/*
    387 					 * preserve uid/gid
    388 					 */
    389 					pids = 1;
    390 					break;
    391 				case 'p':
    392 					/*
    393 					 * preserve file mode bits
    394 					 */
    395 					pmode = 1;
    396 					break;
    397 				default:
    398 					tty_warn(1, "Invalid -p string: %c",
    399 					    *pt);
    400 					pax_usage();
    401 					break;
    402 				}
    403 			}
    404 			flg |= PF;
    405 			break;
    406 		case 'r':
    407 			/*
    408 			 * read the archive
    409 			 */
    410 			flg |= RF;
    411 			break;
    412 		case 's':
    413 			/*
    414 			 * file name substitution name pattern
    415 			 */
    416 			if (rep_add(optarg) < 0) {
    417 				pax_usage();
    418 				break;
    419 			}
    420 			flg |= SF;
    421 			break;
    422 		case 't':
    423 			/*
    424 			 * preserve access time on filesystem nodes we read
    425 			 */
    426 			tflag = 1;
    427 			flg |= TF;
    428 			break;
    429 		case 'u':
    430 			/*
    431 			 * ignore those older files
    432 			 */
    433 			uflag = 1;
    434 			flg |= UF;
    435 			break;
    436 		case 'v':
    437 			/*
    438 			 * verbose operation mode
    439 			 */
    440 			vflag = 1;
    441 			flg |= VF;
    442 			break;
    443 		case 'w':
    444 			/*
    445 			 * write an archive
    446 			 */
    447 			flg |= WF;
    448 			break;
    449 		case 'x':
    450 			/*
    451 			 * specify an archive format on write
    452 			 */
    453 			tmp.name = optarg;
    454 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
    455 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
    456 			if (frmt != NULL) {
    457 				flg |= XF;
    458 				break;
    459 			}
    460 			tty_warn(1, "Unknown -x format: %s", optarg);
    461 			(void)fputs("pax: Known -x formats are:", stderr);
    462 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
    463 				(void)fprintf(stderr, " %s", fsub[i].name);
    464 			(void)fputs("\n\n", stderr);
    465 			pax_usage();
    466 			break;
    467 		case 'z':
    468 			/*
    469 			 * use gzip.  Non standard option.
    470 			 */
    471 			gzip_program = GZIP_CMD;
    472 			break;
    473 		case 'A':
    474 			Aflag = 1;
    475 			flg |= CAF;
    476 			break;
    477 		case 'B':
    478 			/*
    479 			 * non-standard option on number of bytes written on a
    480 			 * single archive volume.
    481 			 */
    482 			if ((wrlimit = str_offt(optarg)) <= 0) {
    483 				tty_warn(1, "Invalid write limit %s", optarg);
    484 				pax_usage();
    485 			}
    486 			if (wrlimit % BLKMULT) {
    487 				tty_warn(1,
    488 				    "Write limit is not a %d byte multiple",
    489 				    BLKMULT);
    490 				pax_usage();
    491 			}
    492 			flg |= CBF;
    493 			break;
    494 		case 'D':
    495 			/*
    496 			 * On extraction check file inode change time before the
    497 			 * modification of the file name. Non standard option.
    498 			 */
    499 			Dflag = 1;
    500 			flg |= CDF;
    501 			break;
    502 		case 'E':
    503 			/*
    504 			 * non-standard limit on read faults
    505 			 * 0 indicates stop after first error, values
    506 			 * indicate a limit, "none" try forever
    507 			 */
    508 			flg |= CEF;
    509 			if (strcmp(none, optarg) == 0)
    510 				maxflt = -1;
    511 			else if ((maxflt = atoi(optarg)) < 0) {
    512 				tty_warn(1,
    513 				    "Error count value must be positive");
    514 				pax_usage();
    515 			}
    516 			break;
    517 		case 'G':
    518 			/*
    519 			 * non-standard option for selecting files within an
    520 			 * archive by group (gid or name)
    521 			 */
    522 			if (grp_add(optarg) < 0) {
    523 				pax_usage();
    524 				break;
    525 			}
    526 			flg |= CGF;
    527 			break;
    528 		case 'H':
    529 			/*
    530 			 * follow command line symlinks only
    531 			 */
    532 			Hflag = 1;
    533 			flg |= CHF;
    534 			break;
    535 		case 'L':
    536 			/*
    537 			 * follow symlinks
    538 			 */
    539 			Lflag = 1;
    540 			flg |= CLF;
    541 			break;
    542 #ifdef SMALL
    543 		case 'M':
    544 		case 'N':
    545 			tty_warn(1, "Support for -%c is not compiled in", c);
    546 			exit(1);
    547 #else	/* !SMALL */
    548 		case 'M':
    549 			/*
    550 			 * Treat list of filenames on stdin as an
    551 			 * mtree(8) specfile.  Non standard option.
    552 			 */
    553 			Mflag = 1;
    554 			flg |= CMF;
    555 			break;
    556 		case 'N':
    557 			/*
    558 			 * Use alternative directory for user db lookups.
    559 			 */
    560 			if (!setup_getid(optarg)) {
    561 				tty_warn(1,
    562 			    "Unable to use user and group databases in `%s'",
    563 				    optarg);
    564 				pax_usage();
    565 			}
    566 			break;
    567 #endif	/* !SMALL */
    568 		case 'O':
    569 			/*
    570 			 * Force one volume.  Non standard option.
    571 			 */
    572 			force_one_volume = 1;
    573 			break;
    574 		case 'P':
    575 			/*
    576 			 * do NOT follow symlinks (default)
    577 			 */
    578 			Lflag = 0;
    579 			flg |= CPF;
    580 			break;
    581 		case 'T':
    582 			/*
    583 			 * non-standard option for selecting files within an
    584 			 * archive by modification time range (lower,upper)
    585 			 */
    586 			if (trng_add(optarg) < 0) {
    587 				pax_usage();
    588 				break;
    589 			}
    590 			flg |= CTF;
    591 			break;
    592 		case 'U':
    593 			/*
    594 			 * non-standard option for selecting files within an
    595 			 * archive by user (uid or name)
    596 			 */
    597 			if (usr_add(optarg) < 0) {
    598 				pax_usage();
    599 				break;
    600 			}
    601 			flg |= CUF;
    602 			break;
    603 		case 'X':
    604 			/*
    605 			 * do not pass over mount points in the file system
    606 			 */
    607 			Xflag = 1;
    608 			flg |= CXF;
    609 			break;
    610 		case 'Y':
    611 			/*
    612 			 * On extraction check file inode change time after the
    613 			 * modification of the file name. Non standard option.
    614 			 */
    615 			Yflag = 1;
    616 			flg |= CYF;
    617 			break;
    618 		case 'Z':
    619 			/*
    620 			 * On extraction check modification time after the
    621 			 * modification of the file name. Non standard option.
    622 			 */
    623 			Zflag = 1;
    624 			flg |= CZF;
    625 			break;
    626 		case OPT_INSECURE:
    627 			secure = 0;
    628 			break;
    629 		case OPT_FORCE_LOCAL:
    630 			forcelocal = 0;
    631 			break;
    632 		case '?':
    633 		default:
    634 			pax_usage();
    635 			break;
    636 		}
    637 	}
    638 
    639 	/*
    640 	 * figure out the operation mode of pax read,write,extract,copy,append
    641 	 * or list. check that we have not been given a bogus set of flags
    642 	 * for the operation mode.
    643 	 */
    644 	if (ISLIST(flg)) {
    645 		act = LIST;
    646 		listf = stdout;
    647 		bflg = flg & BDLIST;
    648 	} else if (ISEXTRACT(flg)) {
    649 		act = EXTRACT;
    650 		bflg = flg & BDEXTR;
    651 	} else if (ISARCHIVE(flg)) {
    652 		act = ARCHIVE;
    653 		bflg = flg & BDARCH;
    654 	} else if (ISAPPND(flg)) {
    655 		act = APPND;
    656 		bflg = flg & BDARCH;
    657 	} else if (ISCOPY(flg)) {
    658 		act = COPY;
    659 		bflg = flg & BDCOPY;
    660 	} else
    661 		pax_usage();
    662 	if (bflg) {
    663 		printflg(flg);
    664 		pax_usage();
    665 	}
    666 
    667 	/*
    668 	 * if we are writing (ARCHIVE) we use the default format if the user
    669 	 * did not specify a format. when we write during an APPEND, we will
    670 	 * adopt the format of the existing archive if none was supplied.
    671 	 */
    672 	if (!(flg & XF) && (act == ARCHIVE))
    673 		frmt = &(fsub[DEFLT]);
    674 
    675 	/*
    676 	 * process the args as they are interpreted by the operation mode
    677 	 */
    678 	switch (act) {
    679 	case LIST:
    680 	case EXTRACT:
    681 		for (; optind < argc; optind++)
    682 			if (pat_add(argv[optind], NULL) < 0)
    683 				pax_usage();
    684 		break;
    685 	case COPY:
    686 		if (optind >= argc) {
    687 			tty_warn(0, "Destination directory was not supplied");
    688 			pax_usage();
    689 		}
    690 		--argc;
    691 		dirptr = argv[argc];
    692 		if (mkpath(dirptr) < 0)
    693 			exit(1);
    694 		/* FALLTHROUGH */
    695 	case ARCHIVE:
    696 	case APPND:
    697 		for (; optind < argc; optind++)
    698 			if (ftree_add(argv[optind], 0) < 0)
    699 				pax_usage();
    700 		/*
    701 		 * no read errors allowed on updates/append operation!
    702 		 */
    703 		maxflt = 0;
    704 		break;
    705 	}
    706 }
    707 
    708 
    709 /*
    710  * tar_options()
    711  *	look at the user specified flags. set globals as required and check if
    712  *	the user specified a legal set of flags. If not, complain and exit
    713  */
    714 
    715 struct option tar_longopts[] = {
    716 	{ "block-size",		required_argument,	0,	'b' },
    717 	{ "bunzip2",		no_argument,		0,	'j' },
    718 	{ "bzip2",		no_argument,		0,	'j' },
    719 	{ "create",		no_argument,		0,	'c' },	/* F */
    720 	/* -e -- no corresponding long option */
    721 	{ "file",		required_argument,	0,	'f' },
    722 	{ "dereference",	no_argument,		0,	'h' },
    723 	{ "keep-old-files",	no_argument,		0,	'k' },
    724 	{ "one-file-system",	no_argument,		0,	'l' },
    725 	{ "modification-time",	no_argument,		0,	'm' },
    726 	{ "old-archive",	no_argument,		0,	'o' },
    727 	{ "portability",	no_argument,		0,	'o' },
    728 	{ "same-permissions",	no_argument,		0,	'p' },
    729 	{ "preserve-permissions", no_argument,		0,	'p' },
    730 	{ "preserve",		no_argument,		0,	'p' },
    731 	{ "fast-read",		no_argument,		0,	'q' },
    732 	{ "append",		no_argument,		0,	'r' },	/* F */
    733 	{ "update",		no_argument,		0,	'u' },	/* F */
    734 	{ "list",		no_argument,		0,	't' },	/* F */
    735 	{ "verbose",		no_argument,		0,	'v' },
    736 	{ "interactive",	no_argument,		0,	'w' },
    737 	{ "confirmation",	no_argument,		0,	'w' },
    738 	{ "extract",		no_argument,		0,	'x' },	/* F */
    739 	{ "get",		no_argument,		0,	'x' },	/* F */
    740 	{ "gzip",		no_argument,		0,	'z' },
    741 	{ "gunzip",		no_argument,		0,	'z' },
    742 	{ "read-full-blocks",	no_argument,		0,	'B' },
    743 	{ "directory",		required_argument,	0,	'C' },
    744 	{ "to-stdout",		no_argument,		0,	'O' },
    745 	{ "absolute-paths",	no_argument,		0,	'P' },
    746 	{ "sparse",		no_argument,		0,	'S' },
    747 	{ "files-from",		required_argument,	0,	'T' },
    748 	{ "exclude-from",	required_argument,	0,	'X' },
    749 	{ "compress",		no_argument,		0,	'Z' },
    750 	{ "uncompress",		no_argument,		0,	'Z' },
    751 	{ "strict",		no_argument,		0,
    752 						OPT_STRICT },
    753 	{ "atime-preserve",	no_argument,		0,
    754 						OPT_ATIME_PRESERVE },
    755 	{ "unlink",		no_argument,		0,
    756 						OPT_UNLINK },
    757 	{ "use-compress-program", required_argument,	0,
    758 						OPT_USE_COMPRESS_PROGRAM },
    759 	{ "force-local",	no_argument,		0,
    760 						OPT_FORCE_LOCAL },
    761 	{ "insecure",		no_argument,		0,
    762 						OPT_INSECURE },
    763 	{ "exclude",		required_argument,	0,
    764 						OPT_EXCLUDE },
    765 	{ "no-recursion",	no_argument,		0,
    766 						OPT_NORECURSE },
    767 #if !HAVE_NBTOOL_CONFIG_H
    768 	{ "chroot",		no_argument,		0,
    769 						OPT_CHROOT },
    770 #endif
    771 #if 0 /* Not implemented */
    772 	{ "catenate",		no_argument,		0,	'A' },	/* F */
    773 	{ "concatenate",	no_argument,		0,	'A' },	/* F */
    774 	{ "diff",		no_argument,		0,	'd' },	/* F */
    775 	{ "compare",		no_argument,		0,	'd' },	/* F */
    776 	{ "checkpoint",		no_argument,		0,
    777 						OPT_CHECKPOINT },
    778 	{ "help",		no_argument,		0,
    779 						OPT_HELP },
    780 	{ "info-script",	required_argument,	0,	'F' },
    781 	{ "new-volume-script",	required_argument,	0,	'F' },
    782 	{ "incremental",	no_argument,		0,	'G' },
    783 	{ "listed-incremental",	required_argument,	0,	'g' },
    784 	{ "ignore-zeros",	no_argument,		0,	'i' },
    785 	{ "ignore-failed-read",	no_argument,		0,
    786 						OPT_IGNORE_FAILED_READ },
    787 	{ "starting-file",	no_argument,		0,	'K' },
    788 	{ "tape-length",	required_argument,	0,	'L' },
    789 	{ "multi-volume",	no_argument,		0,	'M' },
    790 	{ "after-date",		required_argument,	0,	'N' },
    791 	{ "newer",		required_argument,	0,	'N' },
    792 	{ "record-number",	no_argument,		0,	'R' },
    793 	{ "remove-files",	no_argument,		0,
    794 						OPT_REMOVE_FILES },
    795 	{ "same-order",		no_argument,		0,	's' },
    796 	{ "preserve-order",	no_argument,		0,	's' },
    797 	{ "null",		no_argument,		0,
    798 						OPT_NULL },
    799 	{ "totals",		no_argument,		0,
    800 						OPT_TOTALS },
    801 	{ "volume-name",	required_argument,	0,	'V' },
    802 	{ "label",		required_argument,	0,	'V' },
    803 	{ "version",		no_argument,		0,
    804 						OPT_VERSION },
    805 	{ "verify",		no_argument,		0,	'W' },
    806 	{ "block-compress",	no_argument,		0,
    807 						OPT_BLOCK_COMPRESS },
    808 #endif
    809 	{ 0,			0,			0,	0 },
    810 };
    811 
    812 static void
    813 tar_options(int argc, char **argv)
    814 {
    815 	int c;
    816 	int fstdin = 0;
    817 	int Oflag = 0;
    818 	int nincfiles = 0;
    819 	int incfiles_max = 0;
    820 	struct incfile {
    821 		char *file;
    822 		char *dir;
    823 	};
    824 	struct incfile *incfiles = NULL;
    825 
    826 	/*
    827 	 * Set default values.
    828 	 */
    829 	rmleadslash = 1;
    830 	is_gnutar = 1;
    831 
    832 	/*
    833 	 * process option flags
    834 	 */
    835 	while ((c = getoldopt(argc, argv,
    836 	    "+b:cef:hjklmopqrs:tuvwxzBC:HI:OPST:X:Z014578",
    837 	    tar_longopts, NULL))
    838 	    != -1)  {
    839 		switch(c) {
    840 		case 'b':
    841 			/*
    842 			 * specify blocksize in 512-byte blocks
    843 			 */
    844 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
    845 				tty_warn(1, "Invalid block size %s", optarg);
    846 				tar_usage();
    847 			}
    848 			wrblksz *= 512;		/* XXX - check for int oflow */
    849 			break;
    850 		case 'c':
    851 			/*
    852 			 * create an archive
    853 			 */
    854 			act = ARCHIVE;
    855 			break;
    856 		case 'e':
    857 			/*
    858 			 * stop after first error
    859 			 */
    860 			maxflt = 0;
    861 			break;
    862 		case 'f':
    863 			/*
    864 			 * filename where the archive is stored
    865 			 */
    866 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
    867 				/*
    868 				 * treat a - as stdin
    869 				 */
    870 				fstdin = 1;
    871 				arcname = NULL;
    872 				break;
    873 			}
    874 			fstdin = 0;
    875 			arcname = optarg;
    876 			break;
    877 		case 'h':
    878 			/*
    879 			 * follow symlinks
    880 			 */
    881 			Lflag = 1;
    882 			break;
    883 		case 'j':
    884 			/*
    885 			 * pass through bzip2. not a standard option
    886 			 */
    887 			jflag = 1;
    888 			gzip_program = BZIP2_CMD;
    889 			break;
    890 		case 'k':
    891 			/*
    892 			 * do not clobber files that exist
    893 			 */
    894 			kflag = 1;
    895 			break;
    896 		case 'l':
    897 			/*
    898 			 * do not pass over mount points in the file system
    899 			 */
    900 			Xflag = 1;
    901 			break;
    902 		case 'm':
    903 			/*
    904 			 * do not preserve modification time
    905 			 */
    906 			pmtime = 0;
    907 			break;
    908 		case 'o':
    909 			/*
    910 			 * This option does several things based on whether
    911 			 * this is a create or extract operation.
    912 			 */
    913 			if (act == ARCHIVE) {
    914 				/* GNU tar: write V7 format archives. */
    915 				Oflag = 1;
    916 				/* 4.2BSD: don't add directory entries. */
    917 				if (opt_add("write_opt=nodir") < 0)
    918 					tar_usage();
    919 
    920 			} else {
    921 				/* SUS: don't preserve owner/group. */
    922 				pids = 0;
    923 				nopids = 1;
    924 			}
    925 			break;
    926 		case 'O':
    927 			Oflag = 1;
    928 			break;
    929 		case 'p':
    930 			/*
    931 			 * preserve user id, group id, file
    932 			 * mode, access/modification times
    933 			 */
    934 			if (!nopids)
    935 				pids = 1;
    936 			pmode = 1;
    937 			patime = 1;
    938 			pmtime = 1;
    939 			break;
    940 		case 'q':
    941 			/*
    942 			 * select first match for a pattern only
    943 			 */
    944 			nflag = 1;
    945 			break;
    946 		case 'r':
    947 		case 'u':
    948 			/*
    949 			 * append to the archive
    950 			 */
    951 			act = APPND;
    952 			break;
    953 		case 's':
    954 			/*
    955 			 * file name substitution name pattern
    956 			 */
    957 			if (rep_add(optarg) < 0) {
    958 				tar_usage();
    959 				break;
    960 			}
    961 			break;
    962 		case 't':
    963 			/*
    964 			 * list contents of the tape
    965 			 */
    966 			act = LIST;
    967 			break;
    968 		case 'v':
    969 			/*
    970 			 * verbose operation mode
    971 			 */
    972 			vflag = 1;
    973 			break;
    974 		case 'w':
    975 			/*
    976 			 * interactive file rename
    977 			 */
    978 			iflag = 1;
    979 			break;
    980 		case 'x':
    981 			/*
    982 			 * extract an archive, preserving mode,
    983 			 * and mtime if possible.
    984 			 */
    985 			act = EXTRACT;
    986 			pmtime = 1;
    987 			break;
    988 		case 'z':
    989 			/*
    990 			 * use gzip.  Non standard option.
    991 			 */
    992 			zflag = 1;
    993 			gzip_program = GZIP_CMD;
    994 			break;
    995 		case 'B':
    996 			/*
    997 			 * Nothing to do here, this is pax default
    998 			 */
    999 			break;
   1000 		case 'C':
   1001 			chdname = optarg;
   1002 			break;
   1003 		case 'H':
   1004 			/*
   1005 			 * follow command line symlinks only
   1006 			 */
   1007 			Hflag = 1;
   1008 			break;
   1009 		case 'I':
   1010 		case 'T':
   1011 			if (++nincfiles > incfiles_max) {
   1012 				incfiles_max = nincfiles + 3;
   1013 				incfiles = realloc(incfiles,
   1014 				    sizeof(*incfiles) * incfiles_max);
   1015 				if (incfiles == NULL) {
   1016 					tty_warn(0, "Unable to allocate space "
   1017 					    "for option list");
   1018 					exit(1);
   1019 				}
   1020 			}
   1021 			incfiles[nincfiles - 1].file = optarg;
   1022 			incfiles[nincfiles - 1].dir = chdname;
   1023 			break;
   1024 		case 'P':
   1025 			/*
   1026 			 * do not remove leading '/' from pathnames
   1027 			 */
   1028 			rmleadslash = 0;
   1029 			Aflag = 1;
   1030 			break;
   1031 		case 'S':
   1032 			/* do nothing; we already generate sparse files */
   1033 			break;
   1034 		case 'X':
   1035 			/*
   1036 			 * GNU tar compat: exclude the files listed in optarg
   1037 			 */
   1038 			if (tar_gnutar_X_compat(optarg) != 0)
   1039 				tar_usage();
   1040 			break;
   1041 		case 'Z':
   1042 			/*
   1043 			 * use compress.
   1044 			 */
   1045 			zflag = 1;
   1046 			gzip_program = COMPRESS_CMD;
   1047 			break;
   1048 		case '0':
   1049 			arcname = DEV_0;
   1050 			break;
   1051 		case '1':
   1052 			arcname = DEV_1;
   1053 			break;
   1054 		case '4':
   1055 			arcname = DEV_4;
   1056 			break;
   1057 		case '5':
   1058 			arcname = DEV_5;
   1059 			break;
   1060 		case '7':
   1061 			arcname = DEV_7;
   1062 			break;
   1063 		case '8':
   1064 			arcname = DEV_8;
   1065 			break;
   1066 		case OPT_ATIME_PRESERVE:
   1067 			patime = 1;
   1068 			break;
   1069 		case OPT_UNLINK:
   1070 			/* Just ignore -- we always unlink first. */
   1071 			break;
   1072 		case OPT_USE_COMPRESS_PROGRAM:
   1073 			zflag = 1;
   1074 			gzip_program = optarg;
   1075 			break;
   1076 		case OPT_FORCE_LOCAL:
   1077 			forcelocal = 1;
   1078 			break;
   1079 		case OPT_INSECURE:
   1080 			secure = 0;
   1081 			break;
   1082 		case OPT_STRICT:
   1083 			/* disable gnu extensions */
   1084 			is_gnutar = 0;
   1085 			break;
   1086 		case OPT_EXCLUDE:
   1087 			if (tar_gnutar_minus_minus_exclude(optarg) != 0)
   1088 				tar_usage();
   1089 			break;
   1090 		case OPT_NORECURSE:
   1091 			dflag = 1;
   1092 			break;
   1093 #if !HAVE_NBTOOL_CONFIG_H
   1094 		case OPT_CHROOT:
   1095 			do_chroot = 1;
   1096 			break;
   1097 #endif
   1098 		default:
   1099 			tar_usage();
   1100 			break;
   1101 		}
   1102 	}
   1103 	argc -= optind;
   1104 	argv += optind;
   1105 
   1106 	/* Tar requires an action. */
   1107 	if (act == ERROR)
   1108 		tar_usage();
   1109 
   1110 	/* Traditional tar behaviour (pax uses stderr unless in list mode) */
   1111 	if (fstdin == 1 && act == ARCHIVE)
   1112 		listf = stderr;
   1113 	else
   1114 		listf = stdout;
   1115 
   1116 	/* Traditional tar behaviour (pax wants to read file list from stdin) */
   1117 	if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
   1118 		exit(0);
   1119 	/*
   1120 	 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
   1121 	 * (unless -o specified)
   1122 	 */
   1123 	if (act == ARCHIVE || act == APPND)
   1124 		frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
   1125 	else if (Oflag) {
   1126 		if (act == EXTRACT)
   1127 			to_stdout = 1;
   1128 		else {
   1129 			tty_warn(1, "The -O/-o options are only valid when "
   1130 			    "writing or extracting an archive");
   1131 			tar_usage();
   1132 		}
   1133 	}
   1134 
   1135 	/*
   1136 	 * process the args as they are interpreted by the operation mode
   1137 	 */
   1138 	switch (act) {
   1139 	case LIST:
   1140 	case EXTRACT:
   1141 	default:
   1142 		{
   1143 			int sawpat = 0;
   1144 			int dirisnext = 0;
   1145 			char *file, *dir = NULL;
   1146 
   1147 			while (nincfiles || *argv != NULL) {
   1148 				/*
   1149 				 * If we queued up any include files,
   1150 				 * pull them in now.  Otherwise, check
   1151 				 * for -I and -C positional flags.
   1152 				 * Anything else must be a file to
   1153 				 * extract.
   1154 				 */
   1155 				if (nincfiles) {
   1156 					file = incfiles->file;
   1157 					dir = incfiles->dir;
   1158 					incfiles++;
   1159 					nincfiles--;
   1160 				} else if (strcmp(*argv, "-I") == 0) {
   1161 					if (*++argv == NULL)
   1162 						break;
   1163 					file = *argv++;
   1164 					dir = chdname;
   1165 				} else {
   1166 					file = NULL;
   1167 					dir = NULL;
   1168 				}
   1169 				if (file != NULL) {
   1170 					FILE *fp;
   1171 					char *str;
   1172 
   1173 					if (strcmp(file, "-") == 0)
   1174 						fp = stdin;
   1175 					else if ((fp = fopen(file, "r")) == NULL) {
   1176 						tty_warn(1, "Unable to open file '%s' for read", file);
   1177 						tar_usage();
   1178 					}
   1179 					while ((str = getline(fp)) != NULL) {
   1180 						if (dirisnext) {
   1181 							if (dir)
   1182 								free(dir);
   1183 							dir = str;
   1184 							dirisnext = 0;
   1185 							continue;
   1186 						}
   1187 						if (strcmp(str, "-C") == 0) {
   1188 							dirisnext = 1;
   1189 							free(str);
   1190 							continue;
   1191 						}
   1192 						if (strncmp(str, "-C ", 3) == 0) {
   1193 							if (dir)
   1194 								free(dir);
   1195 							dir = strdup(str + 3);
   1196 							free(str);
   1197 							continue;
   1198 						}
   1199 						if (pat_add(str, dir) < 0)
   1200 							tar_usage();
   1201 						free(str);
   1202 						sawpat = 1;
   1203 					}
   1204 					/* Bomb if given -C w/out a dir. */
   1205 					if (dirisnext)
   1206 						tar_usage();
   1207 					if (dir)
   1208 						free(dir);
   1209 					if (strcmp(file, "-") != 0)
   1210 						fclose(fp);
   1211 					if (getline_error) {
   1212 						tty_warn(1, "Problem with file '%s'", file);
   1213 						tar_usage();
   1214 					}
   1215 				} else if (strcmp(*argv, "-C") == 0) {
   1216 					if (*++argv == NULL)
   1217  						break;
   1218 					chdname = *argv++;
   1219 				} else if (pat_add(*argv++, chdname) < 0)
   1220 					tar_usage();
   1221 				else
   1222 					sawpat = 1;
   1223 			}
   1224 			/*
   1225 			 * if patterns were added, we are doing	chdir()
   1226 			 * on a file-by-file basis, else, just one
   1227 			 * global chdir (if any) after opening input.
   1228 			 */
   1229 			if (sawpat > 0)
   1230 				chdname = NULL;
   1231 		}
   1232 		break;
   1233 	case ARCHIVE:
   1234 	case APPND:
   1235 		if (chdname != NULL) {	/* initial chdir() */
   1236 			if (ftree_add(chdname, 1) < 0)
   1237 				tar_usage();
   1238 		}
   1239 
   1240 		while (nincfiles || *argv != NULL) {
   1241 			char *file, *dir;
   1242 
   1243 			/*
   1244 			 * If we queued up any include files, pull them in
   1245 			 * now.  Otherwise, check for -I and -C positional
   1246 			 * flags.  Anything else must be a file to include
   1247 			 * in the archive.
   1248 			 */
   1249 			if (nincfiles) {
   1250 				file = incfiles->file;
   1251 				dir = incfiles->dir;
   1252 				incfiles++;
   1253 				nincfiles--;
   1254 			} else if (strcmp(*argv, "-I") == 0) {
   1255 				if (*++argv == NULL)
   1256 					break;
   1257 				file = *argv++;
   1258 				dir = NULL;
   1259 			} else {
   1260 				file = NULL;
   1261 				dir = NULL;
   1262 			}
   1263 			if (file != NULL) {
   1264 				FILE *fp;
   1265 				char *str;
   1266 				int dirisnext = 0;
   1267 
   1268 				/* Set directory if needed */
   1269 				if (dir) {
   1270 					if (ftree_add(dir, 1) < 0)
   1271 						tar_usage();
   1272 				}
   1273 
   1274 				if (strcmp(file, "-") == 0)
   1275 					fp = stdin;
   1276 				else if ((fp = fopen(file, "r")) == NULL) {
   1277 					tty_warn(1, "Unable to open file '%s' for read", file);
   1278 					tar_usage();
   1279 				}
   1280 				while ((str = getline(fp)) != NULL) {
   1281 					if (dirisnext) {
   1282 						if (ftree_add(str, 1) < 0)
   1283 							tar_usage();
   1284 						dirisnext = 0;
   1285 						continue;
   1286 					}
   1287 					if (strcmp(str, "-C") == 0) {
   1288 						dirisnext = 1;
   1289 						continue;
   1290 					}
   1291 					if (strncmp(str, "-C ", 3) == 0) {
   1292 						if (ftree_add(str + 3, 1) < 0)
   1293 							tar_usage();
   1294 						continue;
   1295 					}
   1296 					if (ftree_add(str, 0) < 0)
   1297 						tar_usage();
   1298 				}
   1299 				/* Bomb if given -C w/out a dir. */
   1300 				if (dirisnext)
   1301 					tar_usage();
   1302 				if (strcmp(file, "-") != 0)
   1303 					fclose(fp);
   1304 				if (getline_error) {
   1305 					tty_warn(1, "Problem with file '%s'",
   1306 					    file);
   1307 					tar_usage();
   1308 				}
   1309 			} else if (strcmp(*argv, "-C") == 0) {
   1310 				if (*++argv == NULL)
   1311 					break;
   1312 				if (ftree_add(*argv++, 1) < 0)
   1313 					tar_usage();
   1314 			} else if (ftree_add(*argv++, 0) < 0)
   1315 				tar_usage();
   1316 		}
   1317 		/*
   1318 		 * no read errors allowed on updates/append operation!
   1319 		 */
   1320 		maxflt = 0;
   1321 		break;
   1322 	}
   1323 	if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
   1324 		arcname = getenv("TAPE");
   1325 		if ((arcname == NULL) || (*arcname == '\0'))
   1326 			arcname = _PATH_DEFTAPE;
   1327 	}
   1328 }
   1329 
   1330 int
   1331 mkpath(path)
   1332 	char *path;
   1333 {
   1334 	char *slash;
   1335 	int done = 0;
   1336 
   1337 	slash = path;
   1338 
   1339 	while (!done) {
   1340 		slash += strspn(slash, "/");
   1341 		slash += strcspn(slash, "/");
   1342 
   1343 		done = (*slash == '\0');
   1344 		*slash = '\0';
   1345 
   1346 		if (domkdir(path, 0777) == -1)
   1347 			goto out;
   1348 
   1349 		if (!done)
   1350 			*slash = '/';
   1351 	}
   1352 
   1353 	return 0;
   1354 out:
   1355 	/* Can't create or or not a directory */
   1356 	syswarn(1, errno, "Cannot create directory `%s'", path);
   1357 	return -1;
   1358 }
   1359 
   1360 
   1361 #ifndef NO_CPIO
   1362 struct option cpio_longopts[] = {
   1363 	{ "reset-access-time",	no_argument,		0,	'a' },
   1364 	{ "make-directories",	no_argument,		0, 	'd' },
   1365 	{ "nonmatching",	no_argument,		0,	'f' },
   1366 	{ "extract",		no_argument,		0,	'i' },
   1367 	{ "link",		no_argument,		0,	'l' },
   1368 	{ "preserve-modification-time", no_argument,	0,	'm' },
   1369 	{ "create",		no_argument,		0,	'o' },
   1370 	{ "pass-through",	no_argument,		0,	'p' },
   1371 	{ "rename",		no_argument,		0,	'r' },
   1372 	{ "list",		no_argument,		0,	't' },
   1373 	{ "unconditional",	no_argument,		0,	'u' },
   1374 	{ "verbose",		no_argument,		0,	'v' },
   1375 	{ "append",		no_argument,		0,	'A' },
   1376 	{ "pattern-file",	required_argument,	0,	'E' },
   1377 	{ "file",		required_argument,	0,	'F' },
   1378 	{ "force-local",	no_argument,		0,
   1379 						OPT_FORCE_LOCAL },
   1380 	{ "format",		required_argument,	0,	'H' },
   1381 	{ "dereference",	no_argument,		0,	'L' },
   1382 	{ "swap-halfwords",	no_argument,		0,	'S' },
   1383 	{ "insecure",		no_argument,		0,
   1384 						OPT_INSECURE },
   1385 	{ "sparse",		no_argument,		0,
   1386 						OPT_SPARSE },
   1387 
   1388 #ifdef notyet
   1389 /* Not implemented */
   1390 	{ "null",		no_argument,		0,	'0' },
   1391 	{ "swap",		no_argument,		0,	'b' },
   1392 	{ "numeric-uid-gid",	no_argument,		0,	'n' },
   1393 	{ "swap-bytes",		no_argument,		0,	's' },
   1394 	{ "message",		required_argument,	0,	'M' },
   1395 	{ "owner",		required_argument,	0	'R' },
   1396 	{ "dot",		no_argument,		0,	'V' },
   1397 	{ "block-size",		required_argument,	0,
   1398 						OPT_BLOCK_SIZE },
   1399 	{ "no-absolute-pathnames", no_argument,		0,
   1400 						OPT_NO_ABSOLUTE_PATHNAMES },
   1401 	{ "no-preserve-owner",	no_argument,		0,
   1402 						OPT_NO_PRESERVE_OWNER },
   1403 	{ "only-verify-crc",	no_argument,		0,
   1404 						OPT_ONLY_VERIFY_CRC },
   1405 	{ "rsh-command",	required_argument,	0,
   1406 						OPT_RSH_COMMAND },
   1407 	{ "version",		no_argument,		0,
   1408 						OPT_VERSION },
   1409 #endif
   1410 	{ 0,			0,			0,	0 },
   1411 };
   1412 
   1413 /*
   1414  * cpio_options()
   1415  *	look at the user specified flags. set globals as required and check if
   1416  *	the user specified a legal set of flags. If not, complain and exit
   1417  */
   1418 
   1419 static void
   1420 cpio_options(int argc, char **argv)
   1421 {
   1422 	FSUB tmp;
   1423 	unsigned int flg = 0;
   1424 	unsigned int bflg = 0;
   1425 	int c, i;
   1426 	FILE *fp;
   1427 	char *str;
   1428 
   1429 	uflag = 1;
   1430 	kflag = 1;
   1431 	pids = 1;
   1432 	pmode = 1;
   1433 	pmtime = 0;
   1434 	arcname = NULL;
   1435 	dflag = 1;
   1436 	nodirs = 1;
   1437 	/*
   1438 	 * process option flags
   1439 	 */
   1440 	while ((c = getoldopt(argc, argv,
   1441 	    "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
   1442 	    cpio_longopts, NULL)) != -1) {
   1443 		switch(c) {
   1444 		case 'a':
   1445 			/*
   1446 			 * preserve access time on filesystem nodes we read
   1447 			 */
   1448 			tflag = 1;
   1449 			flg |= TF;
   1450 			break;
   1451 #ifdef notyet
   1452 		case 'b':
   1453 			/*
   1454 			 * swap bytes and half-words when reading data
   1455 			 */
   1456 			break;
   1457 #endif
   1458 		case 'c':
   1459 			/*
   1460 			 * ASCII cpio header
   1461 			 */
   1462 			frmt = &fsub[F_SV4CPIO];
   1463 			break;
   1464 		case 'd':
   1465 			/*
   1466 			 * create directories as needed
   1467 			 * pax does this by default ..
   1468 			 */
   1469 			nodirs = 0;
   1470 			break;
   1471 		case 'f':
   1472 			/*
   1473 			 * inverse match on patterns
   1474 			 */
   1475 			cflag = 1;
   1476 			flg |= CF;
   1477 			break;
   1478 		case 'i':
   1479 			/*
   1480 			 * read the archive
   1481 			 */
   1482 			act = EXTRACT;
   1483 			flg |= RF;
   1484 			break;
   1485 #ifdef notyet
   1486 		case 'k':
   1487 			break;
   1488 #endif
   1489 		case 'l':
   1490 			/*
   1491 			 * try to link src to dest with copy (-rw)
   1492 			 */
   1493 			lflag = 1;
   1494 			flg |= LF;
   1495 			break;
   1496 		case 'm':
   1497 			/*
   1498 			 * preserve mtime
   1499 			 */
   1500 			flg |= PF;
   1501 			pmtime = 1;
   1502 			break;
   1503 		case 'o':
   1504 			/*
   1505 			 * write an archive
   1506 			 */
   1507 			act = ARCHIVE;
   1508 			frmt = &(fsub[F_SV4CRC]);
   1509 			flg |= WF;
   1510 			break;
   1511 		case 'p':
   1512 			/*
   1513 			 * cpio -p is like pax -rw
   1514 			 */
   1515 			act = COPY;
   1516 			flg |= RF | WF;
   1517 			break;
   1518 		case 'r':
   1519 			/*
   1520 			 * interactive file rename
   1521 			 */
   1522 			iflag = 1;
   1523 			flg |= IF;
   1524 			break;
   1525 #ifdef notyet
   1526 		case 's':
   1527 			/*
   1528 			 * swap bytes after reading data
   1529 			 */
   1530 			break;
   1531 #endif
   1532 		case 't':
   1533 			/*
   1534 			 * list contents of archive
   1535 			 */
   1536 			act = LIST;
   1537 			listf = stdout;
   1538 			flg &= ~RF;
   1539 			break;
   1540 		case 'u':
   1541 			/*
   1542 			 * don't ignore those older files
   1543 			 */
   1544 			uflag = 0;
   1545 			kflag = 0;
   1546 			flg |= UF;
   1547 			break;
   1548 		case 'v':
   1549 			/*
   1550 			 * verbose operation mode
   1551 			 */
   1552 			vflag = 1;
   1553 			flg |= VF;
   1554 			break;
   1555 		case 'z':
   1556 			/*
   1557 			 * use gzip.  Non standard option.
   1558 			 */
   1559 			gzip_program = GZIP_CMD;
   1560 			break;
   1561 		case 'A':
   1562 			/*
   1563 			 * append to an archive
   1564 			 */
   1565 			act = APPND;
   1566 			flg |= AF;
   1567 			break;
   1568 		case 'B':
   1569 			/*
   1570 			 * set blocksize to 5120
   1571 			 */
   1572 			blksz = 5120;
   1573 			break;
   1574 		case 'C':
   1575 			/*
   1576 			 * specify blocksize
   1577 			 */
   1578 			if ((blksz = (int)str_offt(optarg)) <= 0) {
   1579 				tty_warn(1, "Invalid block size %s", optarg);
   1580 				cpio_usage();
   1581 			}
   1582 			break;
   1583 		case 'E':
   1584 			/*
   1585 			 * file with patterns to extract or list
   1586 			 */
   1587 			if ((fp = fopen(optarg, "r")) == NULL) {
   1588 				tty_warn(1, "Unable to open file '%s' for read",
   1589 				    optarg);
   1590 				cpio_usage();
   1591 			}
   1592 			while ((str = getline(fp)) != NULL) {
   1593 				pat_add(str, NULL);
   1594 			}
   1595 			fclose(fp);
   1596 			if (getline_error) {
   1597 				tty_warn(1, "Problem with file '%s'", optarg);
   1598 				cpio_usage();
   1599 			}
   1600 			break;
   1601 		case 'H':
   1602 			/*
   1603 			 * specify an archive format on write
   1604 			 */
   1605 			tmp.name = optarg;
   1606 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
   1607 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
   1608 			if (frmt != NULL) {
   1609 				flg |= XF;
   1610 				break;
   1611 			}
   1612 			tty_warn(1, "Unknown -H format: %s", optarg);
   1613 			(void)fputs("cpio: Known -H formats are:", stderr);
   1614 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
   1615 				(void)fprintf(stderr, " %s", fsub[i].name);
   1616 			(void)fputs("\n\n", stderr);
   1617 			cpio_usage();
   1618 			break;
   1619 		case 'I':
   1620 		case 'O':
   1621 			/*
   1622 			 * filename where the archive is stored
   1623 			 */
   1624 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
   1625 				/*
   1626 				 * treat a - as stdin
   1627 				 */
   1628 				arcname = NULL;
   1629 				break;
   1630 			}
   1631 			arcname = optarg;
   1632 			break;
   1633 		case 'L':
   1634 			/*
   1635 			 * follow symlinks
   1636 			 */
   1637 			Lflag = 1;
   1638 			flg |= CLF;
   1639 			break;
   1640 #ifdef notyet
   1641 		case 'M':
   1642 			arg = optarg;
   1643 			break;
   1644 		case 'R':
   1645 			arg = optarg;
   1646 			break;
   1647 #endif
   1648 		case 'S':
   1649 			/*
   1650 			 * swap halfwords after reading data
   1651 			 */
   1652 			cpio_swp_head = 1;
   1653 			break;
   1654 #ifdef notyet
   1655 		case 'V':
   1656 			break;
   1657 #endif
   1658 		case 'Z':
   1659 			/*
   1660 			 * use compress.  Non standard option.
   1661 			 */
   1662 			gzip_program = COMPRESS_CMD;
   1663 			break;
   1664 		case '6':
   1665 			/*
   1666 			 * process Version 6 cpio format
   1667 			 */
   1668 			frmt = &(fsub[F_BCPIO]);
   1669 		case OPT_FORCE_LOCAL:
   1670 			forcelocal = 1;
   1671 			break;
   1672 		case OPT_INSECURE:
   1673 			secure = 0;
   1674 			break;
   1675 
   1676 		case OPT_SPARSE:
   1677 			/* do nothing; we already generate sparse files */
   1678 			break;
   1679 		default:
   1680 			cpio_usage();
   1681 			break;
   1682 		}
   1683 	}
   1684 
   1685 	/*
   1686 	 * figure out the operation mode of cpio. check that we have not been
   1687 	 * given a bogus set of flags for the operation mode.
   1688 	 */
   1689 	if (ISLIST(flg)) {
   1690 		act = LIST;
   1691 		bflg = flg & BDLIST;
   1692 	} else if (ISEXTRACT(flg)) {
   1693 		act = EXTRACT;
   1694 		bflg = flg & BDEXTR;
   1695 	} else if (ISARCHIVE(flg)) {
   1696 		act = ARCHIVE;
   1697 		bflg = flg & BDARCH;
   1698 	} else if (ISAPPND(flg)) {
   1699 		act = APPND;
   1700 		bflg = flg & BDARCH;
   1701 	} else if (ISCOPY(flg)) {
   1702 		act = COPY;
   1703 		bflg = flg & BDCOPY;
   1704 	} else
   1705 		cpio_usage();
   1706 	if (bflg) {
   1707 		cpio_usage();
   1708 	}
   1709 
   1710 	/*
   1711 	 * if we are writing (ARCHIVE) we use the default format if the user
   1712 	 * did not specify a format. when we write during an APPEND, we will
   1713 	 * adopt the format of the existing archive if none was supplied.
   1714 	 */
   1715 	if (!(flg & XF) && (act == ARCHIVE))
   1716 		frmt = &(fsub[F_BCPIO]);
   1717 
   1718 	/*
   1719 	 * process the args as they are interpreted by the operation mode
   1720 	 */
   1721 	switch (act) {
   1722 	case LIST:
   1723 	case EXTRACT:
   1724 		for (; optind < argc; optind++)
   1725 			if (pat_add(argv[optind], 0) < 0)
   1726 				cpio_usage();
   1727 		break;
   1728 	case COPY:
   1729 		if (optind >= argc) {
   1730 			tty_warn(0, "Destination directory was not supplied");
   1731 			cpio_usage();
   1732 		}
   1733 		--argc;
   1734 		dirptr = argv[argc];
   1735 		/* FALLTHROUGH */
   1736 	case ARCHIVE:
   1737 	case APPND:
   1738 		if (argc != optind) {
   1739 			for (; optind < argc; optind++)
   1740 				if (ftree_add(argv[optind], 0) < 0)
   1741 					cpio_usage();
   1742 			break;
   1743 		}
   1744 		/*
   1745 		 * no read errors allowed on updates/append operation!
   1746 		 */
   1747 		maxflt = 0;
   1748 		while ((str = getline(stdin)) != NULL) {
   1749 			ftree_add(str, 0);
   1750 		}
   1751 		if (getline_error) {
   1752 			tty_warn(1, "Problem while reading stdin");
   1753 			cpio_usage();
   1754 		}
   1755 		break;
   1756 	default:
   1757 		cpio_usage();
   1758 		break;
   1759 	}
   1760 }
   1761 #endif
   1762 
   1763 /*
   1764  * printflg()
   1765  *	print out those invalid flag sets found to the user
   1766  */
   1767 
   1768 static void
   1769 printflg(unsigned int flg)
   1770 {
   1771 	int nxt;
   1772 
   1773 	(void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
   1774 	while ((nxt = ffs(flg)) != 0) {
   1775 		flg &= ~(1 << (nxt - 1));
   1776 		(void)fprintf(stderr, " -%c", flgch[nxt - 1]);
   1777 	}
   1778 	(void)putc('\n', stderr);
   1779 }
   1780 
   1781 /*
   1782  * c_frmt()
   1783  *	comparison routine used by bsearch to find the format specified
   1784  *	by the user
   1785  */
   1786 
   1787 static int
   1788 c_frmt(const void *a, const void *b)
   1789 {
   1790 	return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
   1791 }
   1792 
   1793 /*
   1794  * opt_next()
   1795  *	called by format specific options routines to get each format specific
   1796  *	flag and value specified with -o
   1797  * Return:
   1798  *	pointer to next OPLIST entry or NULL (end of list).
   1799  */
   1800 
   1801 OPLIST *
   1802 opt_next(void)
   1803 {
   1804 	OPLIST *opt;
   1805 
   1806 	if ((opt = ophead) != NULL)
   1807 		ophead = ophead->fow;
   1808 	return opt;
   1809 }
   1810 
   1811 /*
   1812  * bad_opt()
   1813  *	generic routine used to complain about a format specific options
   1814  *	when the format does not support options.
   1815  */
   1816 
   1817 int
   1818 bad_opt(void)
   1819 {
   1820 	OPLIST *opt;
   1821 
   1822 	if (ophead == NULL)
   1823 		return 0;
   1824 	/*
   1825 	 * print all we were given
   1826 	 */
   1827 	tty_warn(1," These format options are not supported for %s",
   1828 	    frmt->name);
   1829 	while ((opt = opt_next()) != NULL)
   1830 		(void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
   1831 	if (strcmp(NM_TAR, argv0) == 0)
   1832 		tar_usage();
   1833 #ifndef NO_CPIO
   1834 	else if (strcmp(NM_CPIO, argv0) == 0)
   1835 		cpio_usage();
   1836 #endif
   1837 	else
   1838 		pax_usage();
   1839 	return 0;
   1840 }
   1841 
   1842 /*
   1843  * opt_add()
   1844  *	breaks the value supplied to -o into a option name and value. options
   1845  *	are given to -o in the form -o name-value,name=value
   1846  *	multiple -o may be specified.
   1847  * Return:
   1848  *	0 if format in name=value format, -1 if -o is passed junk
   1849  */
   1850 
   1851 int
   1852 opt_add(const char *str)
   1853 {
   1854 	OPLIST *opt;
   1855 	char *frpt;
   1856 	char *pt;
   1857 	char *endpt;
   1858 	char *dstr;
   1859 
   1860 	if ((str == NULL) || (*str == '\0')) {
   1861 		tty_warn(0, "Invalid option name");
   1862 		return -1;
   1863 	}
   1864 	if ((dstr = strdup(str)) == NULL) {
   1865 		tty_warn(0, "Unable to allocate space for option list");
   1866 		return -1;
   1867 	}
   1868 	frpt = endpt = dstr;
   1869 
   1870 	/*
   1871 	 * break into name and values pieces and stuff each one into a
   1872 	 * OPLIST structure. When we know the format, the format specific
   1873 	 * option function will go through this list
   1874 	 */
   1875 	while ((frpt != NULL) && (*frpt != '\0')) {
   1876 		if ((endpt = strchr(frpt, ',')) != NULL)
   1877 			*endpt = '\0';
   1878 		if ((pt = strchr(frpt, '=')) == NULL) {
   1879 			tty_warn(0, "Invalid options format");
   1880 			free(dstr);
   1881 			return -1;
   1882 		}
   1883 		if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
   1884 			tty_warn(0, "Unable to allocate space for option list");
   1885 			free(dstr);
   1886 			return -1;
   1887 		}
   1888 		*pt++ = '\0';
   1889 		opt->name = frpt;
   1890 		opt->value = pt;
   1891 		opt->fow = NULL;
   1892 		if (endpt != NULL)
   1893 			frpt = endpt + 1;
   1894 		else
   1895 			frpt = NULL;
   1896 		if (ophead == NULL) {
   1897 			optail = ophead = opt;
   1898 			continue;
   1899 		}
   1900 		optail->fow = opt;
   1901 		optail = opt;
   1902 	}
   1903 	return 0;
   1904 }
   1905 
   1906 /*
   1907  * str_offt()
   1908  *	Convert an expression of the following forms to an off_t > 0.
   1909  *	1) A positive decimal number.
   1910  *	2) A positive decimal number followed by a b (mult by 512).
   1911  *	3) A positive decimal number followed by a k (mult by 1024).
   1912  *	4) A positive decimal number followed by a m (mult by 512).
   1913  *	5) A positive decimal number followed by a w (mult by sizeof int)
   1914  *	6) Two or more positive decimal numbers (with/without k,b or w).
   1915  *	   separated by x (also * for backwards compatibility), specifying
   1916  *	   the product of the indicated values.
   1917  * Return:
   1918  *	0 for an error, a positive value o.w.
   1919  */
   1920 
   1921 static off_t
   1922 str_offt(char *val)
   1923 {
   1924 	char *expr;
   1925 	off_t num, t;
   1926 
   1927 	num = STRTOOFFT(val, &expr, 0);
   1928 	if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
   1929 		return 0;
   1930 
   1931 	switch(*expr) {
   1932 	case 'b':
   1933 		t = num;
   1934 		num *= 512;
   1935 		if (t > num)
   1936 			return 0;
   1937 		++expr;
   1938 		break;
   1939 	case 'k':
   1940 		t = num;
   1941 		num *= 1024;
   1942 		if (t > num)
   1943 			return 0;
   1944 		++expr;
   1945 		break;
   1946 	case 'm':
   1947 		t = num;
   1948 		num *= 1048576;
   1949 		if (t > num)
   1950 			return 0;
   1951 		++expr;
   1952 		break;
   1953 	case 'w':
   1954 		t = num;
   1955 		num *= sizeof(int);
   1956 		if (t > num)
   1957 			return 0;
   1958 		++expr;
   1959 		break;
   1960 	}
   1961 
   1962 	switch(*expr) {
   1963 		case '\0':
   1964 			break;
   1965 		case '*':
   1966 		case 'x':
   1967 			t = num;
   1968 			num *= str_offt(expr + 1);
   1969 			if (t > num)
   1970 				return 0;
   1971 			break;
   1972 		default:
   1973 			return 0;
   1974 	}
   1975 	return num;
   1976 }
   1977 
   1978 char *
   1979 getline(FILE *f)
   1980 {
   1981 	char *name, *temp;
   1982 	size_t len;
   1983 
   1984 	name = fgetln(f, &len);
   1985 	if (!name) {
   1986 		getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
   1987 		return 0;
   1988 	}
   1989 	if (name[len-1] != '\n')
   1990 		len++;
   1991 	temp = malloc(len);
   1992 	if (!temp) {
   1993 		getline_error = GETLINE_OUT_OF_MEM;
   1994 		return 0;
   1995 	}
   1996 	memcpy(temp, name, len-1);
   1997 	temp[len-1] = 0;
   1998 	return temp;
   1999 }
   2000 
   2001 /*
   2002  * no_op()
   2003  *	for those option functions where the archive format has nothing to do.
   2004  * Return:
   2005  *	0
   2006  */
   2007 
   2008 static int
   2009 no_op(void)
   2010 {
   2011 	return 0;
   2012 }
   2013 
   2014 /*
   2015  * pax_usage()
   2016  *	print the usage summary to the user
   2017  */
   2018 
   2019 void
   2020 pax_usage(void)
   2021 {
   2022 	fprintf(stderr,
   2023 "usage: pax [-cdjnvzO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
   2024 "           [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
   2025 "           [pattern ...]\n");
   2026 	fprintf(stderr,
   2027 "       pax -r [-cdijknuvzADOYZ] [-E limit] [-f archive] [-N dbdir]\n"
   2028 "           [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
   2029 "           [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
   2030 	fprintf(stderr,
   2031 "       pax -w [-dijtuvzAHLMOPX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
   2032 "           [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
   2033 "           [-U user] ... [-G group] ...\n"
   2034 "           [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
   2035 	fprintf(stderr,
   2036 "       pax -r -w [-dijklntuvzADHLMOPXYZ] [-N dbdir] [-p string] ...\n"
   2037 "           [-s replstr] ... [-U user] ... [-G group] ...\n"
   2038 "           [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
   2039 	exit(1);
   2040 	/* NOTREACHED */
   2041 }
   2042 
   2043 /*
   2044  * tar_usage()
   2045  *	print the usage summary to the user
   2046  */
   2047 
   2048 void
   2049 tar_usage(void)
   2050 {
   2051 	(void)fputs("usage: tar [-]{crtux}[-befhjlmopqvwzHOPSXZ014578] [archive] "
   2052 		    "[blocksize]\n"
   2053 		    "           [-C directory] [-T file] [-s replstr] "
   2054 		    "[file ...]\n", stderr);
   2055 	exit(1);
   2056 	/* NOTREACHED */
   2057 }
   2058 
   2059 #ifndef NO_CPIO
   2060 /*
   2061  * cpio_usage()
   2062  *	print the usage summary to the user
   2063  */
   2064 
   2065 void
   2066 cpio_usage(void)
   2067 {
   2068 
   2069 	(void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
   2070 		    "[-H format] [-O archive]\n"
   2071 		    "               < name-list [> archive]\n"
   2072 		    "       cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
   2073 		    "[-F archive] [-H format] \n"
   2074 		    "               [-I archive] "
   2075 		    "[pattern ...] [< archive]\n"
   2076 		    "       cpio -p [-adlLmuv] destination-directory "
   2077 		    "< name-list\n", stderr);
   2078 	exit(1);
   2079 	/* NOTREACHED */
   2080 }
   2081 #endif
   2082