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