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