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