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