/bin/pax/options.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 1581 lines · 1016 code · 82 blank · 483 comment · 236 complexity · e92f4d14ed1447d1ebdab691c70cce07 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1992 Keith Muller.
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Keith Muller of the University of California, San Diego.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #if 0
  34. #ifndef lint
  35. static char sccsid[] = "@(#)options.c 8.2 (Berkeley) 4/18/94";
  36. #endif /* not lint */
  37. #endif
  38. #include <sys/cdefs.h>
  39. __FBSDID("$FreeBSD$");
  40. #include <sys/types.h>
  41. #include <sys/stat.h>
  42. #include <sys/mtio.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <errno.h>
  46. #include <unistd.h>
  47. #include <stdlib.h>
  48. #include <limits.h>
  49. #include <paths.h>
  50. #include "pax.h"
  51. #include "options.h"
  52. #include "cpio.h"
  53. #include "tar.h"
  54. #include "extern.h"
  55. /*
  56. * Routines which handle command line options
  57. */
  58. static char flgch[] = FLGCH; /* list of all possible flags */
  59. static OPLIST *ophead = NULL; /* head for format specific options -x */
  60. static OPLIST *optail = NULL; /* option tail */
  61. static int no_op(void);
  62. static void printflg(unsigned int);
  63. static int c_frmt(const void *, const void *);
  64. static off_t str_offt(char *);
  65. static char *getline(FILE *fp);
  66. static void pax_options(int, char **);
  67. static void pax_usage(void);
  68. static void tar_options(int, char **);
  69. static void tar_usage(void);
  70. static void cpio_options(int, char **);
  71. static void cpio_usage(void);
  72. /* errors from getline */
  73. #define GETLINE_FILE_CORRUPT 1
  74. #define GETLINE_OUT_OF_MEM 2
  75. static int getline_error;
  76. #define GZIP_CMD "gzip" /* command to run as gzip */
  77. #define COMPRESS_CMD "compress" /* command to run as compress */
  78. #define BZIP2_CMD "bzip2" /* command to run as gzip */
  79. /*
  80. * Format specific routine table - MUST BE IN SORTED ORDER BY NAME
  81. * (see pax.h for description of each function)
  82. *
  83. * name, blksz, hdsz, udev, hlk, blkagn, inhead, id, st_read,
  84. * read, end_read, st_write, write, end_write, trail,
  85. * rd_data, wr_data, options
  86. */
  87. FSUB fsub[] = {
  88. /* 0: OLD BINARY CPIO */
  89. {"bcpio", 5120, sizeof(HD_BCPIO), 1, 0, 0, 1, bcpio_id, cpio_strd,
  90. bcpio_rd, bcpio_endrd, cpio_stwr, bcpio_wr, cpio_endwr, cpio_trail,
  91. NULL, rd_wrfile, wr_rdfile, bad_opt},
  92. /* 1: OLD OCTAL CHARACTER CPIO */
  93. {"cpio", 5120, sizeof(HD_CPIO), 1, 0, 0, 1, cpio_id, cpio_strd,
  94. cpio_rd, cpio_endrd, cpio_stwr, cpio_wr, cpio_endwr, cpio_trail,
  95. NULL, rd_wrfile, wr_rdfile, bad_opt},
  96. /* 2: SVR4 HEX CPIO */
  97. {"sv4cpio", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, vcpio_id, cpio_strd,
  98. vcpio_rd, vcpio_endrd, cpio_stwr, vcpio_wr, cpio_endwr, cpio_trail,
  99. NULL, rd_wrfile, wr_rdfile, bad_opt},
  100. /* 3: SVR4 HEX CPIO WITH CRC */
  101. {"sv4crc", 5120, sizeof(HD_VCPIO), 1, 0, 0, 1, crc_id, crc_strd,
  102. vcpio_rd, vcpio_endrd, crc_stwr, vcpio_wr, cpio_endwr, cpio_trail,
  103. NULL, rd_wrfile, wr_rdfile, bad_opt},
  104. /* 4: OLD TAR */
  105. {"tar", 10240, BLKMULT, 0, 1, BLKMULT, 0, tar_id, no_op,
  106. tar_rd, tar_endrd, no_op, tar_wr, tar_endwr, NULL, tar_trail,
  107. rd_wrfile, wr_rdfile, tar_opt},
  108. /* 5: POSIX USTAR */
  109. {"ustar", 10240, BLKMULT, 0, 1, BLKMULT, 0, ustar_id, ustar_strd,
  110. ustar_rd, tar_endrd, ustar_stwr, ustar_wr, tar_endwr, NULL, tar_trail,
  111. rd_wrfile, wr_rdfile, bad_opt},
  112. };
  113. #define F_OCPIO 0 /* format when called as cpio -6 */
  114. #define F_ACPIO 1 /* format when called as cpio -c */
  115. #define F_CPIO 3 /* format when called as cpio */
  116. #define F_OTAR 4 /* format when called as tar -o */
  117. #define F_TAR 5 /* format when called as tar */
  118. #define DEFLT 5 /* default write format from list above */
  119. /*
  120. * ford is the archive search order used by get_arc() to determine what kind
  121. * of archive we are dealing with. This helps to properly id archive formats
  122. * some formats may be subsets of others....
  123. */
  124. int ford[] = {5, 4, 3, 2, 1, 0, -1 };
  125. /*
  126. * options()
  127. * figure out if we are pax, tar or cpio. Call the appropriate options
  128. * parser
  129. */
  130. void
  131. options(int argc, char **argv)
  132. {
  133. /*
  134. * Are we acting like pax, tar or cpio (based on argv[0])
  135. */
  136. if ((argv0 = strrchr(argv[0], '/')) != NULL)
  137. argv0++;
  138. else
  139. argv0 = argv[0];
  140. if (strcmp(NM_TAR, argv0) == 0) {
  141. tar_options(argc, argv);
  142. return;
  143. }
  144. else if (strcmp(NM_CPIO, argv0) == 0) {
  145. cpio_options(argc, argv);
  146. return;
  147. }
  148. /*
  149. * assume pax as the default
  150. */
  151. argv0 = NM_PAX;
  152. pax_options(argc, argv);
  153. return;
  154. }
  155. /*
  156. * pax_options()
  157. * look at the user specified flags. set globals as required and check if
  158. * the user specified a legal set of flags. If not, complain and exit
  159. */
  160. static void
  161. pax_options(int argc, char **argv)
  162. {
  163. int c;
  164. size_t i;
  165. unsigned int flg = 0;
  166. unsigned int bflg = 0;
  167. char *pt;
  168. FSUB tmp;
  169. /*
  170. * process option flags
  171. */
  172. while ((c=getopt(argc,argv,"ab:cdf:iklno:p:rs:tuvwx:zB:DE:G:HLPT:U:XYZ"))
  173. != -1) {
  174. switch (c) {
  175. case 'a':
  176. /*
  177. * append
  178. */
  179. flg |= AF;
  180. break;
  181. case 'b':
  182. /*
  183. * specify blocksize
  184. */
  185. flg |= BF;
  186. if ((wrblksz = (int)str_offt(optarg)) <= 0) {
  187. paxwarn(1, "Invalid block size %s", optarg);
  188. pax_usage();
  189. }
  190. break;
  191. case 'c':
  192. /*
  193. * inverse match on patterns
  194. */
  195. cflag = 1;
  196. flg |= CF;
  197. break;
  198. case 'd':
  199. /*
  200. * match only dir on extract, not the subtree at dir
  201. */
  202. dflag = 1;
  203. flg |= DF;
  204. break;
  205. case 'f':
  206. /*
  207. * filename where the archive is stored
  208. */
  209. arcname = optarg;
  210. flg |= FF;
  211. break;
  212. case 'i':
  213. /*
  214. * interactive file rename
  215. */
  216. iflag = 1;
  217. flg |= IF;
  218. break;
  219. case 'k':
  220. /*
  221. * do not clobber files that exist
  222. */
  223. kflag = 1;
  224. flg |= KF;
  225. break;
  226. case 'l':
  227. /*
  228. * try to link src to dest with copy (-rw)
  229. */
  230. lflag = 1;
  231. flg |= LF;
  232. break;
  233. case 'n':
  234. /*
  235. * select first match for a pattern only
  236. */
  237. nflag = 1;
  238. flg |= NF;
  239. break;
  240. case 'o':
  241. /*
  242. * pass format specific options
  243. */
  244. flg |= OF;
  245. if (opt_add(optarg) < 0)
  246. pax_usage();
  247. break;
  248. case 'p':
  249. /*
  250. * specify file characteristic options
  251. */
  252. for (pt = optarg; *pt != '\0'; ++pt) {
  253. switch(*pt) {
  254. case 'a':
  255. /*
  256. * do not preserve access time
  257. */
  258. patime = 0;
  259. break;
  260. case 'e':
  261. /*
  262. * preserve user id, group id, file
  263. * mode, access/modification times
  264. */
  265. pids = 1;
  266. pmode = 1;
  267. patime = 1;
  268. pmtime = 1;
  269. break;
  270. case 'm':
  271. /*
  272. * do not preserve modification time
  273. */
  274. pmtime = 0;
  275. break;
  276. case 'o':
  277. /*
  278. * preserve uid/gid
  279. */
  280. pids = 1;
  281. break;
  282. case 'p':
  283. /*
  284. * preserver file mode bits
  285. */
  286. pmode = 1;
  287. break;
  288. default:
  289. paxwarn(1, "Invalid -p string: %c", *pt);
  290. pax_usage();
  291. break;
  292. }
  293. }
  294. flg |= PF;
  295. break;
  296. case 'r':
  297. /*
  298. * read the archive
  299. */
  300. flg |= RF;
  301. break;
  302. case 's':
  303. /*
  304. * file name substitution name pattern
  305. */
  306. if (rep_add(optarg) < 0) {
  307. pax_usage();
  308. break;
  309. }
  310. flg |= SF;
  311. break;
  312. case 't':
  313. /*
  314. * preserve access time on file system nodes we read
  315. */
  316. tflag = 1;
  317. flg |= TF;
  318. break;
  319. case 'u':
  320. /*
  321. * ignore those older files
  322. */
  323. uflag = 1;
  324. flg |= UF;
  325. break;
  326. case 'v':
  327. /*
  328. * verbose operation mode
  329. */
  330. vflag = 1;
  331. flg |= VF;
  332. break;
  333. case 'w':
  334. /*
  335. * write an archive
  336. */
  337. flg |= WF;
  338. break;
  339. case 'x':
  340. /*
  341. * specify an archive format on write
  342. */
  343. tmp.name = optarg;
  344. if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
  345. sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL) {
  346. flg |= XF;
  347. break;
  348. }
  349. paxwarn(1, "Unknown -x format: %s", optarg);
  350. (void)fputs("pax: Known -x formats are:", stderr);
  351. for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
  352. (void)fprintf(stderr, " %s", fsub[i].name);
  353. (void)fputs("\n\n", stderr);
  354. pax_usage();
  355. break;
  356. case 'z':
  357. /*
  358. * use gzip. Non standard option.
  359. */
  360. gzip_program = GZIP_CMD;
  361. break;
  362. case 'B':
  363. /*
  364. * non-standard option on number of bytes written on a
  365. * single archive volume.
  366. */
  367. if ((wrlimit = str_offt(optarg)) <= 0) {
  368. paxwarn(1, "Invalid write limit %s", optarg);
  369. pax_usage();
  370. }
  371. if (wrlimit % BLKMULT) {
  372. paxwarn(1, "Write limit is not a %d byte multiple",
  373. BLKMULT);
  374. pax_usage();
  375. }
  376. flg |= CBF;
  377. break;
  378. case 'D':
  379. /*
  380. * On extraction check file inode change time before the
  381. * modification of the file name. Non standard option.
  382. */
  383. Dflag = 1;
  384. flg |= CDF;
  385. break;
  386. case 'E':
  387. /*
  388. * non-standard limit on read faults
  389. * 0 indicates stop after first error, values
  390. * indicate a limit, "NONE" try forever
  391. */
  392. flg |= CEF;
  393. if (strcmp(NONE, optarg) == 0)
  394. maxflt = -1;
  395. else if ((maxflt = atoi(optarg)) < 0) {
  396. paxwarn(1, "Error count value must be positive");
  397. pax_usage();
  398. }
  399. break;
  400. case 'G':
  401. /*
  402. * non-standard option for selecting files within an
  403. * archive by group (gid or name)
  404. */
  405. if (grp_add(optarg) < 0) {
  406. pax_usage();
  407. break;
  408. }
  409. flg |= CGF;
  410. break;
  411. case 'H':
  412. /*
  413. * follow command line symlinks only
  414. */
  415. Hflag = 1;
  416. flg |= CHF;
  417. break;
  418. case 'L':
  419. /*
  420. * follow symlinks
  421. */
  422. Lflag = 1;
  423. flg |= CLF;
  424. break;
  425. case 'P':
  426. /*
  427. * do NOT follow symlinks (default)
  428. */
  429. Lflag = 0;
  430. flg |= CPF;
  431. break;
  432. case 'T':
  433. /*
  434. * non-standard option for selecting files within an
  435. * archive by modification time range (lower,upper)
  436. */
  437. if (trng_add(optarg) < 0) {
  438. pax_usage();
  439. break;
  440. }
  441. flg |= CTF;
  442. break;
  443. case 'U':
  444. /*
  445. * non-standard option for selecting files within an
  446. * archive by user (uid or name)
  447. */
  448. if (usr_add(optarg) < 0) {
  449. pax_usage();
  450. break;
  451. }
  452. flg |= CUF;
  453. break;
  454. case 'X':
  455. /*
  456. * do not pass over mount points in the file system
  457. */
  458. Xflag = 1;
  459. flg |= CXF;
  460. break;
  461. case 'Y':
  462. /*
  463. * On extraction check file inode change time after the
  464. * modification of the file name. Non standard option.
  465. */
  466. Yflag = 1;
  467. flg |= CYF;
  468. break;
  469. case 'Z':
  470. /*
  471. * On extraction check modification time after the
  472. * modification of the file name. Non standard option.
  473. */
  474. Zflag = 1;
  475. flg |= CZF;
  476. break;
  477. default:
  478. pax_usage();
  479. break;
  480. }
  481. }
  482. /*
  483. * figure out the operation mode of pax read,write,extract,copy,append
  484. * or list. check that we have not been given a bogus set of flags
  485. * for the operation mode.
  486. */
  487. if (ISLIST(flg)) {
  488. act = LIST;
  489. listf = stdout;
  490. bflg = flg & BDLIST;
  491. } else if (ISEXTRACT(flg)) {
  492. act = EXTRACT;
  493. bflg = flg & BDEXTR;
  494. } else if (ISARCHIVE(flg)) {
  495. act = ARCHIVE;
  496. bflg = flg & BDARCH;
  497. } else if (ISAPPND(flg)) {
  498. act = APPND;
  499. bflg = flg & BDARCH;
  500. } else if (ISCOPY(flg)) {
  501. act = COPY;
  502. bflg = flg & BDCOPY;
  503. } else
  504. pax_usage();
  505. if (bflg) {
  506. printflg(flg);
  507. pax_usage();
  508. }
  509. /*
  510. * if we are writing (ARCHIVE) we use the default format if the user
  511. * did not specify a format. when we write during an APPEND, we will
  512. * adopt the format of the existing archive if none was supplied.
  513. */
  514. if (!(flg & XF) && (act == ARCHIVE))
  515. frmt = &(fsub[DEFLT]);
  516. /*
  517. * process the args as they are interpreted by the operation mode
  518. */
  519. switch (act) {
  520. case LIST:
  521. case EXTRACT:
  522. for (; optind < argc; optind++)
  523. if (pat_add(argv[optind], NULL) < 0)
  524. pax_usage();
  525. break;
  526. case COPY:
  527. if (optind >= argc) {
  528. paxwarn(0, "Destination directory was not supplied");
  529. pax_usage();
  530. }
  531. --argc;
  532. dirptr = argv[argc];
  533. /* FALLTHROUGH */
  534. case ARCHIVE:
  535. case APPND:
  536. for (; optind < argc; optind++)
  537. if (ftree_add(argv[optind], 0) < 0)
  538. pax_usage();
  539. /*
  540. * no read errors allowed on updates/append operation!
  541. */
  542. maxflt = 0;
  543. break;
  544. }
  545. }
  546. /*
  547. * tar_options()
  548. * look at the user specified flags. set globals as required and check if
  549. * the user specified a legal set of flags. If not, complain and exit
  550. */
  551. static void
  552. tar_options(int argc, char **argv)
  553. {
  554. int c;
  555. int fstdin = 0;
  556. int Oflag = 0;
  557. int nincfiles = 0;
  558. int incfiles_max = 0;
  559. struct incfile {
  560. char *file;
  561. char *dir;
  562. };
  563. struct incfile *incfiles = NULL;
  564. /*
  565. * Set default values.
  566. */
  567. rmleadslash = 1;
  568. /*
  569. * process option flags
  570. */
  571. while ((c = getoldopt(argc, argv,
  572. "b:cef:hjmopqruts:vwxyzBC:HI:LOPXZ014578")) != -1) {
  573. switch(c) {
  574. case 'b':
  575. /*
  576. * specify blocksize in 512-byte blocks
  577. */
  578. if ((wrblksz = (int)str_offt(optarg)) <= 0) {
  579. paxwarn(1, "Invalid block size %s", optarg);
  580. tar_usage();
  581. }
  582. wrblksz *= 512; /* XXX - check for int oflow */
  583. break;
  584. case 'c':
  585. /*
  586. * create an archive
  587. */
  588. act = ARCHIVE;
  589. break;
  590. case 'e':
  591. /*
  592. * stop after first error
  593. */
  594. maxflt = 0;
  595. break;
  596. case 'f':
  597. /*
  598. * filename where the archive is stored
  599. */
  600. if ((optarg[0] == '-') && (optarg[1]== '\0')) {
  601. /*
  602. * treat a - as stdin
  603. */
  604. fstdin = 1;
  605. arcname = NULL;
  606. break;
  607. }
  608. fstdin = 0;
  609. arcname = optarg;
  610. break;
  611. case 'h':
  612. /*
  613. * follow symlinks
  614. */
  615. Lflag = 1;
  616. break;
  617. case 'j':
  618. case 'y':
  619. /*
  620. * use bzip2. Non standard option.
  621. */
  622. gzip_program = BZIP2_CMD;
  623. break;
  624. case 'm':
  625. /*
  626. * do not preserve modification time
  627. */
  628. pmtime = 0;
  629. break;
  630. case 'o':
  631. if (opt_add("write_opt=nodir") < 0)
  632. tar_usage();
  633. case 'O':
  634. Oflag = 1;
  635. break;
  636. case 'p':
  637. /*
  638. * preserve uid/gid and file mode, regardless of umask
  639. */
  640. pmode = 1;
  641. pids = 1;
  642. break;
  643. case 'q':
  644. /*
  645. * select first match for a pattern only
  646. */
  647. nflag = 1;
  648. break;
  649. case 'r':
  650. case 'u':
  651. /*
  652. * append to the archive
  653. */
  654. act = APPND;
  655. break;
  656. case 's':
  657. /*
  658. * file name substitution name pattern
  659. */
  660. if (rep_add(optarg) < 0) {
  661. tar_usage();
  662. break;
  663. }
  664. break;
  665. case 't':
  666. /*
  667. * list contents of the tape
  668. */
  669. act = LIST;
  670. break;
  671. case 'v':
  672. /*
  673. * verbose operation mode
  674. */
  675. vflag++;
  676. break;
  677. case 'w':
  678. /*
  679. * interactive file rename
  680. */
  681. iflag = 1;
  682. break;
  683. case 'x':
  684. /*
  685. * extract an archive, preserving mode,
  686. * and mtime if possible.
  687. */
  688. act = EXTRACT;
  689. pmtime = 1;
  690. break;
  691. case 'z':
  692. /*
  693. * use gzip. Non standard option.
  694. */
  695. gzip_program = GZIP_CMD;
  696. break;
  697. case 'B':
  698. /*
  699. * Nothing to do here, this is pax default
  700. */
  701. break;
  702. case 'C':
  703. chdname = optarg;
  704. break;
  705. case 'H':
  706. /*
  707. * follow command line symlinks only
  708. */
  709. Hflag = 1;
  710. break;
  711. case 'I':
  712. if (++nincfiles > incfiles_max) {
  713. incfiles_max = nincfiles + 3;
  714. incfiles = realloc(incfiles,
  715. sizeof(*incfiles) * incfiles_max);
  716. if (incfiles == NULL) {
  717. paxwarn(0, "Unable to allocate space "
  718. "for option list");
  719. exit(1);
  720. }
  721. }
  722. incfiles[nincfiles - 1].file = optarg;
  723. incfiles[nincfiles - 1].dir = chdname;
  724. break;
  725. case 'L':
  726. /*
  727. * follow symlinks
  728. */
  729. Lflag = 1;
  730. break;
  731. case 'P':
  732. /*
  733. * do not remove leading '/' from pathnames
  734. */
  735. rmleadslash = 0;
  736. break;
  737. case 'X':
  738. /*
  739. * do not pass over mount points in the file system
  740. */
  741. Xflag = 1;
  742. break;
  743. case 'Z':
  744. /*
  745. * use compress.
  746. */
  747. gzip_program = COMPRESS_CMD;
  748. break;
  749. case '0':
  750. arcname = DEV_0;
  751. break;
  752. case '1':
  753. arcname = DEV_1;
  754. break;
  755. case '4':
  756. arcname = DEV_4;
  757. break;
  758. case '5':
  759. arcname = DEV_5;
  760. break;
  761. case '7':
  762. arcname = DEV_7;
  763. break;
  764. case '8':
  765. arcname = DEV_8;
  766. break;
  767. default:
  768. tar_usage();
  769. break;
  770. }
  771. }
  772. argc -= optind;
  773. argv += optind;
  774. /* Traditional tar behaviour (pax uses stderr unless in list mode) */
  775. if (fstdin == 1 && act == ARCHIVE)
  776. listf = stderr;
  777. else
  778. listf = stdout;
  779. /* Traditional tar behaviour (pax wants to read file list from stdin) */
  780. if ((act == ARCHIVE || act == APPND) && argc == 0 && nincfiles == 0)
  781. exit(0);
  782. /*
  783. * if we are writing (ARCHIVE) specify tar, otherwise run like pax
  784. * (unless -o specified)
  785. */
  786. if (act == ARCHIVE || act == APPND)
  787. frmt = &(fsub[Oflag ? F_OTAR : F_TAR]);
  788. else if (Oflag) {
  789. paxwarn(1, "The -O/-o options are only valid when writing an archive");
  790. tar_usage(); /* only valid when writing */
  791. }
  792. /*
  793. * process the args as they are interpreted by the operation mode
  794. */
  795. switch (act) {
  796. case LIST:
  797. case EXTRACT:
  798. default:
  799. {
  800. int sawpat = 0;
  801. char *file, *dir = NULL;
  802. while (nincfiles || *argv != NULL) {
  803. /*
  804. * If we queued up any include files,
  805. * pull them in now. Otherwise, check
  806. * for -I and -C positional flags.
  807. * Anything else must be a file to
  808. * extract.
  809. */
  810. if (nincfiles) {
  811. file = incfiles->file;
  812. dir = incfiles->dir;
  813. incfiles++;
  814. nincfiles--;
  815. } else if (strcmp(*argv, "-I") == 0) {
  816. if (*++argv == NULL)
  817. break;
  818. file = *argv++;
  819. dir = chdname;
  820. } else
  821. file = NULL;
  822. if (file != NULL) {
  823. FILE *fp;
  824. char *str;
  825. if (strcmp(file, "-") == 0)
  826. fp = stdin;
  827. else if ((fp = fopen(file, "r")) == NULL) {
  828. paxwarn(1, "Unable to open file '%s' for read", file);
  829. tar_usage();
  830. }
  831. while ((str = getline(fp)) != NULL) {
  832. if (pat_add(str, dir) < 0)
  833. tar_usage();
  834. sawpat = 1;
  835. }
  836. if (strcmp(file, "-") != 0)
  837. fclose(fp);
  838. if (getline_error) {
  839. paxwarn(1, "Problem with file '%s'", file);
  840. tar_usage();
  841. }
  842. } else if (strcmp(*argv, "-C") == 0) {
  843. if (*++argv == NULL)
  844. break;
  845. chdname = *argv++;
  846. } else if (pat_add(*argv++, chdname) < 0)
  847. tar_usage();
  848. else
  849. sawpat = 1;
  850. }
  851. /*
  852. * if patterns were added, we are doing chdir()
  853. * on a file-by-file basis, else, just one
  854. * global chdir (if any) after opening input.
  855. */
  856. if (sawpat > 0)
  857. chdname = NULL;
  858. }
  859. break;
  860. case ARCHIVE:
  861. case APPND:
  862. if (chdname != NULL) { /* initial chdir() */
  863. if (ftree_add(chdname, 1) < 0)
  864. tar_usage();
  865. }
  866. while (nincfiles || *argv != NULL) {
  867. char *file, *dir = NULL;
  868. /*
  869. * If we queued up any include files, pull them in
  870. * now. Otherwise, check for -I and -C positional
  871. * flags. Anything else must be a file to include
  872. * in the archive.
  873. */
  874. if (nincfiles) {
  875. file = incfiles->file;
  876. dir = incfiles->dir;
  877. incfiles++;
  878. nincfiles--;
  879. } else if (strcmp(*argv, "-I") == 0) {
  880. if (*++argv == NULL)
  881. break;
  882. file = *argv++;
  883. dir = NULL;
  884. } else
  885. file = NULL;
  886. if (file != NULL) {
  887. FILE *fp;
  888. char *str;
  889. /* Set directory if needed */
  890. if (dir) {
  891. if (ftree_add(dir, 1) < 0)
  892. tar_usage();
  893. }
  894. if (strcmp(file, "-") == 0)
  895. fp = stdin;
  896. else if ((fp = fopen(file, "r")) == NULL) {
  897. paxwarn(1, "Unable to open file '%s' for read", file);
  898. tar_usage();
  899. }
  900. while ((str = getline(fp)) != NULL) {
  901. if (ftree_add(str, 0) < 0)
  902. tar_usage();
  903. }
  904. if (strcmp(file, "-") != 0)
  905. fclose(fp);
  906. if (getline_error) {
  907. paxwarn(1, "Problem with file '%s'",
  908. file);
  909. tar_usage();
  910. }
  911. } else if (strcmp(*argv, "-C") == 0) {
  912. if (*++argv == NULL)
  913. break;
  914. if (ftree_add(*argv++, 1) < 0)
  915. tar_usage();
  916. } else if (ftree_add(*argv++, 0) < 0)
  917. tar_usage();
  918. }
  919. /*
  920. * no read errors allowed on updates/append operation!
  921. */
  922. maxflt = 0;
  923. break;
  924. }
  925. if (!fstdin && ((arcname == NULL) || (*arcname == '\0'))) {
  926. arcname = getenv("TAPE");
  927. if ((arcname == NULL) || (*arcname == '\0'))
  928. arcname = _PATH_DEFTAPE;
  929. }
  930. }
  931. static int
  932. mkpath(char *path)
  933. {
  934. struct stat sb;
  935. char *slash;
  936. int done = 0;
  937. slash = path;
  938. while (!done) {
  939. slash += strspn(slash, "/");
  940. slash += strcspn(slash, "/");
  941. done = (*slash == '\0');
  942. *slash = '\0';
  943. if (stat(path, &sb)) {
  944. if (errno != ENOENT || mkdir(path, 0777)) {
  945. paxwarn(1, "%s", path);
  946. return (-1);
  947. }
  948. } else if (!S_ISDIR(sb.st_mode)) {
  949. syswarn(1, ENOTDIR, "%s", path);
  950. return (-1);
  951. }
  952. if (!done)
  953. *slash = '/';
  954. }
  955. return (0);
  956. }
  957. /*
  958. * cpio_options()
  959. * look at the user specified flags. set globals as required and check if
  960. * the user specified a legal set of flags. If not, complain and exit
  961. */
  962. static void
  963. cpio_options(int argc, char **argv)
  964. {
  965. int c;
  966. size_t i;
  967. char *str;
  968. FSUB tmp;
  969. FILE *fp;
  970. kflag = 1;
  971. pids = 1;
  972. pmode = 1;
  973. pmtime = 0;
  974. arcname = NULL;
  975. dflag = 1;
  976. act = -1;
  977. nodirs = 1;
  978. while ((c=getopt(argc,argv,"abcdfiklmoprstuvzABC:E:F:H:I:LO:SZ6")) != -1)
  979. switch (c) {
  980. case 'a':
  981. /*
  982. * preserve access time on files read
  983. */
  984. tflag = 1;
  985. break;
  986. case 'b':
  987. /*
  988. * swap bytes and half-words when reading data
  989. */
  990. break;
  991. case 'c':
  992. /*
  993. * ASCII cpio header
  994. */
  995. frmt = &(fsub[F_ACPIO]);
  996. break;
  997. case 'd':
  998. /*
  999. * create directories as needed
  1000. */
  1001. nodirs = 0;
  1002. break;
  1003. case 'f':
  1004. /*
  1005. * invert meaning of pattern list
  1006. */
  1007. cflag = 1;
  1008. break;
  1009. case 'i':
  1010. /*
  1011. * restore an archive
  1012. */
  1013. act = EXTRACT;
  1014. break;
  1015. case 'k':
  1016. break;
  1017. case 'l':
  1018. /*
  1019. * use links instead of copies when possible
  1020. */
  1021. lflag = 1;
  1022. break;
  1023. case 'm':
  1024. /*
  1025. * preserve modification time
  1026. */
  1027. pmtime = 1;
  1028. break;
  1029. case 'o':
  1030. /*
  1031. * create an archive
  1032. */
  1033. act = ARCHIVE;
  1034. frmt = &(fsub[F_CPIO]);
  1035. break;
  1036. case 'p':
  1037. /*
  1038. * copy-pass mode
  1039. */
  1040. act = COPY;
  1041. break;
  1042. case 'r':
  1043. /*
  1044. * interactively rename files
  1045. */
  1046. iflag = 1;
  1047. break;
  1048. case 's':
  1049. /*
  1050. * swap bytes after reading data
  1051. */
  1052. break;
  1053. case 't':
  1054. /*
  1055. * list contents of archive
  1056. */
  1057. act = LIST;
  1058. listf = stdout;
  1059. break;
  1060. case 'u':
  1061. /*
  1062. * replace newer files
  1063. */
  1064. kflag = 0;
  1065. break;
  1066. case 'v':
  1067. /*
  1068. * verbose operation mode
  1069. */
  1070. vflag = 1;
  1071. break;
  1072. case 'z':
  1073. /*
  1074. * use gzip. Non standard option.
  1075. */
  1076. gzip_program = GZIP_CMD;
  1077. break;
  1078. case 'A':
  1079. /*
  1080. * append mode
  1081. */
  1082. act = APPND;
  1083. break;
  1084. case 'B':
  1085. /*
  1086. * Use 5120 byte block size
  1087. */
  1088. wrblksz = 5120;
  1089. break;
  1090. case 'C':
  1091. /*
  1092. * set block size in bytes
  1093. */
  1094. wrblksz = atoi(optarg);
  1095. break;
  1096. case 'E':
  1097. /*
  1098. * file with patterns to extract or list
  1099. */
  1100. if ((fp = fopen(optarg, "r")) == NULL) {
  1101. paxwarn(1, "Unable to open file '%s' for read", optarg);
  1102. cpio_usage();
  1103. }
  1104. while ((str = getline(fp)) != NULL) {
  1105. pat_add(str, NULL);
  1106. }
  1107. fclose(fp);
  1108. if (getline_error) {
  1109. paxwarn(1, "Problem with file '%s'", optarg);
  1110. cpio_usage();
  1111. }
  1112. break;
  1113. case 'F':
  1114. case 'I':
  1115. case 'O':
  1116. /*
  1117. * filename where the archive is stored
  1118. */
  1119. if ((optarg[0] == '-') && (optarg[1]== '\0')) {
  1120. /*
  1121. * treat a - as stdin
  1122. */
  1123. arcname = NULL;
  1124. break;
  1125. }
  1126. arcname = optarg;
  1127. break;
  1128. case 'H':
  1129. /*
  1130. * specify an archive format on write
  1131. */
  1132. tmp.name = optarg;
  1133. if ((frmt = (FSUB *)bsearch((void *)&tmp, (void *)fsub,
  1134. sizeof(fsub)/sizeof(FSUB), sizeof(FSUB), c_frmt)) != NULL)
  1135. break;
  1136. paxwarn(1, "Unknown -H format: %s", optarg);
  1137. (void)fputs("cpio: Known -H formats are:", stderr);
  1138. for (i = 0; i < (sizeof(fsub)/sizeof(FSUB)); ++i)
  1139. (void)fprintf(stderr, " %s", fsub[i].name);
  1140. (void)fputs("\n\n", stderr);
  1141. cpio_usage();
  1142. break;
  1143. case 'L':
  1144. /*
  1145. * follow symbolic links
  1146. */
  1147. Lflag = 1;
  1148. break;
  1149. case 'S':
  1150. /*
  1151. * swap halfwords after reading data
  1152. */
  1153. break;
  1154. case 'Z':
  1155. /*
  1156. * use compress. Non standard option.
  1157. */
  1158. gzip_program = COMPRESS_CMD;
  1159. break;
  1160. case '6':
  1161. /*
  1162. * process Version 6 cpio format
  1163. */
  1164. frmt = &(fsub[F_OCPIO]);
  1165. break;
  1166. case '?':
  1167. default:
  1168. cpio_usage();
  1169. break;
  1170. }
  1171. argc -= optind;
  1172. argv += optind;
  1173. /*
  1174. * process the args as they are interpreted by the operation mode
  1175. */
  1176. switch (act) {
  1177. case LIST:
  1178. case EXTRACT:
  1179. while (*argv != NULL)
  1180. if (pat_add(*argv++, NULL) < 0)
  1181. cpio_usage();
  1182. break;
  1183. case COPY:
  1184. if (*argv == NULL) {
  1185. paxwarn(0, "Destination directory was not supplied");
  1186. cpio_usage();
  1187. }
  1188. dirptr = *argv;
  1189. if (mkpath(dirptr) < 0)
  1190. cpio_usage();
  1191. --argc;
  1192. ++argv;
  1193. /* FALLTHROUGH */
  1194. case ARCHIVE:
  1195. case APPND:
  1196. if (*argv != NULL)
  1197. cpio_usage();
  1198. /*
  1199. * no read errors allowed on updates/append operation!
  1200. */
  1201. maxflt = 0;
  1202. while ((str = getline(stdin)) != NULL) {
  1203. ftree_add(str, 0);
  1204. }
  1205. if (getline_error) {
  1206. paxwarn(1, "Problem while reading stdin");
  1207. cpio_usage();
  1208. }
  1209. break;
  1210. default:
  1211. cpio_usage();
  1212. break;
  1213. }
  1214. }
  1215. /*
  1216. * printflg()
  1217. * print out those invalid flag sets found to the user
  1218. */
  1219. static void
  1220. printflg(unsigned int flg)
  1221. {
  1222. int nxt;
  1223. int pos = 0;
  1224. (void)fprintf(stderr,"%s: Invalid combination of options:", argv0);
  1225. while ((nxt = ffs(flg)) != 0) {
  1226. flg = flg >> nxt;
  1227. pos += nxt;
  1228. (void)fprintf(stderr, " -%c", flgch[pos-1]);
  1229. }
  1230. (void)putc('\n', stderr);
  1231. }
  1232. /*
  1233. * c_frmt()
  1234. * comparison routine used by bsearch to find the format specified
  1235. * by the user
  1236. */
  1237. static int
  1238. c_frmt(const void *a, const void *b)
  1239. {
  1240. return(strcmp(((const FSUB *)a)->name, ((const FSUB *)b)->name));
  1241. }
  1242. /*
  1243. * opt_next()
  1244. * called by format specific options routines to get each format specific
  1245. * flag and value specified with -o
  1246. * Return:
  1247. * pointer to next OPLIST entry or NULL (end of list).
  1248. */
  1249. OPLIST *
  1250. opt_next(void)
  1251. {
  1252. OPLIST *opt;
  1253. if ((opt = ophead) != NULL)
  1254. ophead = ophead->fow;
  1255. return(opt);
  1256. }
  1257. /*
  1258. * bad_opt()
  1259. * generic routine used to complain about a format specific options
  1260. * when the format does not support options.
  1261. */
  1262. int
  1263. bad_opt(void)
  1264. {
  1265. OPLIST *opt;
  1266. if (ophead == NULL)
  1267. return(0);
  1268. /*
  1269. * print all we were given
  1270. */
  1271. paxwarn(1,"These format options are not supported");
  1272. while ((opt = opt_next()) != NULL)
  1273. (void)fprintf(stderr, "\t%s = %s\n", opt->name, opt->value);
  1274. pax_usage();
  1275. return(0);
  1276. }
  1277. /*
  1278. * opt_add()
  1279. * breaks the value supplied to -o into an option name and value. Options
  1280. * are given to -o in the form -o name-value,name=value
  1281. * multiple -o may be specified.
  1282. * Return:
  1283. * 0 if format in name=value format, -1 if -o is passed junk.
  1284. */
  1285. int
  1286. opt_add(const char *str)
  1287. {
  1288. OPLIST *opt;
  1289. char *frpt;
  1290. char *pt;
  1291. char *endpt;
  1292. char *lstr;
  1293. if ((str == NULL) || (*str == '\0')) {
  1294. paxwarn(0, "Invalid option name");
  1295. return(-1);
  1296. }
  1297. if ((lstr = strdup(str)) == NULL) {
  1298. paxwarn(0, "Unable to allocate space for option list");
  1299. return(-1);
  1300. }
  1301. frpt = endpt = lstr;
  1302. /*
  1303. * break into name and values pieces and stuff each one into a
  1304. * OPLIST structure. When we know the format, the format specific
  1305. * option function will go through this list
  1306. */
  1307. while ((frpt != NULL) && (*frpt != '\0')) {
  1308. if ((endpt = strchr(frpt, ',')) != NULL)
  1309. *endpt = '\0';
  1310. if ((pt = strchr(frpt, '=')) == NULL) {
  1311. paxwarn(0, "Invalid options format");
  1312. free(lstr);
  1313. return(-1);
  1314. }
  1315. if ((opt = (OPLIST *)malloc(sizeof(OPLIST))) == NULL) {
  1316. paxwarn(0, "Unable to allocate space for option list");
  1317. free(lstr);
  1318. return(-1);
  1319. }
  1320. *pt++ = '\0';
  1321. opt->name = frpt;
  1322. opt->value = pt;
  1323. opt->fow = NULL;
  1324. if (endpt != NULL)
  1325. frpt = endpt + 1;
  1326. else
  1327. frpt = NULL;
  1328. if (ophead == NULL) {
  1329. optail = ophead = opt;
  1330. continue;
  1331. }
  1332. optail->fow = opt;
  1333. optail = opt;
  1334. }
  1335. return(0);
  1336. }
  1337. /*
  1338. * str_offt()
  1339. * Convert an expression of the following forms to an off_t > 0.
  1340. * 1) A positive decimal number.
  1341. * 2) A positive decimal number followed by a b (mult by 512).
  1342. * 3) A positive decimal number followed by a k (mult by 1024).
  1343. * 4) A positive decimal number followed by a m (mult by 512).
  1344. * 5) A positive decimal number followed by a w (mult by sizeof int)
  1345. * 6) Two or more positive decimal numbers (with/without k,b or w).
  1346. * separated by x (also * for backwards compatibility), specifying
  1347. * the product of the indicated values.
  1348. * Return:
  1349. * 0 for an error, a positive value o.w.
  1350. */
  1351. static off_t
  1352. str_offt(char *val)
  1353. {
  1354. char *expr;
  1355. off_t num, t;
  1356. # ifdef NET2_STAT
  1357. num = strtol(val, &expr, 0);
  1358. if ((num == LONG_MAX) || (num <= 0) || (expr == val))
  1359. # else
  1360. num = strtoq(val, &expr, 0);
  1361. if ((num == QUAD_MAX) || (num <= 0) || (expr == val))
  1362. # endif
  1363. return(0);
  1364. switch(*expr) {
  1365. case 'b':
  1366. t = num;
  1367. num *= 512;
  1368. if (t > num)
  1369. return(0);
  1370. ++expr;
  1371. break;
  1372. case 'k':
  1373. t = num;
  1374. num *= 1024;
  1375. if (t > num)
  1376. return(0);
  1377. ++expr;
  1378. break;
  1379. case 'm':
  1380. t = num;
  1381. num *= 1048576;
  1382. if (t > num)
  1383. return(0);
  1384. ++expr;
  1385. break;
  1386. case 'w':
  1387. t = num;
  1388. num *= sizeof(int);
  1389. if (t > num)
  1390. return(0);
  1391. ++expr;
  1392. break;
  1393. }
  1394. switch(*expr) {
  1395. case '\0':
  1396. break;
  1397. case '*':
  1398. case 'x':
  1399. t = num;
  1400. num *= str_offt(expr + 1);
  1401. if (t > num)
  1402. return(0);
  1403. break;
  1404. default:
  1405. return(0);
  1406. }
  1407. return(num);
  1408. }
  1409. char *
  1410. getline(FILE *f)
  1411. {
  1412. char *name, *temp;
  1413. size_t len;
  1414. name = fgetln(f, &len);
  1415. if (!name) {
  1416. getline_error = ferror(f) ? GETLINE_FILE_CORRUPT : 0;
  1417. return(0);
  1418. }
  1419. if (name[len-1] != '\n')
  1420. len++;
  1421. temp = malloc(len);
  1422. if (!temp) {
  1423. getline_error = GETLINE_OUT_OF_MEM;
  1424. return(0);
  1425. }
  1426. memcpy(temp, name, len-1);
  1427. temp[len-1] = 0;
  1428. return(temp);
  1429. }
  1430. /*
  1431. * no_op()
  1432. * for those option functions where the archive format has nothing to do.
  1433. * Return:
  1434. * 0
  1435. */
  1436. static int
  1437. no_op(void)
  1438. {
  1439. return(0);
  1440. }
  1441. /*
  1442. * pax_usage()
  1443. * print the usage summary to the user
  1444. */
  1445. void
  1446. pax_usage(void)
  1447. {
  1448. (void)fputs("usage: pax [-cdnvz] [-E limit] [-f archive] ", stderr);
  1449. (void)fputs("[-s replstr] ... [-U user] ...", stderr);
  1450. (void)fputs("\n [-G group] ... ", stderr);
  1451. (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
  1452. (void)fputs("[pattern ...]\n", stderr);
  1453. (void)fputs(" pax -r [-cdiknuvzDYZ] [-E limit] ", stderr);
  1454. (void)fputs("[-f archive] [-o options] ... \n", stderr);
  1455. (void)fputs(" [-p string] ... [-s replstr] ... ", stderr);
  1456. (void)fputs("[-U user] ... [-G group] ...\n ", stderr);
  1457. (void)fputs("[-T [from_date][,to_date]] ... ", stderr);
  1458. (void)fputs(" [pattern ...]\n", stderr);
  1459. (void)fputs(" pax -w [-dituvzHLPX] [-b blocksize] ", stderr);
  1460. (void)fputs("[ [-a] [-f archive] ] [-x format] \n", stderr);
  1461. (void)fputs(" [-B bytes] [-s replstr] ... ", stderr);
  1462. (void)fputs("[-o options] ... [-U user] ...", stderr);
  1463. (void)fputs("\n [-G group] ... ", stderr);
  1464. (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
  1465. (void)fputs("[file ...]\n", stderr);
  1466. (void)fputs(" pax -r -w [-diklntuvDHLPXYZ] ", stderr);
  1467. (void)fputs("[-p string] ... [-s replstr] ...", stderr);
  1468. (void)fputs("\n [-U user] ... [-G group] ... ", stderr);
  1469. (void)fputs("[-T [from_date][,to_date][/[c][m]]] ... ", stderr);
  1470. (void)fputs("\n [file ...] directory\n", stderr);
  1471. exit(1);
  1472. }
  1473. /*
  1474. * tar_usage()
  1475. * print the usage summary to the user
  1476. */
  1477. void
  1478. tar_usage(void)
  1479. {
  1480. (void)fputs("usage: tar [-]{crtux}[-befhjmopqsvwyzHLOPXZ014578] [blocksize] ",
  1481. stderr);
  1482. (void)fputs("[archive] [replstr] [-C directory] [-I file] [file ...]\n",
  1483. stderr);
  1484. exit(1);
  1485. }
  1486. /*
  1487. * cpio_usage()
  1488. * print the usage summary to the user
  1489. */
  1490. void
  1491. cpio_usage(void)
  1492. {
  1493. (void)fputs("usage: cpio -o [-aABcLvVzZ] [-C bytes] [-H format] [-O archive]\n", stderr);
  1494. (void)fputs(" [-F archive] < name-list [> archive]\n", stderr);
  1495. (void)fputs(" cpio -i [-bBcdfmnrsStuvVzZ6] [-C bytes] [-E file] [-H format]\n", stderr);
  1496. (void)fputs(" [-I archive] [-F archive] [pattern...] [< archive]\n", stderr);
  1497. (void)fputs(" cpio -p [-adlLmuvV] destination-directory < name-list\n", stderr);
  1498. exit(1);
  1499. }