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