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