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