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