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