xref: /openbsd-src/bin/pax/options.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: options.c,v 1.100 2016/08/26 04:40:27 guenther Exp $	*/
2 /*	$NetBSD: options.c,v 1.6 1996/03/26 23:54:18 mrg Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992 Keith Muller.
6  * Copyright (c) 1992, 1993
7  *	The Regents of the University of California.  All rights reserved.
8  *
9  * This code is derived from software contributed to Berkeley by
10  * Keith Muller of the University of California, San Diego.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <errno.h>
40 #include <limits.h>
41 #include <paths.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 #include "pax.h"
48 #include "cpio.h"
49 #include "tar.h"
50 #include "extern.h"
51 
52 /*
53  * argv[0] names. Used for tar and cpio emulation
54  */
55 
56 #define NM_TAR  "tar"
57 #define NM_CPIO "cpio"
58 #define NM_PAX  "pax"
59 
60 /*
61  * Constants used to specify the legal sets of flags in pax. For each major
62  * operation mode of pax, a set of illegal flags is defined. If any one of
63  * those illegal flags are found set, we scream and exit
64  */
65 
66 /*
67  * flags (one for each option).
68  */
69 #define	AF	0x00000001
70 #define	BF	0x00000002
71 #define	CF	0x00000004
72 #define	DF	0x00000008
73 #define	FF	0x00000010
74 #define	IF	0x00000020
75 #define	KF	0x00000040
76 #define	LF	0x00000080
77 #define	NF	0x00000100
78 #define	OF	0x00000200
79 #define	PF	0x00000400
80 #define	RF	0x00000800
81 #define	SF	0x00001000
82 #define	TF	0x00002000
83 #define	UF	0x00004000
84 #define	VF	0x00008000
85 #define	WF	0x00010000
86 #define	XF	0x00020000
87 #define	CBF	0x00040000	/* nonstandard extension */
88 #define	CDF	0x00080000	/* nonstandard extension */
89 #define	CEF	0x00100000	/* nonstandard extension */
90 #define	CGF	0x00200000	/* nonstandard extension */
91 #define	CHF	0x00400000	/* nonstandard extension */
92 #define	CLF	0x00800000	/* nonstandard extension */
93 #define	CPF	0x01000000	/* nonstandard extension */
94 #define	CTF	0x02000000	/* nonstandard extension */
95 #define	CUF	0x04000000	/* nonstandard extension */
96 #define	CXF	0x08000000
97 #define	CYF	0x10000000	/* nonstandard extension */
98 #define	CZF	0x20000000	/* nonstandard extension */
99 #define	C0F	0x40000000	/* nonstandard extension */
100 
101 /*
102  * ascii string indexed by bit position above (alter the above and you must
103  * alter this string) used to tell the user what flags caused us to complain
104  */
105 #define FLGCH	"abcdfiklnoprstuvwxBDEGHLPTUXYZ0"
106 
107 /*
108  * legal pax operation bit patterns
109  */
110 
111 #define ISLIST(x)	(((x) & (RF|WF)) == 0)
112 #define	ISEXTRACT(x)	(((x) & (RF|WF)) == RF)
113 #define ISARCHIVE(x)	(((x) & (AF|RF|WF)) == WF)
114 #define ISAPPND(x)	(((x) & (AF|RF|WF)) == (AF|WF))
115 #define	ISCOPY(x)	(((x) & (RF|WF)) == (RF|WF))
116 #define	ISWRITE(x)	(((x) & (RF|WF)) == WF)
117 
118 /*
119  * Illegal option flag subsets based on pax operation
120  */
121 
122 #define	BDEXTR	(AF|BF|LF|TF|WF|XF|CBF|CHF|CLF|CPF|CXF)
123 #define	BDARCH	(CF|KF|LF|NF|PF|RF|CDF|CEF|CYF|CZF)
124 #define	BDCOPY	(AF|BF|FF|OF|XF|CBF|CEF)
125 #define	BDLIST (AF|BF|IF|KF|LF|OF|PF|RF|TF|UF|WF|XF|CBF|CDF|CHF|CLF|CPF|CXF|CYF|CZF)
126 
127 
128 /*
129  * Routines which handle command line options
130  */
131 
132 static char flgch[] = FLGCH;	/* list of all possible flags */
133 static OPLIST *ophead = NULL;	/* head for format specific options -x */
134 static OPLIST *optail = NULL;	/* option tail */
135 
136 static int no_op(void);
137 static void printflg(unsigned int);
138 static off_t str_offt(char *);
139 static char *get_line(FILE *fp);
140 static void pax_options(int, char **);
141 static void pax_usage(void);
142 static void tar_options(int, char **);
143 static void tar_usage(void);
144 static void cpio_options(int, char **);
145 static void cpio_usage(void);
146 
147 static int compress_id(char *_blk, int _size);
148 static int gzip_id(char *_blk, int _size);
149 static int bzip2_id(char *_blk, int _size);
150 static int xz_id(char *_blk, int _size);
151 
152 #define GZIP_CMD	"gzip"		/* command to run as gzip */
153 #define COMPRESS_CMD	"compress"	/* command to run as compress */
154 #define BZIP2_CMD	"bzip2"		/* command to run as bzip2 */
155 
156 /*
157  *	Format specific routine table
158  *	(see pax.h for description of each function)
159  *
160  *	name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
161  *	read, end_read, st_write, write, end_write, trail,
162  *	rd_data, wr_data, options
163  */
164 
165 FSUB fsub[] = {
166 #ifdef NOCPIO
167 /* 0: OLD BINARY CPIO */
168 	{ },
169 /* 1: OLD OCTAL CHARACTER CPIO */
170 	{ },
171 /* 2: SVR4 HEX CPIO */
172 	{ },
173 /* 3: SVR4 HEX CPIO WITH CRC */
174 	{ },
175 #else
176 /* 0: OLD BINARY CPIO */
177 	{"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
178 	bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
179 	bad_opt},
180 
181 /* 1: OLD OCTAL CHARACTER CPIO */
182 	{"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
183 	cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
184 	bad_opt},
185 
186 /* 2: SVR4 HEX CPIO */
187 	{"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
188 	vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
189 	bad_opt},
190 
191 /* 3: SVR4 HEX CPIO WITH CRC */
192 	{"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
193 	vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
194 	bad_opt},
195 #endif
196 /* 4: OLD TAR */
197 	{"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
198 	tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
199 	tar_opt},
200 
201 /* 5: POSIX USTAR */
202 	{"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
203 	ustar_rd, tar_endrd, no_op, ustar_wr, tar_endwr, tar_trail,
204 	tar_opt},
205 
206 #ifdef SMALL
207 /* 6: compress, to detect failure to use -Z */
208 	{ },
209 /* 7: xz, to detect failure to decompress it */
210 	{ },
211 /* 8: bzip2, to detect failure to use -j */
212 	{ },
213 /* 9: gzip, to detect failure to use -z */
214 	{ },
215 #else
216 /* 6: compress, to detect failure to use -Z */
217 	{NULL, 0, 4, 0, 0, 0, 0, compress_id},
218 /* 7: xz, to detect failure to decompress it */
219 	{NULL, 0, 4, 0, 0, 0, 0, xz_id},
220 /* 8: bzip2, to detect failure to use -j */
221 	{NULL, 0, 4, 0, 0, 0, 0, bzip2_id},
222 /* 9: gzip, to detect failure to use -z */
223 	{NULL, 0, 4, 0, 0, 0, 0, gzip_id},
224 #endif
225 };
226 #define	F_OCPIO	0	/* format when called as cpio -6 */
227 #define	F_ACPIO	1	/* format when called as cpio -c */
228 #define	F_CPIO	3	/* format when called as cpio */
229 #define F_OTAR	4	/* format when called as tar -o */
230 #define F_TAR	5	/* format when called as tar */
231 #define DEFLT	5	/* default write format from list above */
232 
233 /*
234  * ford is the archive search order used by get_arc() to determine what kind
235  * of archive we are dealing with. This helps to properly id archive formats
236  * some formats may be subsets of others....
237  */
238 int ford[] = {5, 4, 9, 8, 7, 6, 3, 2, 1, 0, -1};
239 
240 /*
241  * Do we have -C anywhere and what is it?
242  */
243 int havechd = 0;
244 char *chdname = NULL;
245 
246 /*
247  * options()
248  *	figure out if we are pax, tar or cpio. Call the appropriate options
249  *	parser
250  */
251 
252 void
253 options(int argc, char **argv)
254 {
255 	extern char *__progname;
256 
257 	/*
258 	 * Are we acting like pax, tar or cpio (based on argv[0])
259 	 */
260 	argv0 = __progname;
261 
262 	if (strcmp(NM_TAR, argv0) == 0) {
263 		op_mode = OP_TAR;
264 		tar_options(argc, argv);
265 		return;
266 	}
267 #ifndef NOCPIO
268 	else if (strcmp(NM_CPIO, argv0) == 0) {
269 		op_mode = OP_CPIO;
270 		cpio_options(argc, argv);
271 		return;
272 	}
273 #endif /* !NOCPIO */
274 	/*
275 	 * assume pax as the default
276 	 */
277 	argv0 = NM_PAX;
278 	op_mode = OP_PAX;
279 	pax_options(argc, argv);
280 }
281 
282 /*
283  * pax_options()
284  *	look at the user specified flags. set globals as required and check if
285  *	the user specified a legal set of flags. If not, complain and exit
286  */
287 
288 static void
289 pax_options(int argc, char **argv)
290 {
291 	int c;
292 	unsigned i;
293 	unsigned int flg = 0;
294 	unsigned int bflg = 0;
295 	const char *errstr;
296 	char *pt;
297 
298 	/*
299 	 * process option flags
300 	 */
301 	while ((c=getopt(argc,argv,"ab:cdf:ijklno:p:rs:tuvwx:zB:DE:G:HLOPT:U:XYZ0"))
302 	    != -1) {
303 		switch (c) {
304 		case 'a':
305 			/*
306 			 * append
307 			 */
308 			flg |= AF;
309 			break;
310 		case 'b':
311 			/*
312 			 * specify blocksize
313 			 */
314 			flg |= BF;
315 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
316 				paxwarn(1, "Invalid block size %s", optarg);
317 				pax_usage();
318 			}
319 			break;
320 		case 'c':
321 			/*
322 			 * inverse match on patterns
323 			 */
324 			cflag = 1;
325 			flg |= CF;
326 			break;
327 		case 'd':
328 			/*
329 			 * match only dir on extract, not the subtree at dir
330 			 */
331 			dflag = 1;
332 			flg |= DF;
333 			break;
334 		case 'f':
335 			/*
336 			 * filename where the archive is stored
337 			 */
338 			arcname = optarg;
339 			flg |= FF;
340 			break;
341 		case 'i':
342 			/*
343 			 * interactive file rename
344 			 */
345 			iflag = 1;
346 			flg |= IF;
347 			break;
348 		case 'j':
349 			/*
350 			 * use bzip2.  Non standard option.
351 			 */
352 			gzip_program = BZIP2_CMD;
353 			break;
354 		case 'k':
355 			/*
356 			 * do not clobber files that exist
357 			 */
358 			kflag = 1;
359 			flg |= KF;
360 			break;
361 		case 'l':
362 			/*
363 			 * try to link src to dest with copy (-rw)
364 			 */
365 			lflag = 1;
366 			flg |= LF;
367 			break;
368 		case 'n':
369 			/*
370 			 * select first match for a pattern only
371 			 */
372 			nflag = 1;
373 			flg |= NF;
374 			break;
375 		case 'o':
376 			/*
377 			 * pass format specific options
378 			 */
379 			flg |= OF;
380 			if (opt_add(optarg) < 0)
381 				pax_usage();
382 			break;
383 		case 'p':
384 			/*
385 			 * specify file characteristic options
386 			 */
387 			for (pt = optarg; *pt != '\0'; ++pt) {
388 				switch (*pt) {
389 				case 'a':
390 					/*
391 					 * do not preserve access time
392 					 */
393 					patime = 0;
394 					break;
395 				case 'e':
396 					/*
397 					 * preserve user id, group id, file
398 					 * mode, access/modification times
399 					 */
400 					pids = 1;
401 					pmode = 1;
402 					patime = 1;
403 					pmtime = 1;
404 					break;
405 				case 'm':
406 					/*
407 					 * do not preserve modification time
408 					 */
409 					pmtime = 0;
410 					break;
411 				case 'o':
412 					/*
413 					 * preserve uid/gid
414 					 */
415 					pids = 1;
416 					break;
417 				case 'p':
418 					/*
419 					 * preserve file mode bits
420 					 */
421 					pmode = 1;
422 					break;
423 				default:
424 					paxwarn(1, "Invalid -p string: %c", *pt);
425 					pax_usage();
426 					break;
427 				}
428 			}
429 			flg |= PF;
430 			break;
431 		case 'r':
432 			/*
433 			 * read the archive
434 			 */
435 			flg |= RF;
436 			break;
437 		case 's':
438 			/*
439 			 * file name substitution name pattern
440 			 */
441 			if (rep_add(optarg) < 0) {
442 				pax_usage();
443 				break;
444 			}
445 			flg |= SF;
446 			break;
447 		case 't':
448 			/*
449 			 * preserve access time on filesystem nodes we read
450 			 */
451 			tflag = 1;
452 			flg |= TF;
453 			break;
454 		case 'u':
455 			/*
456 			 * ignore those older files
457 			 */
458 			uflag = 1;
459 			flg |= UF;
460 			break;
461 		case 'v':
462 			/*
463 			 * verbose operation mode
464 			 */
465 			vflag = 1;
466 			flg |= VF;
467 			break;
468 		case 'w':
469 			/*
470 			 * write an archive
471 			 */
472 			flg |= WF;
473 			break;
474 		case 'x':
475 			/*
476 			 * specify an archive format on write
477 			 */
478 			for (i = 0; i < sizeof(fsub)/sizeof(FSUB); ++i)
479 				if (fsub[i].name != NULL &&
480 				    strcmp(fsub[i].name, optarg) == 0)
481 					break;
482 			if (i < sizeof(fsub)/sizeof(FSUB)) {
483 				frmt = &fsub[i];
484 				flg |= XF;
485 				break;
486 			}
487 			paxwarn(1, "Unknown -x format: %s", optarg);
488 			(void)fputs("pax: Known -x formats are:", stderr);
489 			for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
490 				if (fsub[i].name != NULL)
491 					(void)fprintf(stderr, " %s",
492 					    fsub[i].name);
493 			(void)fputs("\n\n", stderr);
494 			pax_usage();
495 			break;
496 		case 'z':
497 			/*
498 			 * use gzip.  Non standard option.
499 			 */
500 			gzip_program = GZIP_CMD;
501 			break;
502 		case 'B':
503 			/*
504 			 * non-standard option on number of bytes written on a
505 			 * single archive volume.
506 			 */
507 			if ((wrlimit = str_offt(optarg)) <= 0) {
508 				paxwarn(1, "Invalid write limit %s", optarg);
509 				pax_usage();
510 			}
511 			if (wrlimit % BLKMULT) {
512 				paxwarn(1, "Write limit is not a %d byte multiple",
513 				    BLKMULT);
514 				pax_usage();
515 			}
516 			flg |= CBF;
517 			break;
518 		case 'D':
519 			/*
520 			 * On extraction check file inode change time before the
521 			 * modification of the file name. Non standard option.
522 			 */
523 			Dflag = 1;
524 			flg |= CDF;
525 			break;
526 		case 'E':
527 			/*
528 			 * non-standard limit on read faults
529 			 * 0 indicates stop after first error, values
530 			 * indicate a limit
531 			 */
532 			flg |= CEF;
533 			maxflt = strtonum(optarg, 0, INT_MAX, &errstr);
534 			if (errstr) {
535 				paxwarn(1, "Error count value: %s", errstr);
536 				pax_usage();
537 			}
538 			break;
539 		case 'G':
540 			/*
541 			 * non-standard option for selecting files within an
542 			 * archive by group (gid or name)
543 			 */
544 			if (grp_add(optarg) < 0) {
545 				pax_usage();
546 				break;
547 			}
548 			flg |= CGF;
549 			break;
550 		case 'H':
551 			/*
552 			 * follow command line symlinks only
553 			 */
554 			Hflag = 1;
555 			flg |= CHF;
556 			break;
557 		case 'L':
558 			/*
559 			 * follow symlinks
560 			 */
561 			Lflag = 1;
562 			flg |= CLF;
563 			break;
564 		case 'O':
565 			/*
566 			 * Force one volume.  Non standard option.
567 			 */
568 			force_one_volume = 1;
569 			break;
570 		case 'P':
571 			/*
572 			 * do NOT follow symlinks (default)
573 			 */
574 			Lflag = 0;
575 			flg |= CPF;
576 			break;
577 		case 'T':
578 			/*
579 			 * non-standard option for selecting files within an
580 			 * archive by modification time range (lower,upper)
581 			 */
582 			if (trng_add(optarg) < 0) {
583 				pax_usage();
584 				break;
585 			}
586 			flg |= CTF;
587 			break;
588 		case 'U':
589 			/*
590 			 * non-standard option for selecting files within an
591 			 * archive by user (uid or name)
592 			 */
593 			if (usr_add(optarg) < 0) {
594 				pax_usage();
595 				break;
596 			}
597 			flg |= CUF;
598 			break;
599 		case 'X':
600 			/*
601 			 * do not pass over mount points in the file system
602 			 */
603 			Xflag = 1;
604 			flg |= CXF;
605 			break;
606 		case 'Y':
607 			/*
608 			 * On extraction check file inode change time after the
609 			 * modification of the file name. Non standard option.
610 			 */
611 			Yflag = 1;
612 			flg |= CYF;
613 			break;
614 		case 'Z':
615 			/*
616 			 * On extraction check modification time after the
617 			 * modification of the file name. Non standard option.
618 			 */
619 			Zflag = 1;
620 			flg |= CZF;
621 			break;
622 		case '0':
623 			/*
624 			 * Use \0 as pathname terminator.
625 			 * (For use with the -print0 option of find(1).)
626 			 */
627 			zeroflag = 1;
628 			flg |= C0F;
629 			break;
630 		default:
631 			pax_usage();
632 			break;
633 		}
634 	}
635 
636 	/*
637 	 * figure out the operation mode of pax read,write,extract,copy,append
638 	 * or list. check that we have not been given a bogus set of flags
639 	 * for the operation mode.
640 	 */
641 	if (ISLIST(flg)) {
642 		act = LIST;
643 		listf = stdout;
644 		bflg = flg & BDLIST;
645 	} else if (ISEXTRACT(flg)) {
646 		act = EXTRACT;
647 		bflg = flg & BDEXTR;
648 	} else if (ISARCHIVE(flg)) {
649 		act = ARCHIVE;
650 		bflg = flg & BDARCH;
651 	} else if (ISAPPND(flg)) {
652 		act = APPND;
653 		bflg = flg & BDARCH;
654 	} else if (ISCOPY(flg)) {
655 		act = COPY;
656 		bflg = flg & BDCOPY;
657 	} else
658 		pax_usage();
659 	if (bflg) {
660 		printflg(flg);
661 		pax_usage();
662 	}
663 
664 	/*
665 	 * if we are writing (ARCHIVE) we use the default format if the user
666 	 * did not specify a format. when we write during an APPEND, we will
667 	 * adopt the format of the existing archive if none was supplied.
668 	 */
669 	if (!(flg & XF) && (act == ARCHIVE))
670 		frmt = &(fsub[DEFLT]);
671 
672 	/*
673 	 * process the args as they are interpreted by the operation mode
674 	 */
675 	switch (act) {
676 	case LIST:
677 	case EXTRACT:
678 		for (; optind < argc; optind++)
679 			if (pat_add(argv[optind], NULL) < 0)
680 				pax_usage();
681 		break;
682 	case COPY:
683 		if (optind >= argc) {
684 			paxwarn(0, "Destination directory was not supplied");
685 			pax_usage();
686 		}
687 		--argc;
688 		dirptr = argv[argc];
689 		/* FALL THROUGH */
690 	case ARCHIVE:
691 	case APPND:
692 		for (; optind < argc; optind++)
693 			if (ftree_add(argv[optind], 0) < 0)
694 				pax_usage();
695 		/*
696 		 * no read errors allowed on updates/append operation!
697 		 */
698 		maxflt = 0;
699 		break;
700 	}
701 }
702 
703 
704 /*
705  * tar_options()
706  *	look at the user specified flags. set globals as required and check if
707  *	the user specified a legal set of flags. If not, complain and exit
708  */
709 
710 static void
711 tar_options(int argc, char **argv)
712 {
713 	int c;
714 	int Oflag = 0;
715 	int nincfiles = 0;
716 	int incfiles_max = 0;
717 	struct incfile {
718 		char *file;
719 		char *dir;
720 	};
721 	struct incfile *incfiles = NULL;
722 
723 	/*
724 	 * Set default values.
725 	 */
726 	rmleadslash = 1;
727 
728 	/*
729 	 * process option flags
730 	 */
731 	while ((c = getoldopt(argc, argv,
732 	    "b:cef:hjmopqruts:vwxzBC:HI:LNOPXZ014578")) != -1) {
733 		switch (c) {
734 		case 'b':
735 			/*
736 			 * specify blocksize in 512-byte blocks
737 			 */
738 			if ((wrblksz = (int)str_offt(optarg)) <= 0) {
739 				paxwarn(1, "Invalid block size %s", optarg);
740 				tar_usage();
741 			}
742 			wrblksz *= 512;		/* XXX - check for int oflow */
743 			break;
744 		case 'c':
745 			/*
746 			 * create an archive
747 			 */
748 			act = ARCHIVE;
749 			break;
750 		case 'e':
751 			/*
752 			 * stop after first error
753 			 */
754 			maxflt = 0;
755 			break;
756 		case 'f':
757 			/*
758 			 * filename where the archive is stored
759 			 */
760 			arcname = optarg;
761 			break;
762 		case 'h':
763 			/*
764 			 * follow symlinks
765 			 */
766 			Lflag = 1;
767 			break;
768 		case 'j':
769 			/*
770 			 * use bzip2.  Non standard option.
771 			 */
772 			gzip_program = BZIP2_CMD;
773 			break;
774 		case 'm':
775 			/*
776 			 * do not preserve modification time
777 			 */
778 			pmtime = 0;
779 			break;
780 		case 'O':
781 			Oflag = 1;
782 			break;
783 		case 'o':
784 			Oflag = 2;
785 			tar_nodir = 1;
786 			break;
787 		case 'p':
788 			/*
789 			 * preserve uid/gid and file mode, regardless of umask
790 			 */
791 			pmode = 1;
792 			pids = 1;
793 			break;
794 		case 'q':
795 			/*
796 			 * select first match for a pattern only
797 			 */
798 			nflag = 1;
799 			break;
800 		case 'r':
801 		case 'u':
802 			/*
803 			 * append to the archive
804 			 */
805 			act = APPND;
806 			break;
807 		case 's':
808 			/*
809 			 * file name substitution name pattern
810 			 */
811 			if (rep_add(optarg) < 0) {
812 				tar_usage();
813 				break;
814 			}
815 			break;
816 		case 't':
817 			/*
818 			 * list contents of the tape
819 			 */
820 			act = LIST;
821 			break;
822 		case 'v':
823 			/*
824 			 * verbose operation mode
825 			 */
826 			vflag++;
827 			break;
828 		case 'w':
829 			/*
830 			 * interactive file rename
831 			 */
832 			iflag = 1;
833 			break;
834 		case 'x':
835 			/*
836 			 * extract an archive, preserving mode,
837 			 * and mtime if possible.
838 			 */
839 			act = EXTRACT;
840 			pmtime = 1;
841 			break;
842 		case 'z':
843 			/*
844 			 * use gzip.  Non standard option.
845 			 */
846 			gzip_program = GZIP_CMD;
847 			break;
848 		case 'B':
849 			/*
850 			 * Nothing to do here, this is pax default
851 			 */
852 			break;
853 		case 'C':
854 			havechd++;
855 			chdname = optarg;
856 			break;
857 		case 'H':
858 			/*
859 			 * follow command line symlinks only
860 			 */
861 			Hflag = 1;
862 			break;
863 		case 'I':
864 			if (++nincfiles > incfiles_max) {
865 				size_t n = nincfiles + 3;
866 				struct incfile *p;
867 
868 				p = reallocarray(incfiles, n,
869 				    sizeof(*incfiles));
870 				if (p == NULL) {
871 					paxwarn(0, "Unable to allocate space "
872 					    "for option list");
873 					exit(1);
874 				}
875 				incfiles = p;
876 				incfiles_max = n;
877 			}
878 			incfiles[nincfiles - 1].file = optarg;
879 			incfiles[nincfiles - 1].dir = chdname;
880 			break;
881 		case 'L':
882 			/*
883 			 * follow symlinks
884 			 */
885 			Lflag = 1;
886 			break;
887 		case 'N':
888 			/* numeric uid and gid only */
889 			Nflag = 1;
890 			break;
891 		case 'P':
892 			/*
893 			 * do not remove leading '/' from pathnames
894 			 */
895 			rmleadslash = 0;
896 			break;
897 		case 'X':
898 			/*
899 			 * do not pass over mount points in the file system
900 			 */
901 			Xflag = 1;
902 			break;
903 		case 'Z':
904 			/*
905 			 * use compress.
906 			 */
907 			gzip_program = COMPRESS_CMD;
908 			break;
909 		case '0':
910 			arcname = DEV_0;
911 			break;
912 		case '1':
913 			arcname = DEV_1;
914 			break;
915 		case '4':
916 			arcname = DEV_4;
917 			break;
918 		case '5':
919 			arcname = DEV_5;
920 			break;
921 		case '7':
922 			arcname = DEV_7;
923 			break;
924 		case '8':
925 			arcname = DEV_8;
926 			break;
927 		default:
928 			tar_usage();
929 			break;
930 		}
931 	}
932 	argc -= optind;
933 	argv += optind;
934 
935 	if ((arcname == NULL) || (*arcname == '\0')) {
936 		arcname = getenv("TAPE");
937 		if ((arcname == NULL) || (*arcname == '\0'))
938 			arcname = _PATH_DEFTAPE;
939 	}
940 	if ((arcname[0] == '-') && (arcname[1]== '\0'))
941 		arcname = NULL;
942 
943 	/*
944 	 * Traditional tar behaviour: list-like output goes to stdout unless
945 	 * writing the archive there.  (pax uses stderr unless in list mode)
946 	 */
947         if (act == LIST || act == EXTRACT || arcname != NULL)
948 		listf = stdout;
949 
950 	/* Traditional tar behaviour (pax wants to read file list from stdin) */
951 	if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
952 		exit(0);
953 
954 	/*
955 	 * process the args as they are interpreted by the operation mode
956 	 */
957 	switch (act) {
958 	case LIST:
959 	case EXTRACT:
960 	default:
961 		{
962 			int sawpat = 0;
963 			char *file, *dir;
964 
965 			while (nincfiles || *argv != NULL) {
966 				/*
967 				 * If we queued up any include files,
968 				 * pull them in now.  Otherwise, check
969 				 * for -I and -C positional flags.
970 				 * Anything else must be a file to
971 				 * extract.
972 				 */
973 				if (nincfiles) {
974 					file = incfiles->file;
975 					dir = incfiles->dir;
976 					incfiles++;
977 					nincfiles--;
978 				} else if (strcmp(*argv, "-I") == 0) {
979 					if (*++argv == NULL)
980 						break;
981 					file = *argv++;
982 					dir = chdname;
983 				} else
984 					file = NULL;
985 				if (file != NULL) {
986 					FILE *fp;
987 					char *str;
988 
989 					if (strcmp(file, "-") == 0)
990 						fp = stdin;
991 					else if ((fp = fopen(file, "r")) == NULL) {
992 						syswarn(1, errno,
993 						    "Unable to open %s", file);
994 						tar_usage();
995 					}
996 					while ((str = get_line(fp)) != NULL) {
997 						if (pat_add(str, dir) < 0)
998 							tar_usage();
999 						sawpat = 1;
1000 					}
1001 					if (ferror(fp)) {
1002 						syswarn(1, errno,
1003 						    "Unable to read from %s",
1004 						    strcmp(file, "-") ? file :
1005 						    "stdin");
1006 						tar_usage();
1007 					}
1008 					if (strcmp(file, "-") != 0)
1009 						fclose(fp);
1010 				} else if (strcmp(*argv, "-C") == 0) {
1011 					if (*++argv == NULL)
1012 						break;
1013 					chdname = *argv++;
1014 					havechd++;
1015 				} else if (pat_add(*argv++, chdname) < 0)
1016 					tar_usage();
1017 				else
1018 					sawpat = 1;
1019 			}
1020 			/*
1021 			 * if patterns were added, we are doing	chdir()
1022 			 * on a file-by-file basis, else, just one
1023 			 * global chdir (if any) after opening input.
1024 			 */
1025 			if (sawpat > 0)
1026 				chdname = NULL;
1027 		}
1028 		break;
1029 	case ARCHIVE:
1030 	case APPND:
1031 		frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
1032 
1033 		if (chdname != NULL) {	/* initial chdir() */
1034 			if (ftree_add(chdname, 1) < 0)
1035 				tar_usage();
1036 		}
1037 
1038 		while (nincfiles || *argv != NULL) {
1039 			char *file, *dir;
1040 
1041 			/*
1042 			 * If we queued up any include files, pull them in
1043 			 * now.  Otherwise, check for -I and -C positional
1044 			 * flags.  Anything else must be a file to include
1045 			 * in the archive.
1046 			 */
1047 			if (nincfiles) {
1048 				file = incfiles->file;
1049 				dir = incfiles->dir;
1050 				incfiles++;
1051 				nincfiles--;
1052 			} else if (strcmp(*argv, "-I") == 0) {
1053 				if (*++argv == NULL)
1054 					break;
1055 				file = *argv++;
1056 				dir = NULL;
1057 			} else
1058 				file = NULL;
1059 			if (file != NULL) {
1060 				FILE *fp;
1061 				char *str;
1062 
1063 				/* Set directory if needed */
1064 				if (dir) {
1065 					if (ftree_add(dir, 1) < 0)
1066 						tar_usage();
1067 				}
1068 
1069 				if (strcmp(file, "-") == 0)
1070 					fp = stdin;
1071 				else if ((fp = fopen(file, "r")) == NULL) {
1072 					syswarn(1, errno, "Unable to open %s",
1073 					    file);
1074 					tar_usage();
1075 				}
1076 				while ((str = get_line(fp)) != NULL) {
1077 					if (ftree_add(str, 0) < 0)
1078 						tar_usage();
1079 				}
1080 				if (ferror(fp)) {
1081 					syswarn(1, errno,
1082 					    "Unable to read from %s",
1083 					    strcmp(file, "-") ? file : "stdin");
1084 					tar_usage();
1085 				}
1086 				if (strcmp(file, "-") != 0)
1087 					fclose(fp);
1088 			} else if (strcmp(*argv, "-C") == 0) {
1089 				if (*++argv == NULL)
1090 					break;
1091 				if (ftree_add(*argv++, 1) < 0)
1092 					tar_usage();
1093 				havechd++;
1094 			} else if (ftree_add(*argv++, 0) < 0)
1095 				tar_usage();
1096 		}
1097 		/*
1098 		 * no read errors allowed on updates/append operation!
1099 		 */
1100 		maxflt = 0;
1101 		break;
1102 	}
1103 }
1104 
1105 int mkpath(char *);
1106 
1107 int
1108 mkpath(path)
1109 	char *path;
1110 {
1111 	struct stat sb;
1112 	char *slash;
1113 	int done = 0;
1114 
1115 	slash = path;
1116 
1117 	while (!done) {
1118 		slash += strspn(slash, "/");
1119 		slash += strcspn(slash, "/");
1120 
1121 		done = (*slash == '\0');
1122 		*slash = '\0';
1123 
1124 		if (stat(path, &sb)) {
1125 			if (errno != ENOENT || mkdir(path, 0777)) {
1126 				paxwarn(1, "%s", path);
1127 				return (-1);
1128 			}
1129 		} else if (!S_ISDIR(sb.st_mode)) {
1130 			syswarn(1, ENOTDIR, "%s", path);
1131 			return (-1);
1132 		}
1133 
1134 		if (!done)
1135 			*slash = '/';
1136 	}
1137 
1138 	return (0);
1139 }
1140 
1141 #ifndef NOCPIO
1142 /*
1143  * cpio_options()
1144  *	look at the user specified flags. set globals as required and check if
1145  *	the user specified a legal set of flags. If not, complain and exit
1146  */
1147 
1148 static void
1149 cpio_options(int argc, char **argv)
1150 {
1151 	const char *errstr;
1152 	int c, list_only = 0;
1153 	unsigned i;
1154 	char *str;
1155 	FILE *fp;
1156 
1157 	kflag = 1;
1158 	pids = 1;
1159 	pmode = 1;
1160 	pmtime = 0;
1161 	arcname = NULL;
1162 	dflag = 1;
1163 	act = -1;
1164 	nodirs = 1;
1165 	while ((c=getopt(argc,argv,"abcdfijklmoprstuvzABC:E:F:H:I:LO:SZ6")) != -1)
1166 		switch (c) {
1167 			case 'a':
1168 				/*
1169 				 * preserve access time on files read
1170 				 */
1171 				tflag = 1;
1172 				break;
1173 			case 'b':
1174 				/*
1175 				 * swap bytes and half-words when reading data
1176 				 */
1177 				break;
1178 			case 'c':
1179 				/*
1180 				 * ASCII cpio header
1181 				 */
1182 				frmt = &(fsub[F_ACPIO]);
1183 				break;
1184 			case 'd':
1185 				/*
1186 				 * create directories as needed
1187 				 */
1188 				nodirs = 0;
1189 				break;
1190 			case 'f':
1191 				/*
1192 				 * invert meaning of pattern list
1193 				 */
1194 				cflag = 1;
1195 				break;
1196 			case 'i':
1197 				/*
1198 				 * restore an archive
1199 				 */
1200 				act = EXTRACT;
1201 				break;
1202 			case 'j':
1203 				/*
1204 				 * use bzip2.  Non standard option.
1205 				 */
1206 				gzip_program = BZIP2_CMD;
1207 				break;
1208 			case 'k':
1209 				break;
1210 			case 'l':
1211 				/*
1212 				 * use links instead of copies when possible
1213 				 */
1214 				lflag = 1;
1215 				break;
1216 			case 'm':
1217 				/*
1218 				 * preserve modification time
1219 				 */
1220 				pmtime = 1;
1221 				break;
1222 			case 'o':
1223 				/*
1224 				 * create an archive
1225 				 */
1226 				act = ARCHIVE;
1227 				frmt = &(fsub[F_CPIO]);
1228 				break;
1229 			case 'p':
1230 				/*
1231 				 * copy-pass mode
1232 				 */
1233 				act = COPY;
1234 				break;
1235 			case 'r':
1236 				/*
1237 				 * interactively rename files
1238 				 */
1239 				iflag = 1;
1240 				break;
1241 			case 's':
1242 				/*
1243 				 * swap bytes after reading data
1244 				 */
1245 				break;
1246 			case 't':
1247 				/*
1248 				 * list contents of archive
1249 				 */
1250 				list_only = 1;
1251 				break;
1252 			case 'u':
1253 				/*
1254 				 * replace newer files
1255 				 */
1256 				kflag = 0;
1257 				break;
1258 			case 'v':
1259 				/*
1260 				 * verbose operation mode
1261 				 */
1262 				vflag = 1;
1263 				break;
1264 			case 'z':
1265 				/*
1266 				 * use gzip.  Non standard option.
1267 				 */
1268 				gzip_program = GZIP_CMD;
1269 				break;
1270 			case 'A':
1271 				/*
1272 				 * append mode
1273 				 */
1274 				act = APPND;
1275 				break;
1276 			case 'B':
1277 				/*
1278 				 * Use 5120 byte block size
1279 				 */
1280 				wrblksz = 5120;
1281 				break;
1282 			case 'C':
1283 				/*
1284 				 * set block size in bytes
1285 				 */
1286 				wrblksz = strtonum(optarg, 0, INT_MAX, &errstr);
1287 				if (errstr) {
1288 					paxwarn(1, "Invalid block size %s: %s",
1289 					    optarg, errstr);
1290 					pax_usage();
1291 				}
1292 				break;
1293 			case 'E':
1294 				/*
1295 				 * file with patterns to extract or list
1296 				 */
1297 				if ((fp = fopen(optarg, "r")) == NULL) {
1298 					syswarn(1, errno, "Unable to open %s",
1299 					    optarg);
1300 					cpio_usage();
1301 				}
1302 				while ((str = get_line(fp)) != NULL) {
1303 					pat_add(str, NULL);
1304 				}
1305 				if (ferror(fp)) {
1306 					syswarn(1, errno,
1307 					    "Unable to read from %s", optarg);
1308 					cpio_usage();
1309 				}
1310 				fclose(fp);
1311 				break;
1312 			case 'F':
1313 			case 'I':
1314 			case 'O':
1315 				/*
1316 				 * filename where the archive is stored
1317 				 */
1318 				if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1319 					/*
1320 					 * treat a - as stdin
1321 					 */
1322 					arcname = NULL;
1323 					break;
1324 				}
1325 				arcname = optarg;
1326 				break;
1327 			case 'H':
1328 				/*
1329 				 * specify an archive format on write
1330 				 */
1331 				for (i = 0; i < sizeof(fsub)/sizeof(FSUB); ++i)
1332 					if (fsub[i].name != NULL &&
1333 					    strcmp(fsub[i].name, optarg) == 0)
1334 						break;
1335 				if (i < sizeof(fsub)/sizeof(FSUB)) {
1336 					frmt = &fsub[i];
1337 					break;
1338 				}
1339 				paxwarn(1, "Unknown -H format: %s", optarg);
1340 				(void)fputs("cpio: Known -H formats are:", stderr);
1341 				for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1342 					if (fsub[i].name != NULL)
1343 						(void)fprintf(stderr, " %s",
1344 						    fsub[i].name);
1345 				(void)fputs("\n\n", stderr);
1346 				cpio_usage();
1347 				break;
1348 			case 'L':
1349 				/*
1350 				 * follow symbolic links
1351 				 */
1352 				Lflag = 1;
1353 				break;
1354 			case 'S':
1355 				/*
1356 				 * swap halfwords after reading data
1357 				 */
1358 				break;
1359 			case 'Z':
1360 				/*
1361 				 * use compress.  Non standard option.
1362 				 */
1363 				gzip_program = COMPRESS_CMD;
1364 				break;
1365 			case '6':
1366 				/*
1367 				 * process Version 6 cpio format
1368 				 */
1369 				frmt = &(fsub[F_OCPIO]);
1370 				break;
1371 			case '?':
1372 			default:
1373 				cpio_usage();
1374 				break;
1375 		}
1376 	argc -= optind;
1377 	argv += optind;
1378 
1379 	/*
1380 	 * process the args as they are interpreted by the operation mode
1381 	 */
1382 	switch (act) {
1383 		case EXTRACT:
1384 			if (list_only) {
1385 				act = LIST;
1386 
1387 				/*
1388 				 * cpio is like pax: list to stderr
1389 				 * unless in list mode
1390 				 */
1391 				listf = stdout;
1392 			}
1393 			while (*argv != NULL)
1394 				if (pat_add(*argv++, NULL) < 0)
1395 					cpio_usage();
1396 			break;
1397 		case COPY:
1398 			if (*argv == NULL) {
1399 				paxwarn(0, "Destination directory was not supplied");
1400 				cpio_usage();
1401 			}
1402 			dirptr = *argv;
1403 			if (mkpath(dirptr) < 0)
1404 				cpio_usage();
1405 			--argc;
1406 			++argv;
1407 			/* FALL THROUGH */
1408 		case ARCHIVE:
1409 		case APPND:
1410 			if (*argv != NULL)
1411 				cpio_usage();
1412 			/*
1413 			 * no read errors allowed on updates/append operation!
1414 			 */
1415 			maxflt = 0;
1416 			while ((str = get_line(stdin)) != NULL) {
1417 				ftree_add(str, 0);
1418 			}
1419 			if (ferror(stdin)) {
1420 				syswarn(1, errno, "Unable to read from %s",
1421 				    "stdin");
1422 				cpio_usage();
1423 			}
1424 			break;
1425 		default:
1426 			cpio_usage();
1427 			break;
1428 	}
1429 }
1430 #endif /* !NOCPIO */
1431 
1432 /*
1433  * printflg()
1434  *	print out those invalid flag sets found to the user
1435  */
1436 
1437 static void
1438 printflg(unsigned int flg)
1439 {
1440 	int nxt;
1441 	int pos = 0;
1442 
1443 	(void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1444 	while ((nxt = ffs(flg)) != 0) {
1445 		flg >>= nxt;
1446 		pos += nxt;
1447 		(void)fprintf(stderr, " -%c", flgch[pos-1]);
1448 	}
1449 	(void)putc('\n', stderr);
1450 }
1451 
1452 /*
1453  * opt_next()
1454  *	called by format specific options routines to get each format specific
1455  *	flag and value specified with -o
1456  * Return:
1457  *	pointer to next OPLIST entry or NULL (end of list).
1458  */
1459 
1460 OPLIST *
1461 opt_next(void)
1462 {
1463 	OPLIST *opt;
1464 
1465 	if ((opt = ophead) != NULL)
1466 		ophead = ophead->fow;
1467 	return(opt);
1468 }
1469 
1470 /*
1471  * bad_opt()
1472  *	generic routine used to complain about a format specific options
1473  *	when the format does not support options.
1474  */
1475 
1476 int
1477 bad_opt(void)
1478 {
1479 	OPLIST *opt;
1480 
1481 	if (ophead == NULL)
1482 		return(0);
1483 	/*
1484 	 * print all we were given
1485 	 */
1486 	paxwarn(1,"These format options are not supported");
1487 	while ((opt = opt_next()) != NULL)
1488 		(void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1489 	pax_usage();
1490 	return(0);
1491 }
1492 
1493 /*
1494  * opt_add()
1495  *	breaks the value supplied to -o into a option name and value. options
1496  *	are given to -o in the form -o name-value,name=value
1497  *	multiple -o may be specified.
1498  * Return:
1499  *	0 if format in name=value format, -1 if -o is passed junk
1500  */
1501 
1502 int
1503 opt_add(const char *str)
1504 {
1505 	OPLIST *opt;
1506 	char *frpt;
1507 	char *pt;
1508 	char *endpt;
1509 	char *dstr;
1510 
1511 	if ((str == NULL) || (*str == '\0')) {
1512 		paxwarn(0, "Invalid option name");
1513 		return(-1);
1514 	}
1515 	if ((dstr = strdup(str)) == NULL) {
1516 		paxwarn(0, "Unable to allocate space for option list");
1517 		return(-1);
1518 	}
1519 	frpt = endpt = dstr;
1520 
1521 	/*
1522 	 * break into name and values pieces and stuff each one into a
1523 	 * OPLIST structure. When we know the format, the format specific
1524 	 * option function will go through this list
1525 	 */
1526 	while ((frpt != NULL) && (*frpt != '\0')) {
1527 		if ((endpt = strchr(frpt, ',')) != NULL)
1528 			*endpt = '\0';
1529 		if ((pt = strchr(frpt, '=')) == NULL) {
1530 			paxwarn(0, "Invalid options format");
1531 			free(dstr);
1532 			return(-1);
1533 		}
1534 		if ((opt = malloc(sizeof(OPLIST))) == NULL) {
1535 			paxwarn(0, "Unable to allocate space for option list");
1536 			free(dstr);
1537 			return(-1);
1538 		}
1539 		dstr = NULL;	/* parts of string going onto the OPLIST */
1540 		*pt++ = '\0';
1541 		opt->name = frpt;
1542 		opt->value = pt;
1543 		opt->fow = NULL;
1544 		if (endpt != NULL)
1545 			frpt = endpt + 1;
1546 		else
1547 			frpt = NULL;
1548 		if (ophead == NULL) {
1549 			optail = ophead = opt;
1550 			continue;
1551 		}
1552 		optail->fow = opt;
1553 		optail = opt;
1554 	}
1555 	free(dstr);
1556 	return(0);
1557 }
1558 
1559 /*
1560  * str_offt()
1561  *	Convert an expression of the following forms to an off_t > 0.
1562  *	1) A positive decimal number.
1563  *	2) A positive decimal number followed by a b (mult by 512).
1564  *	3) A positive decimal number followed by a k (mult by 1024).
1565  *	4) A positive decimal number followed by a m (mult by 512).
1566  *	5) A positive decimal number followed by a w (mult by sizeof int)
1567  *	6) Two or more positive decimal numbers (with/without k,b or w).
1568  *	   separated by x (also * for backwards compatibility), specifying
1569  *	   the product of the indicated values.
1570  * Return:
1571  *	0 for an error, a positive value o.w.
1572  */
1573 
1574 static off_t
1575 str_offt(char *val)
1576 {
1577 	char *expr;
1578 	off_t num, t;
1579 
1580 	num = strtoll(val, &expr, 0);
1581 	if ((num == LLONG_MAX) || (num <= 0) || (expr == val))
1582 		return(0);
1583 
1584 	switch (*expr) {
1585 	case 'b':
1586 		t = num;
1587 		num *= 512;
1588 		if (t > num)
1589 			return(0);
1590 		++expr;
1591 		break;
1592 	case 'k':
1593 		t = num;
1594 		num *= 1024;
1595 		if (t > num)
1596 			return(0);
1597 		++expr;
1598 		break;
1599 	case 'm':
1600 		t = num;
1601 		num *= 1048576;
1602 		if (t > num)
1603 			return(0);
1604 		++expr;
1605 		break;
1606 	case 'w':
1607 		t = num;
1608 		num *= sizeof(int);
1609 		if (t > num)
1610 			return(0);
1611 		++expr;
1612 		break;
1613 	}
1614 
1615 	switch (*expr) {
1616 		case '\0':
1617 			break;
1618 		case '*':
1619 		case 'x':
1620 			t = num;
1621 			num *= str_offt(expr + 1);
1622 			if (t > num)
1623 				return(0);
1624 			break;
1625 		default:
1626 			return(0);
1627 	}
1628 	return(num);
1629 }
1630 
1631 char *
1632 get_line(FILE *f)
1633 {
1634 	char *str = NULL;
1635 	size_t size = 0;
1636 	ssize_t len;
1637 
1638 	do {
1639 		len = getline(&str, &size, f);
1640 		if (len == -1) {
1641 			free(str);
1642 			return NULL;
1643 		}
1644 		if (str[len - 1] == '\n')
1645 			str[len - 1] = '\0';
1646 	} while (str[0] == '\0');
1647 	return str;
1648 }
1649 
1650 /*
1651  * no_op()
1652  *	for those option functions where the archive format has nothing to do.
1653  * Return:
1654  *	0
1655  */
1656 
1657 static int
1658 no_op(void)
1659 {
1660 	return(0);
1661 }
1662 
1663 /*
1664  * pax_usage()
1665  *	print the usage summary to the user
1666  */
1667 
1668 void
1669 pax_usage(void)
1670 {
1671 	(void)fputs(
1672 	    "usage: pax [-0cdjnOvz] [-E limit] [-f archive] [-G group] [-s replstr]\n"
1673 	    "           [-T range] [-U user] [pattern ...]\n"
1674 	    "       pax -r [-0cDdijknOuvYZz] [-E limit] [-f archive] [-G group] [-o options]\n"
1675 	    "           [-p string] [-s replstr] [-T range] [-U user] [pattern ...]\n"
1676 	    "       pax -w [-0adHijLOPtuvXz] [-B bytes] [-b blocksize] [-f archive]\n"
1677 	    "           [-G group] [-o options] [-s replstr] [-T range] [-U user]\n"
1678 	    "           [-x format] [file ...]\n"
1679 	    "       pax -rw [-0DdHikLlnOPtuvXYZ] [-G group] [-p string] [-s replstr]\n"
1680 	    "           [-T range] [-U user] [file ...] directory\n",
1681 	    stderr);
1682 	exit(1);
1683 }
1684 
1685 /*
1686  * tar_usage()
1687  *	print the usage summary to the user
1688  */
1689 
1690 void
1691 tar_usage(void)
1692 {
1693 	(void)fputs(
1694 	    "usage: tar {crtux}[014578befHhjLmNOoPpqsvwXZz]\n"
1695 	    "           [blocking-factor | archive | replstr] [-C directory] [-I file]\n"
1696 	    "           [file ...]\n"
1697 	    "       tar {-crtux} [-014578eHhjLmNOoPpqvwXZz] [-b blocking-factor]\n"
1698 	    "           [-C directory] [-f archive] [-I file] [-s replstr] [file ...]\n",
1699 	    stderr);
1700 	exit(1);
1701 }
1702 
1703 #ifndef NOCPIO
1704 /*
1705  * cpio_usage()
1706  *	print the usage summary to the user
1707  */
1708 
1709 void
1710 cpio_usage(void)
1711 {
1712 	(void)fputs(
1713 	    "usage: cpio -o [-AaBcjLvZz] [-C bytes] [-F archive] [-H format]\n"
1714 	    "            [-O archive] < name-list [> archive]\n"
1715 	    "       cpio -i [-6BbcdfjmrSstuvZz] [-C bytes] [-E file] [-F archive] [-H format]\n"
1716 	    "            [-I archive] [pattern ...] [< archive]\n"
1717 	    "       cpio -p [-adLlmuv] destination-directory < name-list\n",
1718 	    stderr);
1719 	exit(1);
1720 }
1721 #endif /* !NOCPIO */
1722 
1723 #ifndef SMALL
1724 static int
1725 compress_id(char *blk, int size)
1726 {
1727 	if (size >= 2 && blk[0] == '\037' && blk[1] == '\235') {
1728 		paxwarn(0, "input compressed with %s; use the -%c option"
1729 		    " to decompress it", "compress", 'Z');
1730 		exit(1);
1731 	}
1732 	return (-1);
1733 }
1734 
1735 static int
1736 gzip_id(char *blk, int size)
1737 {
1738 	if (size >= 2 && blk[0] == '\037' && blk[1] == '\213') {
1739 		paxwarn(0, "input compressed with %s; use the -%c option"
1740 		    " to decompress it", "gzip", 'z');
1741 		exit(1);
1742 	}
1743 	return (-1);
1744 }
1745 
1746 static int
1747 bzip2_id(char *blk, int size)
1748 {
1749 	if (size >= 3 && blk[0] == 'B' && blk[1] == 'Z' && blk[2] == 'h') {
1750 		paxwarn(0, "input compressed with %s; use the -%c option"
1751 		    " to decompress it", "bzip2", 'j');
1752 		exit(1);
1753 	}
1754 	return (-1);
1755 }
1756 
1757 static int
1758 xz_id(char *blk, int size)
1759 {
1760 	if (size >= 6 && memcmp(blk, "\xFD\x37\x7A\x58\x5A", 6) == 0) {
1761 		paxwarn(0, "input compressed with xz");
1762 		exit(1);
1763 	}
1764 	return (-1);
1765 }
1766 #endif /* !SMALL */
1767