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