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