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