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