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