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