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