options.c revision 1.56 1 /* $NetBSD: options.c,v 1.56 2002/11/27 18:40:34 grant 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.56 2002/11/27 18:40:34 grant 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 #if 0 /* Not implemented */
719 { "catenate", no_argument, 0, 'A' }, /* F */
720 { "concatenate", no_argument, 0, 'A' }, /* F */
721 { "diff", no_argument, 0, 'd' }, /* F */
722 { "compare", no_argument, 0, 'd' }, /* F */
723 { "checkpoint", no_argument, 0,
724 OPT_CHECKPOINT },
725 { "help", no_argument, 0,
726 OPT_HELP },
727 { "info-script", required_argument, 0, 'F' },
728 { "new-volume-script", required_argument, 0, 'F' },
729 { "incremental", no_argument, 0, 'G' },
730 { "listed-incremental", required_argument, 0, 'g' },
731 { "ignore-zeros", no_argument, 0, 'i' },
732 { "ignore-failed-read", no_argument, 0,
733 OPT_IGNORE_FAILED_READ },
734 { "keep-old-files", no_argument, 0, 'k' },
735 { "starting-file", no_argument, 0, 'K' },
736 /* XXX: Conflicts with current -L */
737 { "tape-length", required_argument, 0, 'L' },
738 { "multi-volume", no_argument, 0, 'M' },
739 { "after-date", required_argument, 0, 'N' },
740 { "newer", required_argument, 0, 'N' },
741 { "to-stdout", no_argument, 0, 'O' },
742 { "record-number", no_argument, 0, 'R' },
743 { "remove-files", no_argument, 0,
744 OPT_REMOVE_FILES },
745 { "same-order", no_argument, 0, 's' },
746 { "preserve-order", no_argument, 0, 's' },
747 { "sparse", no_argument, 0, 'S' },
748 { "null", no_argument, 0,
749 OPT_NULL },
750 { "totals", no_argument, 0,
751 OPT_TOTALS },
752 { "volume-name", required_argument, 0, 'V' },
753 { "label", required_argument, 0, 'V' },
754 { "version", no_argument, 0,
755 OPT_VERSION },
756 { "verify", no_argument, 0, 'W' },
757 { "exclude", required_argument, 0,
758 OPT_EXCLUDE },
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:LOPT: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 command line symlinks only
835 */
836 Hflag = 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 'L':
967 /*
968 * follow symlinks
969 */
970 Lflag = 1;
971 break;
972 case 'P':
973 /*
974 * do not remove leading '/' from pathnames
975 */
976 rmleadslash = 0;
977 Aflag = 1;
978 break;
979 case 'X':
980 /*
981 * GNU tar compat: exclude the files listed in optarg
982 */
983 if (tar_gnutar_X_compat(optarg) != 0)
984 tar_usage();
985 break;
986 case 'Z':
987 /*
988 * use compress.
989 */
990 zflag = 1;
991 gzip_program = COMPRESS_CMD;
992 break;
993 case '0':
994 arcname = DEV_0;
995 break;
996 case '1':
997 arcname = DEV_1;
998 break;
999 case '4':
1000 arcname = DEV_4;
1001 break;
1002 case '5':
1003 arcname = DEV_5;
1004 break;
1005 case '7':
1006 arcname = DEV_7;
1007 break;
1008 case '8':
1009 arcname = DEV_8;
1010 break;
1011 case OPT_ATIME_PRESERVE:
1012 patime = 1;
1013 break;
1014 case OPT_UNLINK:
1015 /* Just ignore -- we always unlink first. */
1016 break;
1017 case OPT_USE_COMPRESS_PROGRAM:
1018 zflag = 1;
1019 gzip_program = optarg;
1020 break;
1021 case OPT_FORCE_LOCAL:
1022 forcelocal = 1;
1023 break;
1024 case OPT_INSECURE:
1025 secure = 0;
1026 break;
1027 case OPT_STRICT:
1028 /* disable gnu extensions */
1029 is_gnutar = 0;
1030 break;
1031 default:
1032 tar_usage();
1033 break;
1034 }
1035 }
1036 argc -= optind;
1037 argv += optind;
1038
1039 /* Tar requires an action. */
1040 if (act == ERROR)
1041 tar_usage();
1042
1043 /* Traditional tar behaviour (pax uses stderr unless in list mode) */
1044 if (fstdin == 1 && act == ARCHIVE)
1045 listf = stderr;
1046 else
1047 listf = stdout;
1048
1049 /* Traditional tar behaviour (pax wants to read file list from stdin) */
1050 if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
1051 exit(0);
1052 /*
1053 * if we are writing (ARCHIVE) specify tar, otherwise run like pax
1054 * (unless -o specified)
1055 */
1056 if (act == ARCHIVE || act == APPND)
1057 frmt = &(fsub[Oflag ? F_TAR : F_USTAR]);
1058 else if (Oflag) {
1059 tty_warn(1, "The -O/-o options are only valid when writing an archive");
1060 tar_usage(); /* only valid when writing */
1061 }
1062
1063 /*
1064 * process the args as they are interpreted by the operation mode
1065 */
1066 switch (act) {
1067 case LIST:
1068 case EXTRACT:
1069 default:
1070 {
1071 int sawpat = 0;
1072 int dirisnext = 0;
1073 char *file, *dir;
1074
1075 while (nincfiles || *argv != NULL) {
1076 /*
1077 * If we queued up any include files,
1078 * pull them in now. Otherwise, check
1079 * for -I and -C positional flags.
1080 * Anything else must be a file to
1081 * extract.
1082 */
1083 if (nincfiles) {
1084 file = incfiles->file;
1085 dir = incfiles->dir;
1086 incfiles++;
1087 nincfiles--;
1088 } else if (strcmp(*argv, "-I") == 0) {
1089 if (*++argv == NULL)
1090 break;
1091 file = *argv++;
1092 dir = chdname;
1093 } else
1094 file = NULL;
1095 if (file != NULL) {
1096 FILE *fp;
1097 char *str;
1098
1099 if (strcmp(file, "-") == 0)
1100 fp = stdin;
1101 else if ((fp = fopen(file, "r")) == NULL) {
1102 tty_warn(1, "Unable to open file '%s' for read", file);
1103 tar_usage();
1104 }
1105 while ((str = getline(fp)) != NULL) {
1106 if (dirisnext) {
1107 dir = str;
1108 dirisnext = 0;
1109 continue;
1110 }
1111 if (strcmp(str, "-C") == 0) {
1112 dirisnext = 1;
1113 continue;
1114 }
1115 if (pat_add(str, dir) < 0)
1116 tar_usage();
1117 sawpat = 1;
1118 }
1119 /* Bomb if given -C w/out a dir. */
1120 if (dirisnext)
1121 tar_usage();
1122 if (strcmp(file, "-") != 0)
1123 fclose(fp);
1124 if (getline_error) {
1125 tty_warn(1, "Problem with file '%s'", file);
1126 tar_usage();
1127 }
1128 } else if (strcmp(*argv, "-C") == 0) {
1129 if (*++argv == NULL)
1130 break;
1131 chdname = *argv++;
1132 } else if (pat_add(*argv++, chdname) < 0)
1133 tar_usage();
1134 else
1135 sawpat = 1;
1136 }
1137 /*
1138 * if patterns were added, we are doing chdir()
1139 * on a file-by-file basis, else, just one
1140 * global chdir (if any) after opening input.
1141 */
1142 if (sawpat > 0)
1143 chdname = NULL;
1144 }
1145 break;
1146 case ARCHIVE:
1147 case APPND:
1148 if (chdname != NULL) { /* initial chdir() */
1149 if (ftree_add(chdname, 1) < 0)
1150 tar_usage();
1151 }
1152
1153 while (nincfiles || *argv != NULL) {
1154 char *file, *dir;
1155
1156 /*
1157 * If we queued up any include files, pull them in
1158 * now. Otherwise, check for -I and -C positional
1159 * flags. Anything else must be a file to include
1160 * in the archive.
1161 */
1162 if (nincfiles) {
1163 file = incfiles->file;
1164 dir = incfiles->dir;
1165 incfiles++;
1166 nincfiles--;
1167 } else if (strcmp(*argv, "-I") == 0) {
1168 if (*++argv == NULL)
1169 break;
1170 file = *argv++;
1171 dir = NULL;
1172 } else
1173 file = NULL;
1174 if (file != NULL) {
1175 FILE *fp;
1176 char *str;
1177 int dirisnext = 0;
1178
1179 /* Set directory if needed */
1180 if (dir) {
1181 if (ftree_add(dir, 1) < 0)
1182 tar_usage();
1183 }
1184
1185 if (strcmp(file, "-") == 0)
1186 fp = stdin;
1187 else if ((fp = fopen(file, "r")) == NULL) {
1188 tty_warn(1, "Unable to open file '%s' for read", file);
1189 tar_usage();
1190 }
1191 while ((str = getline(fp)) != NULL) {
1192 if (dirisnext) {
1193 if (ftree_add(str, 1) < 0)
1194 tar_usage();
1195 dirisnext = 0;
1196 continue;
1197 }
1198 if (strcmp(str, "-C") == 0) {
1199 dirisnext = 1;
1200 continue;
1201 }
1202 if (ftree_add(str, 0) < 0)
1203 tar_usage();
1204 }
1205 /* Bomb if given -C w/out a dir. */
1206 if (dirisnext)
1207 tar_usage();
1208 if (strcmp(file, "-") != 0)
1209 fclose(fp);
1210 if (getline_error) {
1211 tty_warn(1, "Problem with file '%s'",
1212 file);
1213 tar_usage();
1214 }
1215 } else if (strcmp(*argv, "-C") == 0) {
1216 if (*++argv == NULL)
1217 break;
1218 if (ftree_add(*argv++, 1) < 0)
1219 tar_usage();
1220 } else if (ftree_add(*argv++, 0) < 0)
1221 tar_usage();
1222 }
1223 /*
1224 * no read errors allowed on updates/append operation!
1225 */
1226 maxflt = 0;
1227 break;
1228 }
1229 if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
1230 arcname = getenv("TAPE");
1231 if ((arcname == NULL) || (*arcname == '\0'))
1232 arcname = _PATH_DEFTAPE;
1233 }
1234 }
1235
1236 int
1237 mkpath(path)
1238 char *path;
1239 {
1240 struct stat sb;
1241 char *slash;
1242 int done = 0;
1243
1244 slash = path;
1245
1246 while (!done) {
1247 slash += strspn(slash, "/");
1248 slash += strcspn(slash, "/");
1249
1250 done = (*slash == '\0');
1251 *slash = '\0';
1252
1253 if (stat(path, &sb)) {
1254 if (errno != ENOENT || mkdir(path, 0777)) {
1255 tty_warn(1, "%s", path);
1256 return (-1);
1257 }
1258 } else if (!S_ISDIR(sb.st_mode)) {
1259 syswarn(1, ENOTDIR, "%s", path);
1260 return (-1);
1261 }
1262
1263 if (!done)
1264 *slash = '/';
1265 }
1266
1267 return (0);
1268 }
1269
1270
1271 struct option cpio_longopts[] = {
1272 { "reset-access-time", no_argument, 0, 'a' },
1273 { "make-directories", no_argument, 0, 'd' },
1274 { "nonmatching", no_argument, 0, 'f' },
1275 { "extract", no_argument, 0, 'i' },
1276 { "link", no_argument, 0, 'l' },
1277 { "preserve-modification-time", no_argument, 0, 'm' },
1278 { "create", no_argument, 0, 'o' },
1279 { "pass-through", no_argument, 0, 'p' },
1280 { "rename", no_argument, 0, 'r' },
1281 { "list", no_argument, 0, 't' },
1282 { "unconditional", no_argument, 0, 'u' },
1283 { "verbose", no_argument, 0, 'v' },
1284 { "append", no_argument, 0, 'A' },
1285 { "pattern-file", required_argument, 0, 'E' },
1286 { "file", required_argument, 0, 'F' },
1287 { "force-local", no_argument, 0,
1288 OPT_FORCE_LOCAL },
1289 { "format", required_argument, 0, 'H' },
1290 { "dereference", no_argument, 0, 'L' },
1291 { "swap-halfwords", no_argument, 0, 'S' },
1292 { "insecure", no_argument, 0,
1293 OPT_INSECURE },
1294
1295 #ifdef notyet
1296 /* Not implemented */
1297 { "null", no_argument, 0, '0' },
1298 { "swap", no_argument, 0, 'b' },
1299 { "numeric-uid-gid", no_argument, 0, 'n' },
1300 { "swap-bytes", no_argument, 0, 's' },
1301 { "message", required_argument, 0, 'M' },
1302 { "owner", required_argument, 0 'R' },
1303 { "dot", no_argument, 0, 'V' },
1304 { "block-size", required_argument, 0,
1305 OPT_BLOCK_SIZE },
1306 { "no-absolute-pathnames", no_argument, 0,
1307 OPT_NO_ABSOLUTE_PATHNAMES },
1308 { "no-preserve-owner", no_argument, 0,
1309 OPT_NO_PRESERVE_OWNER },
1310 { "only-verify-crc", no_argument, 0,
1311 OPT_ONLY_VERIFY_CRC },
1312 { "rsh-command", required_argument, 0,
1313 OPT_RSH_COMMAND },
1314 { "sparce", no_argument, 0,
1315 OPT_SPARSE },
1316 { "version", no_argument, 0,
1317 OPT_VERSION },
1318 #endif
1319 };
1320
1321 /*
1322 * cpio_options()
1323 * look at the user specified flags. set globals as required and check if
1324 * the user specified a legal set of flags. If not, complain and exit
1325 */
1326
1327 static void
1328 cpio_options(int argc, char **argv)
1329 {
1330 FSUB tmp;
1331 unsigned int flg = 0;
1332 unsigned int bflg = 0;
1333 int c, i;
1334 FILE *fp;
1335 char *str;
1336
1337 uflag = 1;
1338 kflag = 1;
1339 pids = 1;
1340 pmode = 1;
1341 pmtime = 0;
1342 arcname = NULL;
1343 dflag = 1;
1344 nodirs = 1;
1345 /*
1346 * process option flags
1347 */
1348 while ((c = getoldopt(argc, argv,
1349 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1350 cpio_longopts, NULL)) != -1) {
1351 switch(c) {
1352 case 'a':
1353 /*
1354 * preserve access time on filesystem nodes we read
1355 */
1356 tflag = 1;
1357 flg |= TF;
1358 break;
1359 #ifdef notyet
1360 case 'b':
1361 /*
1362 * swap bytes and half-words when reading data
1363 */
1364 break;
1365 #endif
1366 case 'c':
1367 /*
1368 * ASCII cpio header
1369 */
1370 frmt = &fsub[F_SV4CPIO];
1371 break;
1372 case 'd':
1373 /*
1374 * create directories as needed
1375 * pax does this by default ..
1376 */
1377 nodirs = 0;
1378 flg |= RF;
1379 break;
1380 case 'f':
1381 /*
1382 * inverse match on patterns
1383 */
1384 cflag = 1;
1385 flg |= CF;
1386 break;
1387 case 'i':
1388 /*
1389 * read the archive
1390 */
1391 act = EXTRACT;
1392 flg |= RF;
1393 break;
1394 #ifdef notyet
1395 case 'k':
1396 break;
1397 #endif
1398 case 'l':
1399 /*
1400 * try to link src to dest with copy (-rw)
1401 */
1402 lflag = 1;
1403 flg |= LF;
1404 break;
1405 case 'm':
1406 /*
1407 * preserve mtime
1408 */
1409 flg |= PF;
1410 pmtime = 1;
1411 break;
1412 case 'o':
1413 /*
1414 * write an archive
1415 */
1416 act = ARCHIVE;
1417 frmt = &(fsub[F_SV4CRC]);
1418 flg |= WF;
1419 break;
1420 case 'p':
1421 /*
1422 * cpio -p is like pax -rw
1423 */
1424 act = COPY;
1425 flg |= RF | WF;
1426 break;
1427 case 'r':
1428 /*
1429 * interactive file rename
1430 */
1431 iflag = 1;
1432 flg |= IF;
1433 break;
1434 #ifdef notyet
1435 case 's':
1436 /*
1437 * swap bytes after reading data
1438 */
1439 break;
1440 #endif
1441 case 't':
1442 /*
1443 * list contents of archive
1444 */
1445 act = LIST;
1446 listf = stdout;
1447 break;
1448 case 'u':
1449 /*
1450 * don't ignore those older files
1451 */
1452 uflag = 0;
1453 kflag = 0;
1454 flg |= UF;
1455 break;
1456 case 'v':
1457 /*
1458 * verbose operation mode
1459 */
1460 vflag = 1;
1461 flg |= VF;
1462 break;
1463 case 'z':
1464 /*
1465 * use gzip. Non standard option.
1466 */
1467 gzip_program = GZIP_CMD;
1468 break;
1469 case 'A':
1470 /*
1471 * append to an archive
1472 */
1473 act = APPND;
1474 flg |= AF;
1475 break;
1476 case 'B':
1477 /*
1478 * set blocksize to 5120
1479 */
1480 blksz = 5120;
1481 break;
1482 case 'C':
1483 /*
1484 * specify blocksize
1485 */
1486 if ((blksz = (int)str_offt(optarg)) <= 0) {
1487 tty_warn(1, "Invalid block size %s", optarg);
1488 tar_usage();
1489 }
1490 break;
1491 case 'E':
1492 /*
1493 * file with patterns to extract or list
1494 */
1495 if ((fp = fopen(optarg, "r")) == NULL) {
1496 tty_warn(1, "Unable to open file '%s' for read",
1497 optarg);
1498 cpio_usage();
1499 }
1500 while ((str = getline(fp)) != NULL) {
1501 pat_add(str, NULL);
1502 }
1503 fclose(fp);
1504 if (getline_error) {
1505 tty_warn(1, "Problem with file '%s'", optarg);
1506 cpio_usage();
1507 }
1508 break;
1509 case 'H':
1510 /*
1511 * specify an archive format on write
1512 */
1513 tmp.name = optarg;
1514 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1515 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
1516 if (frmt != NULL) {
1517 flg |= XF;
1518 break;
1519 }
1520 tty_warn(1, "Unknown -H format: %s", optarg);
1521 (void)fputs("cpio: Known -H formats are:", stderr);
1522 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1523 (void)fprintf(stderr, " %s", fsub[i].name);
1524 (void)fputs("\n\n", stderr);
1525 tar_usage();
1526 break;
1527 case 'I':
1528 case 'O':
1529 /*
1530 * filename where the archive is stored
1531 */
1532 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1533 /*
1534 * treat a - as stdin
1535 */
1536 arcname = NULL;
1537 break;
1538 }
1539 arcname = optarg;
1540 break;
1541 case 'L':
1542 /*
1543 * follow symlinks
1544 */
1545 Lflag = 1;
1546 flg |= CLF;
1547 break;
1548 #ifdef notyet
1549 case 'M':
1550 arg = optarg;
1551 break;
1552 case 'R':
1553 arg = optarg;
1554 break;
1555 #endif
1556 case 'S':
1557 /*
1558 * swap halfwords after reading data
1559 */
1560 cpio_swp_head = 1;
1561 break;
1562 #ifdef notyet
1563 case 'V':
1564 break;
1565 #endif
1566 case 'Z':
1567 /*
1568 * use compress. Non standard option.
1569 */
1570 gzip_program = COMPRESS_CMD;
1571 break;
1572 case '6':
1573 /*
1574 * process Version 6 cpio format
1575 */
1576 frmt = &(fsub[F_BCPIO]);
1577 case OPT_FORCE_LOCAL:
1578 forcelocal = 1;
1579 break;
1580 case OPT_INSECURE:
1581 secure = 0;
1582 break;
1583 default:
1584 cpio_usage();
1585 break;
1586 }
1587 }
1588
1589 /*
1590 * figure out the operation mode of cpio. check that we have not been
1591 * given a bogus set of flags for the operation mode.
1592 */
1593 if (ISLIST(flg)) {
1594 act = LIST;
1595 bflg = flg & BDLIST;
1596 } else if (ISEXTRACT(flg)) {
1597 act = EXTRACT;
1598 bflg = flg & BDEXTR;
1599 } else if (ISARCHIVE(flg)) {
1600 act = ARCHIVE;
1601 bflg = flg & BDARCH;
1602 } else if (ISAPPND(flg)) {
1603 act = APPND;
1604 bflg = flg & BDARCH;
1605 } else if (ISCOPY(flg)) {
1606 act = COPY;
1607 bflg = flg & BDCOPY;
1608 } else
1609 cpio_usage();
1610 if (bflg) {
1611 cpio_usage();
1612 }
1613
1614 /*
1615 * if we are writing (ARCHIVE) we use the default format if the user
1616 * did not specify a format. when we write during an APPEND, we will
1617 * adopt the format of the existing archive if none was supplied.
1618 */
1619 if (!(flg & XF) && (act == ARCHIVE))
1620 frmt = &(fsub[F_BCPIO]);
1621
1622 /*
1623 * process the args as they are interpreted by the operation mode
1624 */
1625 switch (act) {
1626 case LIST:
1627 case EXTRACT:
1628 for (; optind < argc; optind++)
1629 if (pat_add(argv[optind], 0) < 0)
1630 cpio_usage();
1631 break;
1632 case COPY:
1633 if (optind >= argc) {
1634 tty_warn(0, "Destination directory was not supplied");
1635 cpio_usage();
1636 }
1637 --argc;
1638 dirptr = argv[argc];
1639 /* FALLTHROUGH */
1640 case ARCHIVE:
1641 case APPND:
1642 if (argc != optind) {
1643 for (; optind < argc; optind++)
1644 if (ftree_add(argv[optind], 0) < 0)
1645 cpio_usage();
1646 break;
1647 }
1648 /*
1649 * no read errors allowed on updates/append operation!
1650 */
1651 maxflt = 0;
1652 while ((str = getline(stdin)) != NULL) {
1653 ftree_add(str, NULL);
1654 }
1655 if (getline_error) {
1656 tty_warn(1, "Problem while reading stdin");
1657 cpio_usage();
1658 }
1659 break;
1660 default:
1661 cpio_usage();
1662 break;
1663 }
1664 }
1665
1666 /*
1667 * printflg()
1668 * print out those invalid flag sets found to the user
1669 */
1670
1671 static void
1672 printflg(unsigned int flg)
1673 {
1674 int nxt;
1675 int pos = 0;
1676
1677 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1678 while ((nxt = ffs(flg)) != 0) {
1679 flg = flg >> nxt;
1680 pos += nxt;
1681 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1682 }
1683 (void)putc('\n', stderr);
1684 }
1685
1686 /*
1687 * c_frmt()
1688 * comparison routine used by bsearch to find the format specified
1689 * by the user
1690 */
1691
1692 static int
1693 c_frmt(const void *a, const void *b)
1694 {
1695 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1696 }
1697
1698 /*
1699 * opt_next()
1700 * called by format specific options routines to get each format specific
1701 * flag and value specified with -o
1702 * Return:
1703 * pointer to next OPLIST entry or NULL (end of list).
1704 */
1705
1706 OPLIST *
1707 opt_next(void)
1708 {
1709 OPLIST *opt;
1710
1711 if ((opt = ophead) != NULL)
1712 ophead = ophead->fow;
1713 return(opt);
1714 }
1715
1716 /*
1717 * bad_opt()
1718 * generic routine used to complain about a format specific options
1719 * when the format does not support options.
1720 */
1721
1722 int
1723 bad_opt(void)
1724 {
1725 OPLIST *opt;
1726
1727 if (ophead == NULL)
1728 return(0);
1729 /*
1730 * print all we were given
1731 */
1732 tty_warn(1," These format options are not supported for %s",
1733 frmt->name);
1734 while ((opt = opt_next()) != NULL)
1735 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1736 if (strcmp(NM_TAR, argv0) == 0)
1737 tar_usage();
1738 else if (strcmp(NM_CPIO, argv0) == 0)
1739 cpio_usage();
1740 else
1741 pax_usage();
1742 return(0);
1743 }
1744
1745 /*
1746 * opt_add()
1747 * breaks the value supplied to -o into a option name and value. options
1748 * are given to -o in the form -o name-value,name=value
1749 * multiple -o may be specified.
1750 * Return:
1751 * 0 if format in name=value format, -1 if -o is passed junk
1752 */
1753
1754 int
1755 opt_add(const char *str)
1756 {
1757 OPLIST *opt;
1758 char *frpt;
1759 char *pt;
1760 char *endpt;
1761 char *dstr;
1762
1763 if ((str == NULL) || (*str == '\0')) {
1764 tty_warn(0, "Invalid option name");
1765 return(-1);
1766 }
1767 if ((dstr = strdup(str)) == NULL) {
1768 tty_warn(0, "Unable to allocate space for option list");
1769 return(-1);
1770 }
1771 frpt = endpt = dstr;
1772
1773 /*
1774 * break into name and values pieces and stuff each one into a
1775 * OPLIST structure. When we know the format, the format specific
1776 * option function will go through this list
1777 */
1778 while ((frpt != NULL) && (*frpt != '\0')) {
1779 if ((endpt = strchr(frpt, ',')) != NULL)
1780 *endpt = '\0';
1781 if ((pt = strchr(frpt, '=')) == NULL) {
1782 tty_warn(0, "Invalid options format");
1783 free(dstr);
1784 return(-1);
1785 }
1786 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1787 tty_warn(0, "Unable to allocate space for option list");
1788 free(dstr);
1789 return(-1);
1790 }
1791 *pt++ = '\0';
1792 opt->name = frpt;
1793 opt->value = pt;
1794 opt->fow = NULL;
1795 if (endpt != NULL)
1796 frpt = endpt + 1;
1797 else
1798 frpt = NULL;
1799 if (ophead == NULL) {
1800 optail = ophead = opt;
1801 continue;
1802 }
1803 optail->fow = opt;
1804 optail = opt;
1805 }
1806 return(0);
1807 }
1808
1809 /*
1810 * str_offt()
1811 * Convert an expression of the following forms to an off_t > 0.
1812 * 1) A positive decimal number.
1813 * 2) A positive decimal number followed by a b (mult by 512).
1814 * 3) A positive decimal number followed by a k (mult by 1024).
1815 * 4) A positive decimal number followed by a m (mult by 512).
1816 * 5) A positive decimal number followed by a w (mult by sizeof int)
1817 * 6) Two or more positive decimal numbers (with/without k,b or w).
1818 * separated by x (also * for backwards compatibility), specifying
1819 * the product of the indicated values.
1820 * Return:
1821 * 0 for an error, a positive value o.w.
1822 */
1823
1824 static off_t
1825 str_offt(char *val)
1826 {
1827 char *expr;
1828 off_t num, t;
1829
1830 num = STRTOOFFT(val, &expr, 0);
1831 if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
1832 return(0);
1833
1834 switch(*expr) {
1835 case 'b':
1836 t = num;
1837 num *= 512;
1838 if (t > num)
1839 return(0);
1840 ++expr;
1841 break;
1842 case 'k':
1843 t = num;
1844 num *= 1024;
1845 if (t > num)
1846 return(0);
1847 ++expr;
1848 break;
1849 case 'm':
1850 t = num;
1851 num *= 1048576;
1852 if (t > num)
1853 return(0);
1854 ++expr;
1855 break;
1856 case 'w':
1857 t = num;
1858 num *= sizeof(int);
1859 if (t > num)
1860 return(0);
1861 ++expr;
1862 break;
1863 }
1864
1865 switch(*expr) {
1866 case '\0':
1867 break;
1868 case '*':
1869 case 'x':
1870 t = num;
1871 num *= str_offt(expr + 1);
1872 if (t > num)
1873 return(0);
1874 break;
1875 default:
1876 return(0);
1877 }
1878 return(num);
1879 }
1880
1881 char *
1882 getline(FILE *f)
1883 {
1884 char *name, *temp;
1885 size_t len;
1886
1887 name = fgetln(f, &len);
1888 if (!name) {
1889 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1890 return(0);
1891 }
1892 if (name[len-1] != '\n')
1893 len++;
1894 temp = malloc(len);
1895 if (!temp) {
1896 getline_error = GETLINE_OUT_OF_MEM;
1897 return(0);
1898 }
1899 memcpy(temp, name, len-1);
1900 temp[len-1] = 0;
1901 return(temp);
1902 }
1903
1904 /*
1905 * no_op()
1906 * for those option functions where the archive format has nothing to do.
1907 * Return:
1908 * 0
1909 */
1910
1911 static int
1912 no_op(void)
1913 {
1914 return(0);
1915 }
1916
1917 /*
1918 * pax_usage()
1919 * print the usage summary to the user
1920 */
1921
1922 void
1923 pax_usage(void)
1924 {
1925 fprintf(stderr,
1926 "Usage: pax [-cdnvzO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
1927 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
1928 " [pattern ...]\n");
1929 fprintf(stderr,
1930 " pax -r [-cdiknuvzADOYZ] [-E limit] [-f archive] [-N dbdir]\n"
1931 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
1932 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
1933 fprintf(stderr,
1934 " pax -w [-dituvzAHLMOPX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
1935 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
1936 " [-U user] ... [-G group] ...\n"
1937 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
1938 fprintf(stderr,
1939 " pax -r -w [-diklntuvzADHLMOPXYZ] [-N dbdir] [-p string] ...\n"
1940 " [-s replstr] ... [-U user] ... [-G group] ...\n"
1941 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
1942 exit(1);
1943 /* NOTREACHED */
1944 }
1945
1946 /*
1947 * tar_usage()
1948 * print the usage summary to the user
1949 */
1950
1951 void
1952 tar_usage(void)
1953 {
1954 (void)fputs("Usage: tar [-]{crtux}[-befhmopqvwzHLOPXZ014578] [archive] "
1955 "[blocksize]\n"
1956 " [-C directory] [-T file] [-s replstr] "
1957 "[file ...]\n", stderr);
1958 exit(1);
1959 /* NOTREACHED */
1960 }
1961
1962 /*
1963 * cpio_usage()
1964 * print the usage summary to the user
1965 */
1966
1967 void
1968 cpio_usage(void)
1969 {
1970
1971 (void)fputs("Usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
1972 "[-H format] [-O archive]\n"
1973 " < name-list [> archive]\n"
1974 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
1975 "[-F archive] [-H format] \n"
1976 " [-I archive] "
1977 "[pattern ...] [< archive]\n"
1978 " cpio -p [-adlLmuv] destination-directory "
1979 "< name-list\n", stderr);
1980 exit(1);
1981 /* NOTREACHED */
1982 }
1983