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