tar.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  1. /* A tar (tape archiver) program.
  2. Copyright (C) 1988, 92,93,94,95,96,97, 1999 Free Software Foundation, Inc.
  3. Written by John Gilmore, starting 1985-08-25.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  11. Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. #include "system.h"
  16. #include <getopt.h>
  17. #include <signal.h>
  18. #if ! defined SIGCHLD && defined SIGCLD
  19. # define SIGCHLD SIGCLD
  20. #endif
  21. /* The following causes "common.h" to produce definitions of all the global
  22. variables, rather than just "extern" declarations of them. GNU tar does
  23. depend on the system loader to preset all GLOBAL variables to neutral (or
  24. zero) values, explicit initialisation is usually not done. */
  25. #define GLOBAL
  26. #include "common.h"
  27. #include "xstrtol.h"
  28. time_t get_date ();
  29. /* Local declarations. */
  30. #ifndef DEFAULT_ARCHIVE
  31. # define DEFAULT_ARCHIVE "tar.out"
  32. #endif
  33. #ifndef DEFAULT_BLOCKING
  34. # define DEFAULT_BLOCKING 20
  35. #endif
  36. static void usage PARAMS ((int));
  37. /* Miscellaneous. */
  38. /*----------------------------------------------.
  39. | Doesn't return if stdin already requested. |
  40. `----------------------------------------------*/
  41. /* Name of option using stdin. */
  42. static const char *stdin_used_by = NULL;
  43. void
  44. request_stdin (const char *option)
  45. {
  46. if (stdin_used_by)
  47. USAGE_ERROR ((0, 0, _("Options `-%s' and `-%s' both want standard input"),
  48. stdin_used_by, option));
  49. stdin_used_by = option;
  50. }
  51. /*--------------------------------------------------------.
  52. | Returns true if and only if the user typed 'y' or 'Y'. |
  53. `--------------------------------------------------------*/
  54. int
  55. confirm (const char *message_action, const char *message_name)
  56. {
  57. static FILE *confirm_file = NULL;
  58. if (!confirm_file)
  59. {
  60. if (archive == 0 || stdin_used_by)
  61. confirm_file = fopen (TTY_NAME, "r");
  62. else
  63. {
  64. request_stdin ("-w");
  65. confirm_file = stdin;
  66. }
  67. if (!confirm_file)
  68. FATAL_ERROR ((0, 0, _("Cannot read confirmation from user")));
  69. }
  70. fprintf (stdlis, "%s %s?", message_action, message_name);
  71. fflush (stdlis);
  72. {
  73. int reply = getc (confirm_file);
  74. int character;
  75. for (character = reply;
  76. character != '\n' && character != EOF;
  77. character = getc (confirm_file))
  78. continue;
  79. return reply == 'y' || reply == 'Y';
  80. }
  81. }
  82. /* Options. */
  83. /* For long options that unconditionally set a single flag, we have getopt
  84. do it. For the others, we share the code for the equivalent short
  85. named option, the name of which is stored in the otherwise-unused `val'
  86. field of the `struct option'; for long options that have no equivalent
  87. short option, we use non-characters as pseudo short options,
  88. starting at CHAR_MAX + 1 and going upwards. */
  89. enum
  90. {
  91. BACKUP_OPTION = CHAR_MAX + 1,
  92. DELETE_OPTION,
  93. EXCLUDE_OPTION,
  94. GROUP_OPTION,
  95. MODE_OPTION,
  96. NEWER_MTIME_OPTION,
  97. NO_RECURSE_OPTION,
  98. NULL_OPTION,
  99. OWNER_OPTION,
  100. POSIX_OPTION,
  101. PRESERVE_OPTION,
  102. RECORD_SIZE_OPTION,
  103. RSH_COMMAND_OPTION,
  104. SUFFIX_OPTION,
  105. USE_COMPRESS_PROGRAM_OPTION,
  106. VOLNO_FILE_OPTION,
  107. /* Some cleanup is being made in GNU tar long options. Using old names is
  108. allowed for a while, but will also send a warning to stderr. Take old
  109. names out in 1.14, or in summer 1997, whichever happens last. */
  110. OBSOLETE_ABSOLUTE_NAMES,
  111. OBSOLETE_BLOCK_COMPRESS,
  112. OBSOLETE_BLOCKING_FACTOR,
  113. OBSOLETE_BLOCK_NUMBER,
  114. OBSOLETE_READ_FULL_RECORDS,
  115. OBSOLETE_TOUCH,
  116. OBSOLETE_VERSION_CONTROL
  117. };
  118. /* If nonzero, display usage information and exit. */
  119. static int show_help = 0;
  120. /* If nonzero, print the version on standard output and exit. */
  121. static int show_version = 0;
  122. struct option long_options[] =
  123. {
  124. {"absolute-names", no_argument, NULL, 'P'},
  125. {"absolute-paths", no_argument, NULL, OBSOLETE_ABSOLUTE_NAMES},
  126. {"after-date", required_argument, NULL, 'N'},
  127. {"append", no_argument, NULL, 'r'},
  128. {"atime-preserve", no_argument, &atime_preserve_option, 1},
  129. {"backup", optional_argument, NULL, BACKUP_OPTION},
  130. {"block-compress", no_argument, NULL, OBSOLETE_BLOCK_COMPRESS},
  131. {"block-number", no_argument, NULL, 'R'},
  132. {"block-size", required_argument, NULL, OBSOLETE_BLOCKING_FACTOR},
  133. {"blocking-factor", required_argument, NULL, 'b'},
  134. {"bzip2", no_argument, NULL, 'y'},
  135. {"catenate", no_argument, NULL, 'A'},
  136. {"checkpoint", no_argument, &checkpoint_option, 1},
  137. {"compare", no_argument, NULL, 'd'},
  138. {"compress", no_argument, NULL, 'Z'},
  139. {"concatenate", no_argument, NULL, 'A'},
  140. {"confirmation", no_argument, NULL, 'w'},
  141. /* FIXME: --selective as a synonym for --confirmation? */
  142. {"create", no_argument, NULL, 'c'},
  143. {"delete", no_argument, NULL, DELETE_OPTION},
  144. {"dereference", no_argument, NULL, 'h'},
  145. {"diff", no_argument, NULL, 'd'},
  146. {"directory", required_argument, NULL, 'C'},
  147. {"exclude", required_argument, NULL, EXCLUDE_OPTION},
  148. {"exclude-from", required_argument, NULL, 'X'},
  149. {"extract", no_argument, NULL, 'x'},
  150. {"file", required_argument, NULL, 'f'},
  151. {"files-from", required_argument, NULL, 'T'},
  152. {"force-local", no_argument, &force_local_option, 1},
  153. {"get", no_argument, NULL, 'x'},
  154. {"group", required_argument, NULL, GROUP_OPTION},
  155. {"gunzip", no_argument, NULL, 'z'},
  156. {"gzip", no_argument, NULL, 'z'},
  157. {"help", no_argument, &show_help, 1},
  158. {"ignore-failed-read", no_argument, &ignore_failed_read_option, 1},
  159. {"ignore-zeros", no_argument, NULL, 'i'},
  160. /* FIXME: --ignore-end as a new name for --ignore-zeros? */
  161. {"incremental", no_argument, NULL, 'G'},
  162. {"info-script", required_argument, NULL, 'F'},
  163. {"interactive", no_argument, NULL, 'w'},
  164. {"keep-old-files", no_argument, NULL, 'k'},
  165. {"label", required_argument, NULL, 'V'},
  166. {"list", no_argument, NULL, 't'},
  167. {"listed-incremental", required_argument, NULL, 'g'},
  168. {"mode", required_argument, NULL, MODE_OPTION},
  169. {"modification-time", no_argument, NULL, OBSOLETE_TOUCH},
  170. {"multi-volume", no_argument, NULL, 'M'},
  171. {"new-volume-script", required_argument, NULL, 'F'},
  172. {"newer", required_argument, NULL, 'N'},
  173. {"newer-mtime", required_argument, NULL, NEWER_MTIME_OPTION},
  174. {"null", no_argument, NULL, NULL_OPTION},
  175. {"no-recursion", no_argument, NULL, NO_RECURSE_OPTION},
  176. {"numeric-owner", no_argument, &numeric_owner_option, 1},
  177. {"old-archive", no_argument, NULL, 'o'},
  178. {"one-file-system", no_argument, NULL, 'l'},
  179. {"owner", required_argument, NULL, OWNER_OPTION},
  180. {"portability", no_argument, NULL, 'o'},
  181. {"posix", no_argument, NULL, POSIX_OPTION},
  182. {"preserve", no_argument, NULL, PRESERVE_OPTION},
  183. {"preserve-order", no_argument, NULL, 's'},
  184. {"preserve-permissions", no_argument, NULL, 'p'},
  185. {"recursive-unlink", no_argument, &recursive_unlink_option, 1},
  186. {"read-full-blocks", no_argument, NULL, OBSOLETE_READ_FULL_RECORDS},
  187. {"read-full-records", no_argument, NULL, 'B'},
  188. /* FIXME: --partial-blocks might be a synonym for --read-full-records? */
  189. {"record-number", no_argument, NULL, OBSOLETE_BLOCK_NUMBER},
  190. {"record-size", required_argument, NULL, RECORD_SIZE_OPTION},
  191. {"remove-files", no_argument, &remove_files_option, 1},
  192. {"rsh-command", required_argument, NULL, RSH_COMMAND_OPTION},
  193. {"same-order", no_argument, NULL, 's'},
  194. {"same-owner", no_argument, &same_owner_option, 1},
  195. {"same-permissions", no_argument, NULL, 'p'},
  196. {"show-omitted-dirs", no_argument, &show_omitted_dirs_option, 1},
  197. {"sparse", no_argument, NULL, 'S'},
  198. {"starting-file", required_argument, NULL, 'K'},
  199. {"suffix", required_argument, NULL, SUFFIX_OPTION},
  200. {"tape-length", required_argument, NULL, 'L'},
  201. {"to-stdout", no_argument, NULL, 'O'},
  202. {"totals", no_argument, &totals_option, 1},
  203. {"touch", no_argument, NULL, 'm'},
  204. {"uncompress", no_argument, NULL, 'Z'},
  205. {"ungzip", no_argument, NULL, 'z'},
  206. {"unlink-first", no_argument, NULL, 'U'},
  207. {"update", no_argument, NULL, 'u'},
  208. {"use-compress-program", required_argument, NULL, USE_COMPRESS_PROGRAM_OPTION},
  209. {"verbose", no_argument, NULL, 'v'},
  210. {"verify", no_argument, NULL, 'W'},
  211. {"version", no_argument, &show_version, 1},
  212. {"version-control", required_argument, NULL, OBSOLETE_VERSION_CONTROL},
  213. {"volno-file", required_argument, NULL, VOLNO_FILE_OPTION},
  214. {0, 0, 0, 0}
  215. };
  216. /*---------------------------------------------.
  217. | Print a usage message and exit with STATUS. |
  218. `---------------------------------------------*/
  219. static void
  220. usage (int status)
  221. {
  222. if (status != TAREXIT_SUCCESS)
  223. fprintf (stderr, _("Try `%s --help' for more information.\n"),
  224. program_name);
  225. else
  226. {
  227. fputs (_("\
  228. GNU `tar' saves many files together into a single tape or disk archive, and\n\
  229. can restore individual files from the archive.\n"),
  230. stdout);
  231. printf (_("\nUsage: %s [OPTION]... [FILE]...\n"), program_name);
  232. fputs (_("\
  233. \n\
  234. If a long option shows an argument as mandatory, then it is mandatory\n\
  235. for the equivalent short option also. Similarly for optional arguments.\n"),
  236. stdout);
  237. fputs(_("\
  238. \n\
  239. Main operation mode:\n\
  240. -t, --list list the contents of an archive\n\
  241. -x, --extract, --get extract files from an archive\n\
  242. -c, --create create a new archive\n\
  243. -d, --diff, --compare find differences between archive and file system\n\
  244. -r, --append append files to the end of an archive\n\
  245. -u, --update only append files newer than copy in archive\n\
  246. -A, --catenate append tar files to an archive\n\
  247. --concatenate same as -A\n\
  248. --delete delete from the archive (not on mag tapes!)\n"),
  249. stdout);
  250. fputs (_("\
  251. \n\
  252. Operation modifiers:\n\
  253. -W, --verify attempt to verify the archive after writing it\n\
  254. --remove-files remove files after adding them to the archive\n\
  255. -k, --keep-old-files don't overwrite existing files when extracting\n\
  256. -U, --unlink-first remove each file prior to extracting over it\n\
  257. --recursive-unlink empty hierarchies prior to extracting directory\n\
  258. -S, --sparse handle sparse files efficiently\n\
  259. -O, --to-stdout extract files to standard output\n\
  260. -G, --incremental handle old GNU-format incremental backup\n\
  261. -g, --listed-incremental handle new GNU-format incremental backup\n\
  262. --ignore-failed-read do not exit with nonzero on unreadable files\n"),
  263. stdout);
  264. fputs (_("\
  265. \n\
  266. Handling of file attributes:\n\
  267. --owner=NAME force NAME as owner for added files\n\
  268. --group=NAME force NAME as group for added files\n\
  269. --mode=CHANGES force (symbolic) mode CHANGES for added files\n\
  270. --atime-preserve don't change access times on dumped files\n\
  271. -m, --modification-time don't extract file modified time\n\
  272. --same-owner try extracting files with the same ownership\n\
  273. --numeric-owner always use numbers for user/group names\n\
  274. -p, --same-permissions extract all protection information\n\
  275. --preserve-permissions same as -p\n\
  276. -s, --same-order sort names to extract to match archive\n\
  277. --preserve-order same as -s\n\
  278. --preserve same as both -p and -s\n"),
  279. stdout);
  280. fputs (_("\
  281. \n\
  282. Device selection and switching:\n\
  283. -f, --file=ARCHIVE use archive file or device ARCHIVE\n\
  284. --force-local archive file is local even if has a colon\n\
  285. --rsh-command=COMMAND use remote COMMAND instead of rsh\n\
  286. -[0-7][lmh] specify drive and density\n\
  287. -M, --multi-volume create/list/extract multi-volume archive\n\
  288. -L, --tape-length=NUM change tape after writing NUM x 1024 bytes\n\
  289. -F, --info-script=FILE run script at end of each tape (implies -M)\n\
  290. --new-volume-script=FILE same as -F FILE\n\
  291. --volno-file=FILE use/update the volume number in FILE\n"),
  292. stdout);
  293. fputs (_("\
  294. \n\
  295. Device blocking:\n\
  296. -b, --blocking-factor=BLOCKS BLOCKS x 512 bytes per record\n\
  297. --record-size=SIZE SIZE bytes per record, multiple of 512\n\
  298. -i, --ignore-zeros ignore zeroed blocks in archive (means EOF)\n\
  299. -B, --read-full-records reblock as we read (for 4.2BSD pipes)\n"),
  300. stdout);
  301. fputs (_("\
  302. \n\
  303. Archive format selection:\n\
  304. -V, --label=NAME create archive with volume name NAME\n\
  305. PATTERN at list/extract time, a globbing PATTERN\n\
  306. -o, --old-archive, --portability write a V7 format archive\n\
  307. --posix write a POSIX conformant archive\n\
  308. -y, --bzip2 filter the archive through bzip2\n\
  309. -z, --gzip, --ungzip filter the archive through gzip\n\
  310. -Z, --compress, --uncompress filter the archive through compress\n\
  311. --use-compress-program=PROG filter through PROG (must accept -d)\n"),
  312. stdout);
  313. fputs (_("\
  314. \n\
  315. Local file selection:\n\
  316. -C, --directory=DIR change to directory DIR\n\
  317. -T, --files-from=NAME get names to extract or create from file NAME\n\
  318. --null -T reads null-terminated names, disable -C\n\
  319. --exclude=PATTERN exclude files, given as a globbing PATTERN\n\
  320. -X, --exclude-from=FILE exclude globbing patterns listed in FILE\n\
  321. -P, --absolute-names don't strip leading `/'s from file names\n\
  322. -h, --dereference dump instead the files symlinks point to\n\
  323. --no-recursion avoid descending automatically in directories\n\
  324. -l, --one-file-system stay in local file system when creating archive\n\
  325. -K, --starting-file=NAME begin at file NAME in the archive\n"),
  326. stdout);
  327. #if !MSDOS
  328. fputs (_("\
  329. -N, --newer=DATE only store files newer than DATE\n\
  330. --newer-mtime compare date and time when data changed only\n\
  331. --after-date=DATE same as -N\n"),
  332. stdout);
  333. #endif
  334. fputs (_("\
  335. --backup[=CONTROL] backup before removal, choose version control\n\
  336. --suffix=SUFFIX backup before removel, override usual suffix\n"),
  337. stdout);
  338. fputs (_("\
  339. \n\
  340. Informative output:\n\
  341. --help print this help, then exit\n\
  342. --version print tar program version number, then exit\n\
  343. -v, --verbose verbosely list files processed\n\
  344. --checkpoint print directory names while reading the archive\n\
  345. --totals print total bytes written while creating archive\n\
  346. -R, --block-number show block number within archive with each message\n\
  347. -w, --interactive ask for confirmation for every action\n\
  348. --confirmation same as -w\n"),
  349. stdout);
  350. fputs (_("\
  351. \n\
  352. The backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
  353. The version control may be set with --backup or VERSION_CONTROL, values are:\n\
  354. \n\
  355. t, numbered make numbered backups\n\
  356. nil, existing numbered if numbered backups exist, simple otherwise\n\
  357. never, simple always make simple backups\n"),
  358. stdout);
  359. printf (_("\
  360. \n\
  361. GNU tar cannot read nor produce `--posix' archives. If POSIXLY_CORRECT\n\
  362. is set in the environment, GNU extensions are disallowed with `--posix'.\n\
  363. Support for POSIX is only partially implemented, don't count on it yet.\n\
  364. ARCHIVE may be FILE, HOST:FILE or USER@HOST:FILE; and FILE may be a file\n\
  365. or a device. *This* `tar' defaults to `-f%s -b%d'.\n"),
  366. DEFAULT_ARCHIVE, DEFAULT_BLOCKING);
  367. fputs (_("\
  368. \n\
  369. Report bugs to <bug-tar@gnu.org>.\n"),
  370. stdout);
  371. }
  372. exit (status);
  373. }
  374. /*----------------------------.
  375. | Parse the options for tar. |
  376. `----------------------------*/
  377. /* Available option letters are DEHIJQY and aejnqy. Some are reserved:
  378. y per-file gzip compression
  379. Y per-block gzip compression */
  380. #define OPTION_STRING \
  381. "-01234567ABC:F:GK:L:MN:OPRST:UV:WX:Zb:cdf:g:hiklmoprstuvwxyz"
  382. static void
  383. set_subcommand_option (enum subcommand subcommand)
  384. {
  385. if (subcommand_option != UNKNOWN_SUBCOMMAND
  386. && subcommand_option != subcommand)
  387. USAGE_ERROR ((0, 0,
  388. _("You may not specify more than one `-Acdtrux' option")));
  389. subcommand_option = subcommand;
  390. }
  391. static void
  392. set_use_compress_program_option (const char *string)
  393. {
  394. if (use_compress_program_option && strcmp (use_compress_program_option, string) != 0)
  395. USAGE_ERROR ((0, 0, _("Conflicting compression options")));
  396. use_compress_program_option = string;
  397. }
  398. /* Ignore DUMMY (which will always be null in practice), and add
  399. PATTERN to the proper set of patterns to be excluded -- either
  400. patterns with slashes, or patterns without. */
  401. static void
  402. add_filtered_exclude (struct exclude *dummy, char const *pattern)
  403. {
  404. add_exclude ((strchr (pattern, '/')
  405. ? excluded_with_slash
  406. : excluded_without_slash),
  407. pattern);
  408. }
  409. static void
  410. decode_options (int argc, char *const *argv)
  411. {
  412. int optchar; /* option letter */
  413. int input_files; /* number of input files */
  414. const char *backup_suffix_string;
  415. const char *version_control_string = NULL;
  416. /* Set some default option values. */
  417. subcommand_option = UNKNOWN_SUBCOMMAND;
  418. archive_format = DEFAULT_FORMAT;
  419. blocking_factor = DEFAULT_BLOCKING;
  420. record_size = DEFAULT_BLOCKING * BLOCKSIZE;
  421. excluded_with_slash = new_exclude ();
  422. excluded_without_slash = new_exclude ();
  423. newer_mtime_option = TYPE_MINIMUM (time_t);
  424. owner_option = -1;
  425. group_option = -1;
  426. backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  427. /* Convert old-style tar call by exploding option element and rearranging
  428. options accordingly. */
  429. if (argc > 1 && argv[1][0] != '-')
  430. {
  431. int new_argc; /* argc value for rearranged arguments */
  432. char **new_argv; /* argv value for rearranged arguments */
  433. char *const *in; /* cursor into original argv */
  434. char **out; /* cursor into rearranged argv */
  435. const char *letter; /* cursor into old option letters */
  436. char buffer[3]; /* constructed option buffer */
  437. const char *cursor; /* cursor in OPTION_STRING */
  438. /* Initialize a constructed option. */
  439. buffer[0] = '-';
  440. buffer[2] = '\0';
  441. /* Allocate a new argument array, and copy program name in it. */
  442. new_argc = argc - 1 + strlen (argv[1]);
  443. new_argv = (char **) xmalloc (new_argc * sizeof (char *));
  444. in = argv;
  445. out = new_argv;
  446. *out++ = *in++;
  447. /* Copy each old letter option as a separate option, and have the
  448. corresponding argument moved next to it. */
  449. for (letter = *in++; *letter; letter++)
  450. {
  451. buffer[1] = *letter;
  452. *out++ = xstrdup (buffer);
  453. cursor = strchr (OPTION_STRING, *letter);
  454. if (cursor && cursor[1] == ':')
  455. {
  456. if (in < argv + argc)
  457. *out++ = *in++;
  458. else
  459. USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
  460. *letter));
  461. }
  462. }
  463. /* Copy all remaining options. */
  464. while (in < argv + argc)
  465. *out++ = *in++;
  466. /* Replace the old option list by the new one. */
  467. argc = new_argc;
  468. argv = new_argv;
  469. }
  470. /* Parse all options and non-options as they appear. */
  471. input_files = 0;
  472. while (optchar = getopt_long (argc, argv, OPTION_STRING, long_options, NULL),
  473. optchar != EOF)
  474. switch (optchar)
  475. {
  476. case '?':
  477. usage (TAREXIT_FAILURE);
  478. case 0:
  479. break;
  480. case 1:
  481. /* File name or non-parsed option, because of RETURN_IN_ORDER
  482. ordering triggerred by the leading dash in OPTION_STRING. */
  483. name_add (optarg);
  484. input_files++;
  485. break;
  486. case 'A':
  487. set_subcommand_option (CAT_SUBCOMMAND);
  488. break;
  489. case OBSOLETE_BLOCK_COMPRESS:
  490. WARN ((0, 0, _("Obsolete option, now implied by --blocking-factor")));
  491. break;
  492. case OBSOLETE_BLOCKING_FACTOR:
  493. WARN ((0, 0, _("Obsolete option name replaced by --blocking-factor")));
  494. /* Fall through. */
  495. case 'b':
  496. {
  497. uintmax_t u;
  498. if (! (xstrtoumax (optarg, (char **) 0, 10, &u, "") == LONGINT_OK
  499. && u == (blocking_factor = u)
  500. && 0 < blocking_factor
  501. && u == (record_size = u * (size_t) BLOCKSIZE) / BLOCKSIZE))
  502. USAGE_ERROR ((0, 0, _("Invalid blocking factor")));
  503. }
  504. break;
  505. case OBSOLETE_READ_FULL_RECORDS:
  506. WARN ((0, 0,
  507. _("Obsolete option name replaced by --read-full-records")));
  508. /* Fall through. */
  509. case 'B':
  510. /* Try to reblock input records. For reading 4.2BSD pipes. */
  511. /* It would surely make sense to exchange -B and -R, but it seems
  512. that -B has been used for a long while in Sun tar ans most
  513. BSD-derived systems. This is a consequence of the block/record
  514. terminology confusion. */
  515. read_full_records_option = 1;
  516. break;
  517. case 'c':
  518. set_subcommand_option (CREATE_SUBCOMMAND);
  519. break;
  520. case 'C':
  521. name_add ("-C");
  522. name_add (optarg);
  523. break;
  524. case 'd':
  525. set_subcommand_option (DIFF_SUBCOMMAND);
  526. break;
  527. case 'f':
  528. if (archive_names == allocated_archive_names)
  529. {
  530. allocated_archive_names *= 2;
  531. archive_name_array = (const char **)
  532. xrealloc (archive_name_array,
  533. sizeof (const char *) * allocated_archive_names);
  534. }
  535. archive_name_array[archive_names++] = optarg;
  536. break;
  537. case 'F':
  538. /* Since -F is only useful with -M, make it implied. Run this
  539. script at the end of each tape. */
  540. info_script_option = optarg;
  541. multi_volume_option = 1;
  542. break;
  543. case 'g':
  544. listed_incremental_option = optarg;
  545. /* Fall through. */
  546. case 'G':
  547. /* We are making an incremental dump (FIXME: are we?); save
  548. directories at the beginning of the archive, and include in each
  549. directory its contents. */
  550. incremental_option = 1;
  551. break;
  552. case 'h':
  553. /* Follow symbolic links. */
  554. dereference_option = 1;
  555. break;
  556. case 'i':
  557. /* Ignore zero blocks (eofs). This can't be the default,
  558. because Unix tar writes two blocks of zeros, then pads out
  559. the record with garbage. */
  560. ignore_zeros_option = 1;
  561. break;
  562. case 'k':
  563. /* Don't overwrite existing files. */
  564. keep_old_files_option = 1;
  565. break;
  566. case 'K':
  567. starting_file_option = 1;
  568. addname (optarg);
  569. break;
  570. case 'l':
  571. /* When dumping directories, don't dump files/subdirectories
  572. that are on other filesystems. */
  573. one_file_system_option = 1;
  574. break;
  575. case 'L':
  576. {
  577. uintmax_t u;
  578. if (xstrtoumax (optarg, (char **) 0, 10, &u, "") != LONG_MAX)
  579. USAGE_ERROR ((0, 0, _("Invalid tape length")));
  580. tape_length_option = 1024 * (tarlong) u;
  581. multi_volume_option = 1;
  582. }
  583. break;
  584. case OBSOLETE_TOUCH:
  585. WARN ((0, 0, _("Obsolete option name replaced by --touch")));
  586. /* Fall through. */
  587. case 'm':
  588. touch_option = 1;
  589. break;
  590. case 'M':
  591. /* Make multivolume archive: when we can't write any more into
  592. the archive, re-open it, and continue writing. */
  593. multi_volume_option = 1;
  594. break;
  595. #if !MSDOS
  596. case 'N':
  597. after_date_option = 1;
  598. /* Fall through. */
  599. case NEWER_MTIME_OPTION:
  600. if (newer_mtime_option != TYPE_MINIMUM (time_t))
  601. USAGE_ERROR ((0, 0, _("More than one threshold date")));
  602. newer_mtime_option = get_date (optarg, (voidstar) 0);
  603. if (newer_mtime_option == (time_t) -1)
  604. USAGE_ERROR ((0, 0, _("Invalid date format `%s'"), optarg));
  605. break;
  606. #endif /* not MSDOS */
  607. case 'o':
  608. if (archive_format == DEFAULT_FORMAT)
  609. archive_format = V7_FORMAT;
  610. else if (archive_format != V7_FORMAT)
  611. USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
  612. break;
  613. case 'O':
  614. to_stdout_option = 1;
  615. break;
  616. case 'p':
  617. same_permissions_option = 1;
  618. break;
  619. case OBSOLETE_ABSOLUTE_NAMES:
  620. WARN ((0, 0, _("Obsolete option name replaced by --absolute-names")));
  621. /* Fall through. */
  622. case 'P':
  623. absolute_names_option = 1;
  624. break;
  625. case 'r':
  626. set_subcommand_option (APPEND_SUBCOMMAND);
  627. break;
  628. case OBSOLETE_BLOCK_NUMBER:
  629. WARN ((0, 0, _("Obsolete option name replaced by --block-number")));
  630. /* Fall through. */
  631. case 'R':
  632. /* Print block numbers for debugging bad tar archives. */
  633. /* It would surely make sense to exchange -B and -R, but it seems
  634. that -B has been used for a long while in Sun tar ans most
  635. BSD-derived systems. This is a consequence of the block/record
  636. terminology confusion. */
  637. block_number_option = 1;
  638. break;
  639. case 's':
  640. /* Names to extr are sorted. */
  641. same_order_option = 1;
  642. break;
  643. case 'S':
  644. sparse_option = 1;
  645. break;
  646. case 't':
  647. set_subcommand_option (LIST_SUBCOMMAND);
  648. verbose_option++;
  649. break;
  650. case 'T':
  651. files_from_option = optarg;
  652. break;
  653. case 'u':
  654. set_subcommand_option (UPDATE_SUBCOMMAND);
  655. break;
  656. case 'U':
  657. unlink_first_option = 1;
  658. break;
  659. case 'v':
  660. verbose_option++;
  661. break;
  662. case 'V':
  663. volume_label_option = optarg;
  664. break;
  665. case 'w':
  666. interactive_option = 1;
  667. break;
  668. case 'W':
  669. verify_option = 1;
  670. break;
  671. case 'x':
  672. set_subcommand_option (EXTRACT_SUBCOMMAND);
  673. break;
  674. case 'X':
  675. if (add_exclude_file (add_filtered_exclude, NULL, optarg, '\n') != 0)
  676. FATAL_ERROR ((0, errno, "%s", optarg));
  677. break;
  678. case 'y':
  679. set_use_compress_program_option ("bzip2");
  680. break;
  681. case 'z':
  682. set_use_compress_program_option ("gzip");
  683. break;
  684. case 'Z':
  685. set_use_compress_program_option ("compress");
  686. break;
  687. case OBSOLETE_VERSION_CONTROL:
  688. WARN ((0, 0, _("Obsolete option name replaced by --backup")));
  689. /* Fall through. */
  690. case BACKUP_OPTION:
  691. backup_option = 1;
  692. if (optarg)
  693. version_control_string = optarg;
  694. break;
  695. case DELETE_OPTION:
  696. set_subcommand_option (DELETE_SUBCOMMAND);
  697. break;
  698. case EXCLUDE_OPTION:
  699. add_filtered_exclude (NULL, optarg);
  700. break;
  701. case GROUP_OPTION:
  702. if (! (strlen (optarg) < GNAME_FIELD_SIZE
  703. && gname_to_gid (optarg, &group_option)))
  704. {
  705. uintmax_t g;
  706. if (xstrtoumax (optarg, (char **) 0, 10, &g, "") == LONGINT_OK
  707. && g == (gid_t) g)
  708. group_option = g;
  709. else
  710. FATAL_ERROR ((0, 0, _("Invalid group given on option")));
  711. }
  712. break;
  713. case MODE_OPTION:
  714. mode_option
  715. = mode_compile (optarg,
  716. MODE_MASK_EQUALS | MODE_MASK_PLUS | MODE_MASK_MINUS);
  717. if (mode_option == MODE_INVALID)
  718. FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
  719. if (mode_option == MODE_MEMORY_EXHAUSTED)
  720. FATAL_ERROR ((0, 0, _("Memory exhausted")));
  721. break;
  722. case NO_RECURSE_OPTION:
  723. no_recurse_option = 1;
  724. break;
  725. case NULL_OPTION:
  726. filename_terminator = '\0';
  727. break;
  728. case OWNER_OPTION:
  729. if (! (strlen (optarg) < UNAME_FIELD_SIZE
  730. && uname_to_uid (optarg, &owner_option)))
  731. {
  732. uintmax_t u;
  733. if (xstrtoumax (optarg, (char **) 0, 10, &u, "") == LONGINT_OK
  734. && u == (uid_t) u)
  735. owner_option = u;
  736. else
  737. FATAL_ERROR ((0, 0, _("Invalid owner given on option")));
  738. }
  739. break;
  740. case POSIX_OPTION:
  741. #if OLDGNU_COMPATIBILITY
  742. if (archive_format == DEFAULT_FORMAT)
  743. archive_format = GNU_FORMAT;
  744. else if (archive_format != GNU_FORMAT)
  745. USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
  746. #else
  747. if (archive_format == DEFAULT_FORMAT)
  748. archive_format = POSIX_FORMAT;
  749. else if (archive_format != POSIX_FORMAT)
  750. USAGE_ERROR ((0, 0, _("Conflicting archive format options")));
  751. #endif
  752. break;
  753. case PRESERVE_OPTION:
  754. same_permissions_option = 1;
  755. same_order_option = 1;
  756. break;
  757. case RECORD_SIZE_OPTION:
  758. {
  759. uintmax_t u;
  760. if (! (xstrtoumax (optarg, (char **) 0, 10, &u, "") == LONG_MAX
  761. && u == (size_t) u))
  762. USAGE_ERROR ((0, 0, _("Invalid record size")));
  763. record_size = u;
  764. if (record_size % BLOCKSIZE != 0)
  765. USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
  766. BLOCKSIZE));
  767. blocking_factor = record_size / BLOCKSIZE;
  768. }
  769. break;
  770. case RSH_COMMAND_OPTION:
  771. rsh_command_option = optarg;
  772. break;
  773. case SUFFIX_OPTION:
  774. backup_option = 1;
  775. backup_suffix_string = optarg;
  776. break;
  777. case VOLNO_FILE_OPTION:
  778. volno_file_option = optarg;
  779. break;
  780. case USE_COMPRESS_PROGRAM_OPTION:
  781. set_use_compress_program_option (optarg);
  782. break;
  783. case '0':
  784. case '1':
  785. case '2':
  786. case '3':
  787. case '4':
  788. case '5':
  789. case '6':
  790. case '7':
  791. #ifdef DEVICE_PREFIX
  792. {
  793. int device = optchar - '0';
  794. int density;
  795. static char buf[sizeof DEVICE_PREFIX + 10];
  796. char *cursor;
  797. density = getopt_long (argc, argv, "lmh", NULL, NULL);
  798. strcpy (buf, DEVICE_PREFIX);
  799. cursor = buf + strlen (buf);
  800. #ifdef DENSITY_LETTER
  801. sprintf (cursor, "%d%c", device, density);
  802. #else /* not DENSITY_LETTER */
  803. switch (density)
  804. {
  805. case 'l':
  806. #ifdef LOW_NUM
  807. device += LOW_NUM;
  808. #endif
  809. break;
  810. case 'm':
  811. #ifdef MID_NUM
  812. device += MID_NUM;
  813. #else
  814. device += 8;
  815. #endif
  816. break;
  817. case 'h':
  818. #ifdef HGH_NUM
  819. device += HGH_NUM;
  820. #else
  821. device += 16;
  822. #endif
  823. break;
  824. default:
  825. usage (TAREXIT_FAILURE);
  826. }
  827. sprintf (cursor, "%d", device);
  828. #endif /* not DENSITY_LETTER */
  829. if (archive_names == allocated_archive_names)
  830. {
  831. allocated_archive_names *= 2;
  832. archive_name_array = (const char **)
  833. xrealloc (archive_name_array,
  834. sizeof (const char *) * allocated_archive_names);
  835. }
  836. archive_name_array[archive_names++] = buf;
  837. /* FIXME: How comes this works for many archives when buf is
  838. not xstrdup'ed? */
  839. }
  840. break;
  841. #else /* not DEVICE_PREFIX */
  842. USAGE_ERROR ((0, 0,
  843. _("Options `-[0-7][lmh]' not supported by *this* tar")));
  844. #endif /* not DEVICE_PREFIX */
  845. }
  846. /* Process trivial options. */
  847. if (show_version)
  848. {
  849. printf ("tar (GNU %s) %s\n", PACKAGE, VERSION);
  850. fputs (_("\
  851. \n\
  852. Copyright (C) 1988, 92,93,94,95,96,97,98, 1999 Free Software Foundation, Inc.\n"),
  853. stdout);
  854. fputs (_("\
  855. This is free software; see the source for copying conditions. There is NO\n\
  856. warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"),
  857. stdout);
  858. fputs (_("\
  859. \n\
  860. Written by John Gilmore and Jay Fenlason.\n"),
  861. stdout);
  862. exit (TAREXIT_SUCCESS);
  863. }
  864. if (show_help)
  865. usage (TAREXIT_SUCCESS);
  866. /* Derive option values and check option consistency. */
  867. if (archive_format == DEFAULT_FORMAT)
  868. {
  869. #if OLDGNU_COMPATIBILITY
  870. archive_format = OLDGNU_FORMAT;
  871. #else
  872. archive_format = GNU_FORMAT;
  873. #endif
  874. }
  875. if (archive_format == GNU_FORMAT && getenv ("POSIXLY_CORRECT"))
  876. archive_format = POSIX_FORMAT;
  877. if ((volume_label_option != NULL
  878. || incremental_option || multi_volume_option || sparse_option)
  879. && archive_format != OLDGNU_FORMAT && archive_format != GNU_FORMAT)
  880. USAGE_ERROR ((0, 0,
  881. _("GNU features wanted on incompatible archive format")));
  882. if (archive_names == 0)
  883. {
  884. /* If no archive file name given, try TAPE from the environment, or
  885. else, DEFAULT_ARCHIVE from the configuration process. */
  886. archive_names = 1;
  887. archive_name_array[0] = getenv ("TAPE");
  888. if (archive_name_array[0] == NULL)
  889. archive_name_array[0] = DEFAULT_ARCHIVE;
  890. }
  891. /* Allow multiple archives only with `-M'. */
  892. if (archive_names > 1 && !multi_volume_option)
  893. USAGE_ERROR ((0, 0,
  894. _("Multiple archive files requires `-M' option")));
  895. /* If ready to unlink hierarchies, so we are for simpler files. */
  896. if (recursive_unlink_option)
  897. unlink_first_option = 1;
  898. /* Forbid using -c with no input files whatsoever. Check that `-f -',
  899. explicit or implied, is used correctly. */
  900. switch (subcommand_option)
  901. {
  902. case CREATE_SUBCOMMAND:
  903. if (input_files == 0 && !files_from_option)
  904. USAGE_ERROR ((0, 0,
  905. _("Cowardly refusing to create an empty archive")));
  906. break;
  907. case EXTRACT_SUBCOMMAND:
  908. case LIST_SUBCOMMAND:
  909. case DIFF_SUBCOMMAND:
  910. for (archive_name_cursor = archive_name_array;
  911. archive_name_cursor < archive_name_array + archive_names;
  912. archive_name_cursor++)
  913. if (!strcmp (*archive_name_cursor, "-"))
  914. request_stdin ("-f");
  915. break;
  916. case CAT_SUBCOMMAND:
  917. case UPDATE_SUBCOMMAND:
  918. case APPEND_SUBCOMMAND:
  919. for (archive_name_cursor = archive_name_array;
  920. archive_name_cursor < archive_name_array + archive_names;
  921. archive_name_cursor++)
  922. if (!strcmp (*archive_name_cursor, "-"))
  923. USAGE_ERROR ((0, 0,
  924. _("Options `-Aru' are incompatible with `-f -'")));
  925. default:
  926. break;
  927. }
  928. archive_name_cursor = archive_name_array;
  929. /* Prepare for generating backup names. */
  930. if (backup_suffix_string)
  931. simple_backup_suffix = xstrdup (backup_suffix_string);
  932. if (backup_option)
  933. backup_type = xget_version ("--backup", version_control_string);
  934. }
  935. /* Tar proper. */
  936. /*-----------------------.
  937. | Main routine for tar. |
  938. `-----------------------*/
  939. int
  940. main (int argc, char *const *argv)
  941. {
  942. program_name = argv[0];
  943. setlocale (LC_ALL, "");
  944. bindtextdomain (PACKAGE, LOCALEDIR);
  945. textdomain (PACKAGE);
  946. exit_status = TAREXIT_SUCCESS;
  947. filename_terminator = '\n';
  948. /* Pre-allocate a few structures. */
  949. allocated_archive_names = 10;
  950. archive_name_array = (const char **)
  951. xmalloc (sizeof (const char *) * allocated_archive_names);
  952. archive_names = 0;
  953. #ifdef SIGCHLD
  954. /* System V fork+wait does not work if SIGCHLD is ignored. */
  955. signal (SIGCHLD, SIG_DFL);
  956. #endif
  957. init_names ();
  958. /* Decode options. */
  959. decode_options (argc, argv);
  960. name_init (argc, argv);
  961. /* Main command execution. */
  962. if (volno_file_option)
  963. init_volume_number ();
  964. switch (subcommand_option)
  965. {
  966. case UNKNOWN_SUBCOMMAND:
  967. USAGE_ERROR ((0, 0,
  968. _("You must specify one of the `-Acdtrux' options")));
  969. case CAT_SUBCOMMAND:
  970. case UPDATE_SUBCOMMAND:
  971. case APPEND_SUBCOMMAND:
  972. update_archive ();
  973. break;
  974. case DELETE_SUBCOMMAND:
  975. delete_archive_members ();
  976. break;
  977. case CREATE_SUBCOMMAND:
  978. create_archive ();
  979. name_close ();
  980. if (totals_option)
  981. print_total_written ();
  982. break;
  983. case EXTRACT_SUBCOMMAND:
  984. extr_init ();
  985. read_and (extract_archive);
  986. break;
  987. case LIST_SUBCOMMAND:
  988. read_and (list_archive);
  989. break;
  990. case DIFF_SUBCOMMAND:
  991. diff_init ();
  992. read_and (diff_archive);
  993. break;
  994. }
  995. if (volno_file_option)
  996. closeout_volume_number ();
  997. /* Dispose of allocated memory, and return. */
  998. free (archive_name_array);
  999. name_term ();
  1000. if (exit_status == TAREXIT_FAILURE)
  1001. error (0, 0, _("Error exit delayed from previous errors"));
  1002. exit (exit_status);
  1003. }