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