options.c revision 1.122 1 /* $NetBSD: options.c,v 1.122 2025/05/04 20:06:23 rillig Exp $ */
2
3 /*-
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. 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.
22 *
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.
34 */
35
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
39
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.122 2025/05/04 20:06:23 rillig Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/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 <strings.h>
65 #include <unistd.h>
66 #include <inttypes.h>
67 #include <paths.h>
68 #include <util.h>
69 #include "pax.h"
70 #include "options.h"
71 #include "cpio.h"
72 #include "tar.h"
73 #include "extern.h"
74 #ifndef SMALL
75 #include "mtree.h"
76 #endif /* SMALL */
77
78 char *chdname;
79 #if !HAVE_NBTOOL_CONFIG_H
80 int do_chroot;
81 #endif
82
83 /*
84 * Routines which handle command line options
85 */
86 struct stat tst; /* Timestamp to set if non-0 */
87
88 static int nopids; /* tar mode: suppress "pids" for -p option */
89 static char flgch[] = FLGCH; /* list of all possible flags (pax) */
90 static OPLIST *ophead = NULL; /* head for format specific options -x */
91 static OPLIST *optail = NULL; /* option tail */
92
93 static int opt_add(const char *);
94 static int no_op(void);
95 static void printflg(unsigned int);
96 static int c_frmt(const void *, const void *);
97 static off_t str_offt(char *);
98 static char *get_line(FILE *fp);
99 #ifndef SMALL
100 static int set_tstamp(const char *, struct stat *);
101 #endif
102 static void pax_options(int, char **);
103 __dead static void pax_usage(void);
104 static void tar_options(int, char **);
105 __dead static void tar_usage(void);
106 #ifndef NO_CPIO
107 static void cpio_options(int, char **);
108 __dead static void cpio_usage(void);
109 #endif
110
111 /* errors from get_line */
112 #define GETLINE_FILE_CORRUPT 1
113 #define GETLINE_OUT_OF_MEM 2
114 static int get_line_error;
115
116 #define BZIP2_CMD "bzip2" /* command to run as bzip2 */
117 #define GZIP_CMD "gzip" /* command to run as gzip */
118 #define XZ_CMD "xz" /* command to run as xz */
119 #define COMPRESS_CMD "compress" /* command to run as compress */
120
121 /*
122 * Long options.
123 */
124 #define OPT_USE_COMPRESS_PROGRAM 0
125 #define OPT_CHECKPOINT 1
126 #define OPT_UNLINK 2
127 #define OPT_HELP 3
128 #define OPT_ATIME_PRESERVE 4
129 #define OPT_IGNORE_FAILED_READ 5
130 #define OPT_REMOVE_FILES 6
131 #define OPT_NULL 7
132 #define OPT_TOTALS 8
133 #define OPT_VERSION 9
134 #define OPT_EXCLUDE 10
135 #define OPT_BLOCK_COMPRESS 11
136 #define OPT_NORECURSE 12
137 #define OPT_FORCE_LOCAL 13
138 #define OPT_INSECURE 14
139 #define OPT_STRICT 15
140 #define OPT_SPARSE 16
141 #define OPT_XZ 17
142 #define OPT_GNU 18
143 #define OPT_TIMESTAMP 19
144 #if !HAVE_NBTOOL_CONFIG_H
145 #define OPT_CHROOT 20
146 #endif
147
148 /*
149 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
150 * (see pax.h for description of each function)
151 *
152 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
153 * read, end_read, st_write, write, end_write, trail,
154 * subtrail, rd_data, wr_data, options
155 */
156
157 FSUB fsub[] = {
158 #ifndef NO_CPIO
159 /* 0: OLD BINARY CPIO */
160 { "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
161 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
162 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
163
164 /* 1: OLD OCTAL CHARACTER CPIO */
165 { "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
166 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
167 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
168
169 /* 2: SVR4 HEX CPIO */
170 { "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
171 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
172 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
173
174 /* 3: SVR4 HEX CPIO WITH CRC */
175 { "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
176 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
177 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
178 #endif
179 /* 4: OLD TAR */
180 { "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
181 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
182 NULL, rd_wrfile, wr_rdfile, tar_opt },
183
184 /* 5: POSIX USTAR */
185 { "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
186 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
187 NULL, rd_wrfile, wr_rdfile, bad_opt }
188 };
189 #ifndef NO_CPIO
190 #define F_BCPIO 0 /* old binary cpio format */
191 #define F_CPIO 1 /* old octal character cpio format */
192 #define F_SV4CPIO 2 /* SVR4 hex cpio format */
193 #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
194 #define F_TAR 4 /* old V7 UNIX tar format */
195 #define F_USTAR 5 /* ustar format */
196 #else
197 #define F_TAR 0 /* old V7 UNIX tar format */
198 #define F_USTAR 1 /* ustar format */
199 #endif
200 #define DEFLT F_USTAR /* default write format from list above */
201
202 /*
203 * ford is the archive search order used by get_arc() to determine what kind
204 * of archive we are dealing with. This helps to properly id archive formats
205 * some formats may be subsets of others....
206 */
207 int ford[] = {F_USTAR, F_TAR,
208 #ifndef NO_CPIO
209 F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO,
210 #endif
211 -1};
212
213 /*
214 * filename record separator
215 */
216 int sep = '\n';
217
218 /*
219 * Do we have -C anywhere?
220 */
221 int havechd = 0;
222
223 /*
224 * options()
225 * figure out if we are pax, tar or cpio. Call the appropriate options
226 * parser
227 */
228
229 void
230 options(int argc, char **argv)
231 {
232
233 /*
234 * Are we acting like pax, tar or cpio (based on argv[0])
235 */
236 if ((argv0 = strrchr(argv[0], '/')) != NULL)
237 argv0++;
238 else
239 argv0 = argv[0];
240
241 if (strstr(argv0, NM_TAR)) {
242 argv0 = NM_TAR;
243 tar_options(argc, argv);
244 #ifndef NO_CPIO
245 } else if (strstr(argv0, NM_CPIO)) {
246 argv0 = NM_CPIO;
247 cpio_options(argc, argv);
248 #endif
249 } else {
250 argv0 = NM_PAX;
251 pax_options(argc, argv);
252 }
253 }
254
255 struct option pax_longopts[] = {
256 { "insecure", no_argument, 0,
257 OPT_INSECURE },
258 { "force-local", no_argument, 0,
259 OPT_FORCE_LOCAL },
260 { "use-compress-program", required_argument, 0,
261 OPT_USE_COMPRESS_PROGRAM },
262 { "xz", no_argument, 0,
263 OPT_XZ },
264 { "gnu", no_argument, 0,
265 OPT_GNU },
266 { "timestamp", required_argument, 0,
267 OPT_TIMESTAMP },
268 { 0, 0, 0,
269 0 },
270 };
271
272 /*
273 * pax_options()
274 * look at the user specified flags. set globals as required and check if
275 * the user specified a legal set of flags. If not, complain and exit
276 */
277
278 static void
279 pax_options(int argc, char **argv)
280 {
281 int c;
282 size_t i;
283 u_int64_t flg = 0;
284 u_int64_t bflg = 0;
285 char *pt;
286 FSUB tmp;
287
288 /*
289 * process option flags
290 */
291 while ((c = getopt_long(argc, argv,
292 "0ab:cdf:ijklno:p:rs:tuvwx:zAB:DE:G:HLMN:OPT:U:VXYZ",
293 pax_longopts, NULL)) != -1) {
294 switch (c) {
295 case '0':
296 sep = '\0';
297 break;
298 case 'a':
299 /*
300 * append
301 */
302 flg |= AF;
303 break;
304 case 'b':
305 /*
306 * specify blocksize
307 */
308 flg |= BF;
309 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
310 tty_warn(1, "Invalid block size %s", optarg);
311 pax_usage();
312 }
313 break;
314 case 'c':
315 /*
316 * inverse match on patterns
317 */
318 cflag = 1;
319 flg |= CF;
320 break;
321 case 'd':
322 /*
323 * match only dir on extract, not the subtree at dir
324 */
325 dflag = 1;
326 flg |= DF;
327 break;
328 case 'f':
329 /*
330 * filename where the archive is stored
331 */
332 arcname = optarg;
333 flg |= FF;
334 break;
335 case 'i':
336 /*
337 * interactive file rename
338 */
339 iflag = 1;
340 flg |= IF;
341 break;
342 case 'j':
343 /*
344 * pass through bzip2
345 */
346 gzip_program = BZIP2_CMD;
347 break;
348 case 'k':
349 /*
350 * do not clobber files that exist
351 */
352 kflag = 1;
353 flg |= KF;
354 break;
355 case 'l':
356 /*
357 * try to link src to dest with copy (-rw)
358 */
359 lflag = 1;
360 flg |= LF;
361 break;
362 case 'n':
363 /*
364 * select first match for a pattern only
365 */
366 nflag = 1;
367 flg |= NF;
368 break;
369 case 'o':
370 /*
371 * pass format specific options
372 */
373 flg |= OF;
374 if (opt_add(optarg) < 0)
375 pax_usage();
376 break;
377 case 'p':
378 /*
379 * specify file characteristic options
380 */
381 for (pt = optarg; *pt != '\0'; ++pt) {
382 switch(*pt) {
383 case 'a':
384 /*
385 * do not preserve access time
386 */
387 patime = 0;
388 break;
389 case 'e':
390 /*
391 * preserve user id, group id, file
392 * mode, access/modification times
393 * and file flags.
394 */
395 pids = 1;
396 pmode = 1;
397 patime = 1;
398 pmtime = 1;
399 pfflags = 1;
400 break;
401 #if 0
402 case 'f':
403 /*
404 * do not preserve file flags
405 */
406 pfflags = 0;
407 break;
408 #endif
409 case 'm':
410 /*
411 * do not preserve modification time
412 */
413 pmtime = 0;
414 break;
415 case 'o':
416 /*
417 * preserve uid/gid
418 */
419 pids = 1;
420 break;
421 case 'p':
422 /*
423 * preserve file mode bits
424 */
425 pmode = 1;
426 break;
427 default:
428 tty_warn(1, "Invalid -p string: %c",
429 *pt);
430 pax_usage();
431 }
432 }
433 flg |= PF;
434 break;
435 case 'r':
436 /*
437 * read the archive
438 */
439 flg |= RF;
440 break;
441 case 's':
442 /*
443 * file name substitution name pattern
444 */
445 if (rep_add(optarg) < 0)
446 pax_usage();
447 flg |= SF;
448 break;
449 case 't':
450 /*
451 * preserve access time on filesystem nodes we read
452 */
453 tflag = 1;
454 flg |= TF;
455 break;
456 case 'u':
457 /*
458 * ignore those older files
459 */
460 uflag = 1;
461 flg |= UF;
462 break;
463 case 'v':
464 /*
465 * verbose operation mode
466 */
467 vflag = 1;
468 flg |= VF;
469 break;
470 case 'w':
471 /*
472 * write an archive
473 */
474 flg |= WF;
475 break;
476 case 'x':
477 /*
478 * specify an archive format on write
479 */
480 tmp.name = optarg;
481 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
482 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
483 if (frmt != NULL) {
484 flg |= XF;
485 break;
486 }
487 tty_warn(1, "Unknown -x format: %s", optarg);
488 (void)fputs("pax: Known -x formats are:", stderr);
489 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
490 (void)fprintf(stderr, " %s", fsub[i].name);
491 (void)fputs("\n\n", stderr);
492 pax_usage();
493 case 'z':
494 /*
495 * use gzip. Non standard option.
496 */
497 gzip_program = GZIP_CMD;
498 break;
499 case 'A':
500 Aflag = 1;
501 flg |= CAF;
502 break;
503 case 'B':
504 /*
505 * non-standard option on number of bytes written on a
506 * single archive volume.
507 */
508 if ((wrlimit = str_offt(optarg)) <= 0) {
509 tty_warn(1, "Invalid write limit %s", optarg);
510 pax_usage();
511 }
512 if (wrlimit % BLKMULT) {
513 tty_warn(1,
514 "Write limit is not a %d byte multiple",
515 BLKMULT);
516 pax_usage();
517 }
518 flg |= CBF;
519 break;
520 case 'D':
521 /*
522 * On extraction check file inode change time before the
523 * modification of the file name. Non standard option.
524 */
525 Dflag = 1;
526 flg |= CDF;
527 break;
528 case 'E':
529 /*
530 * non-standard limit on read faults
531 * 0 indicates stop after first error, values
532 * indicate a limit, "none" try forever
533 */
534 flg |= CEF;
535 if (strcmp(none, optarg) == 0)
536 maxflt = -1;
537 else if ((maxflt = atoi(optarg)) < 0) {
538 tty_warn(1,
539 "Error count value must be positive");
540 pax_usage();
541 }
542 break;
543 case 'G':
544 /*
545 * non-standard option for selecting files within an
546 * archive by group (gid or name)
547 */
548 if (grp_add(optarg) < 0)
549 pax_usage();
550 flg |= CGF;
551 break;
552 case 'H':
553 /*
554 * follow command line symlinks only
555 */
556 Hflag = 1;
557 flg |= CHF;
558 break;
559 case 'L':
560 /*
561 * follow symlinks
562 */
563 Lflag = 1;
564 flg |= CLF;
565 break;
566 #ifdef SMALL
567 case 'M':
568 case 'N':
569 tty_warn(1, "Support for -%c is not compiled in", c);
570 exit(1);
571 #else /* !SMALL */
572 case 'M':
573 /*
574 * Treat list of filenames on stdin as an
575 * mtree(8) specfile. Non standard option.
576 */
577 Mflag = 1;
578 flg |= CMF;
579 break;
580 case 'N':
581 /*
582 * Use alternative directory for user db lookups.
583 */
584 if (!setup_getid(optarg)) {
585 tty_warn(1,
586 "Unable to use user and group databases in `%s'",
587 optarg);
588 pax_usage();
589 }
590 break;
591 #endif /* !SMALL */
592 case 'O':
593 /*
594 * Force one volume. Non standard option.
595 */
596 force_one_volume = 1;
597 break;
598 case 'P':
599 /*
600 * do NOT follow symlinks (default)
601 */
602 Lflag = 0;
603 flg |= CPF;
604 break;
605 case 'T':
606 /*
607 * non-standard option for selecting files within an
608 * archive by modification time range (lower,upper)
609 */
610 if (trng_add(optarg) < 0)
611 pax_usage();
612 flg |= CTF;
613 break;
614 case 'U':
615 /*
616 * non-standard option for selecting files within an
617 * archive by user (uid or name)
618 */
619 if (usr_add(optarg) < 0)
620 pax_usage();
621 flg |= CUF;
622 break;
623 case 'V':
624 /*
625 * somewhat verbose operation mode (no listing)
626 */
627 Vflag = 1;
628 flg |= VSF;
629 break;
630 case 'X':
631 /*
632 * do not pass over mount points in the file system
633 */
634 Xflag = 1;
635 flg |= CXF;
636 break;
637 case 'Y':
638 /*
639 * On extraction check file inode change time after the
640 * modification of the file name. Non standard option.
641 */
642 Yflag = 1;
643 flg |= CYF;
644 break;
645 case 'Z':
646 /*
647 * On extraction check modification time after the
648 * modification of the file name. Non standard option.
649 */
650 Zflag = 1;
651 flg |= CZF;
652 break;
653 case OPT_INSECURE:
654 secure = 0;
655 break;
656 case OPT_FORCE_LOCAL:
657 forcelocal = 1;
658 break;
659 case OPT_USE_COMPRESS_PROGRAM:
660 gzip_program = optarg;
661 break;
662 case OPT_XZ:
663 gzip_program = XZ_CMD;
664 break;
665 case OPT_GNU:
666 is_gnutar = 1;
667 break;
668 #ifndef SMALL
669 case OPT_TIMESTAMP:
670 if (set_tstamp(optarg, &tst) == -1) {
671 tty_warn(1, "Invalid timestamp `%s'", optarg);
672 tar_usage();
673 }
674 break;
675 #endif
676 case '?':
677 default:
678 pax_usage();
679 }
680 }
681
682 /*
683 * figure out the operation mode of pax read,write,extract,copy,append
684 * or list. check that we have not been given a bogus set of flags
685 * for the operation mode.
686 */
687 if (ISLIST(flg)) {
688 act = LIST;
689 listf = stdout;
690 bflg = flg & BDLIST;
691 } else if (ISEXTRACT(flg)) {
692 act = EXTRACT;
693 bflg = flg & BDEXTR;
694 } else if (ISARCHIVE(flg)) {
695 act = ARCHIVE;
696 bflg = flg & BDARCH;
697 } else if (ISAPPND(flg)) {
698 act = APPND;
699 bflg = flg & BDARCH;
700 } else if (ISCOPY(flg)) {
701 act = COPY;
702 bflg = flg & BDCOPY;
703 } else
704 pax_usage();
705 if (bflg) {
706 printflg(flg);
707 pax_usage();
708 }
709
710 /*
711 * if we are writing (ARCHIVE) we use the default format if the user
712 * did not specify a format. when we write during an APPEND, we will
713 * adopt the format of the existing archive if none was supplied.
714 */
715 if (!(flg & XF) && (act == ARCHIVE))
716 frmt = &(fsub[DEFLT]);
717
718 /*
719 * process the args as they are interpreted by the operation mode
720 */
721 switch (act) {
722 case LIST:
723 case EXTRACT:
724 for (; optind < argc; optind++)
725 if (pat_add(argv[optind], NULL, 0) < 0)
726 pax_usage();
727 break;
728 case COPY:
729 if (optind >= argc) {
730 tty_warn(0, "Destination directory was not supplied");
731 pax_usage();
732 }
733 --argc;
734 dirptr = argv[argc];
735 if (mkpath(dirptr) < 0)
736 exit(1);
737 /* FALLTHROUGH */
738 case ARCHIVE:
739 case APPND:
740 for (; optind < argc; optind++)
741 if (ftree_add(argv[optind], 0) < 0)
742 pax_usage();
743 /*
744 * no read errors allowed on updates/append operation!
745 */
746 maxflt = 0;
747 break;
748 }
749 }
750
751
752 /*
753 * tar_options()
754 * look at the user specified flags. set globals as required and check if
755 * the user specified a legal set of flags. If not, complain and exit
756 */
757
758 struct option tar_longopts[] = {
759 { "block-size", required_argument, 0, 'b' },
760 { "bunzip2", no_argument, 0, 'j' },
761 { "bzip2", no_argument, 0, 'j' },
762 { "create", no_argument, 0, 'c' }, /* F */
763 /* -e -- no corresponding long option */
764 { "file", required_argument, 0, 'f' },
765 { "dereference", no_argument, 0, 'h' },
766 { "keep-old-files", no_argument, 0, 'k' },
767 { "one-file-system", no_argument, 0, 'l' },
768 { "modification-time", no_argument, 0, 'm' },
769 { "old-archive", no_argument, 0, 'o' },
770 { "portability", no_argument, 0, 'o' },
771 { "same-permissions", no_argument, 0, 'p' },
772 { "preserve-permissions", no_argument, 0, 'p' },
773 { "preserve", no_argument, 0, 'p' },
774 { "fast-read", no_argument, 0, 'q' },
775 { "append", no_argument, 0, 'r' }, /* F */
776 { "update", no_argument, 0, 'u' }, /* F */
777 { "list", no_argument, 0, 't' }, /* F */
778 { "verbose", no_argument, 0, 'v' },
779 { "interactive", no_argument, 0, 'w' },
780 { "confirmation", no_argument, 0, 'w' },
781 { "extract", no_argument, 0, 'x' }, /* F */
782 { "get", no_argument, 0, 'x' }, /* F */
783 { "gzip", no_argument, 0, 'z' },
784 { "gunzip", no_argument, 0, 'z' },
785 { "read-full-blocks", no_argument, 0, 'B' },
786 { "directory", required_argument, 0, 'C' },
787 { "xz", no_argument, 0, 'J' },
788 { "to-stdout", no_argument, 0, 'O' },
789 { "absolute-paths", no_argument, 0, 'P' },
790 { "sparse", no_argument, 0, 'S' },
791 { "files-from", required_argument, 0, 'T' },
792 { "summary", no_argument, 0, 'V' },
793 { "stats", no_argument, 0, 'V' },
794 { "exclude-from", required_argument, 0, 'X' },
795 { "compress", no_argument, 0, 'Z' },
796 { "uncompress", no_argument, 0, 'Z' },
797 { "strict", no_argument, 0,
798 OPT_STRICT },
799 { "atime-preserve", no_argument, 0,
800 OPT_ATIME_PRESERVE },
801 { "unlink", no_argument, 0,
802 OPT_UNLINK },
803 { "use-compress-program", required_argument, 0,
804 OPT_USE_COMPRESS_PROGRAM },
805 { "force-local", no_argument, 0,
806 OPT_FORCE_LOCAL },
807 { "insecure", no_argument, 0,
808 OPT_INSECURE },
809 { "exclude", required_argument, 0,
810 OPT_EXCLUDE },
811 { "no-recursion", no_argument, 0,
812 OPT_NORECURSE },
813 #if !HAVE_NBTOOL_CONFIG_H
814 { "chroot", no_argument, 0,
815 OPT_CHROOT },
816 #endif
817 { "timestamp", required_argument, 0,
818 OPT_TIMESTAMP },
819 #if 0 /* Not implemented */
820 { "catenate", no_argument, 0, 'A' }, /* F */
821 { "concatenate", no_argument, 0, 'A' }, /* F */
822 { "diff", no_argument, 0, 'd' }, /* F */
823 { "compare", no_argument, 0, 'd' }, /* F */
824 { "checkpoint", no_argument, 0,
825 OPT_CHECKPOINT },
826 { "help", no_argument, 0,
827 OPT_HELP },
828 { "info-script", required_argument, 0, 'F' },
829 { "new-volume-script", required_argument, 0, 'F' },
830 { "incremental", no_argument, 0, 'G' },
831 { "listed-incremental", required_argument, 0, 'g' },
832 { "ignore-zeros", no_argument, 0, 'i' },
833 { "ignore-failed-read", no_argument, 0,
834 OPT_IGNORE_FAILED_READ },
835 { "starting-file", no_argument, 0, 'K' },
836 { "tape-length", required_argument, 0, 'L' },
837 { "multi-volume", no_argument, 0, 'M' },
838 { "after-date", required_argument, 0, 'N' },
839 { "newer", required_argument, 0, 'N' },
840 { "record-number", no_argument, 0, 'R' },
841 { "remove-files", no_argument, 0,
842 OPT_REMOVE_FILES },
843 { "same-order", no_argument, 0, 's' },
844 { "preserve-order", no_argument, 0, 's' },
845 { "null", no_argument, 0,
846 OPT_NULL },
847 { "totals", no_argument, 0,
848 OPT_TOTALS },
849 { "volume-name", required_argument, 0, 'V' }, /* XXX */
850 { "label", required_argument, 0, 'V' }, /* XXX */
851 { "version", no_argument, 0,
852 OPT_VERSION },
853 { "verify", no_argument, 0, 'W' },
854 { "block-compress", no_argument, 0,
855 OPT_BLOCK_COMPRESS },
856 #endif
857 { 0, 0, 0, 0 },
858 };
859
860 static void
861 tar_set_action(int op)
862 {
863 if (act != ERROR && act != op)
864 tar_usage();
865 act = op;
866 }
867
868 static void
869 tar_options(int argc, char **argv)
870 {
871 int c;
872 int fstdin = 0;
873 int Oflag = 0;
874 int nincfiles = 0;
875 int incfiles_max = 0;
876 struct incfile {
877 char *file;
878 char *dir;
879 };
880 struct incfile *incfiles = NULL;
881
882 /*
883 * Set default values.
884 */
885 rmleadslash = 1;
886 is_gnutar = 1;
887
888 /*
889 * process option flags
890 */
891 while ((c = getoldopt(argc, argv,
892 "+b:cef:hjklmopqrs:tuvwxzBC:HI:JOPST:X:Z014578",
893 tar_longopts, NULL))
894 != -1) {
895 switch(c) {
896 case 'b':
897 /*
898 * specify blocksize in 512-byte blocks
899 */
900 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
901 tty_warn(1, "Invalid block size %s", optarg);
902 tar_usage();
903 }
904 wrblksz *= 512; /* XXX - check for int oflow */
905 break;
906 case 'c':
907 /*
908 * create an archive
909 */
910 tar_set_action(ARCHIVE);
911 break;
912 case 'e':
913 /*
914 * stop after first error
915 */
916 maxflt = 0;
917 break;
918 case 'f':
919 /*
920 * filename where the archive is stored
921 */
922 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
923 /*
924 * treat a - as stdin
925 */
926 fstdin = 1;
927 arcname = NULL;
928 break;
929 }
930 fstdin = 0;
931 arcname = optarg;
932 break;
933 case 'h':
934 /*
935 * follow symlinks
936 */
937 Lflag = 1;
938 break;
939 case 'j':
940 /*
941 * pass through bzip2. not a standard option
942 */
943 gzip_program = BZIP2_CMD;
944 break;
945 case 'k':
946 /*
947 * do not clobber files that exist
948 */
949 kflag = 1;
950 break;
951 case 'l':
952 /*
953 * do not pass over mount points in the file system
954 */
955 Xflag = 1;
956 break;
957 case 'm':
958 /*
959 * do not preserve modification time
960 */
961 pmtime = 0;
962 break;
963 case 'o':
964 /*
965 * This option does several things based on whether
966 * this is a create or extract operation.
967 */
968 if (act == ARCHIVE) {
969 /* GNU tar: write V7 format archives. */
970 Oflag = 1;
971 /* 4.2BSD: don't add directory entries. */
972 if (opt_add("write_opt=nodir") < 0)
973 tar_usage();
974
975 } else {
976 /* SUS: don't preserve owner/group. */
977 pids = 0;
978 nopids = 1;
979 }
980 break;
981 case 'p':
982 /*
983 * preserve user id, group id, file
984 * mode, access/modification times
985 */
986 if (!nopids)
987 pids = 1;
988 pmode = 1;
989 patime = 1;
990 pmtime = 1;
991 break;
992 case 'q':
993 /*
994 * select first match for a pattern only
995 */
996 nflag = 1;
997 break;
998 case 'r':
999 case 'u':
1000 /*
1001 * append to the archive
1002 */
1003 tar_set_action(APPND);
1004 break;
1005 case 's':
1006 /*
1007 * file name substitution name pattern
1008 */
1009 if (rep_add(optarg) < 0)
1010 tar_usage();
1011 break;
1012 case 't':
1013 /*
1014 * list contents of the tape
1015 */
1016 tar_set_action(LIST);
1017 break;
1018 case 'v':
1019 /*
1020 * verbose operation mode
1021 */
1022 vflag = 1;
1023 break;
1024 case 'w':
1025 /*
1026 * interactive file rename
1027 */
1028 iflag = 1;
1029 break;
1030 case 'x':
1031 /*
1032 * extract an archive, preserving mode,
1033 * and mtime if possible.
1034 */
1035 tar_set_action(EXTRACT);
1036 pmtime = 1;
1037 break;
1038 case 'z':
1039 /*
1040 * use gzip. Non standard option.
1041 */
1042 gzip_program = GZIP_CMD;
1043 break;
1044 case 'B':
1045 /*
1046 * Nothing to do here, this is pax default
1047 */
1048 break;
1049 case 'C':
1050 havechd++;
1051 chdname = optarg;
1052 break;
1053 case 'H':
1054 /*
1055 * follow command line symlinks only
1056 */
1057 Hflag = 1;
1058 break;
1059 case 'I':
1060 case 'T':
1061 if (++nincfiles > incfiles_max) {
1062 incfiles_max = nincfiles + 3;
1063 incfiles = realloc(incfiles,
1064 sizeof(*incfiles) * incfiles_max);
1065 if (incfiles == NULL) {
1066 tty_warn(0, "Unable to allocate space "
1067 "for option list");
1068 exit(1);
1069 }
1070 }
1071 incfiles[nincfiles - 1].file = optarg;
1072 incfiles[nincfiles - 1].dir = chdname;
1073 break;
1074 case 'J':
1075 gzip_program = XZ_CMD;
1076 break;
1077 case 'O':
1078 Oflag = 1;
1079 break;
1080 case 'P':
1081 /*
1082 * do not remove leading '/' from pathnames
1083 */
1084 rmleadslash = 0;
1085 Aflag = 1;
1086 break;
1087 case 'S':
1088 /* do nothing; we already generate sparse files */
1089 break;
1090 case 'V':
1091 /*
1092 * semi-verbose operation mode (no listing)
1093 */
1094 Vflag = 1;
1095 break;
1096 case 'X':
1097 /*
1098 * GNU tar compat: exclude the files listed in optarg
1099 */
1100 if (tar_gnutar_X_compat(optarg) != 0)
1101 tar_usage();
1102 break;
1103 case 'Z':
1104 /*
1105 * use compress.
1106 */
1107 gzip_program = COMPRESS_CMD;
1108 break;
1109 case '0':
1110 arcname = DEV_0;
1111 break;
1112 case '1':
1113 arcname = DEV_1;
1114 break;
1115 case '4':
1116 arcname = DEV_4;
1117 break;
1118 case '5':
1119 arcname = DEV_5;
1120 break;
1121 case '7':
1122 arcname = DEV_7;
1123 break;
1124 case '8':
1125 arcname = DEV_8;
1126 break;
1127 case OPT_ATIME_PRESERVE:
1128 patime = 1;
1129 break;
1130 case OPT_UNLINK:
1131 /* Just ignore -- we always unlink first. */
1132 break;
1133 case OPT_USE_COMPRESS_PROGRAM:
1134 gzip_program = optarg;
1135 break;
1136 case OPT_FORCE_LOCAL:
1137 forcelocal = 1;
1138 break;
1139 case OPT_INSECURE:
1140 secure = 0;
1141 break;
1142 case OPT_STRICT:
1143 /* disable gnu extensions */
1144 is_gnutar = 0;
1145 break;
1146 case OPT_EXCLUDE:
1147 if (tar_gnutar_minus_minus_exclude(optarg) != 0)
1148 tar_usage();
1149 break;
1150 case OPT_NORECURSE:
1151 dflag = 1;
1152 break;
1153 #if !HAVE_NBTOOL_CONFIG_H
1154 case OPT_CHROOT:
1155 do_chroot = 1;
1156 break;
1157 #endif
1158 #ifndef SMALL
1159 case OPT_TIMESTAMP:
1160 if (set_tstamp(optarg, &tst) == -1) {
1161 tty_warn(1, "Invalid timestamp `%s'", optarg);
1162 tar_usage();
1163 }
1164 break;
1165 #endif
1166 default:
1167 tar_usage();
1168 }
1169 }
1170 argc -= optind;
1171 argv += optind;
1172
1173 /* Tar requires an action. */
1174 if (act == ERROR)
1175 tar_usage();
1176
1177 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1178 if (fstdin == 1 && act == ARCHIVE)
1179 listf = stderr;
1180 else
1181 listf = stdout;
1182
1183 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1184 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
1185 exit(0);
1186 /*
1187 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1188 * (unless -o specified)
1189 */
1190 if (act == ARCHIVE || act == APPND)
1191 frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
1192 else if (Oflag) {
1193 if (act == EXTRACT)
1194 to_stdout = 1;
1195 else {
1196 tty_warn(1, "The -O/-o options are only valid when "
1197 "writing or extracting an archive");
1198 tar_usage();
1199 }
1200 }
1201
1202 /*
1203 * process the args as they are interpreted by the operation mode
1204 */
1205 switch (act) {
1206 case LIST:
1207 case EXTRACT:
1208 default:
1209 {
1210 int sawpat = 0;
1211 int dirisnext = 0;
1212 char *file, *dir = NULL;
1213 int mustfreedir = 0;
1214
1215 while (nincfiles || *argv != NULL) {
1216 /*
1217 * If we queued up any include files,
1218 * pull them in now. Otherwise, check
1219 * for -I and -C positional flags.
1220 * Anything else must be a file to
1221 * extract.
1222 */
1223 if (nincfiles) {
1224 file = incfiles->file;
1225 dir = incfiles->dir;
1226 mustfreedir = 0;
1227 incfiles++;
1228 nincfiles--;
1229 } else if (strcmp(*argv, "-I") == 0) {
1230 if (*++argv == NULL)
1231 break;
1232 file = *argv++;
1233 dir = chdname;
1234 mustfreedir = 0;
1235 } else {
1236 file = NULL;
1237 dir = NULL;
1238 mustfreedir = 0;
1239 }
1240 if (file != NULL) {
1241 FILE *fp;
1242 char *str;
1243
1244 if (strcmp(file, "-") == 0)
1245 fp = stdin;
1246 else if ((fp = fopen(file, "r")) == NULL) {
1247 tty_warn(1, "Unable to open file '%s' for read", file);
1248 tar_usage();
1249 }
1250 while ((str = get_line(fp)) != NULL) {
1251 if (dirisnext) {
1252 if (dir && mustfreedir)
1253 free(dir);
1254 dir = str;
1255 mustfreedir = 1;
1256 dirisnext = 0;
1257 continue;
1258 }
1259 if (strcmp(str, "-C") == 0) {
1260 havechd++;
1261 dirisnext = 1;
1262 free(str);
1263 continue;
1264 }
1265 if (strncmp(str, "-C ", 3) == 0) {
1266 havechd++;
1267 if (dir && mustfreedir)
1268 free(dir);
1269 dir = strdup(str + 3);
1270 mustfreedir = 1;
1271 free(str);
1272 continue;
1273 }
1274 if (pat_add(str, dir, NOGLOB_MTCH) < 0)
1275 tar_usage();
1276 sawpat = 1;
1277 }
1278 /* Bomb if given -C w/out a dir. */
1279 if (dirisnext)
1280 tar_usage();
1281 if (dir && mustfreedir)
1282 free(dir);
1283 if (strcmp(file, "-") != 0)
1284 fclose(fp);
1285 if (get_line_error) {
1286 tty_warn(1, "Problem with file '%s'", file);
1287 tar_usage();
1288 }
1289 } else if (strcmp(*argv, "-C") == 0) {
1290 if (*++argv == NULL)
1291 break;
1292 chdname = *argv++;
1293 havechd++;
1294 } else if (pat_add(*argv++, chdname, 0) < 0)
1295 tar_usage();
1296 else
1297 sawpat = 1;
1298 }
1299 /*
1300 * if patterns were added, we are doing chdir()
1301 * on a file-by-file basis, else, just one
1302 * global chdir (if any) after opening input.
1303 */
1304 if (sawpat > 0)
1305 chdname = NULL;
1306 }
1307 break;
1308 case ARCHIVE:
1309 case APPND:
1310 if (chdname != NULL) { /* initial chdir() */
1311 if (ftree_add(chdname, 1) < 0)
1312 tar_usage();
1313 }
1314
1315 while (nincfiles || *argv != NULL) {
1316 char *file, *dir;
1317
1318 /*
1319 * If we queued up any include files, pull them in
1320 * now. Otherwise, check for -I and -C positional
1321 * flags. Anything else must be a file to include
1322 * in the archive.
1323 */
1324 if (nincfiles) {
1325 file = incfiles->file;
1326 dir = incfiles->dir;
1327 incfiles++;
1328 nincfiles--;
1329 } else if (strcmp(*argv, "-I") == 0) {
1330 if (*++argv == NULL)
1331 break;
1332 file = *argv++;
1333 dir = NULL;
1334 } else {
1335 file = NULL;
1336 dir = NULL;
1337 }
1338 if (file != NULL) {
1339 FILE *fp;
1340 char *str;
1341 int dirisnext = 0;
1342
1343 /* Set directory if needed */
1344 if (dir) {
1345 if (ftree_add(dir, 1) < 0)
1346 tar_usage();
1347 }
1348
1349 if (strcmp(file, "-") == 0)
1350 fp = stdin;
1351 else if ((fp = fopen(file, "r")) == NULL) {
1352 tty_warn(1, "Unable to open file '%s' for read", file);
1353 tar_usage();
1354 }
1355 while ((str = get_line(fp)) != NULL) {
1356 if (dirisnext) {
1357 if (ftree_add(str, 1) < 0)
1358 tar_usage();
1359 dirisnext = 0;
1360 continue;
1361 }
1362 if (strcmp(str, "-C") == 0) {
1363 dirisnext = 1;
1364 continue;
1365 }
1366 if (strncmp(str, "-C ", 3) == 0) {
1367 if (ftree_add(str + 3, 1) < 0)
1368 tar_usage();
1369 continue;
1370 }
1371 if (ftree_add(str, 0) < 0)
1372 tar_usage();
1373 }
1374 /* Bomb if given -C w/out a dir. */
1375 if (dirisnext)
1376 tar_usage();
1377 if (strcmp(file, "-") != 0)
1378 fclose(fp);
1379 if (get_line_error) {
1380 tty_warn(1, "Problem with file '%s'",
1381 file);
1382 tar_usage();
1383 }
1384 } else if (strcmp(*argv, "-C") == 0) {
1385 if (*++argv == NULL)
1386 break;
1387 if (ftree_add(*argv++, 1) < 0)
1388 tar_usage();
1389 } else if (ftree_add(*argv++, 0) < 0)
1390 tar_usage();
1391 }
1392 /*
1393 * no read errors allowed on updates/append operation!
1394 */
1395 maxflt = 0;
1396 break;
1397 }
1398 if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
1399 arcname = getenv("TAPE");
1400 if ((arcname == NULL) || (*arcname == '\0'))
1401 arcname = _PATH_DEFTAPE;
1402 }
1403 }
1404
1405 int
1406 mkpath(char *path)
1407 {
1408 char *slash;
1409 int done = 0;
1410
1411 slash = path;
1412
1413 while (!done) {
1414 slash += strspn(slash, "/");
1415 slash += strcspn(slash, "/");
1416
1417 done = (*slash == '\0');
1418 *slash = '\0';
1419
1420 if (domkdir(path, 0777) == -1)
1421 goto out;
1422
1423 if (!done)
1424 *slash = '/';
1425 }
1426
1427 return 0;
1428 out:
1429 /* Can't create or or not a directory */
1430 syswarn(1, errno, "Cannot create directory `%s'", path);
1431 return -1;
1432 }
1433
1434
1435 #ifndef NO_CPIO
1436 struct option cpio_longopts[] = {
1437 { "reset-access-time", no_argument, 0, 'a' },
1438 { "make-directories", no_argument, 0, 'd' },
1439 { "nonmatching", no_argument, 0, 'f' },
1440 { "extract", no_argument, 0, 'i' },
1441 { "link", no_argument, 0, 'l' },
1442 { "preserve-modification-time", no_argument, 0, 'm' },
1443 { "create", no_argument, 0, 'o' },
1444 { "pass-through", no_argument, 0, 'p' },
1445 { "rename", no_argument, 0, 'r' },
1446 { "list", no_argument, 0, 't' },
1447 { "unconditional", no_argument, 0, 'u' },
1448 { "verbose", no_argument, 0, 'v' },
1449 { "append", no_argument, 0, 'A' },
1450 { "pattern-file", required_argument, 0, 'E' },
1451 { "file", required_argument, 0, 'F' },
1452 { "force-local", no_argument, 0,
1453 OPT_FORCE_LOCAL },
1454 { "format", required_argument, 0, 'H' },
1455 { "dereference", no_argument, 0, 'L' },
1456 { "swap-halfwords", no_argument, 0, 'S' },
1457 { "summary", no_argument, 0, 'V' },
1458 { "stats", no_argument, 0, 'V' },
1459 { "insecure", no_argument, 0,
1460 OPT_INSECURE },
1461 { "sparse", no_argument, 0,
1462 OPT_SPARSE },
1463 { "xz", no_argument, 0,
1464 OPT_XZ },
1465
1466 #ifdef notyet
1467 /* Not implemented */
1468 { "null", no_argument, 0, '0' },
1469 { "swap", no_argument, 0, 'b' },
1470 { "numeric-uid-gid", no_argument, 0, 'n' },
1471 { "swap-bytes", no_argument, 0, 's' },
1472 { "message", required_argument, 0, 'M' },
1473 { "owner", required_argument, 0 'R' },
1474 { "dot", no_argument, 0, 'V' }, /* xxx */
1475 { "block-size", required_argument, 0,
1476 OPT_BLOCK_SIZE },
1477 { "no-absolute-pathnames", no_argument, 0,
1478 OPT_NO_ABSOLUTE_PATHNAMES },
1479 { "no-preserve-owner", no_argument, 0,
1480 OPT_NO_PRESERVE_OWNER },
1481 { "only-verify-crc", no_argument, 0,
1482 OPT_ONLY_VERIFY_CRC },
1483 { "rsh-command", required_argument, 0,
1484 OPT_RSH_COMMAND },
1485 { "version", no_argument, 0,
1486 OPT_VERSION },
1487 #endif
1488 { 0, 0, 0, 0 },
1489 };
1490
1491 static void
1492 cpio_set_action(int op)
1493 {
1494 if ((act == APPND && op == ARCHIVE) || (act == ARCHIVE && op == APPND))
1495 act = APPND;
1496 else if (act == EXTRACT && op == LIST)
1497 act = op;
1498 else if (act != ERROR && act != op)
1499 cpio_usage();
1500 else
1501 act = op;
1502 }
1503
1504 /*
1505 * cpio_options()
1506 * look at the user specified flags. set globals as required and check if
1507 * the user specified a legal set of flags. If not, complain and exit
1508 */
1509
1510 static void
1511 cpio_options(int argc, char **argv)
1512 {
1513 FSUB tmp;
1514 u_int64_t flg = 0;
1515 u_int64_t bflg = 0;
1516 int c;
1517 size_t i;
1518 FILE *fp;
1519 char *str;
1520
1521 uflag = 1;
1522 kflag = 1;
1523 pids = 1;
1524 pmode = 1;
1525 pmtime = 0;
1526 arcname = NULL;
1527 dflag = 1;
1528 nodirs = 1;
1529 /*
1530 * process option flags
1531 */
1532 while ((c = getoldopt(argc, argv,
1533 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1534 cpio_longopts, NULL)) != -1) {
1535 switch(c) {
1536 case 'a':
1537 /*
1538 * preserve access time on filesystem nodes we read
1539 */
1540 tflag = 1;
1541 flg |= TF;
1542 break;
1543 #ifdef notyet
1544 case 'b':
1545 /*
1546 * swap bytes and half-words when reading data
1547 */
1548 break;
1549 #endif
1550 case 'c':
1551 /*
1552 * ASCII cpio header
1553 */
1554 frmt = &fsub[F_SV4CPIO];
1555 break;
1556 case 'd':
1557 /*
1558 * create directories as needed
1559 * pax does this by default ..
1560 */
1561 nodirs = 0;
1562 break;
1563 case 'f':
1564 /*
1565 * inverse match on patterns
1566 */
1567 cflag = 1;
1568 flg |= CF;
1569 break;
1570 case 'i':
1571 /*
1572 * read the archive
1573 */
1574 cpio_set_action(EXTRACT);
1575 flg |= RF;
1576 break;
1577 #ifdef notyet
1578 case 'k':
1579 break;
1580 #endif
1581 case 'l':
1582 /*
1583 * try to link src to dest with copy (-rw)
1584 */
1585 lflag = 1;
1586 flg |= LF;
1587 break;
1588 case 'm':
1589 /*
1590 * preserve mtime
1591 */
1592 flg |= PF;
1593 pmtime = 1;
1594 break;
1595 case 'o':
1596 /*
1597 * write an archive
1598 */
1599 cpio_set_action(ARCHIVE);
1600 frmt = &(fsub[F_SV4CRC]);
1601 flg |= WF;
1602 break;
1603 case 'p':
1604 /*
1605 * cpio -p is like pax -rw
1606 */
1607 cpio_set_action(COPY);
1608 flg |= RF | WF;
1609 break;
1610 case 'r':
1611 /*
1612 * interactive file rename
1613 */
1614 iflag = 1;
1615 flg |= IF;
1616 break;
1617 #ifdef notyet
1618 case 's':
1619 /*
1620 * swap bytes after reading data
1621 */
1622 break;
1623 #endif
1624 case 't':
1625 /*
1626 * list contents of archive
1627 */
1628 cpio_set_action(LIST);
1629 listf = stdout;
1630 flg &= ~RF;
1631 break;
1632 case 'u':
1633 /*
1634 * don't ignore those older files
1635 */
1636 uflag = 0;
1637 kflag = 0;
1638 flg |= UF;
1639 break;
1640 case 'v':
1641 /*
1642 * verbose operation mode
1643 */
1644 vflag = 1;
1645 flg |= VF;
1646 break;
1647 case 'z':
1648 /*
1649 * use gzip. Non standard option.
1650 */
1651 gzip_program = GZIP_CMD;
1652 break;
1653 case 'A':
1654 /*
1655 * append to an archive
1656 */
1657 cpio_set_action(APPND);
1658 flg |= AF;
1659 break;
1660 case 'B':
1661 /*
1662 * set blocksize to 5120
1663 */
1664 blksz = 5120;
1665 break;
1666 case 'C':
1667 /*
1668 * specify blocksize
1669 */
1670 if ((blksz = (int)str_offt(optarg)) <= 0) {
1671 tty_warn(1, "Invalid block size %s", optarg);
1672 cpio_usage();
1673 }
1674 break;
1675 case 'E':
1676 /*
1677 * file with patterns to extract or list
1678 */
1679 if ((fp = fopen(optarg, "r")) == NULL) {
1680 tty_warn(1, "Unable to open file '%s' for read",
1681 optarg);
1682 cpio_usage();
1683 }
1684 while ((str = get_line(fp)) != NULL) {
1685 pat_add(str, NULL, 0);
1686 }
1687 fclose(fp);
1688 if (get_line_error) {
1689 tty_warn(1, "Problem with file '%s'", optarg);
1690 cpio_usage();
1691 }
1692 break;
1693 case 'H':
1694 /*
1695 * specify an archive format on write
1696 */
1697 tmp.name = optarg;
1698 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1699 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
1700 if (frmt != NULL) {
1701 flg |= XF;
1702 break;
1703 }
1704 tty_warn(1, "Unknown -H format: %s", optarg);
1705 (void)fputs("cpio: Known -H formats are:", stderr);
1706 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1707 (void)fprintf(stderr, " %s", fsub[i].name);
1708 (void)fputs("\n\n", stderr);
1709 cpio_usage();
1710 case 'F':
1711 case 'I':
1712 case 'O':
1713 /*
1714 * filename where the archive is stored
1715 */
1716 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1717 /*
1718 * treat a - as stdin
1719 */
1720 arcname = NULL;
1721 break;
1722 }
1723 arcname = optarg;
1724 break;
1725 case 'L':
1726 /*
1727 * follow symlinks
1728 */
1729 Lflag = 1;
1730 flg |= CLF;
1731 break;
1732 #ifdef notyet
1733 case 'M':
1734 arg = optarg;
1735 break;
1736 case 'R':
1737 arg = optarg;
1738 break;
1739 #endif
1740 case 'S':
1741 /*
1742 * swap halfwords after reading data
1743 */
1744 cpio_swp_head = 1;
1745 break;
1746 #ifdef notyet
1747 case 'V': /* print a '.' for each file processed */
1748 break;
1749 #endif
1750 case 'V':
1751 /*
1752 * semi-verbose operation mode (no listing)
1753 */
1754 Vflag = 1;
1755 flg |= VF;
1756 break;
1757 case 'Z':
1758 /*
1759 * use compress. Non standard option.
1760 */
1761 gzip_program = COMPRESS_CMD;
1762 break;
1763 case '6':
1764 /*
1765 * process Version 6 cpio format
1766 */
1767 frmt = &(fsub[F_BCPIO]);
1768 break;
1769 case OPT_FORCE_LOCAL:
1770 forcelocal = 1;
1771 break;
1772 case OPT_INSECURE:
1773 secure = 0;
1774 break;
1775 case OPT_SPARSE:
1776 /* do nothing; we already generate sparse files */
1777 break;
1778 case OPT_XZ:
1779 gzip_program = XZ_CMD;
1780 break;
1781 default:
1782 cpio_usage();
1783 }
1784 }
1785
1786 /*
1787 * figure out the operation mode of cpio. check that we have not been
1788 * given a bogus set of flags for the operation mode.
1789 */
1790 if (ISLIST(flg)) {
1791 act = LIST;
1792 bflg = flg & BDLIST;
1793 } else if (ISEXTRACT(flg)) {
1794 act = EXTRACT;
1795 bflg = flg & BDEXTR;
1796 } else if (ISARCHIVE(flg)) {
1797 act = ARCHIVE;
1798 bflg = flg & BDARCH;
1799 } else if (ISAPPND(flg)) {
1800 act = APPND;
1801 bflg = flg & BDARCH;
1802 } else if (ISCOPY(flg)) {
1803 act = COPY;
1804 bflg = flg & BDCOPY;
1805 } else
1806 cpio_usage();
1807 if (bflg) {
1808 cpio_usage();
1809 }
1810
1811 /*
1812 * if we are writing (ARCHIVE) we use the default format if the user
1813 * did not specify a format. when we write during an APPEND, we will
1814 * adopt the format of the existing archive if none was supplied.
1815 */
1816 if (!(flg & XF) && (act == ARCHIVE))
1817 frmt = &(fsub[F_BCPIO]);
1818
1819 /*
1820 * process the args as they are interpreted by the operation mode
1821 */
1822 switch (act) {
1823 case LIST:
1824 case EXTRACT:
1825 for (; optind < argc; optind++)
1826 if (pat_add(argv[optind], NULL, 0) < 0)
1827 cpio_usage();
1828 break;
1829 case COPY:
1830 if (optind >= argc) {
1831 tty_warn(0, "Destination directory was not supplied");
1832 cpio_usage();
1833 }
1834 --argc;
1835 dirptr = argv[argc];
1836 /* FALLTHROUGH */
1837 case ARCHIVE:
1838 case APPND:
1839 if (argc != optind) {
1840 for (; optind < argc; optind++)
1841 if (ftree_add(argv[optind], 0) < 0)
1842 cpio_usage();
1843 break;
1844 }
1845 /*
1846 * no read errors allowed on updates/append operation!
1847 */
1848 maxflt = 0;
1849 while ((str = get_line(stdin)) != NULL) {
1850 ftree_add(str, 0);
1851 }
1852 if (get_line_error) {
1853 tty_warn(1, "Problem while reading stdin");
1854 cpio_usage();
1855 }
1856 break;
1857 default:
1858 cpio_usage();
1859 }
1860 }
1861 #endif
1862
1863 /*
1864 * printflg()
1865 * print out those invalid flag sets found to the user
1866 */
1867
1868 static void
1869 printflg(unsigned int flg)
1870 {
1871 int nxt;
1872
1873 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1874 while ((nxt = ffs(flg)) != 0) {
1875 flg &= ~(1 << (nxt - 1));
1876 (void)fprintf(stderr, " -%c", flgch[nxt - 1]);
1877 }
1878 (void)putc('\n', stderr);
1879 }
1880
1881 /*
1882 * c_frmt()
1883 * comparison routine used by bsearch to find the format specified
1884 * by the user
1885 */
1886
1887 static int
1888 c_frmt(const void *a, const void *b)
1889 {
1890 return strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name);
1891 }
1892
1893 /*
1894 * opt_next()
1895 * called by format specific options routines to get each format specific
1896 * flag and value specified with -o
1897 * Return:
1898 * pointer to next OPLIST entry or NULL (end of list).
1899 */
1900
1901 OPLIST *
1902 opt_next(void)
1903 {
1904 OPLIST *opt;
1905
1906 if ((opt = ophead) != NULL)
1907 ophead = ophead->fow;
1908 return opt;
1909 }
1910
1911 /*
1912 * bad_opt()
1913 * generic routine used to complain about a format specific options
1914 * when the format does not support options.
1915 */
1916
1917 int
1918 bad_opt(void)
1919 {
1920 OPLIST *opt;
1921
1922 if (ophead == NULL)
1923 return 0;
1924 /*
1925 * print all we were given
1926 */
1927 tty_warn(1," These format options are not supported for %s",
1928 frmt->name);
1929 while ((opt = opt_next()) != NULL)
1930 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1931 if (strcmp(NM_TAR, argv0) == 0)
1932 tar_usage();
1933 #ifndef NO_CPIO
1934 else if (strcmp(NM_CPIO, argv0) == 0)
1935 cpio_usage();
1936 #endif
1937 else
1938 pax_usage();
1939 }
1940
1941 /*
1942 * opt_add()
1943 * breaks the value supplied to -o into a option name and value. options
1944 * are given to -o in the form -o name-value,name=value
1945 * multiple -o may be specified.
1946 * Return:
1947 * 0 if format in name=value format, -1 if -o is passed junk
1948 */
1949
1950 int
1951 opt_add(const char *str)
1952 {
1953 OPLIST *opt;
1954 char *frpt;
1955 char *pt;
1956 char *endpt;
1957 char *dstr;
1958
1959 if ((str == NULL) || (*str == '\0')) {
1960 tty_warn(0, "Invalid option name");
1961 return -1;
1962 }
1963 if ((dstr = strdup(str)) == NULL) {
1964 tty_warn(0, "Unable to allocate space for option list");
1965 return -1;
1966 }
1967 frpt = endpt = dstr;
1968
1969 /*
1970 * break into name and values pieces and stuff each one into a
1971 * OPLIST structure. When we know the format, the format specific
1972 * option function will go through this list
1973 */
1974 while ((frpt != NULL) && (*frpt != '\0')) {
1975 if ((endpt = strchr(frpt, ',')) != NULL)
1976 *endpt = '\0';
1977 if ((pt = strchr(frpt, '=')) == NULL) {
1978 tty_warn(0, "Invalid options format");
1979 free(dstr);
1980 return -1;
1981 }
1982 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1983 tty_warn(0, "Unable to allocate space for option list");
1984 free(dstr);
1985 return -1;
1986 }
1987 *pt++ = '\0';
1988 opt->name = frpt;
1989 opt->value = pt;
1990 opt->fow = NULL;
1991 if (endpt != NULL)
1992 frpt = endpt + 1;
1993 else
1994 frpt = NULL;
1995 if (ophead == NULL) {
1996 optail = ophead = opt;
1997 continue;
1998 }
1999 optail->fow = opt;
2000 optail = opt;
2001 }
2002 return 0;
2003 }
2004
2005 /*
2006 * str_offt()
2007 * Convert an expression of the following forms to an off_t > 0.
2008 * 1) A positive decimal number.
2009 * 2) A positive decimal number followed by a b (mult by 512).
2010 * 3) A positive decimal number followed by a k (mult by 1024).
2011 * 4) A positive decimal number followed by a m (mult by 512).
2012 * 5) A positive decimal number followed by a w (mult by sizeof int)
2013 * 6) Two or more positive decimal numbers (with/without k,b or w).
2014 * separated by x (also * for backwards compatibility), specifying
2015 * the product of the indicated values.
2016 * Return:
2017 * 0 for an error, a positive value o.w.
2018 */
2019
2020 static off_t
2021 str_offt(char *val)
2022 {
2023 char *expr;
2024 off_t num, t;
2025
2026 num = STRTOOFFT(val, &expr, 0);
2027 if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
2028 return 0;
2029
2030 switch(*expr) {
2031 case 'b':
2032 t = num;
2033 num *= 512;
2034 if (t > num)
2035 return 0;
2036 ++expr;
2037 break;
2038 case 'k':
2039 t = num;
2040 num *= 1024;
2041 if (t > num)
2042 return 0;
2043 ++expr;
2044 break;
2045 case 'm':
2046 t = num;
2047 num *= 1048576;
2048 if (t > num)
2049 return 0;
2050 ++expr;
2051 break;
2052 case 'w':
2053 t = num;
2054 num *= sizeof(int);
2055 if (t > num)
2056 return 0;
2057 ++expr;
2058 break;
2059 }
2060
2061 switch(*expr) {
2062 case '\0':
2063 break;
2064 case '*':
2065 case 'x':
2066 t = num;
2067 num *= str_offt(expr + 1);
2068 if (t > num)
2069 return 0;
2070 break;
2071 default:
2072 return 0;
2073 }
2074 return num;
2075 }
2076
2077 static char *
2078 get_line(FILE *f)
2079 {
2080 char *name, *temp;
2081 size_t len;
2082
2083 name = fgetln(f, &len);
2084 if (!name) {
2085 get_line_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
2086 return 0;
2087 }
2088 if (name[len-1] != '\n')
2089 len++;
2090 temp = malloc(len);
2091 if (!temp) {
2092 get_line_error = GETLINE_OUT_OF_MEM;
2093 return 0;
2094 }
2095 memcpy(temp, name, len-1);
2096 temp[len-1] = 0;
2097 return temp;
2098 }
2099
2100 #ifndef SMALL
2101 /*
2102 * set_tstamp()
2103 * Use a specific timestamp for all individual files created in the
2104 * archive
2105 */
2106 static int
2107 set_tstamp(const char *b, struct stat *st)
2108 {
2109 time_t when;
2110 char *eb;
2111 long long l;
2112
2113 if (stat(b, st) != -1)
2114 return 0;
2115
2116 #ifndef HAVE_NBTOOL_CONFIG_H
2117 errno = 0;
2118 if ((when = parsedate(b, NULL, NULL)) == -1 && errno != 0)
2119 #endif
2120 {
2121 errno = 0;
2122 l = strtoll(b, &eb, 0);
2123 if (b == eb || *eb || errno)
2124 return -1;
2125 when = (time_t)l;
2126 }
2127
2128 st->st_ino = 1;
2129 #if HAVE_STRUCT_STAT_BIRTHTIME
2130 st->st_birthtime =
2131 #endif
2132 st->st_mtime = st->st_ctime = st->st_atime = when;
2133 return 0;
2134 }
2135 #endif
2136
2137 /*
2138 * no_op()
2139 * for those option functions where the archive format has nothing to do.
2140 * Return:
2141 * 0
2142 */
2143
2144 static int
2145 no_op(void)
2146 {
2147 return 0;
2148 }
2149
2150 /*
2151 * pax_usage()
2152 * print the usage summary to the user
2153 */
2154
2155 static void
2156 pax_usage(void)
2157 {
2158 fprintf(stderr,
2159 "usage: pax [-0cdjnvzVO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
2160 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
2161 " [pattern ...]\n");
2162 fprintf(stderr,
2163 " pax -r [-cdijknuvzADOVYZ] [-E limit] [-f archive] [-N dbdir]\n"
2164 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
2165 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
2166 fprintf(stderr,
2167 " pax -w [-dijtuvzAHLMOPVX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
2168 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
2169 " [-U user] ... [-G group] ...\n"
2170 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
2171 fprintf(stderr,
2172 " pax -r -w [-dijklntuvzADHLMOPVXYZ] [-N dbdir] [-p string] ...\n"
2173 " [-s replstr] ... [-U user] ... [-G group] ...\n"
2174 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
2175 exit(1);
2176 /* NOTREACHED */
2177 }
2178
2179 /*
2180 * tar_usage()
2181 * print the usage summary to the user
2182 */
2183
2184 static void
2185 tar_usage(void)
2186 {
2187 (void)fputs("usage: tar [-]{crtux}[-befhjklmopqvwzHJOPSXZ014578] "
2188 "[archive] [blocksize]\n"
2189 " [-C directory] [-T file] [-s replstr] "
2190 "[file ...]\n", stderr);
2191 exit(1);
2192 /* NOTREACHED */
2193 }
2194
2195 #ifndef NO_CPIO
2196 /*
2197 * cpio_usage()
2198 * print the usage summary to the user
2199 */
2200
2201 static void
2202 cpio_usage(void)
2203 {
2204
2205 (void)fputs("usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
2206 "[-H format] [-O archive]\n"
2207 " < name-list [> archive]\n"
2208 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
2209 "[-F archive] [-H format] \n"
2210 " [-I archive] "
2211 "[pattern ...] [< archive]\n"
2212 " cpio -p [-adlLmuv] destination-directory "
2213 "< name-list\n", stderr);
2214 exit(1);
2215 /* NOTREACHED */
2216 }
2217 #endif
2218