tar.c 40 KB

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