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