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