options.c revision 1.58 1 /* $NetBSD: options.c,v 1.58 2003/01/09 17:26:21 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1992 Keith Muller.
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Keith Muller of the University of California, San Diego.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. 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.58 2003/01/09 17:26:21 christos Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/mtio.h>
53 #include <sys/param.h>
54 #include <ctype.h>
55 #include <errno.h>
56 #include <getopt.h>
57 #include <limits.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <paths.h>
63 #include "pax.h"
64 #include "options.h"
65 #include "cpio.h"
66 #include "tar.h"
67 #include "extern.h"
68 #ifndef SMALL
69 #include "mtree.h"
70 #endif /* SMALL */
71
72 /*
73 * Routines which handle command line options
74 */
75
76 static int nopids; /* tar mode: suppress "pids" for -p option */
77 static char *flgch = FLGCH; /* list of all possible flags (pax) */
78 static OPLIST *ophead = NULL; /* head for format specific options -x */
79 static OPLIST *optail = NULL; /* option tail */
80
81 static int no_op(void);
82 static void printflg(unsigned int);
83 static int c_frmt(const void *, const void *);
84 static off_t str_offt(char *);
85 static char *getline(FILE *fp);
86 static void pax_options(int, char **);
87 static void pax_usage(void);
88 static void tar_options(int, char **);
89 static void tar_usage(void);
90 static void cpio_options(int, char **);
91 static void cpio_usage(void);
92
93 /* errors from getline */
94 #define GETLINE_FILE_CORRUPT 1
95 #define GETLINE_OUT_OF_MEM 2
96 static int getline_error;
97
98 #define 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 cpio_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 /* XXX: Conflicts with current -L */
739 { "tape-length", required_argument, 0, 'L' },
740 { "multi-volume", no_argument, 0, 'M' },
741 { "after-date", required_argument, 0, 'N' },
742 { "newer", required_argument, 0, 'N' },
743 { "to-stdout", no_argument, 0, 'O' },
744 { "record-number", no_argument, 0, 'R' },
745 { "remove-files", no_argument, 0,
746 OPT_REMOVE_FILES },
747 { "same-order", no_argument, 0, 's' },
748 { "preserve-order", no_argument, 0, 's' },
749 { "sparse", no_argument, 0, 'S' },
750 { "null", no_argument, 0,
751 OPT_NULL },
752 { "totals", no_argument, 0,
753 OPT_TOTALS },
754 { "volume-name", required_argument, 0, 'V' },
755 { "label", required_argument, 0, 'V' },
756 { "version", no_argument, 0,
757 OPT_VERSION },
758 { "verify", no_argument, 0, 'W' },
759 { "block-compress", no_argument, 0,
760 OPT_BLOCK_COMPRESS },
761 { "norecurse", no_argument, 0,
762 OPT_NORECURSE },
763 #endif
764 { 0, 0, 0, 0 },
765 };
766
767 static void
768 tar_options(int argc, char **argv)
769 {
770 int c;
771 int fstdin = 0;
772 int Oflag = 0;
773 int nincfiles = 0;
774 int incfiles_max = 0;
775 struct incfile {
776 char *file;
777 char *dir;
778 };
779 struct incfile *incfiles = NULL;
780
781 /*
782 * Set default values.
783 */
784 rmleadslash = 1;
785 is_gnutar = 1;
786
787 /*
788 * process option flags
789 */
790 while ((c = getoldopt(argc, argv,
791 "+b:cef:hlmopqrstuvwxzBC:HI:OPT:X:Z014578",
792 tar_longopts, NULL))
793 != -1) {
794 switch(c) {
795 case 'b':
796 /*
797 * specify blocksize in 512-byte blocks
798 */
799 if ((wrblksz = (int)str_offt(optarg)) <= 0) {
800 tty_warn(1, "Invalid block size %s", optarg);
801 tar_usage();
802 }
803 wrblksz *= 512; /* XXX - check for int oflow */
804 break;
805 case 'c':
806 /*
807 * create an archive
808 */
809 act = ARCHIVE;
810 break;
811 case 'e':
812 /*
813 * stop after first error
814 */
815 maxflt = 0;
816 break;
817 case 'f':
818 /*
819 * filename where the archive is stored
820 */
821 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
822 /*
823 * treat a - as stdin
824 */
825 fstdin = 1;
826 arcname = NULL;
827 break;
828 }
829 fstdin = 0;
830 arcname = optarg;
831 break;
832 case 'h':
833 /*
834 * follow symlinks
835 */
836 Lflag = 1;
837 break;
838 case 'l':
839 /*
840 * do not pass over mount points in the file system
841 */
842 Xflag = 1;
843 break;
844 case 'm':
845 /*
846 * do not preserve modification time
847 */
848 pmtime = 0;
849 break;
850 case 'o':
851 /*
852 * This option does several things based on whether
853 * this is a create or extract operation.
854 */
855 if (act == ARCHIVE) {
856 /* GNU tar: write V7 format archives. */
857 Oflag = 1;
858 /* 4.2BSD: don't add directory entries. */
859 if (opt_add("write_opt=nodir") < 0)
860 tar_usage();
861
862 } else {
863 /* SUS: don't preserve owner/group. */
864 pids = 0;
865 nopids = 1;
866 }
867 break;
868 case 'O':
869 Oflag = 1;
870 break;
871 case 'p':
872 /*
873 * preserve user id, group id, file
874 * mode, access/modification times
875 */
876 if (!nopids)
877 pids = 1;
878 pmode = 1;
879 patime = 1;
880 pmtime = 1;
881 break;
882 case 'q':
883 /*
884 * select first match for a pattern only
885 */
886 nflag = 1;
887 break;
888 case 'r':
889 case 'u':
890 /*
891 * append to the archive
892 */
893 act = APPND;
894 break;
895 case 's':
896 /*
897 * file name substitution name pattern
898 */
899 if (rep_add(optarg) < 0) {
900 tar_usage();
901 break;
902 }
903 break;
904 case 't':
905 /*
906 * list contents of the tape
907 */
908 act = LIST;
909 break;
910 case 'v':
911 /*
912 * verbose operation mode
913 */
914 vflag = 1;
915 break;
916 case 'w':
917 /*
918 * interactive file rename
919 */
920 iflag = 1;
921 break;
922 case 'x':
923 /*
924 * extract an archive, preserving mode,
925 * and mtime if possible.
926 */
927 act = EXTRACT;
928 pmtime = 1;
929 break;
930 case 'z':
931 /*
932 * use gzip. Non standard option.
933 */
934 zflag = 1;
935 gzip_program = GZIP_CMD;
936 break;
937 case 'B':
938 /*
939 * Nothing to do here, this is pax default
940 */
941 break;
942 case 'C':
943 chdname = optarg;
944 break;
945 case 'H':
946 /*
947 * follow command line symlinks only
948 */
949 Hflag = 1;
950 break;
951 case 'I':
952 case 'T':
953 if (++nincfiles > incfiles_max) {
954 incfiles_max = nincfiles + 3;
955 incfiles = realloc(incfiles,
956 sizeof(*incfiles) * incfiles_max);
957 if (incfiles == NULL) {
958 tty_warn(0, "Unable to allocate space "
959 "for option list");
960 exit(1);
961 }
962 }
963 incfiles[nincfiles - 1].file = optarg;
964 incfiles[nincfiles - 1].dir = chdname;
965 break;
966 case 'P':
967 /*
968 * do not remove leading '/' from pathnames
969 */
970 rmleadslash = 0;
971 Aflag = 1;
972 break;
973 case 'X':
974 /*
975 * GNU tar compat: exclude the files listed in optarg
976 */
977 if (tar_gnutar_X_compat(optarg) != 0)
978 tar_usage();
979 break;
980 case 'Z':
981 /*
982 * use compress.
983 */
984 zflag = 1;
985 gzip_program = COMPRESS_CMD;
986 break;
987 case '0':
988 arcname = DEV_0;
989 break;
990 case '1':
991 arcname = DEV_1;
992 break;
993 case '4':
994 arcname = DEV_4;
995 break;
996 case '5':
997 arcname = DEV_5;
998 break;
999 case '7':
1000 arcname = DEV_7;
1001 break;
1002 case '8':
1003 arcname = DEV_8;
1004 break;
1005 case OPT_ATIME_PRESERVE:
1006 patime = 1;
1007 break;
1008 case OPT_UNLINK:
1009 /* Just ignore -- we always unlink first. */
1010 break;
1011 case OPT_USE_COMPRESS_PROGRAM:
1012 zflag = 1;
1013 gzip_program = optarg;
1014 break;
1015 case OPT_FORCE_LOCAL:
1016 forcelocal = 1;
1017 break;
1018 case OPT_INSECURE:
1019 secure = 0;
1020 break;
1021 case OPT_STRICT:
1022 /* disable gnu extensions */
1023 is_gnutar = 0;
1024 break;
1025 case OPT_EXCLUDE:
1026 if (tar_gnutar_minus_minus_exclude(optarg) != 0)
1027 tar_usage();
1028 break;
1029 default:
1030 tar_usage();
1031 break;
1032 }
1033 }
1034 argc -= optind;
1035 argv += optind;
1036
1037 /* Tar requires an action. */
1038 if (act == ERROR)
1039 tar_usage();
1040
1041 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1042 if (fstdin == 1 && act == ARCHIVE)
1043 listf = stderr;
1044 else
1045 listf = stdout;
1046
1047 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1048 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
1049 exit(0);
1050 /*
1051 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1052 * (unless -o specified)
1053 */
1054 if (act == ARCHIVE || act == APPND)
1055 frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
1056 else if (Oflag) {
1057 tty_warn(1, "The -O/-o options are only valid when writing an archive");
1058 tar_usage(); /* only valid when writing */
1059 }
1060
1061 /*
1062 * process the args as they are interpreted by the operation mode
1063 */
1064 switch (act) {
1065 case LIST:
1066 case EXTRACT:
1067 default:
1068 {
1069 int sawpat = 0;
1070 int dirisnext = 0;
1071 char *file, *dir;
1072
1073 while (nincfiles || *argv != NULL) {
1074 /*
1075 * If we queued up any include files,
1076 * pull them in now. Otherwise, check
1077 * for -I and -C positional flags.
1078 * Anything else must be a file to
1079 * extract.
1080 */
1081 if (nincfiles) {
1082 file = incfiles->file;
1083 dir = incfiles->dir;
1084 incfiles++;
1085 nincfiles--;
1086 } else if (strcmp(*argv, "-I") == 0) {
1087 if (*++argv == NULL)
1088 break;
1089 file = *argv++;
1090 dir = chdname;
1091 } else
1092 file = NULL;
1093 if (file != NULL) {
1094 FILE *fp;
1095 char *str;
1096
1097 if (strcmp(file, "-") == 0)
1098 fp = stdin;
1099 else if ((fp = fopen(file, "r")) == NULL) {
1100 tty_warn(1, "Unable to open file '%s' for read", file);
1101 tar_usage();
1102 }
1103 while ((str = getline(fp)) != NULL) {
1104 if (dirisnext) {
1105 dir = str;
1106 dirisnext = 0;
1107 continue;
1108 }
1109 if (strcmp(str, "-C") == 0) {
1110 dirisnext = 1;
1111 continue;
1112 }
1113 if (pat_add(str, dir) < 0)
1114 tar_usage();
1115 sawpat = 1;
1116 }
1117 /* Bomb if given -C w/out a dir. */
1118 if (dirisnext)
1119 tar_usage();
1120 if (strcmp(file, "-") != 0)
1121 fclose(fp);
1122 if (getline_error) {
1123 tty_warn(1, "Problem with file '%s'", file);
1124 tar_usage();
1125 }
1126 } else if (strcmp(*argv, "-C") == 0) {
1127 if (*++argv == NULL)
1128 break;
1129 chdname = *argv++;
1130 } else if (pat_add(*argv++, chdname) < 0)
1131 tar_usage();
1132 else
1133 sawpat = 1;
1134 }
1135 /*
1136 * if patterns were added, we are doing chdir()
1137 * on a file-by-file basis, else, just one
1138 * global chdir (if any) after opening input.
1139 */
1140 if (sawpat > 0)
1141 chdname = NULL;
1142 }
1143 break;
1144 case ARCHIVE:
1145 case APPND:
1146 if (chdname != NULL) { /* initial chdir() */
1147 if (ftree_add(chdname, 1) < 0)
1148 tar_usage();
1149 }
1150
1151 while (nincfiles || *argv != NULL) {
1152 char *file, *dir;
1153
1154 /*
1155 * If we queued up any include files, pull them in
1156 * now. Otherwise, check for -I and -C positional
1157 * flags. Anything else must be a file to include
1158 * in the archive.
1159 */
1160 if (nincfiles) {
1161 file = incfiles->file;
1162 dir = incfiles->dir;
1163 incfiles++;
1164 nincfiles--;
1165 } else if (strcmp(*argv, "-I") == 0) {
1166 if (*++argv == NULL)
1167 break;
1168 file = *argv++;
1169 dir = NULL;
1170 } else
1171 file = NULL;
1172 if (file != NULL) {
1173 FILE *fp;
1174 char *str;
1175 int dirisnext = 0;
1176
1177 /* Set directory if needed */
1178 if (dir) {
1179 if (ftree_add(dir, 1) < 0)
1180 tar_usage();
1181 }
1182
1183 if (strcmp(file, "-") == 0)
1184 fp = stdin;
1185 else if ((fp = fopen(file, "r")) == NULL) {
1186 tty_warn(1, "Unable to open file '%s' for read", file);
1187 tar_usage();
1188 }
1189 while ((str = getline(fp)) != NULL) {
1190 if (dirisnext) {
1191 if (ftree_add(str, 1) < 0)
1192 tar_usage();
1193 dirisnext = 0;
1194 continue;
1195 }
1196 if (strcmp(str, "-C") == 0) {
1197 dirisnext = 1;
1198 continue;
1199 }
1200 if (ftree_add(str, 0) < 0)
1201 tar_usage();
1202 }
1203 /* Bomb if given -C w/out a dir. */
1204 if (dirisnext)
1205 tar_usage();
1206 if (strcmp(file, "-") != 0)
1207 fclose(fp);
1208 if (getline_error) {
1209 tty_warn(1, "Problem with file '%s'",
1210 file);
1211 tar_usage();
1212 }
1213 } else if (strcmp(*argv, "-C") == 0) {
1214 if (*++argv == NULL)
1215 break;
1216 if (ftree_add(*argv++, 1) < 0)
1217 tar_usage();
1218 } else if (ftree_add(*argv++, 0) < 0)
1219 tar_usage();
1220 }
1221 /*
1222 * no read errors allowed on updates/append operation!
1223 */
1224 maxflt = 0;
1225 break;
1226 }
1227 if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
1228 arcname = getenv("TAPE");
1229 if ((arcname == NULL) || (*arcname == '\0'))
1230 arcname = _PATH_DEFTAPE;
1231 }
1232 }
1233
1234 int
1235 mkpath(path)
1236 char *path;
1237 {
1238 struct stat sb;
1239 char *slash;
1240 int done = 0;
1241
1242 slash = path;
1243
1244 while (!done) {
1245 slash += strspn(slash, "/");
1246 slash += strcspn(slash, "/");
1247
1248 done = (*slash == '\0');
1249 *slash = '\0';
1250
1251 if (stat(path, &sb)) {
1252 if (errno != ENOENT || mkdir(path, 0777)) {
1253 tty_warn(1, "%s", path);
1254 return (-1);
1255 }
1256 } else if (!S_ISDIR(sb.st_mode)) {
1257 syswarn(1, ENOTDIR, "%s", path);
1258 return (-1);
1259 }
1260
1261 if (!done)
1262 *slash = '/';
1263 }
1264
1265 return (0);
1266 }
1267
1268
1269 struct option cpio_longopts[] = {
1270 { "reset-access-time", no_argument, 0, 'a' },
1271 { "make-directories", no_argument, 0, 'd' },
1272 { "nonmatching", no_argument, 0, 'f' },
1273 { "extract", no_argument, 0, 'i' },
1274 { "link", no_argument, 0, 'l' },
1275 { "preserve-modification-time", no_argument, 0, 'm' },
1276 { "create", no_argument, 0, 'o' },
1277 { "pass-through", no_argument, 0, 'p' },
1278 { "rename", no_argument, 0, 'r' },
1279 { "list", no_argument, 0, 't' },
1280 { "unconditional", no_argument, 0, 'u' },
1281 { "verbose", no_argument, 0, 'v' },
1282 { "append", no_argument, 0, 'A' },
1283 { "pattern-file", required_argument, 0, 'E' },
1284 { "file", required_argument, 0, 'F' },
1285 { "force-local", no_argument, 0,
1286 OPT_FORCE_LOCAL },
1287 { "format", required_argument, 0, 'H' },
1288 { "dereference", no_argument, 0, 'L' },
1289 { "swap-halfwords", no_argument, 0, 'S' },
1290 { "insecure", no_argument, 0,
1291 OPT_INSECURE },
1292
1293 #ifdef notyet
1294 /* Not implemented */
1295 { "null", no_argument, 0, '0' },
1296 { "swap", no_argument, 0, 'b' },
1297 { "numeric-uid-gid", no_argument, 0, 'n' },
1298 { "swap-bytes", no_argument, 0, 's' },
1299 { "message", required_argument, 0, 'M' },
1300 { "owner", required_argument, 0 'R' },
1301 { "dot", no_argument, 0, 'V' },
1302 { "block-size", required_argument, 0,
1303 OPT_BLOCK_SIZE },
1304 { "no-absolute-pathnames", no_argument, 0,
1305 OPT_NO_ABSOLUTE_PATHNAMES },
1306 { "no-preserve-owner", no_argument, 0,
1307 OPT_NO_PRESERVE_OWNER },
1308 { "only-verify-crc", no_argument, 0,
1309 OPT_ONLY_VERIFY_CRC },
1310 { "rsh-command", required_argument, 0,
1311 OPT_RSH_COMMAND },
1312 { "sparce", no_argument, 0,
1313 OPT_SPARSE },
1314 { "version", no_argument, 0,
1315 OPT_VERSION },
1316 #endif
1317 };
1318
1319 /*
1320 * cpio_options()
1321 * look at the user specified flags. set globals as required and check if
1322 * the user specified a legal set of flags. If not, complain and exit
1323 */
1324
1325 static void
1326 cpio_options(int argc, char **argv)
1327 {
1328 FSUB tmp;
1329 unsigned int flg = 0;
1330 unsigned int bflg = 0;
1331 int c, i;
1332 FILE *fp;
1333 char *str;
1334
1335 uflag = 1;
1336 kflag = 1;
1337 pids = 1;
1338 pmode = 1;
1339 pmtime = 0;
1340 arcname = NULL;
1341 dflag = 1;
1342 nodirs = 1;
1343 /*
1344 * process option flags
1345 */
1346 while ((c = getoldopt(argc, argv,
1347 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1348 cpio_longopts, NULL)) != -1) {
1349 switch(c) {
1350 case 'a':
1351 /*
1352 * preserve access time on filesystem nodes we read
1353 */
1354 tflag = 1;
1355 flg |= TF;
1356 break;
1357 #ifdef notyet
1358 case 'b':
1359 /*
1360 * swap bytes and half-words when reading data
1361 */
1362 break;
1363 #endif
1364 case 'c':
1365 /*
1366 * ASCII cpio header
1367 */
1368 frmt = &fsub[F_SV4CPIO];
1369 break;
1370 case 'd':
1371 /*
1372 * create directories as needed
1373 * pax does this by default ..
1374 */
1375 nodirs = 0;
1376 flg |= RF;
1377 break;
1378 case 'f':
1379 /*
1380 * inverse match on patterns
1381 */
1382 cflag = 1;
1383 flg |= CF;
1384 break;
1385 case 'i':
1386 /*
1387 * read the archive
1388 */
1389 act = EXTRACT;
1390 flg |= RF;
1391 break;
1392 #ifdef notyet
1393 case 'k':
1394 break;
1395 #endif
1396 case 'l':
1397 /*
1398 * try to link src to dest with copy (-rw)
1399 */
1400 lflag = 1;
1401 flg |= LF;
1402 break;
1403 case 'm':
1404 /*
1405 * preserve mtime
1406 */
1407 flg |= PF;
1408 pmtime = 1;
1409 break;
1410 case 'o':
1411 /*
1412 * write an archive
1413 */
1414 act = ARCHIVE;
1415 frmt = &(fsub[F_SV4CRC]);
1416 flg |= WF;
1417 break;
1418 case 'p':
1419 /*
1420 * cpio -p is like pax -rw
1421 */
1422 act = COPY;
1423 flg |= RF | WF;
1424 break;
1425 case 'r':
1426 /*
1427 * interactive file rename
1428 */
1429 iflag = 1;
1430 flg |= IF;
1431 break;
1432 #ifdef notyet
1433 case 's':
1434 /*
1435 * swap bytes after reading data
1436 */
1437 break;
1438 #endif
1439 case 't':
1440 /*
1441 * list contents of archive
1442 */
1443 act = LIST;
1444 listf = stdout;
1445 break;
1446 case 'u':
1447 /*
1448 * don't ignore those older files
1449 */
1450 uflag = 0;
1451 kflag = 0;
1452 flg |= UF;
1453 break;
1454 case 'v':
1455 /*
1456 * verbose operation mode
1457 */
1458 vflag = 1;
1459 flg |= VF;
1460 break;
1461 case 'z':
1462 /*
1463 * use gzip. Non standard option.
1464 */
1465 gzip_program = GZIP_CMD;
1466 break;
1467 case 'A':
1468 /*
1469 * append to an archive
1470 */
1471 act = APPND;
1472 flg |= AF;
1473 break;
1474 case 'B':
1475 /*
1476 * set blocksize to 5120
1477 */
1478 blksz = 5120;
1479 break;
1480 case 'C':
1481 /*
1482 * specify blocksize
1483 */
1484 if ((blksz = (int)str_offt(optarg)) <= 0) {
1485 tty_warn(1, "Invalid block size %s", optarg);
1486 tar_usage();
1487 }
1488 break;
1489 case 'E':
1490 /*
1491 * file with patterns to extract or list
1492 */
1493 if ((fp = fopen(optarg, "r")) == NULL) {
1494 tty_warn(1, "Unable to open file '%s' for read",
1495 optarg);
1496 cpio_usage();
1497 }
1498 while ((str = getline(fp)) != NULL) {
1499 pat_add(str, NULL);
1500 }
1501 fclose(fp);
1502 if (getline_error) {
1503 tty_warn(1, "Problem with file '%s'", optarg);
1504 cpio_usage();
1505 }
1506 break;
1507 case 'H':
1508 /*
1509 * specify an archive format on write
1510 */
1511 tmp.name = optarg;
1512 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1513 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
1514 if (frmt != NULL) {
1515 flg |= XF;
1516 break;
1517 }
1518 tty_warn(1, "Unknown -H format: %s", optarg);
1519 (void)fputs("cpio: Known -H formats are:", stderr);
1520 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1521 (void)fprintf(stderr, " %s", fsub[i].name);
1522 (void)fputs("\n\n", stderr);
1523 tar_usage();
1524 break;
1525 case 'I':
1526 case 'O':
1527 /*
1528 * filename where the archive is stored
1529 */
1530 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1531 /*
1532 * treat a - as stdin
1533 */
1534 arcname = NULL;
1535 break;
1536 }
1537 arcname = optarg;
1538 break;
1539 case 'L':
1540 /*
1541 * follow symlinks
1542 */
1543 Lflag = 1;
1544 flg |= CLF;
1545 break;
1546 #ifdef notyet
1547 case 'M':
1548 arg = optarg;
1549 break;
1550 case 'R':
1551 arg = optarg;
1552 break;
1553 #endif
1554 case 'S':
1555 /*
1556 * swap halfwords after reading data
1557 */
1558 cpio_swp_head = 1;
1559 break;
1560 #ifdef notyet
1561 case 'V':
1562 break;
1563 #endif
1564 case 'Z':
1565 /*
1566 * use compress. Non standard option.
1567 */
1568 gzip_program = COMPRESS_CMD;
1569 break;
1570 case '6':
1571 /*
1572 * process Version 6 cpio format
1573 */
1574 frmt = &(fsub[F_BCPIO]);
1575 case OPT_FORCE_LOCAL:
1576 forcelocal = 1;
1577 break;
1578 case OPT_INSECURE:
1579 secure = 0;
1580 break;
1581 default:
1582 cpio_usage();
1583 break;
1584 }
1585 }
1586
1587 /*
1588 * figure out the operation mode of cpio. check that we have not been
1589 * given a bogus set of flags for the operation mode.
1590 */
1591 if (ISLIST(flg)) {
1592 act = LIST;
1593 bflg = flg & BDLIST;
1594 } else if (ISEXTRACT(flg)) {
1595 act = EXTRACT;
1596 bflg = flg & BDEXTR;
1597 } else if (ISARCHIVE(flg)) {
1598 act = ARCHIVE;
1599 bflg = flg & BDARCH;
1600 } else if (ISAPPND(flg)) {
1601 act = APPND;
1602 bflg = flg & BDARCH;
1603 } else if (ISCOPY(flg)) {
1604 act = COPY;
1605 bflg = flg & BDCOPY;
1606 } else
1607 cpio_usage();
1608 if (bflg) {
1609 cpio_usage();
1610 }
1611
1612 /*
1613 * if we are writing (ARCHIVE) we use the default format if the user
1614 * did not specify a format. when we write during an APPEND, we will
1615 * adopt the format of the existing archive if none was supplied.
1616 */
1617 if (!(flg & XF) && (act == ARCHIVE))
1618 frmt = &(fsub[F_BCPIO]);
1619
1620 /*
1621 * process the args as they are interpreted by the operation mode
1622 */
1623 switch (act) {
1624 case LIST:
1625 case EXTRACT:
1626 for (; optind < argc; optind++)
1627 if (pat_add(argv[optind], 0) < 0)
1628 cpio_usage();
1629 break;
1630 case COPY:
1631 if (optind >= argc) {
1632 tty_warn(0, "Destination directory was not supplied");
1633 cpio_usage();
1634 }
1635 --argc;
1636 dirptr = argv[argc];
1637 /* FALLTHROUGH */
1638 case ARCHIVE:
1639 case APPND:
1640 if (argc != optind) {
1641 for (; optind < argc; optind++)
1642 if (ftree_add(argv[optind], 0) < 0)
1643 cpio_usage();
1644 break;
1645 }
1646 /*
1647 * no read errors allowed on updates/append operation!
1648 */
1649 maxflt = 0;
1650 while ((str = getline(stdin)) != NULL) {
1651 ftree_add(str, NULL);
1652 }
1653 if (getline_error) {
1654 tty_warn(1, "Problem while reading stdin");
1655 cpio_usage();
1656 }
1657 break;
1658 default:
1659 cpio_usage();
1660 break;
1661 }
1662 }
1663
1664 /*
1665 * printflg()
1666 * print out those invalid flag sets found to the user
1667 */
1668
1669 static void
1670 printflg(unsigned int flg)
1671 {
1672 int nxt;
1673 int pos = 0;
1674
1675 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1676 while ((nxt = ffs(flg)) != 0) {
1677 flg = flg >> nxt;
1678 pos += nxt;
1679 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1680 }
1681 (void)putc('\n', stderr);
1682 }
1683
1684 /*
1685 * c_frmt()
1686 * comparison routine used by bsearch to find the format specified
1687 * by the user
1688 */
1689
1690 static int
1691 c_frmt(const void *a, const void *b)
1692 {
1693 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1694 }
1695
1696 /*
1697 * opt_next()
1698 * called by format specific options routines to get each format specific
1699 * flag and value specified with -o
1700 * Return:
1701 * pointer to next OPLIST entry or NULL (end of list).
1702 */
1703
1704 OPLIST *
1705 opt_next(void)
1706 {
1707 OPLIST *opt;
1708
1709 if ((opt = ophead) != NULL)
1710 ophead = ophead->fow;
1711 return(opt);
1712 }
1713
1714 /*
1715 * bad_opt()
1716 * generic routine used to complain about a format specific options
1717 * when the format does not support options.
1718 */
1719
1720 int
1721 bad_opt(void)
1722 {
1723 OPLIST *opt;
1724
1725 if (ophead == NULL)
1726 return(0);
1727 /*
1728 * print all we were given
1729 */
1730 tty_warn(1," These format options are not supported for %s",
1731 frmt->name);
1732 while ((opt = opt_next()) != NULL)
1733 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1734 if (strcmp(NM_TAR, argv0) == 0)
1735 tar_usage();
1736 else if (strcmp(NM_CPIO, argv0) == 0)
1737 cpio_usage();
1738 else
1739 pax_usage();
1740 return(0);
1741 }
1742
1743 /*
1744 * opt_add()
1745 * breaks the value supplied to -o into a option name and value. options
1746 * are given to -o in the form -o name-value,name=value
1747 * multiple -o may be specified.
1748 * Return:
1749 * 0 if format in name=value format, -1 if -o is passed junk
1750 */
1751
1752 int
1753 opt_add(const char *str)
1754 {
1755 OPLIST *opt;
1756 char *frpt;
1757 char *pt;
1758 char *endpt;
1759 char *dstr;
1760
1761 if ((str == NULL) || (*str == '\0')) {
1762 tty_warn(0, "Invalid option name");
1763 return(-1);
1764 }
1765 if ((dstr = strdup(str)) == NULL) {
1766 tty_warn(0, "Unable to allocate space for option list");
1767 return(-1);
1768 }
1769 frpt = endpt = dstr;
1770
1771 /*
1772 * break into name and values pieces and stuff each one into a
1773 * OPLIST structure. When we know the format, the format specific
1774 * option function will go through this list
1775 */
1776 while ((frpt != NULL) && (*frpt != '\0')) {
1777 if ((endpt = strchr(frpt, ',')) != NULL)
1778 *endpt = '\0';
1779 if ((pt = strchr(frpt, '=')) == NULL) {
1780 tty_warn(0, "Invalid options format");
1781 free(dstr);
1782 return(-1);
1783 }
1784 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1785 tty_warn(0, "Unable to allocate space for option list");
1786 free(dstr);
1787 return(-1);
1788 }
1789 *pt++ = '\0';
1790 opt->name = frpt;
1791 opt->value = pt;
1792 opt->fow = NULL;
1793 if (endpt != NULL)
1794 frpt = endpt + 1;
1795 else
1796 frpt = NULL;
1797 if (ophead == NULL) {
1798 optail = ophead = opt;
1799 continue;
1800 }
1801 optail->fow = opt;
1802 optail = opt;
1803 }
1804 return(0);
1805 }
1806
1807 /*
1808 * str_offt()
1809 * Convert an expression of the following forms to an off_t > 0.
1810 * 1) A positive decimal number.
1811 * 2) A positive decimal number followed by a b (mult by 512).
1812 * 3) A positive decimal number followed by a k (mult by 1024).
1813 * 4) A positive decimal number followed by a m (mult by 512).
1814 * 5) A positive decimal number followed by a w (mult by sizeof int)
1815 * 6) Two or more positive decimal numbers (with/without k,b or w).
1816 * separated by x (also * for backwards compatibility), specifying
1817 * the product of the indicated values.
1818 * Return:
1819 * 0 for an error, a positive value o.w.
1820 */
1821
1822 static off_t
1823 str_offt(char *val)
1824 {
1825 char *expr;
1826 off_t num, t;
1827
1828 num = STRTOOFFT(val, &expr, 0);
1829 if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
1830 return(0);
1831
1832 switch(*expr) {
1833 case 'b':
1834 t = num;
1835 num *= 512;
1836 if (t > num)
1837 return(0);
1838 ++expr;
1839 break;
1840 case 'k':
1841 t = num;
1842 num *= 1024;
1843 if (t > num)
1844 return(0);
1845 ++expr;
1846 break;
1847 case 'm':
1848 t = num;
1849 num *= 1048576;
1850 if (t > num)
1851 return(0);
1852 ++expr;
1853 break;
1854 case 'w':
1855 t = num;
1856 num *= sizeof(int);
1857 if (t > num)
1858 return(0);
1859 ++expr;
1860 break;
1861 }
1862
1863 switch(*expr) {
1864 case '\0':
1865 break;
1866 case '*':
1867 case 'x':
1868 t = num;
1869 num *= str_offt(expr + 1);
1870 if (t > num)
1871 return(0);
1872 break;
1873 default:
1874 return(0);
1875 }
1876 return(num);
1877 }
1878
1879 char *
1880 getline(FILE *f)
1881 {
1882 char *name, *temp;
1883 size_t len;
1884
1885 name = fgetln(f, &len);
1886 if (!name) {
1887 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1888 return(0);
1889 }
1890 if (name[len-1] != '\n')
1891 len++;
1892 temp = malloc(len);
1893 if (!temp) {
1894 getline_error = GETLINE_OUT_OF_MEM;
1895 return(0);
1896 }
1897 memcpy(temp, name, len-1);
1898 temp[len-1] = 0;
1899 return(temp);
1900 }
1901
1902 /*
1903 * no_op()
1904 * for those option functions where the archive format has nothing to do.
1905 * Return:
1906 * 0
1907 */
1908
1909 static int
1910 no_op(void)
1911 {
1912 return(0);
1913 }
1914
1915 /*
1916 * pax_usage()
1917 * print the usage summary to the user
1918 */
1919
1920 void
1921 pax_usage(void)
1922 {
1923 fprintf(stderr,
1924 "Usage: pax [-cdnvzO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
1925 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
1926 " [pattern ...]\n");
1927 fprintf(stderr,
1928 " pax -r [-cdiknuvzADOYZ] [-E limit] [-f archive] [-N dbdir]\n"
1929 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
1930 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
1931 fprintf(stderr,
1932 " pax -w [-dituvzAHLMOPX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
1933 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
1934 " [-U user] ... [-G group] ...\n"
1935 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
1936 fprintf(stderr,
1937 " pax -r -w [-diklntuvzADHLMOPXYZ] [-N dbdir] [-p string] ...\n"
1938 " [-s replstr] ... [-U user] ... [-G group] ...\n"
1939 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
1940 exit(1);
1941 /* NOTREACHED */
1942 }
1943
1944 /*
1945 * tar_usage()
1946 * print the usage summary to the user
1947 */
1948
1949 void
1950 tar_usage(void)
1951 {
1952 (void)fputs("Usage: tar [-]{crtux}[-befhmopqvwzHLOPXZ014578] [archive] "
1953 "[blocksize]\n"
1954 " [-C directory] [-T file] [-s replstr] "
1955 "[file ...]\n", stderr);
1956 exit(1);
1957 /* NOTREACHED */
1958 }
1959
1960 /*
1961 * cpio_usage()
1962 * print the usage summary to the user
1963 */
1964
1965 void
1966 cpio_usage(void)
1967 {
1968
1969 (void)fputs("Usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
1970 "[-H format] [-O archive]\n"
1971 " < name-list [> archive]\n"
1972 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
1973 "[-F archive] [-H format] \n"
1974 " [-I archive] "
1975 "[pattern ...] [< archive]\n"
1976 " cpio -p [-adlLmuv] destination-directory "
1977 "< name-list\n", stderr);
1978 exit(1);
1979 /* NOTREACHED */
1980 }
1981