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