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