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