Sync some manuals from bin & sbin with NetBSD-8
[minix.git] / bin / pax / options.c
blob1af5fe4b7c0fdf39b9fc2d852df0ca9051011075
1 /* $NetBSD: options.c,v 1.116 2015/04/11 15:41:33 christos Exp $ */
3 /*-
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
40 #include <sys/cdefs.h>
41 #if !defined(lint)
42 #if 0
43 static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
44 #else
45 __RCSID("$NetBSD: options.c,v 1.116 2015/04/11 15:41:33 christos Exp $");
46 #endif
47 #endif /* not lint */
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/param.h>
53 #include <ctype.h>
54 #include <errno.h>
55 #if HAVE_NBTOOL_CONFIG_H
56 #include "compat_getopt.h"
57 #else
58 #include <getopt.h>
59 #endif
60 #include <limits.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <inttypes.h>
66 #include <paths.h>
67 #include "pax.h"
68 #include "options.h"
69 #include "cpio.h"
70 #include "tar.h"
71 #include "extern.h"
72 #ifndef SMALL
73 #include "mtree.h"
74 #endif /* SMALL */
77 * Routines which handle command line options
80 static int nopids; /* tar mode: suppress "pids" for -p option */
81 static char flgch[] = FLGCH; /* list of all possible flags (pax) */
82 static OPLIST *ophead = NULL; /* head for format specific options -x */
83 static OPLIST *optail = NULL; /* option tail */
85 static int opt_add(const char *);
86 static int no_op(void);
87 static void printflg(unsigned int);
88 static int c_frmt(const void *, const void *);
89 static off_t str_offt(char *);
90 static char *get_line(FILE *fp);
91 static void pax_options(int, char **);
92 __dead static void pax_usage(void);
93 static void tar_options(int, char **);
94 __dead static void tar_usage(void);
95 #ifndef NO_CPIO
96 static void cpio_options(int, char **);
97 __dead static void cpio_usage(void);
98 #endif
100 /* errors from get_line */
101 #define GETLINE_FILE_CORRUPT 1
102 #define GETLINE_OUT_OF_MEM 2
103 static int get_line_error;
105 #define BZIP2_CMD "bzip2" /* command to run as bzip2 */
106 #define GZIP_CMD "gzip" /* command to run as gzip */
107 #define XZ_CMD "xz" /* command to run as xz */
108 #define COMPRESS_CMD "compress" /* command to run as compress */
111 * Long options.
113 #define OPT_USE_COMPRESS_PROGRAM 0
114 #define OPT_CHECKPOINT 1
115 #define OPT_UNLINK 2
116 #define OPT_HELP 3
117 #define OPT_ATIME_PRESERVE 4
118 #define OPT_IGNORE_FAILED_READ 5
119 #define OPT_REMOVE_FILES 6
120 #define OPT_NULL 7
121 #define OPT_TOTALS 8
122 #define OPT_VERSION 9
123 #define OPT_EXCLUDE 10
124 #define OPT_BLOCK_COMPRESS 11
125 #define OPT_NORECURSE 12
126 #define OPT_FORCE_LOCAL 13
127 #define OPT_INSECURE 14
128 #define OPT_STRICT 15
129 #define OPT_SPARSE 16
130 #define OPT_XZ 17
131 #define OPT_GNU 18
132 #if !HAVE_NBTOOL_CONFIG_H
133 #define OPT_CHROOT 19
134 #endif
137 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
138 * (see pax.h for description of each function)
140 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
141 * read, end_read, st_write, write, end_write, trail,
142 * subtrail, rd_data, wr_data, options
145 FSUB fsub[] = {
146 #ifndef NO_CPIO
147 /* 0: OLD BINARY CPIO */
148 { "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
149 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
150 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
152 /* 1: OLD OCTAL CHARACTER CPIO */
153 { "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
154 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
155 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
157 /* 2: SVR4 HEX CPIO */
158 { "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
159 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
160 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
162 /* 3: SVR4 HEX CPIO WITH CRC */
163 { "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
164 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
165 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
166 #endif
167 /* 4: OLD TAR */
168 { "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
169 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
170 NULL, rd_wrfile, wr_rdfile, tar_opt },
172 /* 5: POSIX USTAR */
173 { "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
174 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
175 NULL, rd_wrfile, wr_rdfile, bad_opt }
177 #ifndef NO_CPIO
178 #define F_BCPIO 0 /* old binary cpio format */
179 #define F_CPIO 1 /* old octal character cpio format */
180 #define F_SV4CPIO 2 /* SVR4 hex cpio format */
181 #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
182 #define F_TAR 4 /* old V7 UNIX tar format */
183 #define F_USTAR 5 /* ustar format */
184 #else
185 #define F_TAR 0 /* old V7 UNIX tar format */
186 #define F_USTAR 1 /* ustar format */
187 #endif
188 #define DEFLT F_USTAR /* default write format from list above */
191 * ford is the archive search order used by get_arc() to determine what kind
192 * of archive we are dealing with. This helps to properly id archive formats
193 * some formats may be subsets of others....
195 int ford[] = {F_USTAR, F_TAR,
196 #ifndef NO_CPIO
197 F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO,
198 #endif
199 -1};
202 * filename record separator
204 int sep = '\n';
207 * Do we have -C anywhere?
209 int havechd = 0;
212 * options()
213 * figure out if we are pax, tar or cpio. Call the appropriate options
214 * parser
217 void
218 options(int argc, char **argv)
222 * Are we acting like pax, tar or cpio (based on argv[0])
224 if ((argv0 = strrchr(argv[0], '/')) != NULL)
225 argv0++;
226 else
227 argv0 = argv[0];
229 if (strstr(argv0, NM_TAR)) {
230 argv0 = NM_TAR;
231 tar_options(argc, argv);
232 #ifndef NO_CPIO
233 } else if (strstr(argv0, NM_CPIO)) {
234 argv0 = NM_CPIO;
235 cpio_options(argc, argv);
236 #endif
237 } else {
238 argv0 = NM_PAX;
239 pax_options(argc, argv);
243 struct option pax_longopts[] = {
244 { "insecure", no_argument, 0,
245 OPT_INSECURE },
246 { "force-local", no_argument, 0,
247 OPT_FORCE_LOCAL },
248 { "use-compress-program", required_argument, 0,
249 OPT_USE_COMPRESS_PROGRAM },
250 { "xz", no_argument, 0,
251 OPT_XZ },
252 { "gnu", no_argument, 0,
253 OPT_GNU },
254 { 0, 0, 0,
255 0 },
259 * pax_options()
260 * look at the user specified flags. set globals as required and check if
261 * the user specified a legal set of flags. If not, complain and exit
264 static void
265 pax_options(int argc, char **argv)
267 int c;
268 size_t i;
269 u_int64_t flg = 0;
270 u_int64_t bflg = 0;
271 char *pt;
272 FSUB tmp;
275 * process option flags
277 while ((c = getopt_long(argc, argv,
278 "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
279 pax_longopts, NULL)) != -1) {
280 switch (c) {
281 case '0':
282 sep = '\0';
283 break;
284 case 'a':
286 * append
288 flg |= AF;
289 break;
290 case 'b':
292 * specify blocksize
294 flg |= BF;
295 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
296 tty_warn(1, "Invalid block size %s", optarg);
297 pax_usage();
299 break;
300 case 'c':
302 * inverse match on patterns
304 cflag = 1;
305 flg |= CF;
306 break;
307 case 'd':
309 * match only dir on extract, not the subtree at dir
311 dflag = 1;
312 flg |= DF;
313 break;
314 case 'f':
316 * filename where the archive is stored
318 arcname = optarg;
319 flg |= FF;
320 break;
321 case 'i':
323 * interactive file rename
325 iflag = 1;
326 flg |= IF;
327 break;
328 case 'j':
330 * pass through bzip2
332 gzip_program = BZIP2_CMD;
333 break;
334 case 'k':
336 * do not clobber files that exist
338 kflag = 1;
339 flg |= KF;
340 break;
341 case 'l':
343 * try to link src to dest with copy (-rw)
345 lflag = 1;
346 flg |= LF;
347 break;
348 case 'n':
350 * select first match for a pattern only
352 nflag = 1;
353 flg |= NF;
354 break;
355 case 'o':
357 * pass format specific options
359 flg |= OF;
360 if (opt_add(optarg) < 0)
361 pax_usage();
362 break;
363 case 'p':
365 * specify file characteristic options
367 for (pt = optarg; *pt != '\0'; ++pt) {
368 switch(*pt) {
369 case 'a':
371 * do not preserve access time
373 patime = 0;
374 break;
375 case 'e':
377 * preserve user id, group id, file
378 * mode, access/modification times
379 * and file flags.
381 pids = 1;
382 pmode = 1;
383 patime = 1;
384 pmtime = 1;
385 pfflags = 1;
386 break;
387 #if 0
388 case 'f':
390 * do not preserve file flags
392 pfflags = 0;
393 break;
394 #endif
395 case 'm':
397 * do not preserve modification time
399 pmtime = 0;
400 break;
401 case 'o':
403 * preserve uid/gid
405 pids = 1;
406 break;
407 case 'p':
409 * preserve file mode bits
411 pmode = 1;
412 break;
413 default:
414 tty_warn(1, "Invalid -p string: %c",
415 *pt);
416 pax_usage();
417 break;
420 flg |= PF;
421 break;
422 case 'r':
424 * read the archive
426 flg |= RF;
427 break;
428 case 's':
430 * file name substitution name pattern
432 if (rep_add(optarg) < 0) {
433 pax_usage();
434 break;
436 flg |= SF;
437 break;
438 case 't':
440 * preserve access time on filesystem nodes we read
442 tflag = 1;
443 flg |= TF;
444 break;
445 case 'u':
447 * ignore those older files
449 uflag = 1;
450 flg |= UF;
451 break;
452 case 'v':
454 * verbose operation mode
456 vflag = 1;
457 flg |= VF;
458 break;
459 case 'w':
461 * write an archive
463 flg |= WF;
464 break;
465 case 'x':
467 * specify an archive format on write
469 tmp.name = optarg;
470 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
471 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
472 if (frmt != NULL) {
473 flg |= XF;
474 break;
476 tty_warn(1, "Unknown -x format: %s", optarg);
477 (void)fputs("pax: Known -x formats are:", stderr);
478 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
479 (void)fprintf(stderr, " %s", fsub[i].name);
480 (void)fputs("\n\n", stderr);
481 pax_usage();
482 break;
483 case 'z':
485 * use gzip. Non standard option.
487 gzip_program = GZIP_CMD;
488 break;
489 case 'A':
490 Aflag = 1;
491 flg |= CAF;
492 break;
493 case 'B':
495 * non-standard option on number of bytes written on a
496 * single archive volume.
498 if ((wrlimit = str_offt(optarg)) <= 0) {
499 tty_warn(1, "Invalid write limit %s", optarg);
500 pax_usage();
502 if (wrlimit % BLKMULT) {
503 tty_warn(1,
504 "Write limit is not a %d byte multiple",
505 BLKMULT);
506 pax_usage();
508 flg |= CBF;
509 break;
510 case 'D':
512 * On extraction check file inode change time before the
513 * modification of the file name. Non standard option.
515 Dflag = 1;
516 flg |= CDF;
517 break;
518 case 'E':
520 * non-standard limit on read faults
521 * 0 indicates stop after first error, values
522 * indicate a limit, "none" try forever
524 flg |= CEF;
525 if (strcmp(none, optarg) == 0)
526 maxflt = -1;
527 else if ((maxflt = atoi(optarg)) < 0) {
528 tty_warn(1,
529 "Error count value must be positive");
530 pax_usage();
532 break;
533 case 'G':
535 * non-standard option for selecting files within an
536 * archive by group (gid or name)
538 if (grp_add(optarg) < 0) {
539 pax_usage();
540 break;
542 flg |= CGF;
543 break;
544 case 'H':
546 * follow command line symlinks only
548 Hflag = 1;
549 flg |= CHF;
550 break;
551 case 'L':
553 * follow symlinks
555 Lflag = 1;
556 flg |= CLF;
557 break;
558 #ifdef SMALL
559 case 'M':
560 case 'N':
561 tty_warn(1, "Support for -%c is not compiled in", c);
562 exit(1);
563 #else /* !SMALL */
564 case 'M':
566 * Treat list of filenames on stdin as an
567 * mtree(8) specfile. Non standard option.
569 Mflag = 1;
570 flg |= CMF;
571 break;
572 case 'N':
574 * Use alternative directory for user db lookups.
576 if (!setup_getid(optarg)) {
577 tty_warn(1,
578 "Unable to use user and group databases in `%s'",
579 optarg);
580 pax_usage();
582 break;
583 #endif /* !SMALL */
584 case 'O':
586 * Force one volume. Non standard option.
588 force_one_volume = 1;
589 break;
590 case 'P':
592 * do NOT follow symlinks (default)
594 Lflag = 0;
595 flg |= CPF;
596 break;
597 case 'T':
599 * non-standard option for selecting files within an
600 * archive by modification time range (lower,upper)
602 if (trng_add(optarg) < 0) {
603 pax_usage();
604 break;
606 flg |= CTF;
607 break;
608 case 'U':
610 * non-standard option for selecting files within an
611 * archive by user (uid or name)
613 if (usr_add(optarg) < 0) {
614 pax_usage();
615 break;
617 flg |= CUF;
618 break;
619 case 'V':
621 * somewhat verbose operation mode (no listing)
623 Vflag = 1;
624 flg |= VSF;
625 break;
626 case 'X':
628 * do not pass over mount points in the file system
630 Xflag = 1;
631 flg |= CXF;
632 break;
633 case 'Y':
635 * On extraction check file inode change time after the
636 * modification of the file name. Non standard option.
638 Yflag = 1;
639 flg |= CYF;
640 break;
641 case 'Z':
643 * On extraction check modification time after the
644 * modification of the file name. Non standard option.
646 Zflag = 1;
647 flg |= CZF;
648 break;
649 case OPT_INSECURE:
650 secure = 0;
651 break;
652 case OPT_FORCE_LOCAL:
653 forcelocal = 1;
654 break;
655 case OPT_USE_COMPRESS_PROGRAM:
656 gzip_program = optarg;
657 break;
658 case OPT_XZ:
659 gzip_program = XZ_CMD;
660 break;
661 case OPT_GNU:
662 is_gnutar = 1;
663 break;
664 case '?':
665 default:
666 pax_usage();
667 break;
672 * figure out the operation mode of pax read,write,extract,copy,append
673 * or list. check that we have not been given a bogus set of flags
674 * for the operation mode.
676 if (ISLIST(flg)) {
677 act = LIST;
678 listf = stdout;
679 bflg = flg & BDLIST;
680 } else if (ISEXTRACT(flg)) {
681 act = EXTRACT;
682 bflg = flg & BDEXTR;
683 } else if (ISARCHIVE(flg)) {
684 act = ARCHIVE;
685 bflg = flg & BDARCH;
686 } else if (ISAPPND(flg)) {
687 act = APPND;
688 bflg = flg & BDARCH;
689 } else if (ISCOPY(flg)) {
690 act = COPY;
691 bflg = flg & BDCOPY;
692 } else
693 pax_usage();
694 if (bflg) {
695 printflg(flg);
696 pax_usage();
700 * if we are writing (ARCHIVE) we use the default format if the user
701 * did not specify a format. when we write during an APPEND, we will
702 * adopt the format of the existing archive if none was supplied.
704 if (!(flg & XF) && (act == ARCHIVE))
705 frmt = &(fsub[DEFLT]);
708 * process the args as they are interpreted by the operation mode
710 switch (act) {
711 case LIST:
712 case EXTRACT:
713 for (; optind < argc; optind++)
714 if (pat_add(argv[optind], NULL, 0) < 0)
715 pax_usage();
716 break;
717 case COPY:
718 if (optind >= argc) {
719 tty_warn(0, "Destination directory was not supplied");
720 pax_usage();
722 --argc;
723 dirptr = argv[argc];
724 if (mkpath(dirptr) < 0)
725 exit(1);
726 /* FALLTHROUGH */
727 case ARCHIVE:
728 case APPND:
729 for (; optind < argc; optind++)
730 if (ftree_add(argv[optind], 0) < 0)
731 pax_usage();
733 * no read errors allowed on updates/append operation!
735 maxflt = 0;
736 break;
742 * tar_options()
743 * look at the user specified flags. set globals as required and check if
744 * the user specified a legal set of flags. If not, complain and exit
747 struct option tar_longopts[] = {
748 { "block-size", required_argument, 0, 'b' },
749 { "bunzip2", no_argument, 0, 'j' },
750 { "bzip2", no_argument, 0, 'j' },
751 { "create", no_argument, 0, 'c' }, /* F */
752 /* -e -- no corresponding long option */
753 { "file", required_argument, 0, 'f' },
754 { "dereference", no_argument, 0, 'h' },
755 { "keep-old-files", no_argument, 0, 'k' },
756 { "one-file-system", no_argument, 0, 'l' },
757 { "modification-time", no_argument, 0, 'm' },
758 { "old-archive", no_argument, 0, 'o' },
759 { "portability", no_argument, 0, 'o' },
760 { "same-permissions", no_argument, 0, 'p' },
761 { "preserve-permissions", no_argument, 0, 'p' },
762 { "preserve", no_argument, 0, 'p' },
763 { "fast-read", no_argument, 0, 'q' },
764 { "append", no_argument, 0, 'r' }, /* F */
765 { "update", no_argument, 0, 'u' }, /* F */
766 { "list", no_argument, 0, 't' }, /* F */
767 { "verbose", no_argument, 0, 'v' },
768 { "interactive", no_argument, 0, 'w' },
769 { "confirmation", no_argument, 0, 'w' },
770 { "extract", no_argument, 0, 'x' }, /* F */
771 { "get", no_argument, 0, 'x' }, /* F */
772 { "gzip", no_argument, 0, 'z' },
773 { "gunzip", no_argument, 0, 'z' },
774 { "read-full-blocks", no_argument, 0, 'B' },
775 { "directory", required_argument, 0, 'C' },
776 { "xz", no_argument, 0, 'J' },
777 { "to-stdout", no_argument, 0, 'O' },
778 { "absolute-paths", no_argument, 0, 'P' },
779 { "sparse", no_argument, 0, 'S' },
780 { "files-from", required_argument, 0, 'T' },
781 { "summary", no_argument, 0, 'V' },
782 { "stats", no_argument, 0, 'V' },
783 { "exclude-from", required_argument, 0, 'X' },
784 { "compress", no_argument, 0, 'Z' },
785 { "uncompress", no_argument, 0, 'Z' },
786 { "strict", no_argument, 0,
787 OPT_STRICT },
788 { "atime-preserve", no_argument, 0,
789 OPT_ATIME_PRESERVE },
790 { "unlink", no_argument, 0,
791 OPT_UNLINK },
792 { "use-compress-program", required_argument, 0,
793 OPT_USE_COMPRESS_PROGRAM },
794 { "force-local", no_argument, 0,
795 OPT_FORCE_LOCAL },
796 { "insecure", no_argument, 0,
797 OPT_INSECURE },
798 { "exclude", required_argument, 0,
799 OPT_EXCLUDE },
800 { "no-recursion", no_argument, 0,
801 OPT_NORECURSE },
802 #if !HAVE_NBTOOL_CONFIG_H
803 { "chroot", no_argument, 0,
804 OPT_CHROOT },
805 #endif
806 #if 0 /* Not implemented */
807 { "catenate", no_argument, 0, 'A' }, /* F */
808 { "concatenate", no_argument, 0, 'A' }, /* F */
809 { "diff", no_argument, 0, 'd' }, /* F */
810 { "compare", no_argument, 0, 'd' }, /* F */
811 { "checkpoint", no_argument, 0,
812 OPT_CHECKPOINT },
813 { "help", no_argument, 0,
814 OPT_HELP },
815 { "info-script", required_argument, 0, 'F' },
816 { "new-volume-script", required_argument, 0, 'F' },
817 { "incremental", no_argument, 0, 'G' },
818 { "listed-incremental", required_argument, 0, 'g' },
819 { "ignore-zeros", no_argument, 0, 'i' },
820 { "ignore-failed-read", no_argument, 0,
821 OPT_IGNORE_FAILED_READ },
822 { "starting-file", no_argument, 0, 'K' },
823 { "tape-length", required_argument, 0, 'L' },
824 { "multi-volume", no_argument, 0, 'M' },
825 { "after-date", required_argument, 0, 'N' },
826 { "newer", required_argument, 0, 'N' },
827 { "record-number", no_argument, 0, 'R' },
828 { "remove-files", no_argument, 0,
829 OPT_REMOVE_FILES },
830 { "same-order", no_argument, 0, 's' },
831 { "preserve-order", no_argument, 0, 's' },
832 { "null", no_argument, 0,
833 OPT_NULL },
834 { "totals", no_argument, 0,
835 OPT_TOTALS },
836 { "volume-name", required_argument, 0, 'V' }, /* XXX */
837 { "label", required_argument, 0, 'V' }, /* XXX */
838 { "version", no_argument, 0,
839 OPT_VERSION },
840 { "verify", no_argument, 0, 'W' },
841 { "block-compress", no_argument, 0,
842 OPT_BLOCK_COMPRESS },
843 #endif
844 { 0, 0, 0, 0 },
847 static void
848 tar_set_action(int op)
850 if (act != ERROR && act != op)
851 tar_usage();
852 act = op;
855 static void
856 tar_options(int argc, char **argv)
858 int c;
859 int fstdin = 0;
860 int Oflag = 0;
861 int nincfiles = 0;
862 int incfiles_max = 0;
863 struct incfile {
864 char *file;
865 char *dir;
867 struct incfile *incfiles = NULL;
870 * Set default values.
872 rmleadslash = 1;
873 is_gnutar = 1;
876 * process option flags
878 while ((c = getoldopt(argc, argv,
879 "+b:cef:hjklmopqrs:tuvwxzBC:HI:JOPST:X:Z014578",
880 tar_longopts, NULL))
881 != -1) {
882 switch(c) {
883 case 'b':
885 * specify blocksize in 512-byte blocks
887 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
888 tty_warn(1, "Invalid block size %s", optarg);
889 tar_usage();
891 wrblksz *= 512; /* XXX - check for int oflow */
892 break;
893 case 'c':
895 * create an archive
897 tar_set_action(ARCHIVE);
898 break;
899 case 'e':
901 * stop after first error
903 maxflt = 0;
904 break;
905 case 'f':
907 * filename where the archive is stored
909 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
911 * treat a - as stdin
913 fstdin = 1;
914 arcname = NULL;
915 break;
917 fstdin = 0;
918 arcname = optarg;
919 break;
920 case 'h':
922 * follow symlinks
924 Lflag = 1;
925 break;
926 case 'j':
928 * pass through bzip2. not a standard option
930 gzip_program = BZIP2_CMD;
931 break;
932 case 'k':
934 * do not clobber files that exist
936 kflag = 1;
937 break;
938 case 'l':
940 * do not pass over mount points in the file system
942 Xflag = 1;
943 break;
944 case 'm':
946 * do not preserve modification time
948 pmtime = 0;
949 break;
950 case 'o':
952 * This option does several things based on whether
953 * this is a create or extract operation.
955 if (act == ARCHIVE) {
956 /* GNU tar: write V7 format archives. */
957 Oflag = 1;
958 /* 4.2BSD: don't add directory entries. */
959 if (opt_add("write_opt=nodir") < 0)
960 tar_usage();
962 } else {
963 /* SUS: don't preserve owner/group. */
964 pids = 0;
965 nopids = 1;
967 break;
968 case 'p':
970 * preserve user id, group id, file
971 * mode, access/modification times
973 if (!nopids)
974 pids = 1;
975 pmode = 1;
976 patime = 1;
977 pmtime = 1;
978 break;
979 case 'q':
981 * select first match for a pattern only
983 nflag = 1;
984 break;
985 case 'r':
986 case 'u':
988 * append to the archive
990 tar_set_action(APPND);
991 break;
992 case 's':
994 * file name substitution name pattern
996 if (rep_add(optarg) < 0) {
997 tar_usage();
998 break;
1000 break;
1001 case 't':
1003 * list contents of the tape
1005 tar_set_action(LIST);
1006 break;
1007 case 'v':
1009 * verbose operation mode
1011 vflag = 1;
1012 break;
1013 case 'w':
1015 * interactive file rename
1017 iflag = 1;
1018 break;
1019 case 'x':
1021 * extract an archive, preserving mode,
1022 * and mtime if possible.
1024 tar_set_action(EXTRACT);
1025 pmtime = 1;
1026 break;
1027 case 'z':
1029 * use gzip. Non standard option.
1031 gzip_program = GZIP_CMD;
1032 break;
1033 case 'B':
1035 * Nothing to do here, this is pax default
1037 break;
1038 case 'C':
1039 havechd++;
1040 chdname = optarg;
1041 break;
1042 case 'H':
1044 * follow command line symlinks only
1046 Hflag = 1;
1047 break;
1048 case 'I':
1049 case 'T':
1050 if (++nincfiles > incfiles_max) {
1051 incfiles_max = nincfiles + 3;
1052 incfiles = realloc(incfiles,
1053 sizeof(*incfiles) * incfiles_max);
1054 if (incfiles == NULL) {
1055 tty_warn(0, "Unable to allocate space "
1056 "for option list");
1057 exit(1);
1060 incfiles[nincfiles - 1].file = optarg;
1061 incfiles[nincfiles - 1].dir = chdname;
1062 break;
1063 case 'J':
1064 gzip_program = XZ_CMD;
1065 break;
1066 case 'O':
1067 Oflag = 1;
1068 break;
1069 case 'P':
1071 * do not remove leading '/' from pathnames
1073 rmleadslash = 0;
1074 Aflag = 1;
1075 break;
1076 case 'S':
1077 /* do nothing; we already generate sparse files */
1078 break;
1079 case 'V':
1081 * semi-verbose operation mode (no listing)
1083 Vflag = 1;
1084 break;
1085 case 'X':
1087 * GNU tar compat: exclude the files listed in optarg
1089 if (tar_gnutar_X_compat(optarg) != 0)
1090 tar_usage();
1091 break;
1092 case 'Z':
1094 * use compress.
1096 gzip_program = COMPRESS_CMD;
1097 break;
1098 case '0':
1099 arcname = DEV_0;
1100 break;
1101 case '1':
1102 arcname = DEV_1;
1103 break;
1104 case '4':
1105 arcname = DEV_4;
1106 break;
1107 case '5':
1108 arcname = DEV_5;
1109 break;
1110 case '7':
1111 arcname = DEV_7;
1112 break;
1113 case '8':
1114 arcname = DEV_8;
1115 break;
1116 case OPT_ATIME_PRESERVE:
1117 patime = 1;
1118 break;
1119 case OPT_UNLINK:
1120 /* Just ignore -- we always unlink first. */
1121 break;
1122 case OPT_USE_COMPRESS_PROGRAM:
1123 gzip_program = optarg;
1124 break;
1125 case OPT_FORCE_LOCAL:
1126 forcelocal = 1;
1127 break;
1128 case OPT_INSECURE:
1129 secure = 0;
1130 break;
1131 case OPT_STRICT:
1132 /* disable gnu extensions */
1133 is_gnutar = 0;
1134 break;
1135 case OPT_EXCLUDE:
1136 if (tar_gnutar_minus_minus_exclude(optarg) != 0)
1137 tar_usage();
1138 break;
1139 case OPT_NORECURSE:
1140 dflag = 1;
1141 break;
1142 #if !HAVE_NBTOOL_CONFIG_H
1143 case OPT_CHROOT:
1144 do_chroot = 1;
1145 break;
1146 #endif
1147 default:
1148 tar_usage();
1149 break;
1152 argc -= optind;
1153 argv += optind;
1155 /* Tar requires an action. */
1156 if (act == ERROR)
1157 tar_usage();
1159 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1160 if (fstdin == 1 && act == ARCHIVE)
1161 listf = stderr;
1162 else
1163 listf = stdout;
1165 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1166 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
1167 exit(0);
1169 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1170 * (unless -o specified)
1172 if (act == ARCHIVE || act == APPND)
1173 frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
1174 else if (Oflag) {
1175 if (act == EXTRACT)
1176 to_stdout = 1;
1177 else {
1178 tty_warn(1, "The -O/-o options are only valid when "
1179 "writing or extracting an archive");
1180 tar_usage();
1185 * process the args as they are interpreted by the operation mode
1187 switch (act) {
1188 case LIST:
1189 case EXTRACT:
1190 default:
1192 int sawpat = 0;
1193 int dirisnext = 0;
1194 char *file, *dir = NULL;
1195 int mustfreedir = 0;
1197 while (nincfiles || *argv != NULL) {
1199 * If we queued up any include files,
1200 * pull them in now. Otherwise, check
1201 * for -I and -C positional flags.
1202 * Anything else must be a file to
1203 * extract.
1205 if (nincfiles) {
1206 file = incfiles->file;
1207 dir = incfiles->dir;
1208 mustfreedir = 0;
1209 incfiles++;
1210 nincfiles--;
1211 } else if (strcmp(*argv, "-I") == 0) {
1212 if (*++argv == NULL)
1213 break;
1214 file = *argv++;
1215 dir = chdname;
1216 mustfreedir = 0;
1217 } else {
1218 file = NULL;
1219 dir = NULL;
1220 mustfreedir = 0;
1222 if (file != NULL) {
1223 FILE *fp;
1224 char *str;
1226 if (strcmp(file, "-") == 0)
1227 fp = stdin;
1228 else if ((fp = fopen(file, "r")) == NULL) {
1229 tty_warn(1, "Unable to open file '%s' for read", file);
1230 tar_usage();
1232 while ((str = get_line(fp)) != NULL) {
1233 if (dirisnext) {
1234 if (dir && mustfreedir)
1235 free(dir);
1236 dir = str;
1237 mustfreedir = 1;
1238 dirisnext = 0;
1239 continue;
1241 if (strcmp(str, "-C") == 0) {
1242 havechd++;
1243 dirisnext = 1;
1244 free(str);
1245 continue;
1247 if (strncmp(str, "-C ", 3) == 0) {
1248 havechd++;
1249 if (dir && mustfreedir)
1250 free(dir);
1251 dir = strdup(str + 3);
1252 mustfreedir = 1;
1253 free(str);
1254 continue;
1256 if (pat_add(str, dir, NOGLOB_MTCH) < 0)
1257 tar_usage();
1258 sawpat = 1;
1260 /* Bomb if given -C w/out a dir. */
1261 if (dirisnext)
1262 tar_usage();
1263 if (dir && mustfreedir)
1264 free(dir);
1265 if (strcmp(file, "-") != 0)
1266 fclose(fp);
1267 if (get_line_error) {
1268 tty_warn(1, "Problem with file '%s'", file);
1269 tar_usage();
1271 } else if (strcmp(*argv, "-C") == 0) {
1272 if (*++argv == NULL)
1273 break;
1274 chdname = *argv++;
1275 havechd++;
1276 } else if (pat_add(*argv++, chdname, 0) < 0)
1277 tar_usage();
1278 else
1279 sawpat = 1;
1282 * if patterns were added, we are doing chdir()
1283 * on a file-by-file basis, else, just one
1284 * global chdir (if any) after opening input.
1286 if (sawpat > 0)
1287 chdname = NULL;
1289 break;
1290 case ARCHIVE:
1291 case APPND:
1292 if (chdname != NULL) { /* initial chdir() */
1293 if (ftree_add(chdname, 1) < 0)
1294 tar_usage();
1297 while (nincfiles || *argv != NULL) {
1298 char *file, *dir;
1301 * If we queued up any include files, pull them in
1302 * now. Otherwise, check for -I and -C positional
1303 * flags. Anything else must be a file to include
1304 * in the archive.
1306 if (nincfiles) {
1307 file = incfiles->file;
1308 dir = incfiles->dir;
1309 incfiles++;
1310 nincfiles--;
1311 } else if (strcmp(*argv, "-I") == 0) {
1312 if (*++argv == NULL)
1313 break;
1314 file = *argv++;
1315 dir = NULL;
1316 } else {
1317 file = NULL;
1318 dir = NULL;
1320 if (file != NULL) {
1321 FILE *fp;
1322 char *str;
1323 int dirisnext = 0;
1325 /* Set directory if needed */
1326 if (dir) {
1327 if (ftree_add(dir, 1) < 0)
1328 tar_usage();
1331 if (strcmp(file, "-") == 0)
1332 fp = stdin;
1333 else if ((fp = fopen(file, "r")) == NULL) {
1334 tty_warn(1, "Unable to open file '%s' for read", file);
1335 tar_usage();
1337 while ((str = get_line(fp)) != NULL) {
1338 if (dirisnext) {
1339 if (ftree_add(str, 1) < 0)
1340 tar_usage();
1341 dirisnext = 0;
1342 continue;
1344 if (strcmp(str, "-C") == 0) {
1345 dirisnext = 1;
1346 continue;
1348 if (strncmp(str, "-C ", 3) == 0) {
1349 if (ftree_add(str + 3, 1) < 0)
1350 tar_usage();
1351 continue;
1353 if (ftree_add(str, 0) < 0)
1354 tar_usage();
1356 /* Bomb if given -C w/out a dir. */
1357 if (dirisnext)
1358 tar_usage();
1359 if (strcmp(file, "-") != 0)
1360 fclose(fp);
1361 if (get_line_error) {
1362 tty_warn(1, "Problem with file '%s'",
1363 file);
1364 tar_usage();
1366 } else if (strcmp(*argv, "-C") == 0) {
1367 if (*++argv == NULL)
1368 break;
1369 if (ftree_add(*argv++, 1) < 0)
1370 tar_usage();
1371 } else if (ftree_add(*argv++, 0) < 0)
1372 tar_usage();
1375 * no read errors allowed on updates/append operation!
1377 maxflt = 0;
1378 break;
1380 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
1381 arcname = getenv("TAPE");
1382 if ((arcname == NULL) || (*arcname == '\0'))
1383 arcname = _PATH_DEFTAPE;
1388 mkpath(char *path)
1390 char *slash;
1391 int done = 0;
1393 slash = path;
1395 while (!done) {
1396 slash += strspn(slash, "/");
1397 slash += strcspn(slash, "/");
1399 done = (*slash == '\0');
1400 *slash = '\0';
1402 if (domkdir(path, 0777) == -1)
1403 goto out;
1405 if (!done)
1406 *slash = '/';
1409 return 0;
1410 out:
1411 /* Can't create or or not a directory */
1412 syswarn(1, errno, "Cannot create directory `%s'", path);
1413 return -1;
1417 #ifndef NO_CPIO
1418 struct option cpio_longopts[] = {
1419 { "reset-access-time", no_argument, 0, 'a' },
1420 { "make-directories", no_argument, 0, 'd' },
1421 { "nonmatching", no_argument, 0, 'f' },
1422 { "extract", no_argument, 0, 'i' },
1423 { "link", no_argument, 0, 'l' },
1424 { "preserve-modification-time", no_argument, 0, 'm' },
1425 { "create", no_argument, 0, 'o' },
1426 { "pass-through", no_argument, 0, 'p' },
1427 { "rename", no_argument, 0, 'r' },
1428 { "list", no_argument, 0, 't' },
1429 { "unconditional", no_argument, 0, 'u' },
1430 { "verbose", no_argument, 0, 'v' },
1431 { "append", no_argument, 0, 'A' },
1432 { "pattern-file", required_argument, 0, 'E' },
1433 { "file", required_argument, 0, 'F' },
1434 { "force-local", no_argument, 0,
1435 OPT_FORCE_LOCAL },
1436 { "format", required_argument, 0, 'H' },
1437 { "dereference", no_argument, 0, 'L' },
1438 { "swap-halfwords", no_argument, 0, 'S' },
1439 { "summary", no_argument, 0, 'V' },
1440 { "stats", no_argument, 0, 'V' },
1441 { "insecure", no_argument, 0,
1442 OPT_INSECURE },
1443 { "sparse", no_argument, 0,
1444 OPT_SPARSE },
1445 { "xz", no_argument, 0,
1446 OPT_XZ },
1448 #ifdef notyet
1449 /* Not implemented */
1450 { "null", no_argument, 0, '0' },
1451 { "swap", no_argument, 0, 'b' },
1452 { "numeric-uid-gid", no_argument, 0, 'n' },
1453 { "swap-bytes", no_argument, 0, 's' },
1454 { "message", required_argument, 0, 'M' },
1455 { "owner", required_argument, 0 'R' },
1456 { "dot", no_argument, 0, 'V' }, /* xxx */
1457 { "block-size", required_argument, 0,
1458 OPT_BLOCK_SIZE },
1459 { "no-absolute-pathnames", no_argument, 0,
1460 OPT_NO_ABSOLUTE_PATHNAMES },
1461 { "no-preserve-owner", no_argument, 0,
1462 OPT_NO_PRESERVE_OWNER },
1463 { "only-verify-crc", no_argument, 0,
1464 OPT_ONLY_VERIFY_CRC },
1465 { "rsh-command", required_argument, 0,
1466 OPT_RSH_COMMAND },
1467 { "version", no_argument, 0,
1468 OPT_VERSION },
1469 #endif
1470 { 0, 0, 0, 0 },
1473 static void
1474 cpio_set_action(int op)
1476 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1477 act = APPND;
1478 else if (act == EXTRACT && op == LIST)
1479 act = op;
1480 else if (act != ERROR && act != op)
1481 cpio_usage();
1482 else
1483 act = op;
1487 * cpio_options()
1488 * look at the user specified flags. set globals as required and check if
1489 * the user specified a legal set of flags. If not, complain and exit
1492 static void
1493 cpio_options(int argc, char **argv)
1495 FSUB tmp;
1496 u_int64_t flg = 0;
1497 u_int64_t bflg = 0;
1498 int c;
1499 size_t i;
1500 FILE *fp;
1501 char *str;
1503 uflag = 1;
1504 kflag = 1;
1505 pids = 1;
1506 pmode = 1;
1507 pmtime = 0;
1508 arcname = NULL;
1509 dflag = 1;
1510 nodirs = 1;
1512 * process option flags
1514 while ((c = getoldopt(argc, argv,
1515 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1516 cpio_longopts, NULL)) != -1) {
1517 switch(c) {
1518 case 'a':
1520 * preserve access time on filesystem nodes we read
1522 tflag = 1;
1523 flg |= TF;
1524 break;
1525 #ifdef notyet
1526 case 'b':
1528 * swap bytes and half-words when reading data
1530 break;
1531 #endif
1532 case 'c':
1534 * ASCII cpio header
1536 frmt = &fsub[F_SV4CPIO];
1537 break;
1538 case 'd':
1540 * create directories as needed
1541 * pax does this by default ..
1543 nodirs = 0;
1544 break;
1545 case 'f':
1547 * inverse match on patterns
1549 cflag = 1;
1550 flg |= CF;
1551 break;
1552 case 'i':
1554 * read the archive
1556 cpio_set_action(EXTRACT);
1557 flg |= RF;
1558 break;
1559 #ifdef notyet
1560 case 'k':
1561 break;
1562 #endif
1563 case 'l':
1565 * try to link src to dest with copy (-rw)
1567 lflag = 1;
1568 flg |= LF;
1569 break;
1570 case 'm':
1572 * preserve mtime
1574 flg |= PF;
1575 pmtime = 1;
1576 break;
1577 case 'o':
1579 * write an archive
1581 cpio_set_action(ARCHIVE);
1582 frmt = &(fsub[F_SV4CRC]);
1583 flg |= WF;
1584 break;
1585 case 'p':
1587 * cpio -p is like pax -rw
1589 cpio_set_action(COPY);
1590 flg |= RF | WF;
1591 break;
1592 case 'r':
1594 * interactive file rename
1596 iflag = 1;
1597 flg |= IF;
1598 break;
1599 #ifdef notyet
1600 case 's':
1602 * swap bytes after reading data
1604 break;
1605 #endif
1606 case 't':
1608 * list contents of archive
1610 cpio_set_action(LIST);
1611 listf = stdout;
1612 flg &= ~RF;
1613 break;
1614 case 'u':
1616 * don't ignore those older files
1618 uflag = 0;
1619 kflag = 0;
1620 flg |= UF;
1621 break;
1622 case 'v':
1624 * verbose operation mode
1626 vflag = 1;
1627 flg |= VF;
1628 break;
1629 case 'z':
1631 * use gzip. Non standard option.
1633 gzip_program = GZIP_CMD;
1634 break;
1635 case 'A':
1637 * append to an archive
1639 cpio_set_action(APPND);
1640 flg |= AF;
1641 break;
1642 case 'B':
1644 * set blocksize to 5120
1646 blksz = 5120;
1647 break;
1648 case 'C':
1650 * specify blocksize
1652 if ((blksz = (int)str_offt(optarg)) <= 0) {
1653 tty_warn(1, "Invalid block size %s", optarg);
1654 cpio_usage();
1656 break;
1657 case 'E':
1659 * file with patterns to extract or list
1661 if ((fp = fopen(optarg, "r")) == NULL) {
1662 tty_warn(1, "Unable to open file '%s' for read",
1663 optarg);
1664 cpio_usage();
1666 while ((str = get_line(fp)) != NULL) {
1667 pat_add(str, NULL, 0);
1669 fclose(fp);
1670 if (get_line_error) {
1671 tty_warn(1, "Problem with file '%s'", optarg);
1672 cpio_usage();
1674 break;
1675 case 'H':
1677 * specify an archive format on write
1679 tmp.name = optarg;
1680 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1681 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
1682 if (frmt != NULL) {
1683 flg |= XF;
1684 break;
1686 tty_warn(1, "Unknown -H format: %s", optarg);
1687 (void)fputs("cpio: Known -H formats are:", stderr);
1688 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1689 (void)fprintf(stderr, " %s", fsub[i].name);
1690 (void)fputs("\n\n", stderr);
1691 cpio_usage();
1692 break;
1693 case 'F':
1694 case 'I':
1695 case 'O':
1697 * filename where the archive is stored
1699 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1701 * treat a - as stdin
1703 arcname = NULL;
1704 break;
1706 arcname = optarg;
1707 break;
1708 case 'L':
1710 * follow symlinks
1712 Lflag = 1;
1713 flg |= CLF;
1714 break;
1715 #ifdef notyet
1716 case 'M':
1717 arg = optarg;
1718 break;
1719 case 'R':
1720 arg = optarg;
1721 break;
1722 #endif
1723 case 'S':
1725 * swap halfwords after reading data
1727 cpio_swp_head = 1;
1728 break;
1729 #ifdef notyet
1730 case 'V': /* print a '.' for each file processed */
1731 break;
1732 #endif
1733 case 'V':
1735 * semi-verbose operation mode (no listing)
1737 Vflag = 1;
1738 flg |= VF;
1739 break;
1740 case 'Z':
1742 * use compress. Non standard option.
1744 gzip_program = COMPRESS_CMD;
1745 break;
1746 case '6':
1748 * process Version 6 cpio format
1750 frmt = &(fsub[F_BCPIO]);
1751 break;
1752 case OPT_FORCE_LOCAL:
1753 forcelocal = 1;
1754 break;
1755 case OPT_INSECURE:
1756 secure = 0;
1757 break;
1758 case OPT_SPARSE:
1759 /* do nothing; we already generate sparse files */
1760 break;
1761 case OPT_XZ:
1762 gzip_program = XZ_CMD;
1763 break;
1764 default:
1765 cpio_usage();
1766 break;
1771 * figure out the operation mode of cpio. check that we have not been
1772 * given a bogus set of flags for the operation mode.
1774 if (ISLIST(flg)) {
1775 act = LIST;
1776 bflg = flg & BDLIST;
1777 } else if (ISEXTRACT(flg)) {
1778 act = EXTRACT;
1779 bflg = flg & BDEXTR;
1780 } else if (ISARCHIVE(flg)) {
1781 act = ARCHIVE;
1782 bflg = flg & BDARCH;
1783 } else if (ISAPPND(flg)) {
1784 act = APPND;
1785 bflg = flg & BDARCH;
1786 } else if (ISCOPY(flg)) {
1787 act = COPY;
1788 bflg = flg & BDCOPY;
1789 } else
1790 cpio_usage();
1791 if (bflg) {
1792 cpio_usage();
1796 * if we are writing (ARCHIVE) we use the default format if the user
1797 * did not specify a format. when we write during an APPEND, we will
1798 * adopt the format of the existing archive if none was supplied.
1800 if (!(flg & XF) && (act == ARCHIVE))
1801 frmt = &(fsub[F_BCPIO]);
1804 * process the args as they are interpreted by the operation mode
1806 switch (act) {
1807 case LIST:
1808 case EXTRACT:
1809 for (; optind < argc; optind++)
1810 if (pat_add(argv[optind], NULL, 0) < 0)
1811 cpio_usage();
1812 break;
1813 case COPY:
1814 if (optind >= argc) {
1815 tty_warn(0, "Destination directory was not supplied");
1816 cpio_usage();
1818 --argc;
1819 dirptr = argv[argc];
1820 /* FALLTHROUGH */
1821 case ARCHIVE:
1822 case APPND:
1823 if (argc != optind) {
1824 for (; optind < argc; optind++)
1825 if (ftree_add(argv[optind], 0) < 0)
1826 cpio_usage();
1827 break;
1830 * no read errors allowed on updates/append operation!
1832 maxflt = 0;
1833 while ((str = get_line(stdin)) != NULL) {
1834 ftree_add(str, 0);
1836 if (get_line_error) {
1837 tty_warn(1, "Problem while reading stdin");
1838 cpio_usage();
1840 break;
1841 default:
1842 cpio_usage();
1843 break;
1846 #endif
1849 * printflg()
1850 * print out those invalid flag sets found to the user
1853 static void
1854 printflg(unsigned int flg)
1856 int nxt;
1858 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1859 while ((nxt = ffs(flg)) != 0) {
1860 flg &= ~(1 << (nxt - 1));
1861 (void)fprintf(stderr, " -%c", flgch[nxt - 1]);
1863 (void)putc('\n', stderr);
1867 * c_frmt()
1868 * comparison routine used by bsearch to find the format specified
1869 * by the user
1872 static int
1873 c_frmt(const void *a, const void *b)
1875 return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
1879 * opt_next()
1880 * called by format specific options routines to get each format specific
1881 * flag and value specified with -o
1882 * Return:
1883 * pointer to next OPLIST entry or NULL (end of list).
1886 OPLIST *
1887 opt_next(void)
1889 OPLIST *opt;
1891 if ((opt = ophead) != NULL)
1892 ophead = ophead->fow;
1893 return opt;
1897 * bad_opt()
1898 * generic routine used to complain about a format specific options
1899 * when the format does not support options.
1903 bad_opt(void)
1905 OPLIST *opt;
1907 if (ophead == NULL)
1908 return 0;
1910 * print all we were given
1912 tty_warn(1," These format options are not supported for %s",
1913 frmt->name);
1914 while ((opt = opt_next()) != NULL)
1915 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1916 if (strcmp(NM_TAR, argv0) == 0)
1917 tar_usage();
1918 #ifndef NO_CPIO
1919 else if (strcmp(NM_CPIO, argv0) == 0)
1920 cpio_usage();
1921 #endif
1922 else
1923 pax_usage();
1924 return 0;
1928 * opt_add()
1929 * breaks the value supplied to -o into a option name and value. options
1930 * are given to -o in the form -o name-value,name=value
1931 * multiple -o may be specified.
1932 * Return:
1933 * 0 if format in name=value format, -1 if -o is passed junk
1937 opt_add(const char *str)
1939 OPLIST *opt;
1940 char *frpt;
1941 char *pt;
1942 char *endpt;
1943 char *dstr;
1945 if ((str == NULL) || (*str == '\0')) {
1946 tty_warn(0, "Invalid option name");
1947 return -1;
1949 if ((dstr = strdup(str)) == NULL) {
1950 tty_warn(0, "Unable to allocate space for option list");
1951 return -1;
1953 frpt = endpt = dstr;
1956 * break into name and values pieces and stuff each one into a
1957 * OPLIST structure. When we know the format, the format specific
1958 * option function will go through this list
1960 while ((frpt != NULL) && (*frpt != '\0')) {
1961 if ((endpt = strchr(frpt, ',')) != NULL)
1962 *endpt = '\0';
1963 if ((pt = strchr(frpt, '=')) == NULL) {
1964 tty_warn(0, "Invalid options format");
1965 free(dstr);
1966 return -1;
1968 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1969 tty_warn(0, "Unable to allocate space for option list");
1970 free(dstr);
1971 return -1;
1973 *pt++ = '\0';
1974 opt->name = frpt;
1975 opt->value = pt;
1976 opt->fow = NULL;
1977 if (endpt != NULL)
1978 frpt = endpt + 1;
1979 else
1980 frpt = NULL;
1981 if (ophead == NULL) {
1982 optail = ophead = opt;
1983 continue;
1985 optail->fow = opt;
1986 optail = opt;
1988 return 0;
1992 * str_offt()
1993 * Convert an expression of the following forms to an off_t > 0.
1994 * 1) A positive decimal number.
1995 * 2) A positive decimal number followed by a b (mult by 512).
1996 * 3) A positive decimal number followed by a k (mult by 1024).
1997 * 4) A positive decimal number followed by a m (mult by 512).
1998 * 5) A positive decimal number followed by a w (mult by sizeof int)
1999 * 6) Two or more positive decimal numbers (with/without k,b or w).
2000 * separated by x (also * for backwards compatibility), specifying
2001 * the product of the indicated values.
2002 * Return:
2003 * 0 for an error, a positive value o.w.
2006 static off_t
2007 str_offt(char *val)
2009 char *expr;
2010 off_t num, t;
2012 num = STRTOOFFT(val, &expr, 0);
2013 if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
2014 return 0;
2016 switch(*expr) {
2017 case 'b':
2018 t = num;
2019 num *= 512;
2020 if (t > num)
2021 return 0;
2022 ++expr;
2023 break;
2024 case 'k':
2025 t = num;
2026 num *= 1024;
2027 if (t > num)
2028 return 0;
2029 ++expr;
2030 break;
2031 case 'm':
2032 t = num;
2033 num *= 1048576;
2034 if (t > num)
2035 return 0;
2036 ++expr;
2037 break;
2038 case 'w':
2039 t = num;
2040 num *= sizeof(int);
2041 if (t > num)
2042 return 0;
2043 ++expr;
2044 break;
2047 switch(*expr) {
2048 case '\0':
2049 break;
2050 case '*':
2051 case 'x':
2052 t = num;
2053 num *= str_offt(expr + 1);
2054 if (t > num)
2055 return 0;
2056 break;
2057 default:
2058 return 0;
2060 return num;
2063 static char *
2064 get_line(FILE *f)
2066 char *name, *temp;
2067 size_t len;
2069 name = fgetln(f, &len);
2070 if (!name) {
2071 get_line_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
2072 return 0;
2074 if (name[len-1] != '\n')
2075 len++;
2076 temp = malloc(len);
2077 if (!temp) {
2078 get_line_error = GETLINE_OUT_OF_MEM;
2079 return 0;
2081 memcpy(temp, name, len-1);
2082 temp[len-1] = 0;
2083 return temp;
2087 * no_op()
2088 * for those option functions where the archive format has nothing to do.
2089 * Return:
2093 static int
2094 no_op(void)
2096 return 0;
2100 * pax_usage()
2101 * print the usage summary to the user
2104 static void
2105 pax_usage(void)
2107 fprintf(stderr,
2108 "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2109 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2110 " [pattern ...]\n");
2111 fprintf(stderr,
2112 " pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
2113 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2114 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2115 fprintf(stderr,
2116 " pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2117 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2118 " [-U user] ... [-G group] ...\n"
2119 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2120 fprintf(stderr,
2121 " pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
2122 " [-s replstr] ... [-U user] ... [-G group] ...\n"
2123 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2124 exit(1);
2125 /* NOTREACHED */
2129 * tar_usage()
2130 * print the usage summary to the user
2133 static void
2134 tar_usage(void)
2136 (void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHJOPSXZ014578] "
2137 "[archive] [blocksize]\n"
2138 " [-C directory] [-T file] [-s replstr] "
2139 "[file ...]\n", stderr);
2140 exit(1);
2141 /* NOTREACHED */
2144 #ifndef NO_CPIO
2146 * cpio_usage()
2147 * print the usage summary to the user
2150 static void
2151 cpio_usage(void)
2154 (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2155 "[-H format] [-O archive]\n"
2156 " < name-list [> archive]\n"
2157 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2158 "[-F archive] [-H format] \n"
2159 " [-I archive] "
2160 "[pattern ...] [< archive]\n"
2161 " cpio -p [-adlLmuv] destination-directory "
2162 "< name-list\n", stderr);
2163 exit(1);
2164 /* NOTREACHED */
2166 #endif