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