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