libexec exec fix
[minix.git] / bin / pax / options.c
blobc6c9eb908285fbb55618f398104168644a1eed76
1 /* $NetBSD: options.c,v 1.114 2012/08/09 11:05:59 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.114 2012/08/09 11:05:59 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 #if !HAVE_NBTOOL_CONFIG_H
132 #define OPT_CHROOT 18
133 #endif
136 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
137 * (see pax.h for description of each function)
139 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
140 * read, end_read, st_write, write, end_write, trail,
141 * subtrail, rd_data, wr_data, options
144 FSUB fsub[] = {
145 #ifndef NO_CPIO
146 /* 0: OLD BINARY CPIO */
147 { "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
148 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
149 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
151 /* 1: OLD OCTAL CHARACTER CPIO */
152 { "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
153 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
154 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
156 /* 2: SVR4 HEX CPIO */
157 { "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
158 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
159 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
161 /* 3: SVR4 HEX CPIO WITH CRC */
162 { "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
163 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
164 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
165 #endif
166 /* 4: OLD TAR */
167 { "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
168 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
169 NULL, rd_wrfile, wr_rdfile, tar_opt },
171 /* 5: POSIX USTAR */
172 { "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
173 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
174 NULL, rd_wrfile, wr_rdfile, bad_opt }
176 #ifndef NO_CPIO
177 #define F_BCPIO 0 /* old binary cpio format */
178 #define F_CPIO 1 /* old octal character cpio format */
179 #define F_SV4CPIO 2 /* SVR4 hex cpio format */
180 #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
181 #define F_TAR 4 /* old V7 UNIX tar format */
182 #define F_USTAR 5 /* ustar format */
183 #else
184 #define F_TAR 0 /* old V7 UNIX tar format */
185 #define F_USTAR 1 /* ustar format */
186 #endif
187 #define DEFLT F_USTAR /* default write format from list above */
190 * ford is the archive search order used by get_arc() to determine what kind
191 * of archive we are dealing with. This helps to properly id archive formats
192 * some formats may be subsets of others....
194 int ford[] = {F_USTAR, F_TAR,
195 #ifndef NO_CPIO
196 F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO,
197 #endif
198 -1};
201 * filename record separator
203 int sep = '\n';
206 * Do we have -C anywhere?
208 int havechd = 0;
211 * options()
212 * figure out if we are pax, tar or cpio. Call the appropriate options
213 * parser
216 void
217 options(int argc, char **argv)
221 * Are we acting like pax, tar or cpio (based on argv[0])
223 if ((argv0 = strrchr(argv[0], '/')) != NULL)
224 argv0++;
225 else
226 argv0 = argv[0];
228 if (strstr(argv0, NM_TAR)) {
229 argv0 = NM_TAR;
230 tar_options(argc, argv);
231 #ifndef NO_CPIO
232 } else if (strstr(argv0, NM_CPIO)) {
233 argv0 = NM_CPIO;
234 cpio_options(argc, argv);
235 #endif
236 } else {
237 argv0 = NM_PAX;
238 pax_options(argc, argv);
242 struct option pax_longopts[] = {
243 { "insecure", no_argument, 0,
244 OPT_INSECURE },
245 { "force-local", no_argument, 0,
246 OPT_FORCE_LOCAL },
247 { "use-compress-program", required_argument, 0,
248 OPT_USE_COMPRESS_PROGRAM },
249 { "xz", no_argument, 0,
250 OPT_XZ },
251 { 0, 0, 0,
252 0 },
256 * pax_options()
257 * look at the user specified flags. set globals as required and check if
258 * the user specified a legal set of flags. If not, complain and exit
261 static void
262 pax_options(int argc, char **argv)
264 int c;
265 size_t i;
266 u_int64_t flg = 0;
267 u_int64_t bflg = 0;
268 char *pt;
269 FSUB tmp;
272 * process option flags
274 while ((c = getopt_long(argc, argv,
275 "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
276 pax_longopts, NULL)) != -1) {
277 switch (c) {
278 case '0':
279 sep = '\0';
280 break;
281 case 'a':
283 * append
285 flg |= AF;
286 break;
287 case 'b':
289 * specify blocksize
291 flg |= BF;
292 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
293 tty_warn(1, "Invalid block size %s", optarg);
294 pax_usage();
296 break;
297 case 'c':
299 * inverse match on patterns
301 cflag = 1;
302 flg |= CF;
303 break;
304 case 'd':
306 * match only dir on extract, not the subtree at dir
308 dflag = 1;
309 flg |= DF;
310 break;
311 case 'f':
313 * filename where the archive is stored
315 arcname = optarg;
316 flg |= FF;
317 break;
318 case 'i':
320 * interactive file rename
322 iflag = 1;
323 flg |= IF;
324 break;
325 case 'j':
327 * pass through bzip2
329 gzip_program = BZIP2_CMD;
330 break;
331 case 'k':
333 * do not clobber files that exist
335 kflag = 1;
336 flg |= KF;
337 break;
338 case 'l':
340 * try to link src to dest with copy (-rw)
342 lflag = 1;
343 flg |= LF;
344 break;
345 case 'n':
347 * select first match for a pattern only
349 nflag = 1;
350 flg |= NF;
351 break;
352 case 'o':
354 * pass format specific options
356 flg |= OF;
357 if (opt_add(optarg) < 0)
358 pax_usage();
359 break;
360 case 'p':
362 * specify file characteristic options
364 for (pt = optarg; *pt != '\0'; ++pt) {
365 switch(*pt) {
366 case 'a':
368 * do not preserve access time
370 patime = 0;
371 break;
372 case 'e':
374 * preserve user id, group id, file
375 * mode, access/modification times
376 * and file flags.
378 pids = 1;
379 pmode = 1;
380 patime = 1;
381 pmtime = 1;
382 pfflags = 1;
383 break;
384 #if 0
385 case 'f':
387 * do not preserve file flags
389 pfflags = 0;
390 break;
391 #endif
392 case 'm':
394 * do not preserve modification time
396 pmtime = 0;
397 break;
398 case 'o':
400 * preserve uid/gid
402 pids = 1;
403 break;
404 case 'p':
406 * preserve file mode bits
408 pmode = 1;
409 break;
410 default:
411 tty_warn(1, "Invalid -p string: %c",
412 *pt);
413 pax_usage();
414 break;
417 flg |= PF;
418 break;
419 case 'r':
421 * read the archive
423 flg |= RF;
424 break;
425 case 's':
427 * file name substitution name pattern
429 if (rep_add(optarg) < 0) {
430 pax_usage();
431 break;
433 flg |= SF;
434 break;
435 case 't':
437 * preserve access time on filesystem nodes we read
439 tflag = 1;
440 flg |= TF;
441 break;
442 case 'u':
444 * ignore those older files
446 uflag = 1;
447 flg |= UF;
448 break;
449 case 'v':
451 * verbose operation mode
453 vflag = 1;
454 flg |= VF;
455 break;
456 case 'w':
458 * write an archive
460 flg |= WF;
461 break;
462 case 'x':
464 * specify an archive format on write
466 tmp.name = optarg;
467 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
468 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
469 if (frmt != NULL) {
470 flg |= XF;
471 break;
473 tty_warn(1, "Unknown -x format: %s", optarg);
474 (void)fputs("pax: Known -x formats are:", stderr);
475 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
476 (void)fprintf(stderr, " %s", fsub[i].name);
477 (void)fputs("\n\n", stderr);
478 pax_usage();
479 break;
480 case 'z':
482 * use gzip. Non standard option.
484 gzip_program = GZIP_CMD;
485 break;
486 case 'A':
487 Aflag = 1;
488 flg |= CAF;
489 break;
490 case 'B':
492 * non-standard option on number of bytes written on a
493 * single archive volume.
495 if ((wrlimit = str_offt(optarg)) <= 0) {
496 tty_warn(1, "Invalid write limit %s", optarg);
497 pax_usage();
499 if (wrlimit % BLKMULT) {
500 tty_warn(1,
501 "Write limit is not a %d byte multiple",
502 BLKMULT);
503 pax_usage();
505 flg |= CBF;
506 break;
507 case 'D':
509 * On extraction check file inode change time before the
510 * modification of the file name. Non standard option.
512 Dflag = 1;
513 flg |= CDF;
514 break;
515 case 'E':
517 * non-standard limit on read faults
518 * 0 indicates stop after first error, values
519 * indicate a limit, "none" try forever
521 flg |= CEF;
522 if (strcmp(none, optarg) == 0)
523 maxflt = -1;
524 else if ((maxflt = atoi(optarg)) < 0) {
525 tty_warn(1,
526 "Error count value must be positive");
527 pax_usage();
529 break;
530 case 'G':
532 * non-standard option for selecting files within an
533 * archive by group (gid or name)
535 if (grp_add(optarg) < 0) {
536 pax_usage();
537 break;
539 flg |= CGF;
540 break;
541 case 'H':
543 * follow command line symlinks only
545 Hflag = 1;
546 flg |= CHF;
547 break;
548 case 'L':
550 * follow symlinks
552 Lflag = 1;
553 flg |= CLF;
554 break;
555 #ifdef SMALL
556 case 'M':
557 case 'N':
558 tty_warn(1, "Support for -%c is not compiled in", c);
559 exit(1);
560 #else /* !SMALL */
561 case 'M':
563 * Treat list of filenames on stdin as an
564 * mtree(8) specfile. Non standard option.
566 Mflag = 1;
567 flg |= CMF;
568 break;
569 case 'N':
571 * Use alternative directory for user db lookups.
573 if (!setup_getid(optarg)) {
574 tty_warn(1,
575 "Unable to use user and group databases in `%s'",
576 optarg);
577 pax_usage();
579 break;
580 #endif /* !SMALL */
581 case 'O':
583 * Force one volume. Non standard option.
585 force_one_volume = 1;
586 break;
587 case 'P':
589 * do NOT follow symlinks (default)
591 Lflag = 0;
592 flg |= CPF;
593 break;
594 case 'T':
596 * non-standard option for selecting files within an
597 * archive by modification time range (lower,upper)
599 if (trng_add(optarg) < 0) {
600 pax_usage();
601 break;
603 flg |= CTF;
604 break;
605 case 'U':
607 * non-standard option for selecting files within an
608 * archive by user (uid or name)
610 if (usr_add(optarg) < 0) {
611 pax_usage();
612 break;
614 flg |= CUF;
615 break;
616 case 'V':
618 * somewhat verbose operation mode (no listing)
620 Vflag = 1;
621 flg |= VSF;
622 break;
623 case 'X':
625 * do not pass over mount points in the file system
627 Xflag = 1;
628 flg |= CXF;
629 break;
630 case 'Y':
632 * On extraction check file inode change time after the
633 * modification of the file name. Non standard option.
635 Yflag = 1;
636 flg |= CYF;
637 break;
638 case 'Z':
640 * On extraction check modification time after the
641 * modification of the file name. Non standard option.
643 Zflag = 1;
644 flg |= CZF;
645 break;
646 case OPT_INSECURE:
647 secure = 0;
648 break;
649 case OPT_FORCE_LOCAL:
650 forcelocal = 1;
651 break;
652 case OPT_USE_COMPRESS_PROGRAM:
653 gzip_program = optarg;
654 break;
655 case OPT_XZ:
656 gzip_program = XZ_CMD;
657 break;
658 case '?':
659 default:
660 pax_usage();
661 break;
666 * figure out the operation mode of pax read,write,extract,copy,append
667 * or list. check that we have not been given a bogus set of flags
668 * for the operation mode.
670 if (ISLIST(flg)) {
671 act = LIST;
672 listf = stdout;
673 bflg = flg & BDLIST;
674 } else if (ISEXTRACT(flg)) {
675 act = EXTRACT;
676 bflg = flg & BDEXTR;
677 } else if (ISARCHIVE(flg)) {
678 act = ARCHIVE;
679 bflg = flg & BDARCH;
680 } else if (ISAPPND(flg)) {
681 act = APPND;
682 bflg = flg & BDARCH;
683 } else if (ISCOPY(flg)) {
684 act = COPY;
685 bflg = flg & BDCOPY;
686 } else
687 pax_usage();
688 if (bflg) {
689 printflg(flg);
690 pax_usage();
694 * if we are writing (ARCHIVE) we use the default format if the user
695 * did not specify a format. when we write during an APPEND, we will
696 * adopt the format of the existing archive if none was supplied.
698 if (!(flg & XF) && (act == ARCHIVE))
699 frmt = &(fsub[DEFLT]);
702 * process the args as they are interpreted by the operation mode
704 switch (act) {
705 case LIST:
706 case EXTRACT:
707 for (; optind < argc; optind++)
708 if (pat_add(argv[optind], NULL, 0) < 0)
709 pax_usage();
710 break;
711 case COPY:
712 if (optind >= argc) {
713 tty_warn(0, "Destination directory was not supplied");
714 pax_usage();
716 --argc;
717 dirptr = argv[argc];
718 if (mkpath(dirptr) < 0)
719 exit(1);
720 /* FALLTHROUGH */
721 case ARCHIVE:
722 case APPND:
723 for (; optind < argc; optind++)
724 if (ftree_add(argv[optind], 0) < 0)
725 pax_usage();
727 * no read errors allowed on updates/append operation!
729 maxflt = 0;
730 break;
736 * tar_options()
737 * look at the user specified flags. set globals as required and check if
738 * the user specified a legal set of flags. If not, complain and exit
741 struct option tar_longopts[] = {
742 { "block-size", required_argument, 0, 'b' },
743 { "bunzip2", no_argument, 0, 'j' },
744 { "bzip2", no_argument, 0, 'j' },
745 { "create", no_argument, 0, 'c' }, /* F */
746 /* -e -- no corresponding long option */
747 { "file", required_argument, 0, 'f' },
748 { "dereference", no_argument, 0, 'h' },
749 { "keep-old-files", no_argument, 0, 'k' },
750 { "one-file-system", no_argument, 0, 'l' },
751 { "modification-time", no_argument, 0, 'm' },
752 { "old-archive", no_argument, 0, 'o' },
753 { "portability", no_argument, 0, 'o' },
754 { "same-permissions", no_argument, 0, 'p' },
755 { "preserve-permissions", no_argument, 0, 'p' },
756 { "preserve", no_argument, 0, 'p' },
757 { "fast-read", no_argument, 0, 'q' },
758 { "append", no_argument, 0, 'r' }, /* F */
759 { "update", no_argument, 0, 'u' }, /* F */
760 { "list", no_argument, 0, 't' }, /* F */
761 { "verbose", no_argument, 0, 'v' },
762 { "interactive", no_argument, 0, 'w' },
763 { "confirmation", no_argument, 0, 'w' },
764 { "extract", no_argument, 0, 'x' }, /* F */
765 { "get", no_argument, 0, 'x' }, /* F */
766 { "gzip", no_argument, 0, 'z' },
767 { "gunzip", no_argument, 0, 'z' },
768 { "read-full-blocks", no_argument, 0, 'B' },
769 { "directory", required_argument, 0, 'C' },
770 { "to-stdout", no_argument, 0, 'O' },
771 { "absolute-paths", no_argument, 0, 'P' },
772 { "sparse", no_argument, 0, 'S' },
773 { "files-from", required_argument, 0, 'T' },
774 { "summary", no_argument, 0, 'V' },
775 { "stats", no_argument, 0, 'V' },
776 { "exclude-from", required_argument, 0, 'X' },
777 { "compress", no_argument, 0, 'Z' },
778 { "uncompress", no_argument, 0, 'Z' },
779 { "strict", no_argument, 0,
780 OPT_STRICT },
781 { "atime-preserve", no_argument, 0,
782 OPT_ATIME_PRESERVE },
783 { "unlink", no_argument, 0,
784 OPT_UNLINK },
785 { "use-compress-program", required_argument, 0,
786 OPT_USE_COMPRESS_PROGRAM },
787 { "force-local", no_argument, 0,
788 OPT_FORCE_LOCAL },
789 { "insecure", no_argument, 0,
790 OPT_INSECURE },
791 { "exclude", required_argument, 0,
792 OPT_EXCLUDE },
793 { "no-recursion", no_argument, 0,
794 OPT_NORECURSE },
795 { "xz", no_argument, 0,
796 OPT_XZ },
797 #if !HAVE_NBTOOL_CONFIG_H
798 { "chroot", no_argument, 0,
799 OPT_CHROOT },
800 #endif
801 #if 0 /* Not implemented */
802 { "catenate", no_argument, 0, 'A' }, /* F */
803 { "concatenate", no_argument, 0, 'A' }, /* F */
804 { "diff", no_argument, 0, 'd' }, /* F */
805 { "compare", no_argument, 0, 'd' }, /* F */
806 { "checkpoint", no_argument, 0,
807 OPT_CHECKPOINT },
808 { "help", no_argument, 0,
809 OPT_HELP },
810 { "info-script", required_argument, 0, 'F' },
811 { "new-volume-script", required_argument, 0, 'F' },
812 { "incremental", no_argument, 0, 'G' },
813 { "listed-incremental", required_argument, 0, 'g' },
814 { "ignore-zeros", no_argument, 0, 'i' },
815 { "ignore-failed-read", no_argument, 0,
816 OPT_IGNORE_FAILED_READ },
817 { "starting-file", no_argument, 0, 'K' },
818 { "tape-length", required_argument, 0, 'L' },
819 { "multi-volume", no_argument, 0, 'M' },
820 { "after-date", required_argument, 0, 'N' },
821 { "newer", required_argument, 0, 'N' },
822 { "record-number", no_argument, 0, 'R' },
823 { "remove-files", no_argument, 0,
824 OPT_REMOVE_FILES },
825 { "same-order", no_argument, 0, 's' },
826 { "preserve-order", no_argument, 0, 's' },
827 { "null", no_argument, 0,
828 OPT_NULL },
829 { "totals", no_argument, 0,
830 OPT_TOTALS },
831 { "volume-name", required_argument, 0, 'V' }, /* XXX */
832 { "label", required_argument, 0, 'V' }, /* XXX */
833 { "version", no_argument, 0,
834 OPT_VERSION },
835 { "verify", no_argument, 0, 'W' },
836 { "block-compress", no_argument, 0,
837 OPT_BLOCK_COMPRESS },
838 #endif
839 { 0, 0, 0, 0 },
842 static void
843 tar_set_action(int op)
845 if (act != ERROR && act != op)
846 tar_usage();
847 act = op;
850 static void
851 tar_options(int argc, char **argv)
853 int c;
854 int fstdin = 0;
855 int Oflag = 0;
856 int nincfiles = 0;
857 int incfiles_max = 0;
858 struct incfile {
859 char *file;
860 char *dir;
862 struct incfile *incfiles = NULL;
865 * Set default values.
867 rmleadslash = 1;
868 is_gnutar = 1;
871 * process option flags
873 while ((c = getoldopt(argc, argv,
874 "+b:cef:hjklmopqrs:tuvwxzBC:HI:OPST:X:Z014578",
875 tar_longopts, NULL))
876 != -1) {
877 switch(c) {
878 case 'b':
880 * specify blocksize in 512-byte blocks
882 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
883 tty_warn(1, "Invalid block size %s", optarg);
884 tar_usage();
886 wrblksz *= 512; /* XXX - check for int oflow */
887 break;
888 case 'c':
890 * create an archive
892 tar_set_action(ARCHIVE);
893 break;
894 case 'e':
896 * stop after first error
898 maxflt = 0;
899 break;
900 case 'f':
902 * filename where the archive is stored
904 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
906 * treat a - as stdin
908 fstdin = 1;
909 arcname = NULL;
910 break;
912 fstdin = 0;
913 arcname = optarg;
914 break;
915 case 'h':
917 * follow symlinks
919 Lflag = 1;
920 break;
921 case 'j':
923 * pass through bzip2. not a standard option
925 gzip_program = BZIP2_CMD;
926 break;
927 case 'k':
929 * do not clobber files that exist
931 kflag = 1;
932 break;
933 case 'l':
935 * do not pass over mount points in the file system
937 Xflag = 1;
938 break;
939 case 'm':
941 * do not preserve modification time
943 pmtime = 0;
944 break;
945 case 'o':
947 * This option does several things based on whether
948 * this is a create or extract operation.
950 if (act == ARCHIVE) {
951 /* GNU tar: write V7 format archives. */
952 Oflag = 1;
953 /* 4.2BSD: don't add directory entries. */
954 if (opt_add("write_opt=nodir") < 0)
955 tar_usage();
957 } else {
958 /* SUS: don't preserve owner/group. */
959 pids = 0;
960 nopids = 1;
962 break;
963 case 'O':
964 Oflag = 1;
965 break;
966 case 'p':
968 * preserve user id, group id, file
969 * mode, access/modification times
971 if (!nopids)
972 pids = 1;
973 pmode = 1;
974 patime = 1;
975 pmtime = 1;
976 break;
977 case 'q':
979 * select first match for a pattern only
981 nflag = 1;
982 break;
983 case 'r':
984 case 'u':
986 * append to the archive
988 tar_set_action(APPND);
989 break;
990 case 's':
992 * file name substitution name pattern
994 if (rep_add(optarg) < 0) {
995 tar_usage();
996 break;
998 break;
999 case 't':
1001 * list contents of the tape
1003 tar_set_action(LIST);
1004 break;
1005 case 'v':
1007 * verbose operation mode
1009 vflag = 1;
1010 break;
1011 case 'w':
1013 * interactive file rename
1015 iflag = 1;
1016 break;
1017 case 'x':
1019 * extract an archive, preserving mode,
1020 * and mtime if possible.
1022 tar_set_action(EXTRACT);
1023 pmtime = 1;
1024 break;
1025 case 'z':
1027 * use gzip. Non standard option.
1029 gzip_program = GZIP_CMD;
1030 break;
1031 case 'B':
1033 * Nothing to do here, this is pax default
1035 break;
1036 case 'C':
1037 havechd++;
1038 chdname = optarg;
1039 break;
1040 case 'H':
1042 * follow command line symlinks only
1044 Hflag = 1;
1045 break;
1046 case 'I':
1047 case 'T':
1048 if (++nincfiles > incfiles_max) {
1049 incfiles_max = nincfiles + 3;
1050 incfiles = realloc(incfiles,
1051 sizeof(*incfiles) * incfiles_max);
1052 if (incfiles == NULL) {
1053 tty_warn(0, "Unable to allocate space "
1054 "for option list");
1055 exit(1);
1058 incfiles[nincfiles - 1].file = optarg;
1059 incfiles[nincfiles - 1].dir = chdname;
1060 break;
1061 case 'P':
1063 * do not remove leading '/' from pathnames
1065 rmleadslash = 0;
1066 Aflag = 1;
1067 break;
1068 case 'S':
1069 /* do nothing; we already generate sparse files */
1070 break;
1071 case 'V':
1073 * semi-verbose operation mode (no listing)
1075 Vflag = 1;
1076 break;
1077 case 'X':
1079 * GNU tar compat: exclude the files listed in optarg
1081 if (tar_gnutar_X_compat(optarg) != 0)
1082 tar_usage();
1083 break;
1084 case 'Z':
1086 * use compress.
1088 gzip_program = COMPRESS_CMD;
1089 break;
1090 case '0':
1091 arcname = DEV_0;
1092 break;
1093 case '1':
1094 arcname = DEV_1;
1095 break;
1096 case '4':
1097 arcname = DEV_4;
1098 break;
1099 case '5':
1100 arcname = DEV_5;
1101 break;
1102 case '7':
1103 arcname = DEV_7;
1104 break;
1105 case '8':
1106 arcname = DEV_8;
1107 break;
1108 case OPT_ATIME_PRESERVE:
1109 patime = 1;
1110 break;
1111 case OPT_UNLINK:
1112 /* Just ignore -- we always unlink first. */
1113 break;
1114 case OPT_USE_COMPRESS_PROGRAM:
1115 gzip_program = optarg;
1116 break;
1117 case OPT_FORCE_LOCAL:
1118 forcelocal = 1;
1119 break;
1120 case OPT_INSECURE:
1121 secure = 0;
1122 break;
1123 case OPT_STRICT:
1124 /* disable gnu extensions */
1125 is_gnutar = 0;
1126 break;
1127 case OPT_EXCLUDE:
1128 if (tar_gnutar_minus_minus_exclude(optarg) != 0)
1129 tar_usage();
1130 break;
1131 case OPT_NORECURSE:
1132 dflag = 1;
1133 break;
1134 #if !HAVE_NBTOOL_CONFIG_H
1135 case OPT_CHROOT:
1136 do_chroot = 1;
1137 break;
1138 #endif
1139 case OPT_XZ:
1140 gzip_program = XZ_CMD;
1141 break;
1142 default:
1143 tar_usage();
1144 break;
1147 argc -= optind;
1148 argv += optind;
1150 /* Tar requires an action. */
1151 if (act == ERROR)
1152 tar_usage();
1154 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1155 if (fstdin == 1 && act == ARCHIVE)
1156 listf = stderr;
1157 else
1158 listf = stdout;
1160 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1161 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
1162 exit(0);
1164 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1165 * (unless -o specified)
1167 if (act == ARCHIVE || act == APPND)
1168 frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
1169 else if (Oflag) {
1170 if (act == EXTRACT)
1171 to_stdout = 1;
1172 else {
1173 tty_warn(1, "The -O/-o options are only valid when "
1174 "writing or extracting an archive");
1175 tar_usage();
1180 * process the args as they are interpreted by the operation mode
1182 switch (act) {
1183 case LIST:
1184 case EXTRACT:
1185 default:
1187 int sawpat = 0;
1188 int dirisnext = 0;
1189 char *file, *dir = NULL;
1190 int mustfreedir = 0;
1192 while (nincfiles || *argv != NULL) {
1194 * If we queued up any include files,
1195 * pull them in now. Otherwise, check
1196 * for -I and -C positional flags.
1197 * Anything else must be a file to
1198 * extract.
1200 if (nincfiles) {
1201 file = incfiles->file;
1202 dir = incfiles->dir;
1203 mustfreedir = 0;
1204 incfiles++;
1205 nincfiles--;
1206 } else if (strcmp(*argv, "-I") == 0) {
1207 if (*++argv == NULL)
1208 break;
1209 file = *argv++;
1210 dir = chdname;
1211 mustfreedir = 0;
1212 } else {
1213 file = NULL;
1214 dir = NULL;
1215 mustfreedir = 0;
1217 if (file != NULL) {
1218 FILE *fp;
1219 char *str;
1221 if (strcmp(file, "-") == 0)
1222 fp = stdin;
1223 else if ((fp = fopen(file, "r")) == NULL) {
1224 tty_warn(1, "Unable to open file '%s' for read", file);
1225 tar_usage();
1227 while ((str = get_line(fp)) != NULL) {
1228 if (dirisnext) {
1229 if (dir && mustfreedir)
1230 free(dir);
1231 dir = str;
1232 mustfreedir = 1;
1233 dirisnext = 0;
1234 continue;
1236 if (strcmp(str, "-C") == 0) {
1237 havechd++;
1238 dirisnext = 1;
1239 free(str);
1240 continue;
1242 if (strncmp(str, "-C ", 3) == 0) {
1243 havechd++;
1244 if (dir && mustfreedir)
1245 free(dir);
1246 dir = strdup(str + 3);
1247 mustfreedir = 1;
1248 free(str);
1249 continue;
1251 if (pat_add(str, dir, NOGLOB_MTCH) < 0)
1252 tar_usage();
1253 sawpat = 1;
1255 /* Bomb if given -C w/out a dir. */
1256 if (dirisnext)
1257 tar_usage();
1258 if (dir && mustfreedir)
1259 free(dir);
1260 if (strcmp(file, "-") != 0)
1261 fclose(fp);
1262 if (get_line_error) {
1263 tty_warn(1, "Problem with file '%s'", file);
1264 tar_usage();
1266 } else if (strcmp(*argv, "-C") == 0) {
1267 if (*++argv == NULL)
1268 break;
1269 chdname = *argv++;
1270 havechd++;
1271 } else if (pat_add(*argv++, chdname, 0) < 0)
1272 tar_usage();
1273 else
1274 sawpat = 1;
1277 * if patterns were added, we are doing chdir()
1278 * on a file-by-file basis, else, just one
1279 * global chdir (if any) after opening input.
1281 if (sawpat > 0)
1282 chdname = NULL;
1284 break;
1285 case ARCHIVE:
1286 case APPND:
1287 if (chdname != NULL) { /* initial chdir() */
1288 if (ftree_add(chdname, 1) < 0)
1289 tar_usage();
1292 while (nincfiles || *argv != NULL) {
1293 char *file, *dir;
1296 * If we queued up any include files, pull them in
1297 * now. Otherwise, check for -I and -C positional
1298 * flags. Anything else must be a file to include
1299 * in the archive.
1301 if (nincfiles) {
1302 file = incfiles->file;
1303 dir = incfiles->dir;
1304 incfiles++;
1305 nincfiles--;
1306 } else if (strcmp(*argv, "-I") == 0) {
1307 if (*++argv == NULL)
1308 break;
1309 file = *argv++;
1310 dir = NULL;
1311 } else {
1312 file = NULL;
1313 dir = NULL;
1315 if (file != NULL) {
1316 FILE *fp;
1317 char *str;
1318 int dirisnext = 0;
1320 /* Set directory if needed */
1321 if (dir) {
1322 if (ftree_add(dir, 1) < 0)
1323 tar_usage();
1326 if (strcmp(file, "-") == 0)
1327 fp = stdin;
1328 else if ((fp = fopen(file, "r")) == NULL) {
1329 tty_warn(1, "Unable to open file '%s' for read", file);
1330 tar_usage();
1332 while ((str = get_line(fp)) != NULL) {
1333 if (dirisnext) {
1334 if (ftree_add(str, 1) < 0)
1335 tar_usage();
1336 dirisnext = 0;
1337 continue;
1339 if (strcmp(str, "-C") == 0) {
1340 dirisnext = 1;
1341 continue;
1343 if (strncmp(str, "-C ", 3) == 0) {
1344 if (ftree_add(str + 3, 1) < 0)
1345 tar_usage();
1346 continue;
1348 if (ftree_add(str, 0) < 0)
1349 tar_usage();
1351 /* Bomb if given -C w/out a dir. */
1352 if (dirisnext)
1353 tar_usage();
1354 if (strcmp(file, "-") != 0)
1355 fclose(fp);
1356 if (get_line_error) {
1357 tty_warn(1, "Problem with file '%s'",
1358 file);
1359 tar_usage();
1361 } else if (strcmp(*argv, "-C") == 0) {
1362 if (*++argv == NULL)
1363 break;
1364 if (ftree_add(*argv++, 1) < 0)
1365 tar_usage();
1366 } else if (ftree_add(*argv++, 0) < 0)
1367 tar_usage();
1370 * no read errors allowed on updates/append operation!
1372 maxflt = 0;
1373 break;
1375 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
1376 arcname = getenv("TAPE");
1377 if ((arcname == NULL) || (*arcname == '\0'))
1378 arcname = _PATH_DEFTAPE;
1383 mkpath(char *path)
1385 char *slash;
1386 int done = 0;
1388 slash = path;
1390 while (!done) {
1391 slash += strspn(slash, "/");
1392 slash += strcspn(slash, "/");
1394 done = (*slash == '\0');
1395 *slash = '\0';
1397 if (domkdir(path, 0777) == -1)
1398 goto out;
1400 if (!done)
1401 *slash = '/';
1404 return 0;
1405 out:
1406 /* Can't create or or not a directory */
1407 syswarn(1, errno, "Cannot create directory `%s'", path);
1408 return -1;
1412 #ifndef NO_CPIO
1413 struct option cpio_longopts[] = {
1414 { "reset-access-time", no_argument, 0, 'a' },
1415 { "make-directories", no_argument, 0, 'd' },
1416 { "nonmatching", no_argument, 0, 'f' },
1417 { "extract", no_argument, 0, 'i' },
1418 { "link", no_argument, 0, 'l' },
1419 { "preserve-modification-time", no_argument, 0, 'm' },
1420 { "create", no_argument, 0, 'o' },
1421 { "pass-through", no_argument, 0, 'p' },
1422 { "rename", no_argument, 0, 'r' },
1423 { "list", no_argument, 0, 't' },
1424 { "unconditional", no_argument, 0, 'u' },
1425 { "verbose", no_argument, 0, 'v' },
1426 { "append", no_argument, 0, 'A' },
1427 { "pattern-file", required_argument, 0, 'E' },
1428 { "file", required_argument, 0, 'F' },
1429 { "force-local", no_argument, 0,
1430 OPT_FORCE_LOCAL },
1431 { "format", required_argument, 0, 'H' },
1432 { "dereference", no_argument, 0, 'L' },
1433 { "swap-halfwords", no_argument, 0, 'S' },
1434 { "summary", no_argument, 0, 'V' },
1435 { "stats", no_argument, 0, 'V' },
1436 { "insecure", no_argument, 0,
1437 OPT_INSECURE },
1438 { "sparse", no_argument, 0,
1439 OPT_SPARSE },
1440 { "xz", no_argument, 0,
1441 OPT_XZ },
1443 #ifdef notyet
1444 /* Not implemented */
1445 { "null", no_argument, 0, '0' },
1446 { "swap", no_argument, 0, 'b' },
1447 { "numeric-uid-gid", no_argument, 0, 'n' },
1448 { "swap-bytes", no_argument, 0, 's' },
1449 { "message", required_argument, 0, 'M' },
1450 { "owner", required_argument, 0 'R' },
1451 { "dot", no_argument, 0, 'V' }, /* xxx */
1452 { "block-size", required_argument, 0,
1453 OPT_BLOCK_SIZE },
1454 { "no-absolute-pathnames", no_argument, 0,
1455 OPT_NO_ABSOLUTE_PATHNAMES },
1456 { "no-preserve-owner", no_argument, 0,
1457 OPT_NO_PRESERVE_OWNER },
1458 { "only-verify-crc", no_argument, 0,
1459 OPT_ONLY_VERIFY_CRC },
1460 { "rsh-command", required_argument, 0,
1461 OPT_RSH_COMMAND },
1462 { "version", no_argument, 0,
1463 OPT_VERSION },
1464 #endif
1465 { 0, 0, 0, 0 },
1468 static void
1469 cpio_set_action(int op)
1471 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1472 act = APPND;
1473 else if (act == EXTRACT && op == LIST)
1474 act = op;
1475 else if (act != ERROR && act != op)
1476 cpio_usage();
1477 else
1478 act = op;
1482 * cpio_options()
1483 * look at the user specified flags. set globals as required and check if
1484 * the user specified a legal set of flags. If not, complain and exit
1487 static void
1488 cpio_options(int argc, char **argv)
1490 FSUB tmp;
1491 u_int64_t flg = 0;
1492 u_int64_t bflg = 0;
1493 int c;
1494 size_t i;
1495 FILE *fp;
1496 char *str;
1498 uflag = 1;
1499 kflag = 1;
1500 pids = 1;
1501 pmode = 1;
1502 pmtime = 0;
1503 arcname = NULL;
1504 dflag = 1;
1505 nodirs = 1;
1507 * process option flags
1509 while ((c = getoldopt(argc, argv,
1510 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1511 cpio_longopts, NULL)) != -1) {
1512 switch(c) {
1513 case 'a':
1515 * preserve access time on filesystem nodes we read
1517 tflag = 1;
1518 flg |= TF;
1519 break;
1520 #ifdef notyet
1521 case 'b':
1523 * swap bytes and half-words when reading data
1525 break;
1526 #endif
1527 case 'c':
1529 * ASCII cpio header
1531 frmt = &fsub[F_SV4CPIO];
1532 break;
1533 case 'd':
1535 * create directories as needed
1536 * pax does this by default ..
1538 nodirs = 0;
1539 break;
1540 case 'f':
1542 * inverse match on patterns
1544 cflag = 1;
1545 flg |= CF;
1546 break;
1547 case 'i':
1549 * read the archive
1551 cpio_set_action(EXTRACT);
1552 flg |= RF;
1553 break;
1554 #ifdef notyet
1555 case 'k':
1556 break;
1557 #endif
1558 case 'l':
1560 * try to link src to dest with copy (-rw)
1562 lflag = 1;
1563 flg |= LF;
1564 break;
1565 case 'm':
1567 * preserve mtime
1569 flg |= PF;
1570 pmtime = 1;
1571 break;
1572 case 'o':
1574 * write an archive
1576 cpio_set_action(ARCHIVE);
1577 frmt = &(fsub[F_SV4CRC]);
1578 flg |= WF;
1579 break;
1580 case 'p':
1582 * cpio -p is like pax -rw
1584 cpio_set_action(COPY);
1585 flg |= RF | WF;
1586 break;
1587 case 'r':
1589 * interactive file rename
1591 iflag = 1;
1592 flg |= IF;
1593 break;
1594 #ifdef notyet
1595 case 's':
1597 * swap bytes after reading data
1599 break;
1600 #endif
1601 case 't':
1603 * list contents of archive
1605 cpio_set_action(LIST);
1606 listf = stdout;
1607 flg &= ~RF;
1608 break;
1609 case 'u':
1611 * don't ignore those older files
1613 uflag = 0;
1614 kflag = 0;
1615 flg |= UF;
1616 break;
1617 case 'v':
1619 * verbose operation mode
1621 vflag = 1;
1622 flg |= VF;
1623 break;
1624 case 'z':
1626 * use gzip. Non standard option.
1628 gzip_program = GZIP_CMD;
1629 break;
1630 case 'A':
1632 * append to an archive
1634 cpio_set_action(APPND);
1635 flg |= AF;
1636 break;
1637 case 'B':
1639 * set blocksize to 5120
1641 blksz = 5120;
1642 break;
1643 case 'C':
1645 * specify blocksize
1647 if ((blksz = (int)str_offt(optarg)) <= 0) {
1648 tty_warn(1, "Invalid block size %s", optarg);
1649 cpio_usage();
1651 break;
1652 case 'E':
1654 * file with patterns to extract or list
1656 if ((fp = fopen(optarg, "r")) == NULL) {
1657 tty_warn(1, "Unable to open file '%s' for read",
1658 optarg);
1659 cpio_usage();
1661 while ((str = get_line(fp)) != NULL) {
1662 pat_add(str, NULL, 0);
1664 fclose(fp);
1665 if (get_line_error) {
1666 tty_warn(1, "Problem with file '%s'", optarg);
1667 cpio_usage();
1669 break;
1670 case 'H':
1672 * specify an archive format on write
1674 tmp.name = optarg;
1675 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1676 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
1677 if (frmt != NULL) {
1678 flg |= XF;
1679 break;
1681 tty_warn(1, "Unknown -H format: %s", optarg);
1682 (void)fputs("cpio: Known -H formats are:", stderr);
1683 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1684 (void)fprintf(stderr, " %s", fsub[i].name);
1685 (void)fputs("\n\n", stderr);
1686 cpio_usage();
1687 break;
1688 case 'F':
1689 case 'I':
1690 case 'O':
1692 * filename where the archive is stored
1694 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1696 * treat a - as stdin
1698 arcname = NULL;
1699 break;
1701 arcname = optarg;
1702 break;
1703 case 'L':
1705 * follow symlinks
1707 Lflag = 1;
1708 flg |= CLF;
1709 break;
1710 #ifdef notyet
1711 case 'M':
1712 arg = optarg;
1713 break;
1714 case 'R':
1715 arg = optarg;
1716 break;
1717 #endif
1718 case 'S':
1720 * swap halfwords after reading data
1722 cpio_swp_head = 1;
1723 break;
1724 #ifdef notyet
1725 case 'V': /* print a '.' for each file processed */
1726 break;
1727 #endif
1728 case 'V':
1730 * semi-verbose operation mode (no listing)
1732 Vflag = 1;
1733 flg |= VF;
1734 break;
1735 case 'Z':
1737 * use compress. Non standard option.
1739 gzip_program = COMPRESS_CMD;
1740 break;
1741 case '6':
1743 * process Version 6 cpio format
1745 frmt = &(fsub[F_BCPIO]);
1746 break;
1747 case OPT_FORCE_LOCAL:
1748 forcelocal = 1;
1749 break;
1750 case OPT_INSECURE:
1751 secure = 0;
1752 break;
1753 case OPT_SPARSE:
1754 /* do nothing; we already generate sparse files */
1755 break;
1756 case OPT_XZ:
1757 gzip_program = XZ_CMD;
1758 break;
1759 default:
1760 cpio_usage();
1761 break;
1766 * figure out the operation mode of cpio. check that we have not been
1767 * given a bogus set of flags for the operation mode.
1769 if (ISLIST(flg)) {
1770 act = LIST;
1771 bflg = flg & BDLIST;
1772 } else if (ISEXTRACT(flg)) {
1773 act = EXTRACT;
1774 bflg = flg & BDEXTR;
1775 } else if (ISARCHIVE(flg)) {
1776 act = ARCHIVE;
1777 bflg = flg & BDARCH;
1778 } else if (ISAPPND(flg)) {
1779 act = APPND;
1780 bflg = flg & BDARCH;
1781 } else if (ISCOPY(flg)) {
1782 act = COPY;
1783 bflg = flg & BDCOPY;
1784 } else
1785 cpio_usage();
1786 if (bflg) {
1787 cpio_usage();
1791 * if we are writing (ARCHIVE) we use the default format if the user
1792 * did not specify a format. when we write during an APPEND, we will
1793 * adopt the format of the existing archive if none was supplied.
1795 if (!(flg & XF) && (act == ARCHIVE))
1796 frmt = &(fsub[F_BCPIO]);
1799 * process the args as they are interpreted by the operation mode
1801 switch (act) {
1802 case LIST:
1803 case EXTRACT:
1804 for (; optind < argc; optind++)
1805 if (pat_add(argv[optind], NULL, 0) < 0)
1806 cpio_usage();
1807 break;
1808 case COPY:
1809 if (optind >= argc) {
1810 tty_warn(0, "Destination directory was not supplied");
1811 cpio_usage();
1813 --argc;
1814 dirptr = argv[argc];
1815 /* FALLTHROUGH */
1816 case ARCHIVE:
1817 case APPND:
1818 if (argc != optind) {
1819 for (; optind < argc; optind++)
1820 if (ftree_add(argv[optind], 0) < 0)
1821 cpio_usage();
1822 break;
1825 * no read errors allowed on updates/append operation!
1827 maxflt = 0;
1828 while ((str = get_line(stdin)) != NULL) {
1829 ftree_add(str, 0);
1831 if (get_line_error) {
1832 tty_warn(1, "Problem while reading stdin");
1833 cpio_usage();
1835 break;
1836 default:
1837 cpio_usage();
1838 break;
1841 #endif
1844 * printflg()
1845 * print out those invalid flag sets found to the user
1848 static void
1849 printflg(unsigned int flg)
1851 int nxt;
1853 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1854 while ((nxt = ffs(flg)) != 0) {
1855 flg &= ~(1 << (nxt - 1));
1856 (void)fprintf(stderr, " -%c", flgch[nxt - 1]);
1858 (void)putc('\n', stderr);
1862 * c_frmt()
1863 * comparison routine used by bsearch to find the format specified
1864 * by the user
1867 static int
1868 c_frmt(const void *a, const void *b)
1870 return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
1874 * opt_next()
1875 * called by format specific options routines to get each format specific
1876 * flag and value specified with -o
1877 * Return:
1878 * pointer to next OPLIST entry or NULL (end of list).
1881 OPLIST *
1882 opt_next(void)
1884 OPLIST *opt;
1886 if ((opt = ophead) != NULL)
1887 ophead = ophead->fow;
1888 return opt;
1892 * bad_opt()
1893 * generic routine used to complain about a format specific options
1894 * when the format does not support options.
1898 bad_opt(void)
1900 OPLIST *opt;
1902 if (ophead == NULL)
1903 return 0;
1905 * print all we were given
1907 tty_warn(1," These format options are not supported for %s",
1908 frmt->name);
1909 while ((opt = opt_next()) != NULL)
1910 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1911 if (strcmp(NM_TAR, argv0) == 0)
1912 tar_usage();
1913 #ifndef NO_CPIO
1914 else if (strcmp(NM_CPIO, argv0) == 0)
1915 cpio_usage();
1916 #endif
1917 else
1918 pax_usage();
1919 return 0;
1923 * opt_add()
1924 * breaks the value supplied to -o into a option name and value. options
1925 * are given to -o in the form -o name-value,name=value
1926 * multiple -o may be specified.
1927 * Return:
1928 * 0 if format in name=value format, -1 if -o is passed junk
1932 opt_add(const char *str)
1934 OPLIST *opt;
1935 char *frpt;
1936 char *pt;
1937 char *endpt;
1938 char *dstr;
1940 if ((str == NULL) || (*str == '\0')) {
1941 tty_warn(0, "Invalid option name");
1942 return -1;
1944 if ((dstr = strdup(str)) == NULL) {
1945 tty_warn(0, "Unable to allocate space for option list");
1946 return -1;
1948 frpt = endpt = dstr;
1951 * break into name and values pieces and stuff each one into a
1952 * OPLIST structure. When we know the format, the format specific
1953 * option function will go through this list
1955 while ((frpt != NULL) && (*frpt != '\0')) {
1956 if ((endpt = strchr(frpt, ',')) != NULL)
1957 *endpt = '\0';
1958 if ((pt = strchr(frpt, '=')) == NULL) {
1959 tty_warn(0, "Invalid options format");
1960 free(dstr);
1961 return -1;
1963 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1964 tty_warn(0, "Unable to allocate space for option list");
1965 free(dstr);
1966 return -1;
1968 *pt++ = '\0';
1969 opt->name = frpt;
1970 opt->value = pt;
1971 opt->fow = NULL;
1972 if (endpt != NULL)
1973 frpt = endpt + 1;
1974 else
1975 frpt = NULL;
1976 if (ophead == NULL) {
1977 optail = ophead = opt;
1978 continue;
1980 optail->fow = opt;
1981 optail = opt;
1983 return 0;
1987 * str_offt()
1988 * Convert an expression of the following forms to an off_t > 0.
1989 * 1) A positive decimal number.
1990 * 2) A positive decimal number followed by a b (mult by 512).
1991 * 3) A positive decimal number followed by a k (mult by 1024).
1992 * 4) A positive decimal number followed by a m (mult by 512).
1993 * 5) A positive decimal number followed by a w (mult by sizeof int)
1994 * 6) Two or more positive decimal numbers (with/without k,b or w).
1995 * separated by x (also * for backwards compatibility), specifying
1996 * the product of the indicated values.
1997 * Return:
1998 * 0 for an error, a positive value o.w.
2001 static off_t
2002 str_offt(char *val)
2004 char *expr;
2005 off_t num, t;
2007 num = STRTOOFFT(val, &expr, 0);
2008 if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
2009 return 0;
2011 switch(*expr) {
2012 case 'b':
2013 t = num;
2014 num *= 512;
2015 if (t > num)
2016 return 0;
2017 ++expr;
2018 break;
2019 case 'k':
2020 t = num;
2021 num *= 1024;
2022 if (t > num)
2023 return 0;
2024 ++expr;
2025 break;
2026 case 'm':
2027 t = num;
2028 num *= 1048576;
2029 if (t > num)
2030 return 0;
2031 ++expr;
2032 break;
2033 case 'w':
2034 t = num;
2035 num *= sizeof(int);
2036 if (t > num)
2037 return 0;
2038 ++expr;
2039 break;
2042 switch(*expr) {
2043 case '\0':
2044 break;
2045 case '*':
2046 case 'x':
2047 t = num;
2048 num *= str_offt(expr + 1);
2049 if (t > num)
2050 return 0;
2051 break;
2052 default:
2053 return 0;
2055 return num;
2058 static char *
2059 get_line(FILE *f)
2061 char *name, *temp;
2062 size_t len;
2064 name = fgetln(f, &len);
2065 if (!name) {
2066 get_line_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
2067 return 0;
2069 if (name[len-1] != '\n')
2070 len++;
2071 temp = malloc(len);
2072 if (!temp) {
2073 get_line_error = GETLINE_OUT_OF_MEM;
2074 return 0;
2076 memcpy(temp, name, len-1);
2077 temp[len-1] = 0;
2078 return temp;
2082 * no_op()
2083 * for those option functions where the archive format has nothing to do.
2084 * Return:
2088 static int
2089 no_op(void)
2091 return 0;
2095 * pax_usage()
2096 * print the usage summary to the user
2099 static void
2100 pax_usage(void)
2102 fprintf(stderr,
2103 "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2104 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2105 " [pattern ...]\n");
2106 fprintf(stderr,
2107 " pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
2108 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2109 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2110 fprintf(stderr,
2111 " pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2112 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2113 " [-U user] ... [-G group] ...\n"
2114 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2115 fprintf(stderr,
2116 " pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
2117 " [-s replstr] ... [-U user] ... [-G group] ...\n"
2118 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2119 exit(1);
2120 /* NOTREACHED */
2124 * tar_usage()
2125 * print the usage summary to the user
2128 static void
2129 tar_usage(void)
2131 (void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHOPSXZ014578] [archive] "
2132 "[blocksize]\n"
2133 " [-C directory] [-T file] [-s replstr] "
2134 "[file ...]\n", stderr);
2135 exit(1);
2136 /* NOTREACHED */
2139 #ifndef NO_CPIO
2141 * cpio_usage()
2142 * print the usage summary to the user
2145 static void
2146 cpio_usage(void)
2149 (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2150 "[-H format] [-O archive]\n"
2151 " < name-list [> archive]\n"
2152 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2153 "[-F archive] [-H format] \n"
2154 " [-I archive] "
2155 "[pattern ...] [< archive]\n"
2156 " cpio -p [-adlLmuv] destination-directory "
2157 "< name-list\n", stderr);
2158 exit(1);
2159 /* NOTREACHED */
2161 #endif