options.c revision 1.54 1 /* $NetBSD: options.c,v 1.54 2002/10/17 00:42:02 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.54 2002/10/17 00:42:02 christos Exp $");
46 #endif
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/time.h>
51 #include <sys/stat.h>
52 #include <sys/mtio.h>
53 #include <sys/param.h>
54 #include <ctype.h>
55 #include <errno.h>
56 #include <getopt.h>
57 #include <limits.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <paths.h>
63 #include "pax.h"
64 #include "options.h"
65 #include "cpio.h"
66 #include "tar.h"
67 #include "extern.h"
68 #ifndef SMALL
69 #include "mtree.h"
70 #endif /* SMALL */
71
72 /*
73 * Routines which handle command line options
74 */
75
76 static int nopids; /* tar mode: suppress "pids" for -p option */
77 static char *flgch = FLGCH; /* list of all possible flags (pax) */
78 static OPLIST *ophead = NULL; /* head for format specific options -x */
79 static OPLIST *optail = NULL; /* option tail */
80
81 static int no_op(void);
82 static void printflg(unsigned int);
83 static int c_frmt(const void *, const void *);
84 static off_t str_offt(char *);
85 static char *getline(FILE *fp);
86 static void pax_options(int, char **);
87 static void pax_usage(void);
88 static void tar_options(int, char **);
89 static void tar_usage(void);
90 static void cpio_options(int, char **);
91 static void cpio_usage(void);
92
93 /* errors from getline */
94 #define GETLINE_FILE_CORRUPT 1
95 #define GETLINE_OUT_OF_MEM 2
96 static int getline_error;
97
98 #define GZIP_CMD "gzip" /* command to run as gzip */
99 #define COMPRESS_CMD "compress" /* command to run as compress */
100
101 /*
102 * Long options.
103 */
104 #define OPT_USE_COMPRESS_PROGRAM 0
105 #define OPT_CHECKPOINT 1
106 #define OPT_UNLINK 2
107 #define OPT_HELP 3
108 #define OPT_ATIME_PRESERVE 4
109 #define OPT_IGNORE_FAILED_READ 5
110 #define OPT_REMOVE_FILES 6
111 #define OPT_NULL 7
112 #define OPT_TOTALS 8
113 #define OPT_VERSION 9
114 #define OPT_EXCLUDE 10
115 #define OPT_BLOCK_COMPRESS 11
116 #define OPT_NORECURSE 12
117 #define OPT_FORCE_LOCAL 13
118 #define OPT_INSECURE 14
119 #define OPT_STRICT 15
120
121 /*
122 * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
123 * (see pax.h for description of each function)
124 *
125 * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
126 * read, end_read, st_write, write, end_write, trail,
127 * rd_data, wr_data, options
128 */
129
130 FSUB fsub[] = {
131 /* 0: OLD BINARY CPIO */
132 { "bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
133 bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, NULL,
134 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
135
136 /* 1: OLD OCTAL CHARACTER CPIO */
137 { "cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
138 cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, NULL,
139 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
140
141 /* 2: SVR4 HEX CPIO */
142 { "sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
143 vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, NULL,
144 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
145
146 /* 3: SVR4 HEX CPIO WITH CRC */
147 { "sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
148 vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, NULL,
149 cpio_subtrail, rd_wrfile, wr_rdfile, bad_opt },
150
151 /* 4: OLD TAR */
152 { "tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
153 tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, tar_trail,
154 NULL, rd_wrfile, wr_rdfile, tar_opt },
155
156 /* 5: POSIX USTAR */
157 { "ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
158 ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, tar_trail,
159 NULL, rd_wrfile, wr_rdfile, bad_opt }
160 };
161 #define F_BCPIO 0 /* old binary cpio format */
162 #define F_CPIO 1 /* old octal character cpio format */
163 #define F_SV4CPIO 2 /* SVR4 hex cpio format */
164 #define F_SV4CRC 3 /* SVR4 hex with crc cpio format */
165 #define F_TAR 4 /* old V7 UNIX tar format */
166 #define F_USTAR 5 /* ustar format */
167 #define DEFLT F_USTAR /* default write format from list above */
168
169 /*
170 * ford is the archive search order used by get_arc() to determine what kind
171 * of archive we are dealing with. This helps to properly id archive formats
172 * some formats may be subsets of others....
173 */
174 int ford[] = {F_USTAR, F_TAR, F_SV4CRC, F_SV4CPIO, F_CPIO, F_BCPIO, -1};
175
176 /*
177 * options()
178 * figure out if we are pax, tar or cpio. Call the appropriate options
179 * parser
180 */
181
182 void
183 options(int argc, char **argv)
184 {
185
186 /*
187 * Are we acting like pax, tar or cpio (based on argv[0])
188 */
189 if ((argv0 = strrchr(argv[0], '/')) != NULL)
190 argv0++;
191 else
192 argv0 = argv[0];
193
194 if (strcmp(NM_TAR, argv0) == 0)
195 return(tar_options(argc, argv));
196 else if (strcmp(NM_CPIO, argv0) == 0)
197 return(cpio_options(argc, argv));
198 else {
199 argv0 = NM_PAX;
200 return(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 char *file, *dir;
1073
1074 while (nincfiles || *argv != NULL) {
1075 /*
1076 * If we queued up any include files,
1077 * pull them in now. Otherwise, check
1078 * for -I and -C positional flags.
1079 * Anything else must be a file to
1080 * extract.
1081 */
1082 if (nincfiles) {
1083 file = incfiles->file;
1084 dir = incfiles->dir;
1085 incfiles++;
1086 nincfiles--;
1087 } else if (strcmp(*argv, "-I") == 0) {
1088 if (*++argv == NULL)
1089 break;
1090 file = *argv++;
1091 dir = chdname;
1092 } else
1093 file = NULL;
1094 if (file != NULL) {
1095 FILE *fp;
1096 char *str;
1097
1098 if (strcmp(file, "-") == 0)
1099 fp = stdin;
1100 else if ((fp = fopen(file, "r")) == NULL) {
1101 tty_warn(1, "Unable to open file '%s' for read", file);
1102 tar_usage();
1103 }
1104 while ((str = getline(fp)) != NULL) {
1105 if (pat_add(str, dir) < 0)
1106 tar_usage();
1107 sawpat = 1;
1108 }
1109 if (strcmp(file, "-") != 0)
1110 fclose(fp);
1111 if (getline_error) {
1112 tty_warn(1, "Problem with file '%s'", file);
1113 tar_usage();
1114 }
1115 } else if (strcmp(*argv, "-C") == 0) {
1116 if (*++argv == NULL)
1117 break;
1118 chdname = *argv++;
1119 } else if (pat_add(*argv++, chdname) < 0)
1120 tar_usage();
1121 else
1122 sawpat = 1;
1123 }
1124 /*
1125 * if patterns were added, we are doing chdir()
1126 * on a file-by-file basis, else, just one
1127 * global chdir (if any) after opening input.
1128 */
1129 if (sawpat > 0)
1130 chdname = NULL;
1131 }
1132 break;
1133 case ARCHIVE:
1134 case APPND:
1135 if (chdname != NULL) { /* initial chdir() */
1136 if (ftree_add(chdname, 1) < 0)
1137 tar_usage();
1138 }
1139
1140 while (nincfiles || *argv != NULL) {
1141 char *file, *dir;
1142
1143 /*
1144 * If we queued up any include files, pull them in
1145 * now. Otherwise, check for -I and -C positional
1146 * flags. Anything else must be a file to include
1147 * in the archive.
1148 */
1149 if (nincfiles) {
1150 file = incfiles->file;
1151 dir = incfiles->dir;
1152 incfiles++;
1153 nincfiles--;
1154 } else if (strcmp(*argv, "-I") == 0) {
1155 if (*++argv == NULL)
1156 break;
1157 file = *argv++;
1158 dir = NULL;
1159 } else
1160 file = NULL;
1161 if (file != NULL) {
1162 FILE *fp;
1163 char *str;
1164
1165 /* Set directory if needed */
1166 if (dir) {
1167 if (ftree_add(dir, 1) < 0)
1168 tar_usage();
1169 }
1170
1171 if (strcmp(file, "-") == 0)
1172 fp = stdin;
1173 else if ((fp = fopen(file, "r")) == NULL) {
1174 tty_warn(1, "Unable to open file '%s' for read", file);
1175 tar_usage();
1176 }
1177 while ((str = getline(fp)) != NULL) {
1178 if (ftree_add(str, 0) < 0)
1179 tar_usage();
1180 }
1181 if (strcmp(file, "-") != 0)
1182 fclose(fp);
1183 if (getline_error) {
1184 tty_warn(1, "Problem with file '%s'",
1185 file);
1186 tar_usage();
1187 }
1188 } else if (strcmp(*argv, "-C") == 0) {
1189 if (*++argv == NULL)
1190 break;
1191 if (ftree_add(*argv++, 1) < 0)
1192 tar_usage();
1193 } else if (ftree_add(*argv++, 0) < 0)
1194 tar_usage();
1195 }
1196 /*
1197 * no read errors allowed on updates/append operation!
1198 */
1199 maxflt = 0;
1200 break;
1201 }
1202 if (!fstdin && ((arcname == (char *)NULL) || (*arcname == '\0'))) {
1203 arcname = getenv("TAPE");
1204 if ((arcname == NULL) || (*arcname == '\0'))
1205 arcname = _PATH_DEFTAPE;
1206 }
1207 }
1208
1209 int
1210 mkpath(path)
1211 char *path;
1212 {
1213 struct stat sb;
1214 char *slash;
1215 int done = 0;
1216
1217 slash = path;
1218
1219 while (!done) {
1220 slash += strspn(slash, "/");
1221 slash += strcspn(slash, "/");
1222
1223 done = (*slash == '\0');
1224 *slash = '\0';
1225
1226 if (stat(path, &sb)) {
1227 if (errno != ENOENT || mkdir(path, 0777)) {
1228 tty_warn(1, "%s", path);
1229 return (-1);
1230 }
1231 } else if (!S_ISDIR(sb.st_mode)) {
1232 syswarn(1, ENOTDIR, "%s", path);
1233 return (-1);
1234 }
1235
1236 if (!done)
1237 *slash = '/';
1238 }
1239
1240 return (0);
1241 }
1242
1243
1244 struct option cpio_longopts[] = {
1245 { "reset-access-time", no_argument, 0, 'a' },
1246 { "make-directories", no_argument, 0, 'd' },
1247 { "nonmatching", no_argument, 0, 'f' },
1248 { "extract", no_argument, 0, 'i' },
1249 { "link", no_argument, 0, 'l' },
1250 { "preserve-modification-time", no_argument, 0, 'm' },
1251 { "create", no_argument, 0, 'o' },
1252 { "pass-through", no_argument, 0, 'p' },
1253 { "rename", no_argument, 0, 'r' },
1254 { "list", no_argument, 0, 't' },
1255 { "unconditional", no_argument, 0, 'u' },
1256 { "verbose", no_argument, 0, 'v' },
1257 { "append", no_argument, 0, 'A' },
1258 { "pattern-file", required_argument, 0, 'E' },
1259 { "file", required_argument, 0, 'F' },
1260 { "force-local", no_argument, 0,
1261 OPT_FORCE_LOCAL },
1262 { "format", required_argument, 0, 'H' },
1263 { "dereference", no_argument, 0, 'L' },
1264 { "swap-halfwords", no_argument, 0, 'S' },
1265 { "insecure", no_argument, 0,
1266 OPT_INSECURE },
1267
1268 #ifdef notyet
1269 /* Not implemented */
1270 { "null", no_argument, 0, '0' },
1271 { "swap", no_argument, 0, 'b' },
1272 { "numeric-uid-gid", no_argument, 0, 'n' },
1273 { "swap-bytes", no_argument, 0, 's' },
1274 { "message", required_argument, 0, 'M' },
1275 { "owner", required_argument, 0 'R' },
1276 { "dot", no_argument, 0, 'V' },
1277 { "block-size", required_argument, 0,
1278 OPT_BLOCK_SIZE },
1279 { "no-absolute-pathnames", no_argument, 0,
1280 OPT_NO_ABSOLUTE_PATHNAMES },
1281 { "no-preserve-owner", no_argument, 0,
1282 OPT_NO_PRESERVE_OWNER },
1283 { "only-verify-crc", no_argument, 0,
1284 OPT_ONLY_VERIFY_CRC },
1285 { "rsh-command", required_argument, 0,
1286 OPT_RSH_COMMAND },
1287 { "sparce", no_argument, 0,
1288 OPT_SPARSE },
1289 { "version", no_argument, 0,
1290 OPT_VERSION },
1291 #endif
1292 };
1293
1294 /*
1295 * cpio_options()
1296 * look at the user specified flags. set globals as required and check if
1297 * the user specified a legal set of flags. If not, complain and exit
1298 */
1299
1300 static void
1301 cpio_options(int argc, char **argv)
1302 {
1303 FSUB tmp;
1304 unsigned int flg = 0;
1305 unsigned int bflg = 0;
1306 int c, i;
1307 FILE *fp;
1308 char *str;
1309
1310 uflag = 1;
1311 kflag = 1;
1312 pids = 1;
1313 pmode = 1;
1314 pmtime = 0;
1315 arcname = NULL;
1316 dflag = 1;
1317 nodirs = 1;
1318 /*
1319 * process option flags
1320 */
1321 while ((c = getoldopt(argc, argv,
1322 "+abcdfiklmoprstuvzABC:E:F:H:I:LM:O:R:SVZ6",
1323 cpio_longopts, NULL)) != -1) {
1324 switch(c) {
1325 case 'a':
1326 /*
1327 * preserve access time on filesystem nodes we read
1328 */
1329 tflag = 1;
1330 flg |= TF;
1331 break;
1332 #ifdef notyet
1333 case 'b':
1334 /*
1335 * swap bytes and half-words when reading data
1336 */
1337 break;
1338 #endif
1339 case 'c':
1340 /*
1341 * ASCII cpio header
1342 */
1343 frmt = &fsub[F_SV4CPIO];
1344 break;
1345 case 'd':
1346 /*
1347 * create directories as needed
1348 * pax does this by default ..
1349 */
1350 nodirs = 0;
1351 flg |= RF;
1352 break;
1353 case 'f':
1354 /*
1355 * inverse match on patterns
1356 */
1357 cflag = 1;
1358 flg |= CF;
1359 break;
1360 case 'i':
1361 /*
1362 * read the archive
1363 */
1364 act = EXTRACT;
1365 flg |= RF;
1366 break;
1367 #ifdef notyet
1368 case 'k':
1369 break;
1370 #endif
1371 case 'l':
1372 /*
1373 * try to link src to dest with copy (-rw)
1374 */
1375 lflag = 1;
1376 flg |= LF;
1377 break;
1378 case 'm':
1379 /*
1380 * preserve mtime
1381 */
1382 flg |= PF;
1383 pmtime = 1;
1384 break;
1385 case 'o':
1386 /*
1387 * write an archive
1388 */
1389 act = ARCHIVE;
1390 frmt = &(fsub[F_SV4CRC]);
1391 flg |= WF;
1392 break;
1393 case 'p':
1394 /*
1395 * cpio -p is like pax -rw
1396 */
1397 act = COPY;
1398 flg |= RF | WF;
1399 break;
1400 case 'r':
1401 /*
1402 * interactive file rename
1403 */
1404 iflag = 1;
1405 flg |= IF;
1406 break;
1407 #ifdef notyet
1408 case 's':
1409 /*
1410 * swap bytes after reading data
1411 */
1412 break;
1413 #endif
1414 case 't':
1415 /*
1416 * list contents of archive
1417 */
1418 act = LIST;
1419 listf = stdout;
1420 break;
1421 case 'u':
1422 /*
1423 * don't ignore those older files
1424 */
1425 uflag = 0;
1426 kflag = 0;
1427 flg |= UF;
1428 break;
1429 case 'v':
1430 /*
1431 * verbose operation mode
1432 */
1433 vflag = 1;
1434 flg |= VF;
1435 break;
1436 case 'z':
1437 /*
1438 * use gzip. Non standard option.
1439 */
1440 gzip_program = GZIP_CMD;
1441 break;
1442 case 'A':
1443 /*
1444 * append to an archive
1445 */
1446 act = APPND;
1447 flg |= AF;
1448 break;
1449 case 'B':
1450 /*
1451 * set blocksize to 5120
1452 */
1453 blksz = 5120;
1454 break;
1455 case 'C':
1456 /*
1457 * specify blocksize
1458 */
1459 if ((blksz = (int)str_offt(optarg)) <= 0) {
1460 tty_warn(1, "Invalid block size %s", optarg);
1461 tar_usage();
1462 }
1463 break;
1464 case 'E':
1465 /*
1466 * file with patterns to extract or list
1467 */
1468 if ((fp = fopen(optarg, "r")) == NULL) {
1469 tty_warn(1, "Unable to open file '%s' for read",
1470 optarg);
1471 cpio_usage();
1472 }
1473 while ((str = getline(fp)) != NULL) {
1474 pat_add(str, NULL);
1475 }
1476 fclose(fp);
1477 if (getline_error) {
1478 tty_warn(1, "Problem with file '%s'", optarg);
1479 cpio_usage();
1480 }
1481 break;
1482 case 'H':
1483 /*
1484 * specify an archive format on write
1485 */
1486 tmp.name = optarg;
1487 frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
1488 sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt);
1489 if (frmt != NULL) {
1490 flg |= XF;
1491 break;
1492 }
1493 tty_warn(1, "Unknown -H format: %s", optarg);
1494 (void)fputs("cpio: Known -H formats are:", stderr);
1495 for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
1496 (void)fprintf(stderr, " %s", fsub[i].name);
1497 (void)fputs("\n\n", stderr);
1498 tar_usage();
1499 break;
1500 case 'I':
1501 case 'O':
1502 /*
1503 * filename where the archive is stored
1504 */
1505 if ((optarg[0] == '-') && (optarg[1]== '\0')) {
1506 /*
1507 * treat a - as stdin
1508 */
1509 arcname = NULL;
1510 break;
1511 }
1512 arcname = optarg;
1513 break;
1514 case 'L':
1515 /*
1516 * follow symlinks
1517 */
1518 Lflag = 1;
1519 flg |= CLF;
1520 break;
1521 #ifdef notyet
1522 case 'M':
1523 arg = optarg;
1524 break;
1525 case 'R':
1526 arg = optarg;
1527 break;
1528 #endif
1529 case 'S':
1530 /*
1531 * swap halfwords after reading data
1532 */
1533 cpio_swp_head = 1;
1534 break;
1535 #ifdef notyet
1536 case 'V':
1537 break;
1538 #endif
1539 case 'Z':
1540 /*
1541 * use compress. Non standard option.
1542 */
1543 gzip_program = COMPRESS_CMD;
1544 break;
1545 case '6':
1546 /*
1547 * process Version 6 cpio format
1548 */
1549 frmt = &(fsub[F_BCPIO]);
1550 case OPT_FORCE_LOCAL:
1551 forcelocal = 1;
1552 break;
1553 case OPT_INSECURE:
1554 secure = 0;
1555 break;
1556 default:
1557 cpio_usage();
1558 break;
1559 }
1560 }
1561
1562 /*
1563 * figure out the operation mode of cpio. check that we have not been
1564 * given a bogus set of flags for the operation mode.
1565 */
1566 if (ISLIST(flg)) {
1567 act = LIST;
1568 bflg = flg & BDLIST;
1569 } else if (ISEXTRACT(flg)) {
1570 act = EXTRACT;
1571 bflg = flg & BDEXTR;
1572 } else if (ISARCHIVE(flg)) {
1573 act = ARCHIVE;
1574 bflg = flg & BDARCH;
1575 } else if (ISAPPND(flg)) {
1576 act = APPND;
1577 bflg = flg & BDARCH;
1578 } else if (ISCOPY(flg)) {
1579 act = COPY;
1580 bflg = flg & BDCOPY;
1581 } else
1582 cpio_usage();
1583 if (bflg) {
1584 cpio_usage();
1585 }
1586
1587 /*
1588 * if we are writing (ARCHIVE) we use the default format if the user
1589 * did not specify a format. when we write during an APPEND, we will
1590 * adopt the format of the existing archive if none was supplied.
1591 */
1592 if (!(flg & XF) && (act == ARCHIVE))
1593 frmt = &(fsub[F_BCPIO]);
1594
1595 /*
1596 * process the args as they are interpreted by the operation mode
1597 */
1598 switch (act) {
1599 case LIST:
1600 case EXTRACT:
1601 for (; optind < argc; optind++)
1602 if (pat_add(argv[optind], 0) < 0)
1603 cpio_usage();
1604 break;
1605 case COPY:
1606 if (optind >= argc) {
1607 tty_warn(0, "Destination directory was not supplied");
1608 cpio_usage();
1609 }
1610 --argc;
1611 dirptr = argv[argc];
1612 /* FALLTHROUGH */
1613 case ARCHIVE:
1614 case APPND:
1615 if (argc != optind) {
1616 for (; optind < argc; optind++)
1617 if (ftree_add(argv[optind], 0) < 0)
1618 cpio_usage();
1619 break;
1620 }
1621 /*
1622 * no read errors allowed on updates/append operation!
1623 */
1624 maxflt = 0;
1625 while ((str = getline(stdin)) != NULL) {
1626 ftree_add(str, NULL);
1627 }
1628 if (getline_error) {
1629 tty_warn(1, "Problem while reading stdin");
1630 cpio_usage();
1631 }
1632 break;
1633 default:
1634 cpio_usage();
1635 break;
1636 }
1637 }
1638
1639 /*
1640 * printflg()
1641 * print out those invalid flag sets found to the user
1642 */
1643
1644 static void
1645 printflg(unsigned int flg)
1646 {
1647 int nxt;
1648 int pos = 0;
1649
1650 (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
1651 while ((nxt = ffs(flg)) != 0) {
1652 flg = flg >> nxt;
1653 pos += nxt;
1654 (void)fprintf(stderr, " -%c", flgch[pos-1]);
1655 }
1656 (void)putc('\n', stderr);
1657 }
1658
1659 /*
1660 * c_frmt()
1661 * comparison routine used by bsearch to find the format specified
1662 * by the user
1663 */
1664
1665 static int
1666 c_frmt(const void *a, const void *b)
1667 {
1668 return(strcmp(((FSUB *)a)->name, ((FSUB *)b)->name));
1669 }
1670
1671 /*
1672 * opt_next()
1673 * called by format specific options routines to get each format specific
1674 * flag and value specified with -o
1675 * Return:
1676 * pointer to next OPLIST entry or NULL (end of list).
1677 */
1678
1679 OPLIST *
1680 opt_next(void)
1681 {
1682 OPLIST *opt;
1683
1684 if ((opt = ophead) != NULL)
1685 ophead = ophead->fow;
1686 return(opt);
1687 }
1688
1689 /*
1690 * bad_opt()
1691 * generic routine used to complain about a format specific options
1692 * when the format does not support options.
1693 */
1694
1695 int
1696 bad_opt(void)
1697 {
1698 OPLIST *opt;
1699
1700 if (ophead == NULL)
1701 return(0);
1702 /*
1703 * print all we were given
1704 */
1705 tty_warn(1," These format options are not supported for %s",
1706 frmt->name);
1707 while ((opt = opt_next()) != NULL)
1708 (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
1709 if (strcmp(NM_TAR, argv0) == 0)
1710 tar_usage();
1711 else if (strcmp(NM_CPIO, argv0) == 0)
1712 cpio_usage();
1713 else
1714 pax_usage();
1715 return(0);
1716 }
1717
1718 /*
1719 * opt_add()
1720 * breaks the value supplied to -o into a option name and value. options
1721 * are given to -o in the form -o name-value,name=value
1722 * multiple -o may be specified.
1723 * Return:
1724 * 0 if format in name=value format, -1 if -o is passed junk
1725 */
1726
1727 int
1728 opt_add(const char *str)
1729 {
1730 OPLIST *opt;
1731 char *frpt;
1732 char *pt;
1733 char *endpt;
1734 char *dstr;
1735
1736 if ((str == NULL) || (*str == '\0')) {
1737 tty_warn(0, "Invalid option name");
1738 return(-1);
1739 }
1740 if ((dstr = strdup(str)) == NULL) {
1741 tty_warn(0, "Unable to allocate space for option list");
1742 return(-1);
1743 }
1744 frpt = endpt = dstr;
1745
1746 /*
1747 * break into name and values pieces and stuff each one into a
1748 * OPLIST structure. When we know the format, the format specific
1749 * option function will go through this list
1750 */
1751 while ((frpt != NULL) && (*frpt != '\0')) {
1752 if ((endpt = strchr(frpt, ',')) != NULL)
1753 *endpt = '\0';
1754 if ((pt = strchr(frpt, '=')) == NULL) {
1755 tty_warn(0, "Invalid options format");
1756 free(dstr);
1757 return(-1);
1758 }
1759 if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
1760 tty_warn(0, "Unable to allocate space for option list");
1761 free(dstr);
1762 return(-1);
1763 }
1764 *pt++ = '\0';
1765 opt->name = frpt;
1766 opt->value = pt;
1767 opt->fow = NULL;
1768 if (endpt != NULL)
1769 frpt = endpt + 1;
1770 else
1771 frpt = NULL;
1772 if (ophead == NULL) {
1773 optail = ophead = opt;
1774 continue;
1775 }
1776 optail->fow = opt;
1777 optail = opt;
1778 }
1779 return(0);
1780 }
1781
1782 /*
1783 * str_offt()
1784 * Convert an expression of the following forms to an off_t > 0.
1785 * 1) A positive decimal number.
1786 * 2) A positive decimal number followed by a b (mult by 512).
1787 * 3) A positive decimal number followed by a k (mult by 1024).
1788 * 4) A positive decimal number followed by a m (mult by 512).
1789 * 5) A positive decimal number followed by a w (mult by sizeof int)
1790 * 6) Two or more positive decimal numbers (with/without k,b or w).
1791 * separated by x (also * for backwards compatibility), specifying
1792 * the product of the indicated values.
1793 * Return:
1794 * 0 for an error, a positive value o.w.
1795 */
1796
1797 static off_t
1798 str_offt(char *val)
1799 {
1800 char *expr;
1801 off_t num, t;
1802
1803 num = STRTOOFFT(val, &expr, 0);
1804 if ((num == OFFT_MAX) || (num <= 0) || (expr == val))
1805 return(0);
1806
1807 switch(*expr) {
1808 case 'b':
1809 t = num;
1810 num *= 512;
1811 if (t > num)
1812 return(0);
1813 ++expr;
1814 break;
1815 case 'k':
1816 t = num;
1817 num *= 1024;
1818 if (t > num)
1819 return(0);
1820 ++expr;
1821 break;
1822 case 'm':
1823 t = num;
1824 num *= 1048576;
1825 if (t > num)
1826 return(0);
1827 ++expr;
1828 break;
1829 case 'w':
1830 t = num;
1831 num *= sizeof(int);
1832 if (t > num)
1833 return(0);
1834 ++expr;
1835 break;
1836 }
1837
1838 switch(*expr) {
1839 case '\0':
1840 break;
1841 case '*':
1842 case 'x':
1843 t = num;
1844 num *= str_offt(expr + 1);
1845 if (t > num)
1846 return(0);
1847 break;
1848 default:
1849 return(0);
1850 }
1851 return(num);
1852 }
1853
1854 char *
1855 getline(FILE *f)
1856 {
1857 char *name, *temp;
1858 size_t len;
1859
1860 name = fgetln(f, &len);
1861 if (!name) {
1862 getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
1863 return(0);
1864 }
1865 if (name[len-1] != '\n')
1866 len++;
1867 temp = malloc(len);
1868 if (!temp) {
1869 getline_error = GETLINE_OUT_OF_MEM;
1870 return(0);
1871 }
1872 memcpy(temp, name, len-1);
1873 temp[len-1] = 0;
1874 return(temp);
1875 }
1876
1877 /*
1878 * no_op()
1879 * for those option functions where the archive format has nothing to do.
1880 * Return:
1881 * 0
1882 */
1883
1884 static int
1885 no_op(void)
1886 {
1887 return(0);
1888 }
1889
1890 /*
1891 * pax_usage()
1892 * print the usage summary to the user
1893 */
1894
1895 void
1896 pax_usage(void)
1897 {
1898 fprintf(stderr,
1899 "Usage: pax [-cdnvzO] [-E limit] [-f archive] [-N dbdir] [-s replstr] ...\n"
1900 " [-U user] ... [-G group] ... [-T [from_date][,to_date]] ...\n"
1901 " [pattern ...]\n");
1902 fprintf(stderr,
1903 " pax -r [-cdiknuvzADOYZ] [-E limit] [-f archive] [-N dbdir]\n"
1904 " [-o options] ... [-p string] ... [-s replstr] ... [-U user] ...\n"
1905 " [-G group] ... [-T [from_date][,to_date]] ... [pattern ...]\n");
1906 fprintf(stderr,
1907 " pax -w [-dituvzAHLMOPX] [-b blocksize] [[-a] [-f archive]] [-x format]\n"
1908 " [-B bytes] [-N dbdir] [-o options] ... [-s replstr] ...\n"
1909 " [-U user] ... [-G group] ...\n"
1910 " [-T [from_date][,to_date][/[c][m]]] ... [file ...]\n");
1911 fprintf(stderr,
1912 " pax -r -w [-diklntuvzADHLMOPXYZ] [-N dbdir] [-p string] ...\n"
1913 " [-s replstr] ... [-U user] ... [-G group] ...\n"
1914 " [-T [from_date][,to_date][/[c][m]]] ... [file ...] directory\n");
1915 exit(1);
1916 /* NOTREACHED */
1917 }
1918
1919 /*
1920 * tar_usage()
1921 * print the usage summary to the user
1922 */
1923
1924 void
1925 tar_usage(void)
1926 {
1927 (void)fputs("Usage: tar [-]{crtux}[-befhmopqvwzHLOPXZ014578] [archive] "
1928 "[blocksize]\n"
1929 " [-C directory] [-T file] [-s replstr] "
1930 "[file ...]\n", stderr);
1931 exit(1);
1932 /* NOTREACHED */
1933 }
1934
1935 /*
1936 * cpio_usage()
1937 * print the usage summary to the user
1938 */
1939
1940 void
1941 cpio_usage(void)
1942 {
1943
1944 (void)fputs("Usage: cpio -o [-aABcLvzZ] [-C bytes] [-F archive] "
1945 "[-H format] [-O archive]\n"
1946 " < name-list [> archive]\n"
1947 " cpio -i [-bBcdfmrsStuvzZ6] [-C bytes] [-E file] "
1948 "[-F archive] [-H format] \n"
1949 " [-I archive] "
1950 "[pattern ...] [< archive]\n"
1951 " cpio -p [-adlLmuv] destination-directory "
1952 "< name-list\n", stderr);
1953 exit(1);
1954 /* NOTREACHED */
1955 }
1956