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