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