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