tar.c 43 KB

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