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