Home | History | Annotate | Line # | Download | only in pax
options.c revision 1.78
      1 /*	$NetBSD: options.c,v 1.78 2004/09/26 23:46:00 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.78 2004/09/26 23:46:00 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 (pat_add(str, dir) < 0)
   1157 							tar_usage();
   1158 						sawpat = 1;
   1159 					}
   1160 					/* Bomb if given -C w/out a dir. */
   1161 					if (dirisnext)
   1162 						tar_usage();
   1163 					if (strcmp(file, "-") != 0)
   1164 						fclose(fp);
   1165 					if (getline_error) {
   1166 						tty_warn(1, "Problem with file '%s'", file);
   1167 						tar_usage();
   1168 					}
   1169 				} else if (strcmp(*argv, "-C") == 0) {
   1170 					if (*++argv == NULL)
   1171  						break;
   1172 					chdname = *argv++;
   1173 				} else if (pat_add(*argv++, chdname) < 0)
   1174 					tar_usage();
   1175 				else
   1176 					sawpat = 1;
   1177 			}
   1178 			/*
   1179 			 * if patterns were added, we are doing	chdir()
   1180 			 * on a file-by-file basis, else, just one
   1181 			 * global chdir (if any) after opening input.
   1182 			 */
   1183 			if (sawpat > 0)
   1184 				chdname = NULL;
   1185 		}
   1186 		break;
   1187 	case ARCHIVE:
   1188 	case APPND:
   1189 		if (chdname != NULL) {	/* initial chdir() */
   1190 			if (ftree_add(chdname, 1) < 0)
   1191 				tar_usage();
   1192 		}
   1193 
   1194 		while (nincfiles || *argv != NULL) {
   1195 			char *file, *dir;
   1196 
   1197 			/*
   1198 			 * If we queued up any include files, pull them in
   1199 			 * now.  Otherwise, check for -I and -C positional
   1200 			 * flags.  Anything else must be a file to include
   1201 			 * in the archive.
   1202 			 */
   1203 			if (nincfiles) {
   1204 				file = incfiles->file;
   1205 				dir = incfiles->dir;
   1206 				incfiles++;
   1207 				nincfiles--;
   1208 			} else if (strcmp(*argv, "-I") == 0) {
   1209 				if (*++argv == NULL)
   1210 					break;
   1211 				file = *argv++;
   1212 				dir = NULL;
   1213 			} else
   1214 				file = NULL;
   1215 			if (file != NULL) {
   1216 				FILE *fp;
   1217 				char *str;
   1218 				int dirisnext = 0;
   1219 
   1220 				/* Set directory if needed */
   1221 				if (dir) {
   1222 					if (ftree_add(dir, 1) < 0)
   1223 						tar_usage();
   1224 				}
   1225 
   1226 				if (strcmp(file, "-") == 0)
   1227 					fp = stdin;
   1228 				else if ((fp = fopen(file, "r")) == NULL) {
   1229 					tty_warn(1, "Unable to open file '%s' for read", file);
   1230 					tar_usage();
   1231 				}
   1232 				while ((str = getline(fp)) != NULL) {
   1233 					if (dirisnext) {
   1234 						if (ftree_add(str, 1) < 0)
   1235 							tar_usage();
   1236 						dirisnext = 0;
   1237 						continue;
   1238 					}
   1239 					if (strcmp(str, "-C") == 0) {
   1240 						dirisnext = 1;
   1241 						continue;
   1242 					}
   1243 					if (ftree_add(str, 0) < 0)
   1244 						tar_usage();
   1245 				}
   1246 				/* Bomb if given -C w/out a dir. */
   1247 				if (dirisnext)
   1248 					tar_usage();
   1249 				if (strcmp(file, "-") != 0)
   1250 					fclose(fp);
   1251 				if (getline_error) {
   1252 					tty_warn(1, "Problem with file '%s'",
   1253 					    file);
   1254 					tar_usage();
   1255 				}
   1256 			} else if (strcmp(*argv, "-C") == 0) {
   1257 				if (*++argv == NULL)
   1258 					break;
   1259 				if (ftree_add(*argv++, 1) < 0)
   1260 					tar_usage();
   1261 			} else if (ftree_add(*argv++, 0) < 0)
   1262 				tar_usage();
   1263 		}
   1264 		/*
   1265 		 * no read errors allowed on updates/append operation!
   1266 		 */
   1267 		maxflt = 0;
   1268 		break;
   1269 	}
   1270 	if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
   1271 		arcname = getenv("TAPE");
   1272 		if ((arcname == NULL) || (*arcname == '\0'))
   1273 			arcname = _PATH_DEFTAPE;
   1274 	}
   1275 }
   1276 
   1277 int
   1278 mkpath(path)
   1279 	char *path;
   1280 {
   1281 	struct stat sb;
   1282 	char *slash;
   1283 	int done = 0;
   1284 
   1285 	slash = path;
   1286 
   1287 	while (!done) {
   1288 		slash += strspn(slash, "/");
   1289 		slash += strcspn(slash, "/");
   1290 
   1291 		done = (*slash == '\0');
   1292 		*slash = '\0';
   1293 
   1294 		if (stat(path, &sb)) {
   1295 			if (errno != ENOENT || mkdir(path, 0777)) {
   1296 				tty_warn(1, "%s", path);
   1297 				return (-1);
   1298 			}
   1299 		} else if (!S_ISDIR(sb.st_mode)) {
   1300 			syswarn(1, ENOTDIR, "%s", path);
   1301 			return (-1);
   1302 		}
   1303 
   1304 		if (!done)
   1305 			*slash = '/';
   1306 	}
   1307 
   1308 	return (0);
   1309 }
   1310 
   1311 
   1312 struct option cpio_longopts[] = {
   1313 	{ "reset-access-time",	no_argument,		0,	'a' },
   1314 	{ "make-directories",	no_argument,		0, 	'd' },
   1315 	{ "nonmatching",	no_argument,		0,	'f' },
   1316 	{ "extract",		no_argument,		0,	'i' },
   1317 	{ "link",		no_argument,		0,	'l' },
   1318 	{ "preserve-modification-time", no_argument,	0,	'm' },
   1319 	{ "create",		no_argument,		0,	'o' },
   1320 	{ "pass-through",	no_argument,		0,	'p' },
   1321 	{ "rename",		no_argument,		0,	'r' },
   1322 	{ "list",		no_argument,		0,	't' },
   1323 	{ "unconditional",	no_argument,		0,	'u' },
   1324 	{ "verbose",		no_argument,		0,	'v' },
   1325 	{ "append",		no_argument,		0,	'A' },
   1326 	{ "pattern-file",	required_argument,	0,	'E' },
   1327 	{ "file",		required_argument,	0,	'F' },
   1328 	{ "force-local",	no_argument,		0,
   1329 						OPT_FORCE_LOCAL },
   1330 	{ "format",		required_argument,	0,	'H' },
   1331 	{ "dereference",	no_argument,		0,	'L' },
   1332 	{ "swap-halfwords",	no_argument,		0,	'S' },
   1333 	{ "insecure",		no_argument,		0,
   1334 						OPT_INSECURE },
   1335 	{ "sparse",		no_argument,		0,
   1336 						OPT_SPARSE },
   1337 
   1338 #ifdef notyet
   1339 /* Not implemented */
   1340 	{ "null",		no_argument,		0,	'0' },
   1341 	{ "swap",		no_argument,		0,	'b' },
   1342 	{ "numeric-uid-gid",	no_argument,		0,	'n' },
   1343 	{ "swap-bytes",		no_argument,		0,	's' },
   1344 	{ "message",		required_argument,	0,	'M' },
   1345 	{ "owner",		required_argument,	0	'R' },
   1346 	{ "dot",		no_argument,		0,	'V' },
   1347 	{ "block-size",		required_argument,	0,
   1348 						OPT_BLOCK_SIZE },
   1349 	{ "no-absolute-pathnames", no_argument,		0,
   1350 						OPT_NO_ABSOLUTE_PATHNAMES },
   1351 	{ "no-preserve-owner",	no_argument,		0,
   1352 						OPT_NO_PRESERVE_OWNER },
   1353 	{ "only-verify-crc",	no_argument,		0,
   1354 						OPT_ONLY_VERIFY_CRC },
   1355 	{ "rsh-command",	required_argument,	0,
   1356 						OPT_RSH_COMMAND },
   1357 	{ "version",		no_argument,		0,
   1358 						OPT_VERSION },
   1359 #endif
   1360 };
   1361 
   1362 /*
   1363  * cpio_options()
   1364  *	look at the user specified flags. set globals as required and check if
   1365  *	the user specified a legal set of flags. If not, complain and exit
   1366  */
   1367 
   1368 static void
   1369 cpio_options(int argc, char **argv)
   1370 {
   1371 	FSUB tmp;
   1372 	unsigned int flg = 0;
   1373 	unsigned int bflg = 0;
   1374 	int c, i;
   1375 	FILE *fp;
   1376 	char *str;
   1377 
   1378 	uflag = 1;
   1379 	kflag = 1;
   1380 	pids = 1;
   1381 	pmode = 1;
   1382 	pmtime = 0;
   1383 	arcname = NULL;
   1384 	dflag = 1;
   1385 	nodirs = 1;
   1386 	/*
   1387 	 * process option flags
   1388 	 */
   1389 	while ((c = getoldopt(argc, argv,
   1390 	    "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
   1391 	    cpio_longopts, NULL)) != -1) {
   1392 		switch(c) {
   1393 		case 'a':
   1394 			/*
   1395 			 * preserve access time on filesystem nodes we read
   1396 			 */
   1397 			tflag = 1;
   1398 			flg |= TF;
   1399 			break;
   1400 #ifdef notyet
   1401 		case 'b':
   1402 			/*
   1403 			 * swap bytes and half-words when reading data
   1404 			 */
   1405 			break;
   1406 #endif
   1407 		case 'c':
   1408 			/*
   1409 			 * ASCII cpio header
   1410 			 */
   1411 			frmt = &fsub[F_SV4CPIO];
   1412 			break;
   1413 		case 'd':
   1414 			/*
   1415 			 * create directories as needed
   1416 			 * pax does this by default ..
   1417 			 */
   1418 			nodirs = 0;
   1419 			flg |= RF;
   1420 			break;
   1421 		case 'f':
   1422 			/*
   1423 			 * inverse match on patterns
   1424 			 */
   1425 			cflag = 1;
   1426 			flg |= CF;
   1427 			break;
   1428 		case 'i':
   1429 			/*
   1430 			 * read the archive
   1431 			 */
   1432 			act = EXTRACT;
   1433 			flg |= RF;
   1434 			break;
   1435 #ifdef notyet
   1436 		case 'k':
   1437 			break;
   1438 #endif
   1439 		case 'l':
   1440 			/*
   1441 			 * try to link src to dest with copy (-rw)
   1442 			 */
   1443 			lflag = 1;
   1444 			flg |= LF;
   1445 			break;
   1446 		case 'm':
   1447 			/*
   1448 			 * preserve mtime
   1449 			 */
   1450 			flg |= PF;
   1451 			pmtime = 1;
   1452 			break;
   1453 		case 'o':
   1454 			/*
   1455 			 * write an archive
   1456 			 */
   1457 			act = ARCHIVE;
   1458 			frmt = &(fsub[F_SV4CRC]);
   1459 			flg |= WF;
   1460 			break;
   1461 		case 'p':
   1462 			/*
   1463 			 * cpio -p is like pax -rw
   1464 			 */
   1465 			act = COPY;
   1466 			flg |= RF | WF;
   1467 			break;
   1468 		case 'r':
   1469 			/*
   1470 			 * interactive file rename
   1471 			 */
   1472 			iflag = 1;
   1473 			flg |= IF;
   1474 			break;
   1475 #ifdef notyet
   1476 		case 's':
   1477 			/*
   1478 			 * swap bytes after reading data
   1479 			 */
   1480 			break;
   1481 #endif
   1482 		case 't':
   1483 			/*
   1484 			 * list contents of archive
   1485 			 */
   1486 			act = LIST;
   1487 			listf = stdout;
   1488 			break;
   1489 		case 'u':
   1490 			/*
   1491 			 * don't ignore those older files
   1492 			 */
   1493 			uflag = 0;
   1494 			kflag = 0;
   1495 			flg |= UF;
   1496 			break;
   1497 		case 'v':
   1498 			/*
   1499 			 * verbose operation mode
   1500 			 */
   1501 			vflag = 1;
   1502 			flg |= VF;
   1503 			break;
   1504 		case 'z':
   1505 			/*
   1506 			 * use gzip.  Non standard option.
   1507 			 */
   1508 			gzip_program = GZIP_CMD;
   1509 			break;
   1510 		case 'A':
   1511 			/*
   1512 			 * append to an archive
   1513 			 */
   1514 			act = APPND;
   1515 			flg |= AF;
   1516 			break;
   1517 		case 'B':
   1518 			/*
   1519 			 * set blocksize to 5120
   1520 			 */
   1521 			blksz = 5120;
   1522 			break;
   1523 		case 'C':
   1524 			/*
   1525 			 * specify blocksize
   1526 			 */
   1527 			if ((blksz = (int)str_offt(optarg)) <= 0) {
   1528 				tty_warn(1, "Invalid block size %s", optarg);
   1529 				cpio_usage();
   1530 			}
   1531 			break;
   1532 		case 'E':
   1533 			/*
   1534 			 * file with patterns to extract or list
   1535 			 */
   1536 			if ((fp = fopen(optarg, "r")) == NULL) {
   1537 				tty_warn(1, "Unable to open file '%s' for read",
   1538 				    optarg);
   1539 				cpio_usage();
   1540 			}
   1541 			while ((str = getline(fp)) != NULL) {
   1542 				pat_add(str, NULL);
   1543 			}
   1544 			fclose(fp);
   1545 			if (getline_error) {
   1546 				tty_warn(1, "Problem with file '%s'", optarg);
   1547 				cpio_usage();
   1548 			}
   1549 			break;
   1550 		case 'H':
   1551 			/*
   1552 			 * specify an archive format on write
   1553 			 */
   1554 			tmp.name = optarg;
   1555 			frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
   1556 			    sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
   1557 			if (frmt != NULL) {
   1558 				flg |= XF;
   1559 				break;
   1560 			}
   1561 			tty_warn(1, "Unknown -H format: %s", optarg);
   1562 			(void)fputs("cpio: Known -H formats are:", stderr);
   1563 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
   1564 				(void)fprintf(stderr, " %s", fsub[i].name);
   1565 			(void)fputs("\n\n", stderr);
   1566 			cpio_usage();
   1567 			break;
   1568 		case 'I':
   1569 		case 'O':
   1570 			/*
   1571 			 * filename where the archive is stored
   1572 			 */
   1573 			if ((optarg[0] == '-') && (optarg[1]== '\0')) {
   1574 				/*
   1575 				 * treat a - as stdin
   1576 				 */
   1577 				arcname = NULL;
   1578 				break;
   1579 			}
   1580 			arcname = optarg;
   1581 			break;
   1582 		case 'L':
   1583 			/*
   1584 			 * follow symlinks
   1585 			 */
   1586 			Lflag = 1;
   1587 			flg |= CLF;
   1588 			break;
   1589 #ifdef notyet
   1590 		case 'M':
   1591 			arg = optarg;
   1592 			break;
   1593 		case 'R':
   1594 			arg = optarg;
   1595 			break;
   1596 #endif
   1597 		case 'S':
   1598 			/*
   1599 			 * swap halfwords after reading data
   1600 			 */
   1601 			cpio_swp_head = 1;
   1602 			break;
   1603 #ifdef notyet
   1604 		case 'V':
   1605 			break;
   1606 #endif
   1607 		case 'Z':
   1608 			/*
   1609 			 * use compress.  Non standard option.
   1610 			 */
   1611 			gzip_program = COMPRESS_CMD;
   1612 			break;
   1613 		case '6':
   1614 			/*
   1615 			 * process Version 6 cpio format
   1616 			 */
   1617 			frmt = &(fsub[F_BCPIO]);
   1618 		case OPT_FORCE_LOCAL:
   1619 			forcelocal = 1;
   1620 			break;
   1621 		case OPT_INSECURE:
   1622 			secure = 0;
   1623 			break;
   1624 
   1625 		case OPT_SPARSE:
   1626 			/* do nothing; we already generate sparse files */
   1627 			break;
   1628 		default:
   1629 			cpio_usage();
   1630 			break;
   1631 		}
   1632 	}
   1633 
   1634 	/*
   1635 	 * figure out the operation mode of cpio. check that we have not been
   1636 	 * given a bogus set of flags for the operation mode.
   1637 	 */
   1638 	if (ISLIST(flg)) {
   1639 		act = LIST;
   1640 		bflg = flg & BDLIST;
   1641 	} else if (ISEXTRACT(flg)) {
   1642 		act = EXTRACT;
   1643 		bflg = flg & BDEXTR;
   1644 	} else if (ISARCHIVE(flg)) {
   1645 		act = ARCHIVE;
   1646 		bflg = flg & BDARCH;
   1647 	} else if (ISAPPND(flg)) {
   1648 		act = APPND;
   1649 		bflg = flg & BDARCH;
   1650 	} else if (ISCOPY(flg)) {
   1651 		act = COPY;
   1652 		bflg = flg & BDCOPY;
   1653 	} else
   1654 		cpio_usage();
   1655 	if (bflg) {
   1656 		cpio_usage();
   1657 	}
   1658 
   1659 	/*
   1660 	 * if we are writing (ARCHIVE) we use the default format if the user
   1661 	 * did not specify a format. when we write during an APPEND, we will
   1662 	 * adopt the format of the existing archive if none was supplied.
   1663 	 */
   1664 	if (!(flg & XF) && (act == ARCHIVE))
   1665 		frmt = &(fsub[F_BCPIO]);
   1666 
   1667 	/*
   1668 	 * process the args as they are interpreted by the operation mode
   1669 	 */
   1670 	switch (act) {
   1671 	case LIST:
   1672 	case EXTRACT:
   1673 		for (; optind < argc; optind++)
   1674 			if (pat_add(argv[optind], 0) < 0)
   1675 				cpio_usage();
   1676 		break;
   1677 	case COPY:
   1678 		if (optind >= argc) {
   1679 			tty_warn(0, "Destination directory was not supplied");
   1680 			cpio_usage();
   1681 		}
   1682 		--argc;
   1683 		dirptr = argv[argc];
   1684 		/* FALLTHROUGH */
   1685 	case ARCHIVE:
   1686 	case APPND:
   1687 		if (argc != optind) {
   1688 			for (; optind < argc; optind++)
   1689 				if (ftree_add(argv[optind], 0) < 0)
   1690 					cpio_usage();
   1691 			break;
   1692 		}
   1693 		/*
   1694 		 * no read errors allowed on updates/append operation!
   1695 		 */
   1696 		maxflt = 0;
   1697 		while ((str = getline(stdin)) != NULL) {
   1698 			ftree_add(str, 0);
   1699 		}
   1700 		if (getline_error) {
   1701 			tty_warn(1, "Problem while reading stdin");
   1702 			cpio_usage();
   1703 		}
   1704 		break;
   1705 	default:
   1706 		cpio_usage();
   1707 		break;
   1708 	}
   1709 }
   1710 
   1711 /*
   1712  * printflg()
   1713  *	print out those invalid flag sets found to the user
   1714  */
   1715 
   1716 static void
   1717 printflg(unsigned int flg)
   1718 {
   1719 	int nxt;
   1720 
   1721 	(void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
   1722 	while ((nxt = ffs(flg)) != 0) {
   1723 		flg &= ~(1 << (nxt - 1));
   1724 		(void)fprintf(stderr, " -%c", flgch[nxt - 1]);
   1725 	}
   1726 	(void)putc('\n', stderr);
   1727 }
   1728 
   1729 /*
   1730  * c_frmt()
   1731  *	comparison routine used by bsearch to find the format specified
   1732  *	by the user
   1733  */
   1734 
   1735 static int
   1736 c_frmt(const void *a, const void *b)
   1737 {
   1738 	return(strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name));
   1739 }
   1740 
   1741 /*
   1742  * opt_next()
   1743  *	called by format specific options routines to get each format specific
   1744  *	flag and value specified with -o
   1745  * Return:
   1746  *	pointer to next OPLIST entry or NULL (end of list).
   1747  */
   1748 
   1749 OPLIST *
   1750 opt_next(void)
   1751 {
   1752 	OPLIST *opt;
   1753 
   1754 	if ((opt = ophead) != NULL)
   1755 		ophead = ophead->fow;
   1756 	return(opt);
   1757 }
   1758 
   1759 /*
   1760  * bad_opt()
   1761  *	generic routine used to complain about a format specific options
   1762  *	when the format does not support options.
   1763  */
   1764 
   1765 int
   1766 bad_opt(void)
   1767 {
   1768 	OPLIST *opt;
   1769 
   1770 	if (ophead == NULL)
   1771 		return(0);
   1772 	/*
   1773 	 * print all we were given
   1774 	 */
   1775 	tty_warn(1," These format options are not supported for %s",
   1776 	    frmt->name);
   1777 	while ((opt = opt_next()) != NULL)
   1778 		(void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
   1779 	if (strcmp(NM_TAR, argv0) == 0)
   1780 		tar_usage();
   1781 	else if (strcmp(NM_CPIO, argv0) == 0)
   1782 		cpio_usage();
   1783 	else
   1784 		pax_usage();
   1785 	return(0);
   1786 }
   1787 
   1788 /*
   1789  * opt_add()
   1790  *	breaks the value supplied to -o into a option name and value. options
   1791  *	are given to -o in the form -o name-value,name=value
   1792  *	multiple -o may be specified.
   1793  * Return:
   1794  *	0 if format in name=value format, -1 if -o is passed junk
   1795  */
   1796 
   1797 int
   1798 opt_add(const char *str)
   1799 {
   1800 	OPLIST *opt;
   1801 	char *frpt;
   1802 	char *pt;
   1803 	char *endpt;
   1804 	char *dstr;
   1805 
   1806 	if ((str == NULL) || (*str == '\0')) {
   1807 		tty_warn(0, "Invalid option name");
   1808 		return(-1);
   1809 	}
   1810 	if ((dstr = strdup(str)) == NULL) {
   1811 		tty_warn(0, "Unable to allocate space for option list");
   1812 		return(-1);
   1813 	}
   1814 	frpt = endpt = dstr;
   1815 
   1816 	/*
   1817 	 * break into name and values pieces and stuff each one into a
   1818 	 * OPLIST structure. When we know the format, the format specific
   1819 	 * option function will go through this list
   1820 	 */
   1821 	while ((frpt != NULL) && (*frpt != '\0')) {
   1822 		if ((endpt = strchr(frpt, ',')) != NULL)
   1823 			*endpt = '\0';
   1824 		if ((pt = strchr(frpt, '=')) == NULL) {
   1825 			tty_warn(0, "Invalid options format");
   1826 			free(dstr);
   1827 			return(-1);
   1828 		}
   1829 		if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
   1830 			tty_warn(0, "Unable to allocate space for option list");
   1831 			free(dstr);
   1832 			return(-1);
   1833 		}
   1834 		*pt++ = '\0';
   1835 		opt->name = frpt;
   1836 		opt->value = pt;
   1837 		opt->fow = NULL;
   1838 		if (endpt != NULL)
   1839 			frpt = endpt + 1;
   1840 		else
   1841 			frpt = NULL;
   1842 		if (ophead == NULL) {
   1843 			optail = ophead = opt;
   1844 			continue;
   1845 		}
   1846 		optail->fow = opt;
   1847 		optail = opt;
   1848 	}
   1849 	return(0);
   1850 }
   1851 
   1852 /*
   1853  * str_offt()
   1854  *	Convert an expression of the following forms to an off_t > 0.
   1855  *	1) A positive decimal number.
   1856  *	2) A positive decimal number followed by a b (mult by 512).
   1857  *	3) A positive decimal number followed by a k (mult by 1024).
   1858  *	4) A positive decimal number followed by a m (mult by 512).
   1859  *	5) A positive decimal number followed by a w (mult by sizeof int)
   1860  *	6) Two or more positive decimal numbers (with/without k,b or w).
   1861  *	   separated by x (also * for backwards compatibility), specifying
   1862  *	   the product of the indicated values.
   1863  * Return:
   1864  *	0 for an error, a positive value o.w.
   1865  */
   1866 
   1867 static off_t
   1868 str_offt(char *val)
   1869 {
   1870 	char *expr;
   1871 	off_t num, t;
   1872 
   1873 	num = STRTOOFFT(val, &expr, 0);
   1874 	if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
   1875 		return(0);
   1876 
   1877 	switch(*expr) {
   1878 	case 'b':
   1879 		t = num;
   1880 		num *= 512;
   1881 		if (t > num)
   1882 			return(0);
   1883 		++expr;
   1884 		break;
   1885 	case 'k':
   1886 		t = num;
   1887 		num *= 1024;
   1888 		if (t > num)
   1889 			return(0);
   1890 		++expr;
   1891 		break;
   1892 	case 'm':
   1893 		t = num;
   1894 		num *= 1048576;
   1895 		if (t > num)
   1896 			return(0);
   1897 		++expr;
   1898 		break;
   1899 	case 'w':
   1900 		t = num;
   1901 		num *= sizeof(int);
   1902 		if (t > num)
   1903 			return(0);
   1904 		++expr;
   1905 		break;
   1906 	}
   1907 
   1908 	switch(*expr) {
   1909 		case '\0':
   1910 			break;
   1911 		case '*':
   1912 		case 'x':
   1913 			t = num;
   1914 			num *= str_offt(expr + 1);
   1915 			if (t > num)
   1916 				return(0);
   1917 			break;
   1918 		default:
   1919 			return(0);
   1920 	}
   1921 	return(num);
   1922 }
   1923 
   1924 char *
   1925 getline(FILE *f)
   1926 {
   1927 	char *name, *temp;
   1928 	size_t len;
   1929 
   1930 	name = fgetln(f, &len);
   1931 	if (!name) {
   1932 		getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
   1933 		return(0);
   1934 	}
   1935 	if (name[len-1] != '\n')
   1936 		len++;
   1937 	temp = malloc(len);
   1938 	if (!temp) {
   1939 		getline_error = GETLINE_OUT_OF_MEM;
   1940 		return(0);
   1941 	}
   1942 	memcpy(temp, name, len-1);
   1943 	temp[len-1] = 0;
   1944 	return(temp);
   1945 }
   1946 
   1947 /*
   1948  * no_op()
   1949  *	for those option functions where the archive format has nothing to do.
   1950  * Return:
   1951  *	0
   1952  */
   1953 
   1954 static int
   1955 no_op(void)
   1956 {
   1957 	return(0);
   1958 }
   1959 
   1960 /*
   1961  * pax_usage()
   1962  *	print the usage summary to the user
   1963  */
   1964 
   1965 void
   1966 pax_usage(void)
   1967 {
   1968 	fprintf(stderr,
   1969 "usage: pax [-cdjnvzO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
   1970 "           [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
   1971 "           [pattern ...]\n");
   1972 	fprintf(stderr,
   1973 "       pax -r [-cdijknuvzADOYZ] [-E limit] [-f archive] [-N dbdir]\n"
   1974 "           [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
   1975 "           [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
   1976 	fprintf(stderr,
   1977 "       pax -w [-dijtuvzAHLMOPX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
   1978 "           [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
   1979 "           [-U user] ... [-G group] ...\n"
   1980 "           [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
   1981 	fprintf(stderr,
   1982 "       pax -r -w [-dijklntuvzADHLMOPXYZ] [-N dbdir] [-p string] ...\n"
   1983 "           [-s replstr] ... [-U user] ... [-G group] ...\n"
   1984 "           [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
   1985 	exit(1);
   1986 	/* NOTREACHED */
   1987 }
   1988 
   1989 /*
   1990  * tar_usage()
   1991  *	print the usage summary to the user
   1992  */
   1993 
   1994 void
   1995 tar_usage(void)
   1996 {
   1997 	(void)fputs("usage: tar [-]{crtux}[-befhjlmopqvwzHLOPXZ014578] [archive] "
   1998 		    "[blocksize]\n"
   1999 		    "           [-C directory] [-T file] [-s replstr] "
   2000 		    "[file ...]\n", stderr);
   2001 	exit(1);
   2002 	/* NOTREACHED */
   2003 }
   2004 
   2005 /*
   2006  * cpio_usage()
   2007  *	print the usage summary to the user
   2008  */
   2009 
   2010 void
   2011 cpio_usage(void)
   2012 {
   2013 
   2014 	(void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
   2015 		    "[-H format] [-O archive]\n"
   2016 		    "               < name-list [> archive]\n"
   2017 		    "       cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
   2018 		    "[-F archive] [-H format] \n"
   2019 		    "               [-I archive] "
   2020 		    "[pattern ...] [< archive]\n"
   2021 		    "       cpio -p [-adlLmuv] destination-directory "
   2022 		    "< name-list\n", stderr);
   2023 	exit(1);
   2024 	/* NOTREACHED */
   2025 }
   2026