Home | History | Annotate | Line # | Download | only in pax
options.c revision 1.29
      1 /*	$NetBSD: options.c,v 1.29 2000/07/04 17:17:49 thorpej 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. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef 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.29 2000/07/04 17:17:49 thorpej 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/mtio.h>
     53 #include <sys/param.h>
     54 #include <stdio.h>
     55 #include <ctype.h>
     56 #include <string.h>
     57 #include <unistd.h>
     58 #include <stdlib.h>
     59 #include <limits.h>
     60 #include <getopt.h>
     61 #include "pax.h"
     62 #include "options.h"
     63 #include "cpio.h"
     64 #include "tar.h"
     65 #include "extern.h"
     66 
     67 /*
     68  * Routines which handle command line options
     69  */
     70 
     71 int cpio_mode;			/* set if we are in cpio mode */
     72 
     73 static int nopids;		/* tar mode: suppress "pids" for -p option */
     74 static char *flgch = FLGCH;	/* list of all possible flags (pax) */
     75 static OPLIST *ophead = NULL;	/* head for format specific options -x */
     76 static OPLIST *optail = NULL;	/* option tail */
     77 static char *firstminusC;	/* first -C argument encountered. */
     78 
     79 static int no_op __P((void));
     80 static void printflg __P((unsigned int));
     81 static int c_frmt __P((const void *, const void *));
     82 static off_t str_offt __P((char *));
     83 static void pax_options __P((int, char **));
     84 static void pax_usage __P((void));
     85 static void tar_options __P((int, char **));
     86 static void tar_usage __P((void));
     87 static void cpio_options __P((int, char **));
     88 static void cpio_usage __P((void));
     89 
     90 static void checkpositionalminusC __P((char ***, int (*)(char *, int)));
     91 
     92 #define GZIP_CMD	"gzip"		/* command to run as gzip */
     93 #define COMPRESS_CMD	"compress"	/* command to run as compress */
     94 
     95 /*
     96  *	Format specific routine table - MUST BE IN SORTED ORDER BY NAME
     97  *	(see pax.h for description of each function)
     98  *
     99  *	name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
    100  *	read, end_read, st_write, write, end_write, trail,
    101  *	rd_data, wr_data, options
    102  */
    103 
    104 FSUB fsub[] = {
    105 /* 0: OLD BINARY CPIO */
    106 	{ "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
    107 	bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
    108 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    109 
    110 /* 1: OLD OCTAL CHARACTER CPIO */
    111 	{ "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
    112 	cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
    113 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    114 
    115 /* 2: SVR4 HEX CPIO */
    116 	{ "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
    117 	vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
    118 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    119 
    120 /* 3: SVR4 HEX CPIO WITH CRC */
    121 	{ "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
    122 	vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
    123 	cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
    124 
    125 /* 4: OLD TAR */
    126 	{ "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
    127 	tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
    128 	NULL, rd_wrfile, wr_rdfile, tar_opt },
    129 
    130 /* 5: POSIX USTAR */
    131 	{ "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
    132 	ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
    133 	NULL, rd_wrfile, wr_rdfile, bad_opt }
    134 };
    135 #define F_BCPIO		0	/* old binary cpio format */
    136 #define F_CPIO		1	/* old octal character cpio format */
    137 #define F_SV4CPIO	2	/* SVR4 hex cpio format */
    138 #define F_SV4CRC	3	/* SVR4 hex with crc cpio format */
    139 #define F_TAR		4	/* old V7 UNIX tar format */
    140 #define F_USTAR		5	/* ustar format */
    141 #define DEFLT		F_USTAR	/* default write format from list above */
    142 
    143 /*
    144  * ford is the archive search order used by get_arc() to determine what kind
    145  * of archive we are dealing with. This helps to properly id  archive formats
    146  * some formats may be subsets of others....
    147  */
    148 int ford[] = {F_USTAR, F_TAR, F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO, -1};
    149 
    150 /*
    151  * options()
    152  *	figure out if we are pax, tar or cpio. Call the appropriate options
    153  *	parser
    154  */
    155 
    156 #if __STDC__
    157 void
    158 options(int argc, char **argv)
    159 #else
    160 void
    161 options(argc, argv)
    162 	int argc;
    163 	char **argv;
    164 #endif
    165 {
    166 
    167 	/*
    168 	 * Are we acting like pax, tar or cpio (based on argv[0])
    169 	 */
    170 	if ((argv0 = strrchr(argv[0], '/')) != NULL)
    171 		argv0++;
    172 	else
    173 		argv0 = argv[0];
    174 
    175 	if (strcmp(NM_TAR, argv0) == 0)
    176 		tar_options(argc, argv);
    177 	else if (strcmp(NM_CPIO, argv0) == 0)
    178 		cpio_options(argc, argv);
    179 	else {
    180 		argv0 = NM_PAX;
    181 		pax_options(argc, argv);
    182 	}
    183 }
    184 
    185 /*
    186  * pax_options()
    187  *	look at the user specified flags. set globals as required and check if
    188  *	the user specified a legal set of flags. If not, complain and exit
    189  */
    190 
    191 #if __STDC__
    192 static void
    193 pax_options(int argc, char **argv)
    194 #else
    195 static void
    196 pax_options(argc, argv)
    197 	int argc;
    198 	char **argv;
    199 #endif
    200 {
    201 	int c;
    202 	int i;
    203 	unsigned int flg = 0;
    204 	unsigned int bflg = 0;
    205 	char *pt;
    206 	FSUB tmp;
    207 
    208 	/*
    209 	 * process option flags
    210 	 */
    211 	while ((c=getopt(argc,argv,"ab:cdf:iklno:p:rs:tuvwx:zAB:DE:G:HLOPT:U:XYZ"))
    212 	    != -1) {
    213 		switch (c) {
    214 		case 'a':
    215 			/*
    216 			 * append
    217 			 */
    218 			flg |= AF;
    219 			break;
    220 		case 'b':
    221 			/*
    222 			 * specify blocksize
    223 			 */
    224 			flg |= BF;
    225 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
    226 				tty_warn(1, "Invalid block size %s", optarg);
    227 				pax_usage();
    228 			}
    229 			break;
    230 		case 'c':
    231 			/*
    232 			 * inverse match on patterns
    233 			 */
    234 			cflag = 1;
    235 			flg |= CF;
    236 			break;
    237 		case 'd':
    238 			/*
    239 			 * match only dir on extract, not the subtree at dir
    240 			 */
    241 			dflag = 1;
    242 			flg |= DF;
    243 			break;
    244 		case 'f':
    245 			/*
    246 			 * filename where the archive is stored
    247 			 */
    248 			arcname = optarg;
    249 			flg |= FF;
    250 			break;
    251 		case 'i':
    252 			/*
    253 			 * interactive file rename
    254 			 */
    255 			iflag = 1;
    256 			flg |= IF;
    257 			break;
    258 		case 'k':
    259 			/*
    260 			 * do not clobber files that exist
    261 			 */
    262 			kflag = 1;
    263 			flg |= KF;
    264 			break;
    265 		case 'l':
    266 			/*
    267 			 * try to link src to dest with copy (-rw)
    268 			 */
    269 			lflag = 1;
    270 			flg |= LF;
    271 			break;
    272 		case 'n':
    273 			/*
    274 			 * select first match for a pattern only
    275 			 */
    276 			nflag = 1;
    277 			flg |= NF;
    278 			break;
    279 		case 'o':
    280 			/*
    281 			 * pass format specific options
    282 			 */
    283 			flg |= OF;
    284 			if (opt_add(optarg) < 0)
    285 				pax_usage();
    286 			break;
    287 		case 'p':
    288 			/*
    289 			 * specify file characteristic options
    290 			 */
    291 			for (pt = optarg; *pt != '\0'; ++pt) {
    292 				switch(*pt) {
    293 				case 'a':
    294 					/*
    295 					 * do not preserve access time
    296 					 */
    297 					patime = 0;
    298 					break;
    299 				case 'e':
    300 					/*
    301 					 * preserve user id, group id, file
    302 					 * mode, access/modification times
    303 					 * and file flags.
    304 					 */
    305 					pids = 1;
    306 					pmode = 1;
    307 					patime = 1;
    308 					pmtime = 1;
    309 					pfflags = 1;
    310 					break;
    311 #if 0
    312 				case 'f':
    313 					/*
    314 					 * do not preserve file flags
    315 					 */
    316 					pfflags = 0;
    317 					break;
    318 #endif
    319 				case 'm':
    320 					/*
    321 					 * do not preserve modification time
    322 					 */
    323 					pmtime = 0;
    324 					break;
    325 				case 'o':
    326 					/*
    327 					 * preserve uid/gid
    328 					 */
    329 					pids = 1;
    330 					break;
    331 				case 'p':
    332 					/*
    333 					 * preserver file mode bits
    334 					 */
    335 					pmode = 1;
    336 					break;
    337 				default:
    338 					tty_warn(1,
    339 					    "Invalid -p string: %c", *pt);
    340 					pax_usage();
    341 					break;
    342 				}
    343 			}
    344 			flg |= PF;
    345 			break;
    346 		case 'r':
    347 			/*
    348 			 * read the archive
    349 			 */
    350 			flg |= RF;
    351 			break;
    352 		case 's':
    353 			/*
    354 			 * file name substitution name pattern
    355 			 */
    356 			if (rep_add(optarg) < 0) {
    357 				pax_usage();
    358 				break;
    359 			}
    360 			flg |= SF;
    361 			break;
    362 		case 't':
    363 			/*
    364 			 * preserve access time on filesystem nodes we read
    365 			 */
    366 			tflag = 1;
    367 			flg |= TF;
    368 			break;
    369 		case 'u':
    370 			/*
    371 			 * ignore those older files
    372 			 */
    373 			uflag = 1;
    374 			flg |= UF;
    375 			break;
    376 		case 'v':
    377 			/*
    378 			 * verbose operation mode
    379 			 */
    380 			vflag = 1;
    381 			flg |= VF;
    382 			break;
    383 		case 'w':
    384 			/*
    385 			 * write an archive
    386 			 */
    387 			flg |= WF;
    388 			break;
    389 		case 'x':
    390 			/*
    391 			 * specify an archive format on write
    392 			 */
    393 			tmp.name = optarg;
    394 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
    395 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
    396 			if (frmt != NULL) {
    397 				flg |= XF;
    398 				break;
    399 			}
    400 			tty_warn(1, "Unknown -x format: %s", optarg);
    401 			(void)fputs("pax: Known -x formats are:", stderr);
    402 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
    403 				(void)fprintf(stderr, " %s", fsub[i].name);
    404 			(void)fputs("\n\n", stderr);
    405 			pax_usage();
    406 			break;
    407 		case 'z':
    408 			/*
    409 			 * use gzip.  Non standard option.
    410 			 */
    411 			zflag = 1;
    412 			gzip_program = GZIP_CMD;
    413 			break;
    414 		case 'A':
    415 			Aflag = 1;
    416 			flg |= CAF;
    417 			break;
    418 		case 'B':
    419 			/*
    420 			 * non-standard option on number of bytes written on a
    421 			 * single archive volume.
    422 			 */
    423 			if ((wrlimit = str_offt(optarg)) <= 0) {
    424 				tty_warn(1, "Invalid write limit %s", optarg);
    425 				pax_usage();
    426 			}
    427 			if (wrlimit % BLKMULT) {
    428 				tty_warn(1,
    429 				    "Write limit is not a %d byte multiple",
    430 				    BLKMULT);
    431 				pax_usage();
    432 			}
    433 			flg |= CBF;
    434 			break;
    435 		case 'D':
    436 			/*
    437 			 * On extraction check file inode change time before the
    438 			 * modification of the file name. Non standard option.
    439 			 */
    440 			Dflag = 1;
    441 			flg |= CDF;
    442 			break;
    443 		case 'E':
    444 			/*
    445 			 * non-standard limit on read faults
    446 			 * 0 indicates stop after first error, values
    447 			 * indicate a limit, "NONE" try forever
    448 			 */
    449 			flg |= CEF;
    450 			if (strcmp(NONE, optarg) == 0)
    451 				maxflt = -1;
    452 			else if ((maxflt = atoi(optarg)) < 0) {
    453 				tty_warn(1,
    454 				    "Error count value must be positive");
    455 				pax_usage();
    456 			}
    457 			break;
    458 		case 'G':
    459 			/*
    460 			 * non-standard option for selecting files within an
    461 			 * archive by group (gid or name)
    462 			 */
    463 			if (grp_add(optarg) < 0) {
    464 				pax_usage();
    465 				break;
    466 			}
    467 			flg |= CGF;
    468 			break;
    469 		case 'H':
    470 			/*
    471 			 * follow command line symlinks only
    472 			 */
    473 			Hflag = 1;
    474 			flg |= CHF;
    475 			break;
    476 		case 'L':
    477 			/*
    478 			 * follow symlinks
    479 			 */
    480 			Lflag = 1;
    481 			flg |= CLF;
    482 			break;
    483 		case 'O':
    484 			/*
    485 			 * Force one volume.  Non standard option.
    486 			 */
    487 			force_one_volume = 1;
    488 			break;
    489 		case 'P':
    490 			/*
    491 			 * do NOT follow symlinks (default)
    492 			 */
    493 			Lflag = 0;
    494 			flg |= CPF;
    495 			break;
    496 		case 'T':
    497 			/*
    498 			 * non-standard option for selecting files within an
    499 			 * archive by modification time range (lower,upper)
    500 			 */
    501 			if (trng_add(optarg) < 0) {
    502 				pax_usage();
    503 				break;
    504 			}
    505 			flg |= CTF;
    506 			break;
    507 		case 'U':
    508 			/*
    509 			 * non-standard option for selecting files within an
    510 			 * archive by user (uid or name)
    511 			 */
    512 			if (usr_add(optarg) < 0) {
    513 				pax_usage();
    514 				break;
    515 			}
    516 			flg |= CUF;
    517 			break;
    518 		case 'X':
    519 			/*
    520 			 * do not pass over mount points in the file system
    521 			 */
    522 			Xflag = 1;
    523 			flg |= CXF;
    524 			break;
    525 		case 'Y':
    526 			/*
    527 			 * On extraction check file inode change time after the
    528 			 * modification of the file name. Non standard option.
    529 			 */
    530 			Yflag = 1;
    531 			flg |= CYF;
    532 			break;
    533 		case 'Z':
    534 			/*
    535 			 * On extraction check modification time after the
    536 			 * modification of the file name. Non standard option.
    537 			 */
    538 			Zflag = 1;
    539 			flg |= CZF;
    540 			break;
    541 		case '?':
    542 		default:
    543 			pax_usage();
    544 			break;
    545 		}
    546 	}
    547 
    548 	/*
    549 	 * figure out the operation mode of pax read,write,extract,copy,append
    550 	 * or list. check that we have not been given a bogus set of flags
    551 	 * for the operation mode.
    552 	 */
    553 	if (ISLIST(flg)) {
    554 		act = LIST;
    555 		bflg = flg & BDLIST;
    556 	} else if (ISEXTRACT(flg)) {
    557 		act = EXTRACT;
    558 		bflg = flg & BDEXTR;
    559 	} else if (ISARCHIVE(flg)) {
    560 		act = ARCHIVE;
    561 		bflg = flg & BDARCH;
    562 	} else if (ISAPPND(flg)) {
    563 		act = APPND;
    564 		bflg = flg & BDARCH;
    565 	} else if (ISCOPY(flg)) {
    566 		act = COPY;
    567 		bflg = flg & BDCOPY;
    568 	} else
    569 		pax_usage();
    570 	if (bflg) {
    571 		printflg(flg);
    572 		pax_usage();
    573 	}
    574 
    575 	/*
    576 	 * if we are writing (ARCHIVE) we use the default format if the user
    577 	 * did not specify a format. when we write during an APPEND, we will
    578 	 * adopt the format of the existing archive if none was supplied.
    579 	 */
    580 	if (!(flg & XF) && (act == ARCHIVE))
    581 		frmt = &(fsub[DEFLT]);
    582 
    583 	/*
    584 	 * process the args as they are interpreted by the operation mode
    585 	 */
    586 	switch (act) {
    587 	case LIST:
    588 	case EXTRACT:
    589 		for (; optind < argc; optind++)
    590 			if (pat_add(argv[optind], 0) < 0)
    591 				pax_usage();
    592 		break;
    593 	case COPY:
    594 		if (optind >= argc) {
    595 			tty_warn(0, "Destination directory was not supplied");
    596 			pax_usage();
    597 		}
    598 		--argc;
    599 		dirptr = argv[argc];
    600 		/* FALLTHROUGH */
    601 	case ARCHIVE:
    602 	case APPND:
    603 		for (; optind < argc; optind++)
    604 			if (ftree_add(argv[optind], 0) < 0)
    605 				pax_usage();
    606 		/*
    607 		 * no read errors allowed on updates/append operation!
    608 		 */
    609 		maxflt = 0;
    610 		break;
    611 	}
    612 }
    613 
    614 
    615 /*
    616  * tar_options()
    617  *	look at the user specified flags. set globals as required and check if
    618  *	the user specified a legal set of flags. If not, complain and exit
    619  */
    620 
    621 #define	OPT_USE_COMPRESS_PROGRAM	0
    622 #define	OPT_CHECKPOINT			1
    623 #define	OPT_UNLINK			2
    624 #define	OPT_HELP			3
    625 #define	OPT_ATIME_PRESERVE		4
    626 #define	OPT_FAST_READ			5
    627 #define	OPT_IGNORE_FAILED_READ		6
    628 #define	OPT_REMOVE_FILES		7
    629 #define	OPT_NULL			8
    630 #define	OPT_TOTALS			9
    631 #define	OPT_VERSION			10
    632 #define	OPT_EXCLUDE			11
    633 #define	OPT_BLOCK_COMPRESS		12
    634 #define	OPT_NORECURSE			13
    635 
    636 struct option tar_longopts[] = {
    637 	{ "block-size",		required_argument,	0,	'b' },
    638 	{ "create",		no_argument,		0,	'c' },	/* F */
    639 	/* -e -- no corresponding long option */
    640 	{ "file",		required_argument,	0,	'f' },
    641 	{ "dereference",	no_argument,		0,	'h' },
    642 	{ "one-file-system",	no_argument,		0,	'l' },
    643 	{ "modification-time",	no_argument,		0,	'm' },
    644 	{ "old-archive",	no_argument,		0,	'o' },
    645 	{ "portability",	no_argument,		0,	'o' },
    646 	{ "same-permissions",	no_argument,		0,	'p' },
    647 	{ "preserve-permissions", no_argument,		0,	'p' },
    648 	{ "preserve",		no_argument,		0,	'p' },
    649 	{ "append",		no_argument,		0,	'r' },	/* F */
    650 	{ "update",		no_argument,		0,	'u' },	/* F */
    651 	{ "list",		no_argument,		0,	't' },	/* F */
    652 	{ "verbose",		no_argument,		0,	'v' },
    653 	{ "interactive",	no_argument,		0,	'w' },
    654 	{ "confirmation",	no_argument,		0,	'w' },
    655 	{ "extract",		no_argument,		0,	'x' },	/* F */
    656 	{ "get",		no_argument,		0,	'x' },	/* F */
    657 	{ "gzip",		no_argument,		0,	'z' },
    658 	{ "gunzip",		no_argument,		0,	'z' },
    659 	{ "read-full-blocks",	no_argument,		0,	'B' },
    660 	{ "directory",		required_argument,	0,	'C' },
    661 	{ "tape-length",	required_argument,	0,	'L' },
    662 	{ "absolute-paths",	no_argument,		0,	'P' },
    663 	{ "exclude-from",	required_argument,	0,	'X' },
    664 	{ "compress",		no_argument,		0,	'Z' },
    665 	{ "uncompress",		no_argument,		0,	'Z' },
    666 	{ "unlink",		no_argument,		0,
    667 						OPT_UNLINK },
    668 #if 0 /* Not implemented */
    669 	{ "catenate",		no_argument,		0,	'A' },	/* F */
    670 	{ "concatenate",	no_argument,		0,	'A' },	/* F */
    671 	{ "diff",		no_argument,		0,	'd' },	/* F */
    672 	{ "compare",		no_argument,		0,	'd' },	/* F */
    673 	{ "use-compress-program", required_argument,	0,
    674 						OPT_USE_COMPRESS_PROGRAM },
    675 	{ "checkpoint",		no_argument,		0,
    676 						OPT_CHECKPOINT },
    677 	{ "help",		no_argument,
    678 						OPT_HELP },
    679 	{ "atime-preserve",	no_argument,
    680 						OPT_ATIME_PRESERVE },
    681 	{ "info-script",	required_argument,	0,	'F' },
    682 	{ "new-volume-script",	required_argument,	0,	'F' },
    683 	{ "fast-read",		no_argument,		0,
    684 						OPT_FAST_READ },
    685 	{ "incremental",	no_argument,		0,	'G' },
    686 	{ "listed-incremental",	required_argument,	0,	'g' },
    687 	{ "ignore-zeros",	no_argument,		0,	'i' },
    688 	{ "ignore-failed-read",	no_argument,		0,
    689 						OPT_IGNORE_FAILED_READ },
    690 	{ "keep-old-files",	no_argument,		0,	'k' },
    691 	{ "starting-file",	no_argument,		0,	'K' },
    692 	{ "multi-volume",	no_argument,		0,	'M' },
    693 	{ "after-date",		required_argument,	0,	'N' },
    694 	{ "newer",		required_argument,	0,	'N' },
    695 	{ "to-stdout",		no_argument,		0,	'O' },
    696 	{ "record-number",	no_argument,		0,	'R' },
    697 	{ "remove-files",	no_argument,		0,
    698 						OPT_REMOVE_FILES },
    699 	{ "same-order",		no_argument,		0,	's' },
    700 	{ "preserve-order",	no_argument,		0,	's' },
    701 	{ "sparse",		no_argument,		0,	'S' },
    702 	{ "files-from",		no_argument,		0,	'T' },
    703 	{ "null",		no_argument,		0,
    704 						OPT_NULL },
    705 	{ "totals",		no_argument,		0,
    706 						OPT_TOTALS },
    707 	{ "volume-name",	required_argument,	0,	'V' },
    708 	{ "label",		required_argument,	0,	'V' },
    709 	{ "version",		no_argument,		0,
    710 						OPT_VERSION },
    711 	{ "verify",		no_argument,		0,	'W' },
    712 	{ "exclude",		required_argument,	0,
    713 						OPT_EXCLUDE },
    714 	{ "block-compress",	no_argument,		0,
    715 						OPT_BLOCK_COMPRESS },
    716 	{ "norecurse",		no_argument,		0,
    717 						OPT_NORECURSE },
    718 #endif
    719 	{ 0,			0,			0,	0 },
    720 };
    721 
    722 #if __STDC__
    723 static void
    724 tar_options(int argc, char **argv)
    725 #else
    726 static void
    727 tar_options(argc, argv)
    728 	int argc;
    729 	char **argv;
    730 #endif
    731 {
    732 	int c;
    733 	int fstdin = 0;
    734 
    735 	/*
    736 	 * process option flags
    737 	 */
    738 	while ((c = getoldopt(argc, argv, "b:cef:hlmoprutvwxzBC:LPX:Z014578",
    739 	    tar_longopts, NULL))
    740 	    != -1)  {
    741 		switch(c) {
    742 		case 'b':
    743 			/*
    744 			 * specify blocksize
    745 			 */
    746 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
    747 				tty_warn(1, "Invalid block size %s", optarg);
    748 				tar_usage();
    749 			}
    750 			break;
    751 		case 'c':
    752 			/*
    753 			 * create an archive
    754 			 */
    755 			act = ARCHIVE;
    756 			break;
    757 		case 'C':
    758 			/*
    759 			 * chdir here before extracting.
    760 			 * do so lazily, in case it's a list
    761 			 */
    762 			firstminusC = optarg;
    763 			break;
    764 		case 'e':
    765 			/*
    766 			 * stop after first error
    767 			 */
    768 			maxflt = 0;
    769 			break;
    770 		case 'f':
    771 			/*
    772 			 * filename where the archive is stored
    773 			 */
    774 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
    775 				/*
    776 				 * treat a - as stdin
    777 				 */
    778 				fstdin = 1;
    779 				arcname = (char *)0;
    780 				break;
    781 			}
    782 			fstdin = 0;
    783 			arcname = optarg;
    784 			break;
    785 		case 'h':
    786 			/*
    787 			 * follow command line symlinks only
    788 			 */
    789 			Hflag = 1;
    790 			break;
    791 		case 'l':
    792 			/*
    793 			 * do not pass over mount points in the file system
    794 			 */
    795 			Xflag = 1;
    796 			break;
    797 		case 'm':
    798 			/*
    799 			 * do not preserve modification time
    800 			 */
    801 			pmtime = 0;
    802 			break;
    803 		case 'o':
    804 			/*
    805 			 * This option does several things based on whether
    806 			 * this is a create or extract operation.
    807 			 */
    808 			if (act == ARCHIVE) {
    809 				/* 4.2BSD: don't add directory entries. */
    810 				if (opt_add("write_opt=nodir") < 0)
    811 					tar_usage();
    812 
    813 				/* GNU tar: write V7 format archives. */
    814 				frmt = &(fsub[F_TAR]);
    815 			} else {
    816 				/* SUS: don't preserve owner/group. */
    817 				pids = 0;
    818 				nopids = 1;
    819 			}
    820 			break;
    821 		case 'p':
    822 			/*
    823 			 * preserve user id, group id, file
    824 			 * mode, access/modification times
    825 			 */
    826 			if (!nopids)
    827 				pids = 1;
    828 			pmode = 1;
    829 			patime = 1;
    830 			pmtime = 1;
    831 			break;
    832 		case 'r':
    833 		case 'u':
    834 			/*
    835 			 * append to the archive
    836 			 */
    837 			act = APPND;
    838 			break;
    839 		case 't':
    840 			/*
    841 			 * list contents of the tape
    842 			 */
    843 			act = LIST;
    844 			break;
    845 		case 'v':
    846 			/*
    847 			 * verbose operation mode
    848 			 */
    849 			vflag = 1;
    850 			break;
    851 		case 'w':
    852 			/*
    853 			 * interactive file rename
    854 			 */
    855 			iflag = 1;
    856 			break;
    857 		case 'x':
    858 			/*
    859 			 * write an archive
    860 			 */
    861 			act = EXTRACT;
    862 			break;
    863 		case 'z':
    864 			/*
    865 			 * use gzip.  Non standard option.
    866 			 */
    867 			zflag = 1;
    868 			gzip_program = GZIP_CMD;
    869 			break;
    870 		case 'B':
    871 			/*
    872 			 * Nothing to do here, this is pax default
    873 			 */
    874 			break;
    875 		case 'L':
    876 			/*
    877 			 * follow symlinks
    878 			 */
    879 			Lflag = 1;
    880 			break;
    881 		case 'P':
    882 			Aflag = 1;
    883 			break;
    884 		case 'X':
    885 			/*
    886 			 * GNU tar compat: exclude the files listed in optarg
    887 			 */
    888 			if (tar_gnutar_X_compat(optarg) != 0)
    889 				tar_usage();
    890 			break;
    891 		case 'Z':
    892 			/*
    893 			 * use compress.
    894 			 */
    895 			zflag = 1;
    896 			gzip_program = COMPRESS_CMD;
    897 			break;
    898 		case '0':
    899 			arcname = DEV_0;
    900 			break;
    901 		case '1':
    902 			arcname = DEV_1;
    903 			break;
    904 		case '4':
    905 			arcname = DEV_4;
    906 			break;
    907 		case '5':
    908 			arcname = DEV_5;
    909 			break;
    910 		case '7':
    911 			arcname = DEV_7;
    912 			break;
    913 		case '8':
    914 			arcname = DEV_8;
    915 			break;
    916 		case OPT_UNLINK:
    917 			/* Just ignore -- we always unlink first. */
    918 			break;
    919 		default:
    920 			tar_usage();
    921 			break;
    922 		}
    923 	}
    924 	argc -= optind;
    925 	argv += optind;
    926 
    927 	if (firstminusC && (opt_chdir(firstminusC) < 0))
    928 		tty_warn(1, "can't remember -C directory");
    929 
    930 	/*
    931 	 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
    932 	 */
    933 	if (act == ARCHIVE && frmt == NULL)
    934 		frmt = &(fsub[F_USTAR]);
    935 
    936 	/*
    937 	 * process the args as they are interpreted by the operation mode
    938 	 */
    939 	switch (act) {
    940 	case LIST:
    941 	default:
    942 		while (*argv != (char *)NULL)
    943 			if (pat_add(*argv++, 0) < 0)
    944 				tar_usage();
    945 		break;
    946 	case EXTRACT:
    947 		checkpositionalminusC(&argv, pat_add);
    948 		break;
    949 	case ARCHIVE:
    950 	case APPND:
    951 		checkpositionalminusC(&argv, ftree_add);
    952 		/*
    953 		 * no read errors allowed on updates/append operation!
    954 		 */
    955 		maxflt = 0;
    956 		break;
    957 	}
    958 	if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
    959 		arcname = getenv("TAPE");
    960 		if ((arcname == (char *)NULL) || (*arcname == '\0'))
    961 			arcname = DEV_8;
    962 	}
    963 }
    964 
    965 /*
    966  * cpio_options()
    967  *	look at the user specified flags. set globals as required and check if
    968  *	the user specified a legal set of flags. If not, complain and exit
    969  */
    970 
    971 #if __STDC__
    972 static void
    973 cpio_options(int argc, char **argv)
    974 #else
    975 static void
    976 cpio_options(argc, argv)
    977 	int argc;
    978 	char **argv;
    979 #endif
    980 {
    981 	FSUB tmp;
    982 	unsigned int flg = 0;
    983 	unsigned int bflg = 0;
    984 	int c, i;
    985 
    986 	cpio_mode = uflag = 1;
    987 	/*
    988 	 * process option flags
    989 	 */
    990 	while ((c = getoldopt(argc, argv,
    991 	    "ABC:E:H:I:LM:O:R:SVabcdfiklmoprstuv", NULL, NULL)) != -1)  {
    992 		switch(c) {
    993 		case 'A':
    994 			/*
    995 			 * append to an archive
    996 			 */
    997 			act = APPND;
    998 			flg |= AF;
    999 			break;
   1000 		case 'B':
   1001 			/*
   1002 			 * set blocksize to 5120
   1003 			 */
   1004 			blksz = 5120;
   1005 			break;
   1006 		case 'C':
   1007 			/*
   1008 			 * specify blocksize
   1009 			 */
   1010 			if ((blksz = (int)str_offt(optarg)) <= 0) {
   1011 				tty_warn(1, "Invalid block size %s", optarg);
   1012 				tar_usage();
   1013 			}
   1014 			break;
   1015 #ifdef notyet
   1016 		case 'E':
   1017 			arg = optarg;
   1018 			break;
   1019 #endif
   1020 		case 'H':
   1021 			/*
   1022 			 * specify an archive format on write
   1023 			 */
   1024 			tmp.name = optarg;
   1025 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
   1026 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
   1027 			if (frmt != NULL) {
   1028 				flg |= XF;
   1029 				break;
   1030 			}
   1031 			tty_warn(1, "Unknown -H format: %s", optarg);
   1032 			(void)fputs("cpio: Known -H formats are:", stderr);
   1033 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
   1034 				(void)fprintf(stderr, " %s", fsub[i].name);
   1035 			(void)fputs("\n\n", stderr);
   1036 			tar_usage();
   1037 			break;
   1038 		case 'I':
   1039 		case 'O':
   1040 			/*
   1041 			 * filename where the archive is stored
   1042 			 */
   1043 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
   1044 				/*
   1045 				 * treat a - as stdin
   1046 				 */
   1047 				arcname = (char *)0;
   1048 				break;
   1049 			}
   1050 			arcname = optarg;
   1051 			break;
   1052 		case 'L':
   1053 			/*
   1054 			 * follow symlinks
   1055 			 */
   1056 			Lflag = 1;
   1057 			flg |= CLF;
   1058 			break;
   1059 #ifdef notyet
   1060 		case 'M':
   1061 			arg = optarg;
   1062 			break;
   1063 		case 'R':
   1064 			arg = optarg;
   1065 			break;
   1066 #endif
   1067 		case 'S':
   1068 			cpio_swp_head = 1;
   1069 			break;
   1070 #ifdef notyet
   1071 		case 'V':
   1072 			break;
   1073 #endif
   1074 		case 'a':
   1075 			/*
   1076 			 * preserve access time on filesystem nodes we read
   1077 			 */
   1078 			tflag = 1;
   1079 			flg |= TF;
   1080 			break;
   1081 #ifdef notyet
   1082 		case 'b':
   1083 			break;
   1084 #endif
   1085 		case 'c':
   1086 			frmt = &fsub[F_SV4CPIO];
   1087 			break;
   1088 		case 'd':
   1089 			/*
   1090 			 * pax does this by default ..
   1091 			 */
   1092 			flg |= RF;
   1093 			break;
   1094 		case 'f':
   1095 			/*
   1096 			 * inverse match on patterns
   1097 			 */
   1098 			cflag = 1;
   1099 			flg |= CF;
   1100 			break;
   1101 		case 'i':
   1102 			/*
   1103 			 * read the archive
   1104 			 */
   1105 			flg |= RF;
   1106 			break;
   1107 #ifdef notyet
   1108 		case 'k':
   1109 			break;
   1110 #endif
   1111 		case 'l':
   1112 			/*
   1113 			 * try to link src to dest with copy (-rw)
   1114 			 */
   1115 			lflag = 1;
   1116 			flg |= LF;
   1117 			break;
   1118 		case 'm':
   1119 			/*
   1120 			 * preserve mtime
   1121 			 */
   1122 			flg |= PF;
   1123 			pmtime = 1;
   1124 			break;
   1125 		case 'o':
   1126 			/*
   1127 			 * write an archive
   1128 			 */
   1129 			flg |= WF;
   1130 			break;
   1131 		case 'p':
   1132 			/*
   1133 			 * cpio -p is like pax -rw
   1134 			 */
   1135 			flg |= RF | WF;
   1136 			break;
   1137 		case 'r':
   1138 			/*
   1139 			 * interactive file rename
   1140 			 */
   1141 			iflag = 1;
   1142 			flg |= IF;
   1143 			break;
   1144 #ifdef notyet
   1145 		case 's':
   1146 			break;
   1147 #endif
   1148 		case 't':
   1149 			act = LIST;
   1150 			break;
   1151 		case 'u':
   1152 			/*
   1153 			 * don't ignore those older files
   1154 			 */
   1155 			uflag = 0;
   1156 			flg |= UF;
   1157 			break;
   1158 		case 'v':
   1159 			/*
   1160 			 * verbose operation mode
   1161 			 */
   1162 			vflag = 1;
   1163 			flg |= VF;
   1164 			break;
   1165 		default:
   1166 			cpio_usage();
   1167 			break;
   1168 		}
   1169 	}
   1170 
   1171 	/*
   1172 	 * figure out the operation mode of cpio. check that we have not been
   1173 	 * given a bogus set of flags for the operation mode.
   1174 	 */
   1175 	if (ISLIST(flg)) {
   1176 		act = LIST;
   1177 		bflg = flg & BDLIST;
   1178 	} else if (ISEXTRACT(flg)) {
   1179 		act = EXTRACT;
   1180 		bflg = flg & BDEXTR;
   1181 	} else if (ISARCHIVE(flg)) {
   1182 		act = ARCHIVE;
   1183 		bflg = flg & BDARCH;
   1184 	} else if (ISAPPND(flg)) {
   1185 		act = APPND;
   1186 		bflg = flg & BDARCH;
   1187 	} else if (ISCOPY(flg)) {
   1188 		act = COPY;
   1189 		bflg = flg & BDCOPY;
   1190 	} else
   1191 		cpio_usage();
   1192 	if (bflg) {
   1193 		cpio_usage();
   1194 	}
   1195 
   1196 	/*
   1197 	 * if we are writing (ARCHIVE) we use the default format if the user
   1198 	 * did not specify a format. when we write during an APPEND, we will
   1199 	 * adopt the format of the existing archive if none was supplied.
   1200 	 */
   1201 	if (!(flg & XF) && (act == ARCHIVE))
   1202 		frmt = &(fsub[F_BCPIO]);
   1203 
   1204 	/*
   1205 	 * process the args as they are interpreted by the operation mode
   1206 	 */
   1207 	switch (act) {
   1208 	case LIST:
   1209 	case EXTRACT:
   1210 		for (; optind < argc; optind++)
   1211 			if (pat_add(argv[optind], 0) < 0)
   1212 				cpio_usage();
   1213 		break;
   1214 	case COPY:
   1215 		if (optind >= argc) {
   1216 			tty_warn(0, "Destination directory was not supplied");
   1217 			cpio_usage();
   1218 		}
   1219 		--argc;
   1220 		dirptr = argv[argc];
   1221 		/* FALLTHROUGH */
   1222 	case ARCHIVE:
   1223 	case APPND:
   1224 		for (; optind < argc; optind++)
   1225 			if (ftree_add(argv[optind], 0) < 0)
   1226 				cpio_usage();
   1227 		/*
   1228 		 * no read errors allowed on updates/append operation!
   1229 		 */
   1230 		maxflt = 0;
   1231 		break;
   1232 	}
   1233 }
   1234 
   1235 /*
   1236  * printflg()
   1237  *	print out those invalid flag sets found to the user
   1238  */
   1239 
   1240 #if __STDC__
   1241 static void
   1242 printflg(unsigned int flg)
   1243 #else
   1244 static void
   1245 printflg(flg)
   1246 	unsigned int flg;
   1247 #endif
   1248 {
   1249 	int nxt;
   1250 	int pos = 0;
   1251 
   1252 	(void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
   1253 	while ((nxt = ffs(flg)) != 0) {
   1254 		flg = flg >> nxt;
   1255 		pos += nxt;
   1256 		(void)fprintf(stderr, " -%c", flgch[pos-1]);
   1257 	}
   1258 	(void)putc('\n', stderr);
   1259 }
   1260 
   1261 /*
   1262  * c_frmt()
   1263  *	comparison routine used by bsearch to find the format specified
   1264  *	by the user
   1265  */
   1266 
   1267 #if __STDC__
   1268 static int
   1269 c_frmt(const void *a, const void *b)
   1270 #else
   1271 static int
   1272 c_frmt(a, b)
   1273 	void *a;
   1274 	void *b;
   1275 #endif
   1276 {
   1277 	return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
   1278 }
   1279 
   1280 /*
   1281  * opt_next()
   1282  *	called by format specific options routines to get each format specific
   1283  *	flag and value specified with -o
   1284  * Return:
   1285  *	pointer to next OPLIST entry or NULL (end of list).
   1286  */
   1287 
   1288 #if __STDC__
   1289 OPLIST *
   1290 opt_next(void)
   1291 #else
   1292 OPLIST *
   1293 opt_next()
   1294 #endif
   1295 {
   1296 	OPLIST *opt;
   1297 
   1298 	if ((opt = ophead) != NULL)
   1299 		ophead = ophead->fow;
   1300 	return(opt);
   1301 }
   1302 
   1303 /*
   1304  * bad_opt()
   1305  *	generic routine used to complain about a format specific options
   1306  *	when the format does not support options.
   1307  */
   1308 
   1309 #if __STDC__
   1310 int
   1311 bad_opt(void)
   1312 #else
   1313 int
   1314 bad_opt()
   1315 #endif
   1316 {
   1317 	OPLIST *opt;
   1318 
   1319 	if (ophead == NULL)
   1320 		return(0);
   1321 	/*
   1322 	 * print all we were given
   1323 	 */
   1324 	tty_warn(1,"These format options are not supported");
   1325 	while ((opt = opt_next()) != NULL)
   1326 		(void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
   1327 	pax_usage();
   1328 	return(0);
   1329 }
   1330 
   1331 /*
   1332  * opt_add()
   1333  *	breaks the value supplied to -o into a option name and value. options
   1334  *	are given to -o in the form -o name-value,name=value
   1335  *	multiple -o may be specified.
   1336  * Return:
   1337  *	0 if format in name=value format, -1 if -o is passed junk
   1338  */
   1339 
   1340 #if __STDC__
   1341 int
   1342 opt_add(const char *str)
   1343 #else
   1344 int
   1345 opt_add(str)
   1346 	const char *str;
   1347 #endif
   1348 {
   1349 	OPLIST *opt;
   1350 	char *frpt;
   1351 	char *pt;
   1352 	char *endpt;
   1353 
   1354 	if ((str == NULL) || (*str == '\0')) {
   1355 		tty_warn(0, "Invalid option name");
   1356 		return(-1);
   1357 	}
   1358 	frpt = endpt = strdup(str);
   1359 
   1360 	/*
   1361 	 * break into name and values pieces and stuff each one into a
   1362 	 * OPLIST structure. When we know the format, the format specific
   1363 	 * option function will go through this list
   1364 	 */
   1365 	while ((frpt != NULL) && (*frpt != '\0')) {
   1366 		if ((endpt = strchr(frpt, ',')) != NULL)
   1367 			*endpt = '\0';
   1368 		if ((pt = strchr(frpt, '=')) == NULL) {
   1369 			tty_warn(0, "Invalid options format");
   1370 			return(-1);
   1371 		}
   1372 		if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
   1373 			tty_warn(0, "Unable to allocate space for option list");
   1374 			return(-1);
   1375 		}
   1376 		*pt++ = '\0';
   1377 		opt->name = frpt;
   1378 		opt->value = pt;
   1379 		opt->fow = NULL;
   1380 		if (endpt != NULL)
   1381 			frpt = endpt + 1;
   1382 		else
   1383 			frpt = NULL;
   1384 		if (ophead == NULL) {
   1385 			optail = ophead = opt;
   1386 			continue;
   1387 		}
   1388 		optail->fow = opt;
   1389 		optail = opt;
   1390 	}
   1391 	return(0);
   1392 }
   1393 
   1394 /*
   1395  * str_offt()
   1396  *	Convert an expression of the following forms to an off_t > 0.
   1397  *	1) A positive decimal number.
   1398  *	2) A positive decimal number followed by a b (mult by 512).
   1399  *	3) A positive decimal number followed by a k (mult by 1024).
   1400  *	4) A positive decimal number followed by a m (mult by 512).
   1401  *	5) A positive decimal number followed by a w (mult by sizeof int)
   1402  *	6) Two or more positive decimal numbers (with/without k,b or w).
   1403  *	   seperated by x (also * for backwards compatibility), specifying
   1404  *	   the product of the indicated values.
   1405  * Return:
   1406  *	0 for an error, a positive value o.w.
   1407  */
   1408 
   1409 #if __STDC__
   1410 static off_t
   1411 str_offt(char *val)
   1412 #else
   1413 static off_t
   1414 str_offt(val)
   1415 	char *val;
   1416 #endif
   1417 {
   1418 	char *expr;
   1419 	off_t num, t;
   1420 
   1421 #	ifdef NET2_STAT
   1422 	num = strtol(val, &expr, 0);
   1423 	if ((num == LONG_MAX) || (num <= 0) || (expr == val))
   1424 #	else
   1425 	num = strtoq(val, &expr, 0);
   1426 	if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
   1427 #	endif
   1428 		return(0);
   1429 
   1430 	switch(*expr) {
   1431 	case 'b':
   1432 		t = num;
   1433 		num *= 512;
   1434 		if (t > num)
   1435 			return(0);
   1436 		++expr;
   1437 		break;
   1438 	case 'k':
   1439 		t = num;
   1440 		num *= 1024;
   1441 		if (t > num)
   1442 			return(0);
   1443 		++expr;
   1444 		break;
   1445 	case 'm':
   1446 		t = num;
   1447 		num *= 1048576;
   1448 		if (t > num)
   1449 			return(0);
   1450 		++expr;
   1451 		break;
   1452 	case 'w':
   1453 		t = num;
   1454 		num *= sizeof(int);
   1455 		if (t > num)
   1456 			return(0);
   1457 		++expr;
   1458 		break;
   1459 	}
   1460 
   1461 	switch(*expr) {
   1462 		case '\0':
   1463 			break;
   1464 		case '*':
   1465 		case 'x':
   1466 			t = num;
   1467 			num *= str_offt(expr + 1);
   1468 			if (t > num)
   1469 				return(0);
   1470 			break;
   1471 		default:
   1472 			return(0);
   1473 	}
   1474 	return(num);
   1475 }
   1476 
   1477 /*
   1478  * no_op()
   1479  *	for those option functions where the archive format has nothing to do.
   1480  * Return:
   1481  *	0
   1482  */
   1483 
   1484 #if __STDC__
   1485 static int
   1486 no_op(void)
   1487 #else
   1488 static int
   1489 no_op()
   1490 #endif
   1491 {
   1492 	return(0);
   1493 }
   1494 
   1495 /*
   1496  * pax_usage()
   1497  *	print the usage summary to the user
   1498  */
   1499 
   1500 #if __STDC__
   1501 void
   1502 pax_usage(void)
   1503 #else
   1504 void
   1505 pax_usage()
   1506 #endif
   1507 {
   1508 	(void)fputs("usage: pax [-cdnvz] [-E limit] [-f archive] ", stderr);
   1509 	(void)fputs("[-s replstr] ... [-U user] ...", stderr);
   1510 	(void)fputs("\n           [-G group] ... ", stderr);
   1511 	(void)fputs("[-T [from_date][,to_date]] ... ", stderr);
   1512 	(void)fputs("[pattern ...]\n", stderr);
   1513 	(void)fputs("       pax -r [-cdiknuvzDYZ] [-E limit] ", stderr);
   1514 	(void)fputs("[-f archive] [-o options] ... \n", stderr);
   1515 	(void)fputs("           [-p string] ... [-s replstr] ... ", stderr);
   1516 	(void)fputs("[-U user] ... [-G group] ...\n           ", stderr);
   1517 	(void)fputs("[-T [from_date][,to_date]] ... ", stderr);
   1518 	(void)fputs(" [pattern ...]\n", stderr);
   1519 	(void)fputs("       pax -w [-dituvzHLPX] [-b blocksize] ", stderr);
   1520 	(void)fputs("[[-a] [-f archive]] [-x format] \n", stderr);
   1521 	(void)fputs("           [-B bytes] [-o options] ... ", stderr);
   1522 	(void)fputs("[-s replstr] ... [-U user] ...", stderr);
   1523 	(void)fputs("\n           [-G group] ... ", stderr);
   1524 	(void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
   1525 	(void)fputs("[file ...]\n", stderr);
   1526 	(void)fputs("       pax -r -w [-diklntuvzDHLPXYZ] ", stderr);
   1527 	(void)fputs("[-p string] ... [-s replstr] ...", stderr);
   1528 	(void)fputs("\n           [-U user] ... [-G group] ... ", stderr);
   1529 	(void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
   1530 	(void)fputs("\n           [file ...] directory\n", stderr);
   1531 	exit(1);
   1532 	/* NOTREACHED */
   1533 }
   1534 
   1535 /*
   1536  * tar_usage()
   1537  *	print the usage summary to the user
   1538  */
   1539 
   1540 #if __STDC__
   1541 void
   1542 tar_usage(void)
   1543 #else
   1544 void
   1545 tar_usage()
   1546 #endif
   1547 {
   1548 	(void)fputs("usage: tar -{txru}[cevfbhlmopwBLPX014578] [tapefile] ",
   1549 		 stderr);
   1550 	(void)fputs("[blocksize] [exclude-file] file1 file2...\n", stderr);
   1551 	exit(1);
   1552 	/* NOTREACHED */
   1553 }
   1554 
   1555 /*
   1556  * cpio_usage()
   1557  *	print the usage summary to the user
   1558  */
   1559 
   1560 #if __STDC__
   1561 void
   1562 cpio_usage(void)
   1563 #else
   1564 void
   1565 cpio_usage()
   1566 #endif
   1567 {
   1568 
   1569 #if 1
   1570 	(void)fputs(
   1571 	    "usage: cpio -i [-BcdfmrStuv] [ -C blksize ] [ -H header ]\n",
   1572 	    stderr);
   1573 	(void)fputs("  [ -I file ] [ pattern ... ]\n", stderr);
   1574 	(void)fputs("usage: cpio -o [-aABcLv] [ -C bufsize ] [ -H header ]\n",
   1575 	    stderr);
   1576 	(void)fputs("  [ -O file ]\n", stderr);
   1577 	(void)fputs("usage: cpio -p [ adlLmuv ] directory\n", stderr);
   1578 #else
   1579 	/* no E, M, R, V, b, k or s */
   1580 	(void)fputs("usage: cpio -i [-bBcdfkmrsStuvV] [ -C bufsize ]\n", stderr);
   1581 	(void)fputs("  [ -E file ] [ -H header ] [ -I file [ -M message ] ]\n",
   1582 	    stderr);
   1583 	(void)fputs("  [ -R id ] [ pattern ... ]\n", stderr);
   1584 	(void)fputs("usage: cpio -o [-aABcLvV] [ -C bufsize ] [ -H header ]\n",
   1585 	    stderr);
   1586 	(void)fputs("  [ -O file [ -M message ] ]\n", stderr);
   1587 	(void)fputs("usage: cpio -p [ adlLmuvV ] [ -R id ] directory\n", stderr);
   1588 #endif
   1589 	exit(1);
   1590 	/* NOTREACHED */
   1591 }
   1592 
   1593 /*
   1594  * opt_chdir
   1595  *	call ftree_add or pat_add, depending on archive type.
   1596  *
   1597  * Returns: -1 for listing, else what ftree_add or pat_add returned.
   1598  */
   1599 
   1600 #ifdef __STDC__
   1601 int
   1602 opt_chdir(char *name)
   1603 #else
   1604 int
   1605 opt_chdir(name)
   1606 	char *name;
   1607 #endif
   1608 {
   1609 	switch (act) {
   1610 	default:
   1611 		return (-1);
   1612 		break;
   1613 	case ARCHIVE:
   1614 	case APPND:
   1615 		return (ftree_add(name, 1));
   1616 		break;
   1617 	case EXTRACT:
   1618 		return (pat_add(name, 1));
   1619 		break;
   1620 	}
   1621 }
   1622 
   1623 /*
   1624  * checkpositionalminusC(argvp, addfunc)
   1625  */
   1626 
   1627 #ifdef __STDC__
   1628 void
   1629 checkpositionalminusC(char ***argvp, int (*addfunc)(char *, int))
   1630 #else
   1631 void
   1632 checkpositionalminusC(argvp, addfunc)
   1633 	char ***argvp;
   1634 	int (*addfunc)();
   1635 #endif
   1636 {
   1637 	while (**argvp != (char *)NULL) {
   1638 		if (!strcmp(**argvp, "-C")) {
   1639 			/* XXX should be allow for positional -C/dir, too? */
   1640 			if ((*addfunc)(*++*argvp, 1) < 0) {
   1641 				tar_usage();
   1642 			}
   1643 			++*argvp;
   1644 			continue;
   1645 		}
   1646 		if ((*addfunc)(*(*argvp)++, 0) < 0)
   1647 			tar_usage();
   1648 	}
   1649 }
   1650