tar.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918
  1. /* A tar (tape archiver) program.
  2. Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
  3. 2001, 2003, 2004, 2005 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 <argp.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 <rmt.h>
  32. #include <prepargs.h>
  33. #include <quotearg.h>
  34. #include <xstrtol.h>
  35. /* Local declarations. */
  36. #ifndef DEFAULT_ARCHIVE_FORMAT
  37. # define DEFAULT_ARCHIVE_FORMAT GNU_FORMAT
  38. #endif
  39. #ifndef DEFAULT_ARCHIVE
  40. # define DEFAULT_ARCHIVE "tar.out"
  41. #endif
  42. #ifndef DEFAULT_BLOCKING
  43. # define DEFAULT_BLOCKING 20
  44. #endif
  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. { "ustar", USTAR_FORMAT },
  102. { "posix", POSIX_FORMAT },
  103. #if 0 /* not fully supported yet */
  104. { "star", STAR_FORMAT },
  105. #endif
  106. { "gnu", GNU_FORMAT },
  107. { "pax", POSIX_FORMAT }, /* An alias for posix */
  108. { NULL, 0 }
  109. };
  110. static void
  111. set_archive_format (char const *name)
  112. {
  113. struct fmttab const *p;
  114. for (p = fmttab; strcmp (p->name, name) != 0; )
  115. if (! (++p)->name)
  116. USAGE_ERROR ((0, 0, _("%s: Invalid archive format"),
  117. quotearg_colon (name)));
  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. CHECK_LINKS_OPTION,
  151. DELETE_OPTION,
  152. EXCLUDE_OPTION,
  153. EXCLUDE_CACHES_OPTION,
  154. FORCE_LOCAL_OPTION,
  155. GROUP_OPTION,
  156. HANG_OPTION,
  157. IGNORE_CASE_OPTION,
  158. IGNORE_COMMAND_ERROR_OPTION,
  159. IGNORE_FAILED_READ_OPTION,
  160. INDEX_FILE_OPTION,
  161. KEEP_NEWER_FILES_OPTION,
  162. LICENSE_OPTION,
  163. MODE_OPTION,
  164. NEWER_MTIME_OPTION,
  165. NO_ANCHORED_OPTION,
  166. NO_IGNORE_CASE_OPTION,
  167. NO_IGNORE_COMMAND_ERROR_OPTION,
  168. NO_OVERWRITE_DIR_OPTION,
  169. NO_RECURSION_OPTION,
  170. NO_SAME_OWNER_OPTION,
  171. NO_SAME_PERMISSIONS_OPTION,
  172. NO_UNQUOTE_OPTION,
  173. NO_WILDCARDS_OPTION,
  174. NO_WILDCARDS_MATCH_SLASH_OPTION,
  175. NULL_OPTION,
  176. NUMERIC_OWNER_OPTION,
  177. OCCURRENCE_OPTION,
  178. OLD_ARCHIVE_OPTION,
  179. ONE_FILE_SYSTEM_OPTION,
  180. OVERWRITE_OPTION,
  181. OWNER_OPTION,
  182. PAX_OPTION,
  183. POSIX_OPTION,
  184. PRESERVE_OPTION,
  185. RECORD_SIZE_OPTION,
  186. RECURSION_OPTION,
  187. RECURSIVE_UNLINK_OPTION,
  188. REMOVE_FILES_OPTION,
  189. RMT_COMMAND_OPTION,
  190. RSH_COMMAND_OPTION,
  191. SAME_OWNER_OPTION,
  192. SHOW_DEFAULTS_OPTION,
  193. SHOW_OMITTED_DIRS_OPTION,
  194. STRIP_COMPONENTS_OPTION,
  195. SUFFIX_OPTION,
  196. TO_COMMAND_OPTION,
  197. TOTALS_OPTION,
  198. UNQUOTE_OPTION,
  199. USAGE_OPTION,
  200. USE_COMPRESS_PROGRAM_OPTION,
  201. UTC_OPTION,
  202. VERSION_OPTION,
  203. VOLNO_FILE_OPTION,
  204. WILDCARDS_OPTION,
  205. WILDCARDS_MATCH_SLASH_OPTION
  206. };
  207. const char *argp_program_version = "tar (" PACKAGE_NAME ") " VERSION;
  208. const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
  209. static char doc[] = N_("GNU `tar' saves many files together into a single tape or disk archive, and can restore individual files from the archive.\n\
  210. \n\
  211. Examples:\n\
  212. tar -cf archive.tar foo bar # Create archive.tar from files foo and bar.\n\
  213. tar -tvf archive.tar # List all files in archive.tar verbosely.\n\
  214. tar -xf archive.tar # Extract all files from archive.tar.\n\
  215. \vThe backup suffix is `~', unless set with --suffix or SIMPLE_BACKUP_SUFFIX.\n\
  216. The version control may be set with --backup or VERSION_CONTROL, values are:\n\n\
  217. none, off never make backups\n\
  218. t, numbered make numbered backups\n\
  219. nil, existing numbered if numbered backups exist, simple otherwise\n\
  220. never, simple always make simple backups\n");
  221. /* NOTE:
  222. Available option letters are DEIJQY and aeqy. Consider the following
  223. assignments:
  224. [For Solaris tar compatibility]
  225. e exit immediately with a nonzero exit status if unexpected errors occur
  226. E use extended headers (--format=posix)
  227. [q alias for --occurrence=1 =/= this would better be used for quiet?]
  228. [I same as T =/= will harm star compatibility]
  229. y per-file gzip compression
  230. Y per-block gzip compression */
  231. static struct argp_option options[] = {
  232. {NULL, 0, NULL, 0,
  233. N_("Main operation mode:"), 0},
  234. {"list", 't', 0, 0,
  235. N_("list the contents of an archive"), 10 },
  236. {"extract", 'x', 0, 0,
  237. N_("extract files from an archive"), 10 },
  238. {"get", 0, 0, OPTION_ALIAS, NULL, 0 },
  239. {"create", 'c', 0, 0,
  240. N_("create a new archive"), 10 },
  241. {"diff", 'd', 0, 0,
  242. N_("find differences between archive and file system"), 10 },
  243. {"compare", 0, 0, OPTION_ALIAS, NULL, 10},
  244. {"append", 'r', 0, 0,
  245. N_("append files to the end of an archive"), 10 },
  246. {"update", 'u', 0, 0,
  247. N_("only append files newer than copy in archive"), 10 },
  248. {"catenate", 'A', 0, 0,
  249. N_("append tar files to an archive"), 10 },
  250. {"concatenate", 0, 0, OPTION_ALIAS, NULL, 10},
  251. {"delete", DELETE_OPTION, 0, 0,
  252. N_("delete from the archive (not on mag tapes!)"), 10 },
  253. {NULL, 0, NULL, 0,
  254. N_("Operation modifiers:"), 20},
  255. {"sparse", 'S', 0, 0,
  256. N_("handle sparse files efficiently"), 21 },
  257. {"incremental", 'G', 0, 0,
  258. N_("handle old GNU-format incremental backup"), 21 },
  259. {"listed-incremental", 'g', N_("FILE"), 0,
  260. N_("handle new GNU-format incremental backup"), 21 },
  261. {"ignore-failed-read", IGNORE_FAILED_READ_OPTION, 0, 0,
  262. N_("do not exit with nonzero on unreadable files"), 21 },
  263. {"occurrence", OCCURRENCE_OPTION, N_("NUMBER"), OPTION_ARG_OPTIONAL,
  264. N_("process only the NUMBERth occurrence of each file in the archive. This option is valid only in conjunction with one of the subcommands --delete, --diff, --extract or --list and when a list of files is given either on the command line or via -T option. NUMBER defaults to 1."), 21 },
  265. {"seek", 'n', NULL, 0,
  266. N_("archive is seekable"), 21 },
  267. {NULL, 0, NULL, 0,
  268. N_("Overwrite control:"), 30},
  269. {"verify", 'W', 0, 0,
  270. N_("attempt to verify the archive after writing it"), 31 },
  271. {"remove-files", REMOVE_FILES_OPTION, 0, 0,
  272. N_("remove files after adding them to the archive"), 31 },
  273. {"keep-old-files", 'k', 0, 0,
  274. N_("don't replace existing files when extracting"), 31 },
  275. {"keep-newer-files", KEEP_NEWER_FILES_OPTION, 0, 0,
  276. N_("don't replace existing files that are newer than their archive copies"), 31 },
  277. {"overwrite", OVERWRITE_OPTION, 0, 0,
  278. N_("overwrite existing files when extracting"), 31 },
  279. {"unlink-first", 'U', 0, 0,
  280. N_("remove each file prior to extracting over it"), 31 },
  281. {"recursive-unlink", RECURSIVE_UNLINK_OPTION, 0, 0,
  282. N_("empty hierarchies prior to extracting directory"), 31 },
  283. {"no-overwrite-dir", NO_OVERWRITE_DIR_OPTION, 0, 0,
  284. N_("preserve metadata of existing directories"), 31 },
  285. {NULL, 0, NULL, 0,
  286. N_("Select output stream:"), 40},
  287. {"to-stdout", 'O', 0, 0,
  288. N_("extract files to standard output"), 41 },
  289. {"to-command", TO_COMMAND_OPTION, N_("COMMAND"), 0,
  290. N_("pipe extracted files to another program"), 41 },
  291. {"ignore-command-error", IGNORE_COMMAND_ERROR_OPTION, 0, 0,
  292. N_("ignore exit codes of children"), 41 },
  293. {"no-ignore-command-error", NO_IGNORE_COMMAND_ERROR_OPTION, 0, 0,
  294. N_("treat non-zero exit codes of children as error"), 41 },
  295. {NULL, 0, NULL, 0,
  296. N_("Handling of file attributes:"), 50 },
  297. {"owner", OWNER_OPTION, N_("NAME"), 0,
  298. N_("force NAME as owner for added files"), 51 },
  299. {"group", GROUP_OPTION, N_("NAME"), 0,
  300. N_("force NAME as group for added files"), 51 },
  301. {"mode", MODE_OPTION, N_("CHANGES"), 0,
  302. N_("force (symbolic) mode CHANGES for added files"), 51 },
  303. {"atime-preserve", ATIME_PRESERVE_OPTION, 0, 0,
  304. N_("don't change access times on dumped files"), 51 },
  305. {"touch", 'm', 0, 0,
  306. N_("don't extract file modified time"), 51 },
  307. {"same-owner", SAME_OWNER_OPTION, 0, 0,
  308. N_("try extracting files with the same ownership"), 51 },
  309. {"no-same-owner", NO_SAME_OWNER_OPTION, 0, 0,
  310. N_("extract files as yourself"), 51 },
  311. {"numeric-owner", NUMERIC_OWNER_OPTION, 0, 0,
  312. N_("always use numbers for user/group names"), 51 },
  313. {"preserve-permissions", 'p', 0, 0,
  314. N_("extract information about file permissions (default for superuser)"),
  315. 51 },
  316. {"same-permissions", 0, 0, OPTION_ALIAS, NULL, 51 },
  317. {"no-same-permissions", NO_SAME_PERMISSIONS_OPTION, 0, 0,
  318. N_("apply the user's umask when extracting permissions from the archive (default for ordinary users)"), 51 },
  319. {"preserve-order", 's', 0, 0,
  320. N_("sort names to extract to match archive"), 51 },
  321. {"same-order", 0, 0, OPTION_ALIAS, NULL, 51 },
  322. {"preserve", PRESERVE_OPTION, 0, 0,
  323. N_("same as both -p and -s"), 51 },
  324. {NULL, 0, NULL, 0,
  325. N_("Device selection and switching:"), 60 },
  326. {"file", 'f', N_("ARCHIVE"), 0,
  327. N_("use archive file or device ARCHIVE"), 61 },
  328. {"force-local", FORCE_LOCAL_OPTION, 0, 0,
  329. N_("archive file is local even if it has a colon"), 61 },
  330. {"rmt-command", RMT_COMMAND_OPTION, N_("COMMAND"), 0,
  331. N_("use given rmt COMMAND instead of rmt"), 61 },
  332. {"rsh-command", RSH_COMMAND_OPTION, N_("COMMAND"), 0,
  333. N_("use remote COMMAND instead of rsh"), 61 },
  334. #ifdef DEVICE_PREFIX
  335. {"-[0-7][lmh]", 0, NULL, OPTION_DOC, /* It is OK, since `name' will never be
  336. translated */
  337. N_("specify drive and density"), 61 },
  338. #endif
  339. {NULL, '0', NULL, OPTION_HIDDEN, NULL, 61 },
  340. {NULL, '1', NULL, OPTION_HIDDEN, NULL, 61 },
  341. {NULL, '2', NULL, OPTION_HIDDEN, NULL, 61 },
  342. {NULL, '3', NULL, OPTION_HIDDEN, NULL, 61 },
  343. {NULL, '4', NULL, OPTION_HIDDEN, NULL, 61 },
  344. {NULL, '5', NULL, OPTION_HIDDEN, NULL, 61 },
  345. {NULL, '6', NULL, OPTION_HIDDEN, NULL, 61 },
  346. {NULL, '7', NULL, OPTION_HIDDEN, NULL, 61 },
  347. {NULL, '8', NULL, OPTION_HIDDEN, NULL, 61 },
  348. {NULL, '9', NULL, OPTION_HIDDEN, NULL, 61 },
  349. {"multi-volume", 'M', 0, 0,
  350. N_("create/list/extract multi-volume archive"), 61 },
  351. {"tape-length", 'L', N_("NUMBER"), 0,
  352. N_("change tape after writing NUMBER x 1024 bytes"), 61 },
  353. {"info-script", 'F', N_("NAME"), 0,
  354. N_("run script at end of each tape (implies -M)"), 61 },
  355. {"new-volume-script", 0, 0, OPTION_ALIAS, NULL, 61 },
  356. {"volno-file", VOLNO_FILE_OPTION, N_("FILE"), 0,
  357. N_("use/update the volume number in FILE"), 61 },
  358. {NULL, 0, NULL, 0,
  359. N_("Device blocking:"), 70 },
  360. {"blocking-factor", 'b', N_("BLOCKS"), 0,
  361. N_("BLOCKS x 512 bytes per record"), 71 },
  362. {"record-size", RECORD_SIZE_OPTION, N_("NUMBER"), 0,
  363. N_("NUMBER of bytes per record, multiple of 512"), 71 },
  364. {"ignore-zeros", 'i', 0, 0,
  365. N_("ignore zeroed blocks in archive (means EOF)"), 71 },
  366. {"read-full-records", 'B', 0, 0,
  367. N_("reblock as we read (for 4.2BSD pipes)"), 71 },
  368. {NULL, 0, NULL, 0,
  369. N_("Archive format selection:"), 80 },
  370. {"format", 'H', N_("FORMAT"), 0,
  371. N_("create archive of the given format."), 81 },
  372. {NULL, 0, NULL, 0, N_("FORMAT is one of the following:"), 82 },
  373. {" v7", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("old V7 tar format"), 83},
  374. {" oldgnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  375. N_("GNU format as per tar <= 1.12"), 83},
  376. {" gnu", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  377. N_("GNU tar 1.13.x format"), 83},
  378. {" ustar", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  379. N_("POSIX 1003.1-1988 (ustar) format"), 83 },
  380. {" pax", 0, NULL, OPTION_DOC|OPTION_NO_TRANS,
  381. N_("POSIX 1003.1-2001 (pax) format"), 83 },
  382. {" posix", 0, NULL, OPTION_DOC|OPTION_NO_TRANS, N_("same as pax"), 83 },
  383. {"old-archive", OLD_ARCHIVE_OPTION, 0, 0, /* FIXME */
  384. N_("same as --format=v7"), 88 },
  385. {"portability", 0, 0, OPTION_ALIAS, NULL, 88 },
  386. {"posix", POSIX_OPTION, 0, 0,
  387. N_("same as --format=posix"), 88 },
  388. {"pax-option", PAX_OPTION, N_("keyword[[:]=value][,keyword[[:]=value], ...]"), 0,
  389. N_("control pax keywords"), 88 },
  390. {"label", 'V', N_("TEXT"), 0,
  391. N_("create archive with volume name TEXT. At list/extract time, use TEXT as a globbing pattern for volume name"), 88 },
  392. {"bzip2", 'j', 0, 0,
  393. N_("filter the archive through bzip2"), 88 },
  394. {"gzip", 'z', 0, 0,
  395. N_("filter the archive through gzip"), 88 },
  396. {"gunzip", 0, 0, OPTION_ALIAS, NULL, 88 },
  397. {"ungzip", 0, 0, OPTION_ALIAS, NULL, 88 },
  398. {"compress", 'Z', 0, 0,
  399. N_("filter the archive through compress"), 88 },
  400. {"uncompress", 0, 0, OPTION_ALIAS, NULL, 88 },
  401. {"use-compress-program", USE_COMPRESS_PROGRAM_OPTION, N_("PROG"), 0,
  402. N_("filter through PROG (must accept -d)"), 88 },
  403. {NULL, 0, NULL, 0,
  404. N_("Local file selection:"), 90 },
  405. {"add-file", ARGP_KEY_ARG, N_("FILE"), 0,
  406. N_("add given FILE to the archive (useful if its name starts with a dash)"), 91},
  407. {"directory", 'C', N_("DIR"), 0,
  408. N_("change to directory DIR"), 91 },
  409. {"files-from", 'T', N_("FILE"), 0,
  410. N_("get names to extract or create from FILE"), 91 },
  411. {"null", NULL_OPTION, 0, 0,
  412. N_("-T reads null-terminated names, disable -C"), 91 },
  413. {"unquote", UNQUOTE_OPTION, 0, 0,
  414. N_("unquote filenames read with -T (default)"), 91 },
  415. {"no-unquote", NO_UNQUOTE_OPTION, 0, 0,
  416. N_("do not unquote filenames read with -T"), 91 },
  417. {"exclude", EXCLUDE_OPTION, N_("PATTERN"), 0,
  418. N_("exclude files, given as a PATTERN"), 91 },
  419. {"exclude-from", 'X', N_("FILE"), 0,
  420. N_("exclude patterns listed in FILE"), 91 },
  421. {"exclude-caches", EXCLUDE_CACHES_OPTION, 0, 0,
  422. N_("exclude directories containing a cache tag"), 91 },
  423. {"ignore-case", IGNORE_CASE_OPTION, 0, 0,
  424. N_("exclusion ignores case"), 91 },
  425. {"anchored", ANCHORED_OPTION, 0, 0,
  426. N_("exclude patterns match file name start"), 91 },
  427. {"no-anchored", NO_ANCHORED_OPTION, 0, 0,
  428. N_("exclude patterns match after any `/' (default)"), 91 },
  429. {"no-ignore-case", NO_IGNORE_CASE_OPTION, 0, 0,
  430. N_("exclusion is case sensitive (default)"), 91 },
  431. {"no-wildcards", NO_WILDCARDS_OPTION, 0, 0,
  432. N_("exclude patterns are plain strings"), 91 },
  433. {"no-wildcards-match-slash", NO_WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  434. N_("exclude pattern wildcards do not match `/'"), 91 },
  435. {"no-recursion", NO_RECURSION_OPTION, 0, 0,
  436. N_("avoid descending automatically in directories"), 91 },
  437. {"one-file-system", ONE_FILE_SYSTEM_OPTION, 0, 0,
  438. N_("stay in local file system when creating archive"), 91 },
  439. {NULL, 'l', 0, OPTION_HIDDEN, "", 91},
  440. {"recursion", RECURSION_OPTION, 0, 0,
  441. N_("recurse into directories (default)"), 91 },
  442. {"absolute-names", 'P', 0, 0,
  443. N_("don't strip leading `/'s from file names"), 91 },
  444. {"dereference", 'h', 0, 0,
  445. N_("follow symlinks; archive and dump the files they point to"), 91 },
  446. {"starting-file", 'K', N_("MEMBER-NAME"), 0,
  447. N_("begin at member MEMBER-NAME in the archive"), 91 },
  448. {"strip-components", STRIP_COMPONENTS_OPTION, N_("NUMBER"), 0,
  449. N_("strip NUMBER leading components from file names"), 91 },
  450. {"newer", 'N', N_("DATE-OR-FILE"), 0,
  451. N_("only store files newer than DATE-OR-FILE"), 91 },
  452. {"newer-mtime", NEWER_MTIME_OPTION, N_("DATE"), 0,
  453. N_("compare date and time when data changed only"), 91 },
  454. {"after-date", 'N', N_("DATE"), 0,
  455. N_("same as -N"), 91 },
  456. {"backup", BACKUP_OPTION, N_("CONTROL"), OPTION_ARG_OPTIONAL,
  457. N_("backup before removal, choose version CONTROL"), 91 },
  458. {"suffix", SUFFIX_OPTION, N_("STRING"), 0,
  459. N_("backup before removal, override usual suffix ('~' unless overridden by environment variable SIMPLE_BACKUP_SUFFIX)"), 91 },
  460. {"wildcards", WILDCARDS_OPTION, 0, 0,
  461. N_("exclude patterns use wildcards (default)"), 91 },
  462. {"wildcards-match-slash", WILDCARDS_MATCH_SLASH_OPTION, 0, 0,
  463. N_("exclude pattern wildcards match `/' (default)"), 91 },
  464. {NULL, 0, NULL, 0,
  465. N_("Informative output:"), 100 },
  466. {"verbose", 'v', 0, 0,
  467. N_("verbosely list files processed"), 101 },
  468. {"checkpoint", CHECKPOINT_OPTION, 0, 0,
  469. N_("display progress messages every 10th record"), 101 },
  470. {"check-links", CHECK_LINKS_OPTION, 0, 0,
  471. N_("print a message if not all links are dumped"), 102 },
  472. {"totals", TOTALS_OPTION, 0, 0,
  473. N_("print total bytes written while creating archive"), 102 },
  474. {"utc", UTC_OPTION, 0, 0,
  475. N_("print file modification dates in UTC"), 102 },
  476. {"index-file", INDEX_FILE_OPTION, N_("FILE"), 0,
  477. N_("send verbose output to FILE"), 102 },
  478. {"block-number", 'R', 0, 0,
  479. N_("show block number within archive with each message"), 102 },
  480. {"interactive", 'w', 0, 0,
  481. N_("ask for confirmation for every action"), 102 },
  482. {"confirmation", 0, 0, OPTION_ALIAS, NULL, 102 },
  483. {"show-defaults", SHOW_DEFAULTS_OPTION, 0, 0,
  484. N_("Show tar defaults"), 102 },
  485. {"show-omitted-dirs", SHOW_OMITTED_DIRS_OPTION, 0, 0,
  486. N_("When listing or extracting, list each directory that does not match search criteria"), 102 },
  487. {NULL, 0, NULL, 0,
  488. N_("Compatibility options:"), 110 },
  489. {NULL, 'o', 0, 0,
  490. N_("when creating, same as --old-archive. When extracting, same as --no-same-owner"), 111 },
  491. {NULL, 0, NULL, 0,
  492. N_("Other options:"), 120 },
  493. {"help", '?', 0, 0, N_("Give this help list"), -1},
  494. {"usage", USAGE_OPTION, 0, 0, N_("Give a short usage message"), -1},
  495. {"license", LICENSE_OPTION, 0, 0, N_("Print license and exit"), -1},
  496. {"version", VERSION_OPTION, 0, 0, N_("Print program version"), -1},
  497. /* FIXME -V (--label) conflicts with the default short option for
  498. --version */
  499. {"HANG", HANG_OPTION, "SECS", OPTION_ARG_OPTIONAL | OPTION_HIDDEN,
  500. N_("Hang for SECS seconds (default 3600)"), 0},
  501. {0, 0, 0, 0, 0, 0}
  502. };
  503. struct tar_args {
  504. char const *textual_date_option;
  505. int exclude_options;
  506. bool o_option;
  507. int pax_option;
  508. char const *backup_suffix_string;
  509. char const *version_control_string;
  510. int input_files;
  511. };
  512. static void
  513. show_default_settings (FILE *stream)
  514. {
  515. fprintf (stream,
  516. "--format=%s -f%s -b%d --rmt-command=%s",
  517. archive_format_string (DEFAULT_ARCHIVE_FORMAT),
  518. DEFAULT_ARCHIVE, DEFAULT_BLOCKING,
  519. DEFAULT_RMT_COMMAND);
  520. #ifdef REMOTE_SHELL
  521. fprintf (stream, " --rsh-command=%s", REMOTE_SHELL);
  522. #endif
  523. fprintf (stream, "\n");
  524. }
  525. static void
  526. set_subcommand_option (enum subcommand subcommand)
  527. {
  528. if (subcommand_option != UNKNOWN_SUBCOMMAND
  529. && subcommand_option != subcommand)
  530. USAGE_ERROR ((0, 0,
  531. _("You may not specify more than one `-Acdtrux' option")));
  532. subcommand_option = subcommand;
  533. }
  534. static void
  535. set_use_compress_program_option (const char *string)
  536. {
  537. if (use_compress_program_option
  538. && strcmp (use_compress_program_option, string) != 0)
  539. USAGE_ERROR ((0, 0, _("Conflicting compression options")));
  540. use_compress_program_option = string;
  541. }
  542. void
  543. license ()
  544. {
  545. printf ("tar (%s) %s\n%s\n", PACKAGE_NAME, PACKAGE_VERSION,
  546. "Copyright (C) 2004 Free Software Foundation, Inc.\n");
  547. puts (_("Based on the work of John Gilmore and Jay Fenlason. See AUTHORS\n\
  548. for complete list of authors.\n"));
  549. printf (_(" GNU tar is free software; you can redistribute it and/or modify\n"
  550. " it under the terms of the GNU General Public License as published by\n"
  551. " the Free Software Foundation; either version 2 of the License, or\n"
  552. " (at your option) any later version.\n"
  553. "\n"
  554. " GNU tar is distributed in the hope that it will be useful,\n"
  555. " but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  556. " MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  557. " GNU General Public License for more details.\n"
  558. "\n"
  559. " You should have received a copy of the GNU General Public License\n"
  560. " along with GNU tar; if not, write to the Free Software\n"
  561. " Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\n\n"));
  562. exit (0);
  563. }
  564. static volatile int _argp_hang;
  565. static bool
  566. read_name_from_file (FILE *fp, struct obstack *stk)
  567. {
  568. int c;
  569. size_t counter = 0;
  570. for (c = getc (fp); c != EOF && c != filename_terminator; c = getc (fp))
  571. {
  572. if (c == 0)
  573. FATAL_ERROR((0, 0, N_("file name contains null character")));
  574. obstack_1grow (stk, c);
  575. counter++;
  576. }
  577. obstack_1grow (stk, 0);
  578. return !(counter == 0 && c == EOF);
  579. }
  580. static bool files_from_option; /* When set, tar will not refuse to create
  581. empty archives */
  582. static struct obstack argv_stk; /* Storage for additional command line options
  583. read using -T option */
  584. /* Prevent recursive inclusion of the same file */
  585. struct file_id_list
  586. {
  587. struct file_id_list *next;
  588. ino_t ino;
  589. dev_t dev;
  590. };
  591. static struct file_id_list *file_id_list;
  592. static void
  593. add_file_id (const char *filename)
  594. {
  595. struct file_id_list *p;
  596. struct stat st;
  597. if (stat (filename, &st))
  598. stat_fatal (filename);
  599. for (p = file_id_list; p; p = p->next)
  600. if (p->ino == st.st_ino && p->dev == st.st_dev)
  601. {
  602. FATAL_ERROR ((0, 0, _("%s: file list already read"),
  603. quotearg_colon (filename)));
  604. }
  605. p = xmalloc (sizeof *p);
  606. p->next = file_id_list;
  607. p->ino = st.st_ino;
  608. p->dev = st.st_dev;
  609. file_id_list = p;
  610. }
  611. static void
  612. update_argv (const char *filename, struct argp_state *state)
  613. {
  614. FILE *fp;
  615. size_t count = 0, i;
  616. char *start, *p;
  617. char **new_argv;
  618. size_t new_argc;
  619. bool is_stdin = false;
  620. if (!strcmp (filename, "-"))
  621. {
  622. is_stdin = true;
  623. request_stdin ("-T");
  624. fp = stdin;
  625. }
  626. else
  627. {
  628. add_file_id (filename);
  629. if ((fp = fopen (filename, "r")) == NULL)
  630. open_fatal (filename);
  631. }
  632. while (read_name_from_file (fp, &argv_stk))
  633. count++;
  634. if (!is_stdin)
  635. fclose (fp);
  636. if (count == 0)
  637. return;
  638. start = obstack_finish (&argv_stk);
  639. if (filename_terminator == 0)
  640. for (p = start; *p; p += strlen (p) + 1)
  641. if (p[0] == '-')
  642. count++;
  643. new_argc = state->argc + count;
  644. new_argv = xmalloc (sizeof (state->argv[0]) * (new_argc + 1));
  645. memcpy (new_argv, state->argv, sizeof (state->argv[0]) * (state->argc + 1));
  646. state->argv = new_argv;
  647. memmove (&state->argv[state->next + count], &state->argv[state->next],
  648. (state->argc - state->next + 1) * sizeof (state->argv[0]));
  649. state->argc = new_argc;
  650. for (i = state->next, p = start; *p; p += strlen (p) + 1, i++)
  651. {
  652. if (filename_terminator == 0 && p[0] == '-')
  653. state->argv[i++] = "--add-file";
  654. state->argv[i] = p;
  655. }
  656. }
  657. static error_t
  658. parse_opt (int key, char *arg, struct argp_state *state)
  659. {
  660. struct tar_args *args = state->input;
  661. switch (key)
  662. {
  663. case ARGP_KEY_ARG:
  664. /* File name or non-parsed option, because of ARGP_IN_ORDER */
  665. name_add (arg);
  666. args->input_files++;
  667. break;
  668. case 'A':
  669. set_subcommand_option (CAT_SUBCOMMAND);
  670. break;
  671. case 'b':
  672. {
  673. uintmax_t u;
  674. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  675. && u == (blocking_factor = u)
  676. && 0 < blocking_factor
  677. && u == (record_size = u * BLOCKSIZE) / BLOCKSIZE))
  678. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  679. _("Invalid blocking factor")));
  680. }
  681. break;
  682. case 'B':
  683. /* Try to reblock input records. For reading 4.2BSD pipes. */
  684. /* It would surely make sense to exchange -B and -R, but it seems
  685. that -B has been used for a long while in Sun tar and most
  686. BSD-derived systems. This is a consequence of the block/record
  687. terminology confusion. */
  688. read_full_records_option = true;
  689. break;
  690. case 'c':
  691. set_subcommand_option (CREATE_SUBCOMMAND);
  692. break;
  693. case 'C':
  694. name_add ("-C");
  695. name_add (arg);
  696. break;
  697. case 'd':
  698. set_subcommand_option (DIFF_SUBCOMMAND);
  699. break;
  700. case 'f':
  701. if (archive_names == allocated_archive_names)
  702. {
  703. allocated_archive_names *= 2;
  704. archive_name_array =
  705. xrealloc (archive_name_array,
  706. sizeof (const char *) * allocated_archive_names);
  707. }
  708. archive_name_array[archive_names++] = arg;
  709. break;
  710. case 'F':
  711. /* Since -F is only useful with -M, make it implied. Run this
  712. script at the end of each tape. */
  713. info_script_option = arg;
  714. multi_volume_option = true;
  715. break;
  716. case 'g':
  717. listed_incremental_option = arg;
  718. after_date_option = true;
  719. /* Fall through. */
  720. case 'G':
  721. /* We are making an incremental dump (FIXME: are we?); save
  722. directories at the beginning of the archive, and include in each
  723. directory its contents. */
  724. incremental_option = true;
  725. break;
  726. case 'h':
  727. /* Follow symbolic links. */
  728. dereference_option = true;
  729. break;
  730. case 'i':
  731. /* Ignore zero blocks (eofs). This can't be the default,
  732. because Unix tar writes two blocks of zeros, then pads out
  733. the record with garbage. */
  734. ignore_zeros_option = true;
  735. break;
  736. case 'I':
  737. USAGE_ERROR ((0, 0,
  738. _("Warning: the -I option is not supported;"
  739. " perhaps you meant -j or -T?")));
  740. break;
  741. case 'j':
  742. set_use_compress_program_option ("bzip2");
  743. break;
  744. case 'k':
  745. /* Don't replace existing files. */
  746. old_files_option = KEEP_OLD_FILES;
  747. break;
  748. case 'K':
  749. starting_file_option = true;
  750. addname (arg, 0);
  751. break;
  752. case 'l':
  753. /* Historically equivalent to --one-file-system. This usage is
  754. incompatible with UNIX98 and POSIX specs and therefore is
  755. deprecated. The semantics of -l option will be changed in
  756. future versions. See TODO.
  757. */
  758. WARN ((0, 0,
  759. _("Semantics of -l option will change in the future releases.")));
  760. WARN ((0, 0,
  761. _("Please use --one-file-system option instead.")));
  762. /* FALL THROUGH */
  763. case ONE_FILE_SYSTEM_OPTION:
  764. /* When dumping directories, don't dump files/subdirectories
  765. that are on other filesystems. */
  766. one_file_system_option = true;
  767. break;
  768. case 'L':
  769. {
  770. uintmax_t u;
  771. if (xstrtoumax (arg, 0, 10, &u, "") != LONGINT_OK)
  772. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  773. _("Invalid tape length")));
  774. tape_length_option = 1024 * (tarlong) u;
  775. multi_volume_option = true;
  776. }
  777. break;
  778. case 'm':
  779. touch_option = true;
  780. break;
  781. case 'M':
  782. /* Make multivolume archive: when we can't write any more into
  783. the archive, re-open it, and continue writing. */
  784. multi_volume_option = true;
  785. break;
  786. case 'n':
  787. seekable_archive = true;
  788. break;
  789. #if !MSDOS
  790. case 'N':
  791. after_date_option = true;
  792. /* Fall through. */
  793. case NEWER_MTIME_OPTION:
  794. if (NEWER_OPTION_INITIALIZED (newer_mtime_option))
  795. USAGE_ERROR ((0, 0, _("More than one threshold date")));
  796. if (FILE_SYSTEM_PREFIX_LEN (arg) != 0
  797. || ISSLASH (*arg)
  798. || *arg == '.')
  799. {
  800. struct stat st;
  801. if (deref_stat (dereference_option, arg, &st) != 0)
  802. {
  803. stat_error (arg);
  804. USAGE_ERROR ((0, 0, _("Date sample file not found")));
  805. }
  806. newer_mtime_option.tv_sec = st.st_mtime;
  807. newer_mtime_option.tv_nsec = TIMESPEC_NS (st.st_mtim);
  808. }
  809. else
  810. {
  811. if (! get_date (&newer_mtime_option, arg, NULL))
  812. {
  813. WARN ((0, 0, _("Substituting %s for unknown date format %s"),
  814. tartime (newer_mtime_option.tv_sec), quote (arg)));
  815. newer_mtime_option.tv_nsec = 0;
  816. }
  817. else
  818. args->textual_date_option = arg;
  819. }
  820. break;
  821. #endif /* not MSDOS */
  822. case 'o':
  823. args->o_option = true;
  824. break;
  825. case 'O':
  826. to_stdout_option = true;
  827. break;
  828. case 'p':
  829. same_permissions_option = true;
  830. break;
  831. case 'P':
  832. absolute_names_option = true;
  833. break;
  834. case 'r':
  835. set_subcommand_option (APPEND_SUBCOMMAND);
  836. break;
  837. case 'R':
  838. /* Print block numbers for debugging bad tar archives. */
  839. /* It would surely make sense to exchange -B and -R, but it seems
  840. that -B has been used for a long while in Sun tar ans most
  841. BSD-derived systems. This is a consequence of the block/record
  842. terminology confusion. */
  843. block_number_option = true;
  844. break;
  845. case 's':
  846. /* Names to extr are sorted. */
  847. same_order_option = true;
  848. break;
  849. case 'S':
  850. sparse_option = true;
  851. break;
  852. case 't':
  853. set_subcommand_option (LIST_SUBCOMMAND);
  854. verbose_option++;
  855. break;
  856. case 'T':
  857. update_argv (arg, state);
  858. /* Indicate we've been given -T option. This is for backward
  859. compatibility only, so that `tar cfT archive /dev/null will
  860. succeed */
  861. files_from_option = true;
  862. break;
  863. case 'u':
  864. set_subcommand_option (UPDATE_SUBCOMMAND);
  865. break;
  866. case 'U':
  867. old_files_option = UNLINK_FIRST_OLD_FILES;
  868. break;
  869. case UTC_OPTION:
  870. utc_option = true;
  871. break;
  872. case 'v':
  873. verbose_option++;
  874. break;
  875. case 'V':
  876. volume_label_option = arg;
  877. break;
  878. case 'w':
  879. interactive_option = true;
  880. break;
  881. case 'W':
  882. verify_option = true;
  883. break;
  884. case 'x':
  885. set_subcommand_option (EXTRACT_SUBCOMMAND);
  886. break;
  887. case 'X':
  888. if (add_exclude_file (add_exclude, excluded, arg,
  889. args->exclude_options | recursion_option, '\n')
  890. != 0)
  891. {
  892. int e = errno;
  893. FATAL_ERROR ((0, e, "%s", quotearg_colon (arg)));
  894. }
  895. break;
  896. case 'y':
  897. USAGE_ERROR ((0, 0,
  898. _("Warning: the -y option is not supported;"
  899. " perhaps you meant -j?")));
  900. break;
  901. case 'z':
  902. set_use_compress_program_option ("gzip");
  903. break;
  904. case 'Z':
  905. set_use_compress_program_option ("compress");
  906. break;
  907. case ANCHORED_OPTION:
  908. args->exclude_options |= EXCLUDE_ANCHORED;
  909. break;
  910. case ATIME_PRESERVE_OPTION:
  911. atime_preserve_option = true;
  912. break;
  913. case CHECKPOINT_OPTION:
  914. checkpoint_option = true;
  915. break;
  916. case BACKUP_OPTION:
  917. backup_option = true;
  918. if (arg)
  919. args->version_control_string = arg;
  920. break;
  921. case DELETE_OPTION:
  922. set_subcommand_option (DELETE_SUBCOMMAND);
  923. break;
  924. case EXCLUDE_OPTION:
  925. add_exclude (excluded, arg, args->exclude_options | recursion_option);
  926. break;
  927. case EXCLUDE_CACHES_OPTION:
  928. exclude_caches_option = true;
  929. break;
  930. case FORCE_LOCAL_OPTION:
  931. force_local_option = true;
  932. break;
  933. case 'H':
  934. set_archive_format (arg);
  935. break;
  936. case INDEX_FILE_OPTION:
  937. index_file_name = arg;
  938. break;
  939. case IGNORE_CASE_OPTION:
  940. args->exclude_options |= FNM_CASEFOLD;
  941. break;
  942. case IGNORE_COMMAND_ERROR_OPTION:
  943. ignore_command_error_option = true;
  944. break;
  945. case IGNORE_FAILED_READ_OPTION:
  946. ignore_failed_read_option = true;
  947. break;
  948. case KEEP_NEWER_FILES_OPTION:
  949. old_files_option = KEEP_NEWER_FILES;
  950. break;
  951. case GROUP_OPTION:
  952. if (! (strlen (arg) < GNAME_FIELD_SIZE
  953. && gname_to_gid (arg, &group_option)))
  954. {
  955. uintmax_t g;
  956. if (xstrtoumax (arg, 0, 10, &g, "") == LONGINT_OK
  957. && g == (gid_t) g)
  958. group_option = g;
  959. else
  960. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  961. _("%s: Invalid group")));
  962. }
  963. break;
  964. case MODE_OPTION:
  965. mode_option = mode_compile (arg);
  966. if (!mode_option)
  967. FATAL_ERROR ((0, 0, _("Invalid mode given on option")));
  968. initial_umask = umask (0);
  969. umask (initial_umask);
  970. break;
  971. case NO_ANCHORED_OPTION:
  972. args->exclude_options &= ~ EXCLUDE_ANCHORED;
  973. break;
  974. case NO_IGNORE_CASE_OPTION:
  975. args->exclude_options &= ~ FNM_CASEFOLD;
  976. break;
  977. case NO_IGNORE_COMMAND_ERROR_OPTION:
  978. ignore_command_error_option = false;
  979. break;
  980. case NO_OVERWRITE_DIR_OPTION:
  981. old_files_option = NO_OVERWRITE_DIR_OLD_FILES;
  982. break;
  983. case NO_WILDCARDS_OPTION:
  984. args->exclude_options &= ~ EXCLUDE_WILDCARDS;
  985. break;
  986. case NO_WILDCARDS_MATCH_SLASH_OPTION:
  987. args->exclude_options |= FNM_FILE_NAME;
  988. break;
  989. case NULL_OPTION:
  990. filename_terminator = '\0';
  991. break;
  992. case NUMERIC_OWNER_OPTION:
  993. numeric_owner_option = true;
  994. break;
  995. case OCCURRENCE_OPTION:
  996. if (!arg)
  997. occurrence_option = 1;
  998. else
  999. {
  1000. uintmax_t u;
  1001. if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK)
  1002. occurrence_option = u;
  1003. else
  1004. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1005. _("Invalid number")));
  1006. }
  1007. break;
  1008. case OVERWRITE_OPTION:
  1009. old_files_option = OVERWRITE_OLD_FILES;
  1010. break;
  1011. case OWNER_OPTION:
  1012. if (! (strlen (arg) < UNAME_FIELD_SIZE
  1013. && uname_to_uid (arg, &owner_option)))
  1014. {
  1015. uintmax_t u;
  1016. if (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1017. && u == (uid_t) u)
  1018. owner_option = u;
  1019. else
  1020. FATAL_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1021. _("Invalid owner")));
  1022. }
  1023. break;
  1024. case PAX_OPTION:
  1025. args->pax_option++;
  1026. xheader_set_option (arg);
  1027. break;
  1028. case POSIX_OPTION:
  1029. set_archive_format ("posix");
  1030. break;
  1031. case PRESERVE_OPTION:
  1032. same_permissions_option = true;
  1033. same_order_option = true;
  1034. break;
  1035. case RECORD_SIZE_OPTION:
  1036. {
  1037. uintmax_t u;
  1038. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1039. && u == (size_t) u))
  1040. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1041. _("Invalid record size")));
  1042. record_size = u;
  1043. if (record_size % BLOCKSIZE != 0)
  1044. USAGE_ERROR ((0, 0, _("Record size must be a multiple of %d."),
  1045. BLOCKSIZE));
  1046. blocking_factor = record_size / BLOCKSIZE;
  1047. }
  1048. break;
  1049. case RECURSIVE_UNLINK_OPTION:
  1050. recursive_unlink_option = true;
  1051. break;
  1052. case REMOVE_FILES_OPTION:
  1053. remove_files_option = true;
  1054. break;
  1055. case RMT_COMMAND_OPTION:
  1056. rmt_command = arg;
  1057. break;
  1058. case RSH_COMMAND_OPTION:
  1059. rsh_command_option = arg;
  1060. break;
  1061. case SHOW_DEFAULTS_OPTION:
  1062. show_default_settings (stdout);
  1063. exit(0);
  1064. case STRIP_COMPONENTS_OPTION:
  1065. {
  1066. uintmax_t u;
  1067. if (! (xstrtoumax (arg, 0, 10, &u, "") == LONGINT_OK
  1068. && u == (size_t) u))
  1069. USAGE_ERROR ((0, 0, "%s: %s", quotearg_colon (arg),
  1070. _("Invalid number of elements")));
  1071. strip_name_components = u;
  1072. }
  1073. break;
  1074. case SHOW_OMITTED_DIRS_OPTION:
  1075. show_omitted_dirs_option = true;
  1076. break;
  1077. case SUFFIX_OPTION:
  1078. backup_option = true;
  1079. args->backup_suffix_string = arg;
  1080. break;
  1081. case TO_COMMAND_OPTION:
  1082. if (to_command_option)
  1083. USAGE_ERROR ((0, 0, _("Only one --to-command option allowed")));
  1084. to_command_option = arg;
  1085. break;
  1086. case TOTALS_OPTION:
  1087. totals_option = true;
  1088. break;
  1089. case USE_COMPRESS_PROGRAM_OPTION:
  1090. set_use_compress_program_option (arg);
  1091. break;
  1092. case VOLNO_FILE_OPTION:
  1093. volno_file_option = arg;
  1094. break;
  1095. case WILDCARDS_OPTION:
  1096. args->exclude_options |= EXCLUDE_WILDCARDS;
  1097. break;
  1098. case WILDCARDS_MATCH_SLASH_OPTION:
  1099. args->exclude_options &= ~ FNM_FILE_NAME;
  1100. break;
  1101. case CHECK_LINKS_OPTION:
  1102. check_links_option = 1;
  1103. break;
  1104. case NO_RECURSION_OPTION:
  1105. recursion_option = 0;
  1106. break;
  1107. case NO_SAME_OWNER_OPTION:
  1108. same_owner_option = -1;
  1109. break;
  1110. case NO_SAME_PERMISSIONS_OPTION:
  1111. same_permissions_option = -1;
  1112. break;
  1113. case RECURSION_OPTION:
  1114. recursion_option = FNM_LEADING_DIR;
  1115. break;
  1116. case SAME_OWNER_OPTION:
  1117. same_owner_option = 1;
  1118. break;
  1119. case UNQUOTE_OPTION:
  1120. unquote_option = true;
  1121. break;
  1122. case NO_UNQUOTE_OPTION:
  1123. unquote_option = false;
  1124. break;
  1125. case '0':
  1126. case '1':
  1127. case '2':
  1128. case '3':
  1129. case '4':
  1130. case '5':
  1131. case '6':
  1132. case '7':
  1133. #ifdef DEVICE_PREFIX
  1134. {
  1135. int device = key - '0';
  1136. int density;
  1137. static char buf[sizeof DEVICE_PREFIX + 10];
  1138. char *cursor;
  1139. if (arg[1])
  1140. argp_error (state, _("Malformed density argument: %s"), quote (arg));
  1141. strcpy (buf, DEVICE_PREFIX);
  1142. cursor = buf + strlen (buf);
  1143. #ifdef DENSITY_LETTER
  1144. sprintf (cursor, "%d%c", device, arg[0]);
  1145. #else /* not DENSITY_LETTER */
  1146. switch (arg[0])
  1147. {
  1148. case 'l':
  1149. #ifdef LOW_NUM
  1150. device += LOW_NUM;
  1151. #endif
  1152. break;
  1153. case 'm':
  1154. #ifdef MID_NUM
  1155. device += MID_NUM;
  1156. #else
  1157. device += 8;
  1158. #endif
  1159. break;
  1160. case 'h':
  1161. #ifdef HGH_NUM
  1162. device += HGH_NUM;
  1163. #else
  1164. device += 16;
  1165. #endif
  1166. break;
  1167. default:
  1168. argp_error (state, _("Unknown density: `%c'"), arg[0]);
  1169. }
  1170. sprintf (cursor, "%d", device);
  1171. #endif /* not DENSITY_LETTER */
  1172. if (archive_names == allocated_archive_names)
  1173. {
  1174. allocated_archive_names *= 2;
  1175. archive_name_array =
  1176. xrealloc (archive_name_array,
  1177. sizeof (const char *) * allocated_archive_names);
  1178. }
  1179. archive_name_array[archive_names++] = xstrdup (buf);
  1180. }
  1181. break;
  1182. #else /* not DEVICE_PREFIX */
  1183. argp_error (state,
  1184. _("Options `-[0-7][lmh]' not supported by *this* tar"));
  1185. #endif /* not DEVICE_PREFIX */
  1186. case '?':
  1187. state->flags |= ARGP_NO_EXIT;
  1188. argp_state_help (state, state->out_stream,
  1189. ARGP_HELP_STD_HELP & ~ARGP_HELP_BUG_ADDR);
  1190. fprintf (state->out_stream, _("\n*This* tar defaults to:\n"));
  1191. show_default_settings (state->out_stream);
  1192. fprintf (state->out_stream, "\n");
  1193. fprintf (state->out_stream, _("Report bugs to %s.\n"),
  1194. argp_program_bug_address);
  1195. exit (0);
  1196. case USAGE_OPTION:
  1197. argp_state_help (state, state->out_stream,
  1198. ARGP_HELP_USAGE | ARGP_HELP_EXIT_OK);
  1199. break;
  1200. case VERSION_OPTION:
  1201. fprintf (state->out_stream, "%s\n", argp_program_version);
  1202. exit (0);
  1203. case LICENSE_OPTION:
  1204. license ();
  1205. break;
  1206. case HANG_OPTION:
  1207. _argp_hang = atoi (arg ? arg : "3600");
  1208. while (_argp_hang-- > 0)
  1209. sleep (1);
  1210. break;
  1211. default:
  1212. return ARGP_ERR_UNKNOWN;
  1213. }
  1214. return 0;
  1215. }
  1216. static struct argp argp = {
  1217. options,
  1218. parse_opt,
  1219. N_("[FILE]..."),
  1220. doc,
  1221. NULL,
  1222. NULL,
  1223. NULL
  1224. };
  1225. void
  1226. usage (int status)
  1227. {
  1228. argp_help (&argp, stderr, ARGP_HELP_SEE, (char*) program_name);
  1229. exit (status);
  1230. }
  1231. /* Parse the options for tar. */
  1232. static struct argp_option *
  1233. find_argp_option (struct argp_option *options, int letter)
  1234. {
  1235. for (;
  1236. !(options->name == NULL
  1237. && options->key == 0
  1238. && options->arg == 0
  1239. && options->flags == 0
  1240. && options->doc == NULL); options++)
  1241. if (options->key == letter)
  1242. return options;
  1243. return NULL;
  1244. }
  1245. static void
  1246. decode_options (int argc, char **argv)
  1247. {
  1248. int index;
  1249. struct tar_args args;
  1250. /* Set some default option values. */
  1251. args.textual_date_option = NULL;
  1252. args.exclude_options = EXCLUDE_WILDCARDS;
  1253. args.o_option = 0;
  1254. args.pax_option = 0;
  1255. args.backup_suffix_string = getenv ("SIMPLE_BACKUP_SUFFIX");
  1256. args.version_control_string = 0;
  1257. args.input_files = 0;
  1258. subcommand_option = UNKNOWN_SUBCOMMAND;
  1259. archive_format = DEFAULT_FORMAT;
  1260. blocking_factor = DEFAULT_BLOCKING;
  1261. record_size = DEFAULT_BLOCKING * BLOCKSIZE;
  1262. excluded = new_exclude ();
  1263. newer_mtime_option.tv_sec = TYPE_MINIMUM (time_t);
  1264. newer_mtime_option.tv_nsec = -1;
  1265. recursion_option = FNM_LEADING_DIR;
  1266. unquote_option = true;
  1267. owner_option = -1;
  1268. group_option = -1;
  1269. /* Convert old-style tar call by exploding option element and rearranging
  1270. options accordingly. */
  1271. if (argc > 1 && argv[1][0] != '-')
  1272. {
  1273. int new_argc; /* argc value for rearranged arguments */
  1274. char **new_argv; /* argv value for rearranged arguments */
  1275. char *const *in; /* cursor into original argv */
  1276. char **out; /* cursor into rearranged argv */
  1277. const char *letter; /* cursor into old option letters */
  1278. char buffer[3]; /* constructed option buffer */
  1279. /* Initialize a constructed option. */
  1280. buffer[0] = '-';
  1281. buffer[2] = '\0';
  1282. /* Allocate a new argument array, and copy program name in it. */
  1283. new_argc = argc - 1 + strlen (argv[1]);
  1284. new_argv = xmalloc ((new_argc + 1) * sizeof (char *));
  1285. in = argv;
  1286. out = new_argv;
  1287. *out++ = *in++;
  1288. /* Copy each old letter option as a separate option, and have the
  1289. corresponding argument moved next to it. */
  1290. for (letter = *in++; *letter; letter++)
  1291. {
  1292. struct argp_option *opt;
  1293. buffer[1] = *letter;
  1294. *out++ = xstrdup (buffer);
  1295. opt = find_argp_option (options, *letter);
  1296. if (opt && opt->arg)
  1297. {
  1298. if (in < argv + argc)
  1299. *out++ = *in++;
  1300. else
  1301. USAGE_ERROR ((0, 0, _("Old option `%c' requires an argument."),
  1302. *letter));
  1303. }
  1304. }
  1305. /* Copy all remaining options. */
  1306. while (in < argv + argc)
  1307. *out++ = *in++;
  1308. *out = 0;
  1309. /* Replace the old option list by the new one. */
  1310. argc = new_argc;
  1311. argv = new_argv;
  1312. }
  1313. /* Parse all options and non-options as they appear. */
  1314. prepend_default_options (getenv ("TAR_OPTIONS"), &argc, &argv);
  1315. if (argp_parse (&argp, argc, argv, ARGP_IN_ORDER|ARGP_NO_HELP,
  1316. &index, &args))
  1317. exit (1);
  1318. /* Special handling for 'o' option:
  1319. GNU tar used to say "output old format".
  1320. UNIX98 tar says don't chown files after extracting (we use
  1321. "--no-same-owner" for this).
  1322. The old GNU tar semantics is retained when used with --create
  1323. option, otherwise UNIX98 semantics is assumed */
  1324. if (args.o_option)
  1325. {
  1326. if (subcommand_option == CREATE_SUBCOMMAND)
  1327. {
  1328. /* GNU Tar <= 1.13 compatibility */
  1329. set_archive_format ("v7");
  1330. }
  1331. else
  1332. {
  1333. /* UNIX98 compatibility */
  1334. same_owner_option = -1;
  1335. }
  1336. }
  1337. /* Handle operands after any "--" argument. */
  1338. for (; index < argc; index++)
  1339. {
  1340. name_add (argv[index]);
  1341. args.input_files++;
  1342. }
  1343. /* Derive option values and check option consistency. */
  1344. if (archive_format == DEFAULT_FORMAT)
  1345. {
  1346. if (args.pax_option)
  1347. archive_format = POSIX_FORMAT;
  1348. else
  1349. archive_format = DEFAULT_ARCHIVE_FORMAT;
  1350. }
  1351. if (volume_label_option && subcommand_option == CREATE_SUBCOMMAND)
  1352. assert_format (FORMAT_MASK (OLDGNU_FORMAT)
  1353. | FORMAT_MASK (GNU_FORMAT));
  1354. if (incremental_option || multi_volume_option)
  1355. assert_format (FORMAT_MASK (OLDGNU_FORMAT) | FORMAT_MASK (GNU_FORMAT));
  1356. if (sparse_option)
  1357. assert_format (FORMAT_MASK (OLDGNU_FORMAT)
  1358. | FORMAT_MASK (GNU_FORMAT)
  1359. | FORMAT_MASK (POSIX_FORMAT));
  1360. if (occurrence_option)
  1361. {
  1362. if (!args.input_files)
  1363. USAGE_ERROR ((0, 0,
  1364. _("--occurrence is meaningless without a file list")));
  1365. if (subcommand_option != DELETE_SUBCOMMAND
  1366. && subcommand_option != DIFF_SUBCOMMAND
  1367. && subcommand_option != EXTRACT_SUBCOMMAND
  1368. && subcommand_option != LIST_SUBCOMMAND)
  1369. USAGE_ERROR ((0, 0,
  1370. _("--occurrence cannot be used in the requested operation mode")));
  1371. }
  1372. if (seekable_archive && subcommand_option == DELETE_SUBCOMMAND)
  1373. {
  1374. /* The current code in delete.c is based on the assumption that
  1375. skip_member() reads all data from the archive. So, we should
  1376. make sure it won't use seeks. On the other hand, the same code
  1377. depends on the ability to backspace a record in the archive,
  1378. so setting seekable_archive to false is technically incorrect.
  1379. However, it is tested only in skip_member(), so it's not a
  1380. problem. */
  1381. seekable_archive = false;
  1382. }
  1383. if (archive_names == 0)
  1384. {
  1385. /* If no archive file name given, try TAPE from the environment, or
  1386. else, DEFAULT_ARCHIVE from the configuration process. */
  1387. archive_names = 1;
  1388. archive_name_array[0] = getenv ("TAPE");
  1389. if (! archive_name_array[0])
  1390. archive_name_array[0] = DEFAULT_ARCHIVE;
  1391. }
  1392. /* Allow multiple archives only with `-M'. */
  1393. if (archive_names > 1 && !multi_volume_option)
  1394. USAGE_ERROR ((0, 0,
  1395. _("Multiple archive files require `-M' option")));
  1396. if (listed_incremental_option
  1397. && NEWER_OPTION_INITIALIZED (newer_mtime_option))
  1398. USAGE_ERROR ((0, 0,
  1399. _("Cannot combine --listed-incremental with --newer")));
  1400. if (volume_label_option)
  1401. {
  1402. size_t volume_label_max_len =
  1403. (sizeof current_header->header.name
  1404. - 1 /* for trailing '\0' */
  1405. - (multi_volume_option
  1406. ? (sizeof " Volume "
  1407. - 1 /* for null at end of " Volume " */
  1408. + INT_STRLEN_BOUND (int) /* for volume number */
  1409. - 1 /* for sign, as 0 <= volno */)
  1410. : 0));
  1411. if (volume_label_max_len < strlen (volume_label_option))
  1412. USAGE_ERROR ((0, 0,
  1413. ngettext ("%s: Volume label is too long (limit is %lu byte)",
  1414. "%s: Volume label is too long (limit is %lu bytes)",
  1415. volume_label_max_len),
  1416. quotearg_colon (volume_label_option),
  1417. (unsigned long) volume_label_max_len));
  1418. }
  1419. if (verify_option)
  1420. {
  1421. if (multi_volume_option)
  1422. USAGE_ERROR ((0, 0, _("Cannot verify multi-volume archives")));
  1423. if (use_compress_program_option)
  1424. USAGE_ERROR ((0, 0, _("Cannot verify compressed archives")));
  1425. }
  1426. if (use_compress_program_option)
  1427. {
  1428. if (multi_volume_option)
  1429. USAGE_ERROR ((0, 0, _("Cannot use multi-volume compressed archives")));
  1430. if (subcommand_option == UPDATE_SUBCOMMAND)
  1431. USAGE_ERROR ((0, 0, _("Cannot update compressed archives")));
  1432. }
  1433. /* It is no harm to use --pax-option on non-pax archives in archive
  1434. reading mode. It may even be useful, since it allows to override
  1435. file attributes from tar headers. Therefore I allow such usage.
  1436. --gray */
  1437. if (args.pax_option
  1438. && archive_format != POSIX_FORMAT
  1439. && (subcommand_option != EXTRACT_SUBCOMMAND
  1440. || subcommand_option != DIFF_SUBCOMMAND
  1441. || subcommand_option != LIST_SUBCOMMAND))
  1442. USAGE_ERROR ((0, 0, _("--pax-option can be used only on POSIX archives")));
  1443. /* If ready to unlink hierarchies, so we are for simpler files. */
  1444. if (recursive_unlink_option)
  1445. old_files_option = UNLINK_FIRST_OLD_FILES;
  1446. if (utc_option)
  1447. verbose_option = 2;
  1448. /* Forbid using -c with no input files whatsoever. Check that `-f -',
  1449. explicit or implied, is used correctly. */
  1450. switch (subcommand_option)
  1451. {
  1452. case CREATE_SUBCOMMAND:
  1453. if (args.input_files == 0 && !files_from_option)
  1454. USAGE_ERROR ((0, 0,
  1455. _("Cowardly refusing to create an empty archive")));
  1456. break;
  1457. case EXTRACT_SUBCOMMAND:
  1458. case LIST_SUBCOMMAND:
  1459. case DIFF_SUBCOMMAND:
  1460. for (archive_name_cursor = archive_name_array;
  1461. archive_name_cursor < archive_name_array + archive_names;
  1462. archive_name_cursor++)
  1463. if (!strcmp (*archive_name_cursor, "-"))
  1464. request_stdin ("-f");
  1465. break;
  1466. case CAT_SUBCOMMAND:
  1467. case UPDATE_SUBCOMMAND:
  1468. case APPEND_SUBCOMMAND:
  1469. for (archive_name_cursor = archive_name_array;
  1470. archive_name_cursor < archive_name_array + archive_names;
  1471. archive_name_cursor++)
  1472. if (!strcmp (*archive_name_cursor, "-"))
  1473. USAGE_ERROR ((0, 0,
  1474. _("Options `-Aru' are incompatible with `-f -'")));
  1475. default:
  1476. break;
  1477. }
  1478. archive_name_cursor = archive_name_array;
  1479. /* Prepare for generating backup names. */
  1480. if (args.backup_suffix_string)
  1481. simple_backup_suffix = xstrdup (args.backup_suffix_string);
  1482. if (backup_option)
  1483. {
  1484. backup_type = xget_version ("--backup", args.version_control_string);
  1485. /* No backup is needed either if explicitely disabled or if
  1486. the extracted files are not being written to disk. */
  1487. if (backup_type == no_backups || EXTRACT_OVER_PIPE)
  1488. backup_option = false;
  1489. }
  1490. if (verbose_option && args.textual_date_option)
  1491. {
  1492. /* FIXME: tartime should support nanoseconds, too, so that this
  1493. comparison doesn't complain about lost nanoseconds. */
  1494. char const *treated_as = tartime (newer_mtime_option.tv_sec);
  1495. if (strcmp (args.textual_date_option, treated_as) != 0)
  1496. WARN ((0, 0,
  1497. ngettext ("Treating date `%s' as %s + %ld nanosecond",
  1498. "Treating date `%s' as %s + %ld nanoseconds",
  1499. newer_mtime_option.tv_nsec),
  1500. args.textual_date_option, treated_as,
  1501. newer_mtime_option.tv_nsec));
  1502. }
  1503. }
  1504. /* Tar proper. */
  1505. /* Main routine for tar. */
  1506. int
  1507. main (int argc, char **argv)
  1508. {
  1509. set_start_time ();
  1510. program_name = argv[0];
  1511. setlocale (LC_ALL, "");
  1512. bindtextdomain (PACKAGE, LOCALEDIR);
  1513. textdomain (PACKAGE);
  1514. exit_status = TAREXIT_SUCCESS;
  1515. filename_terminator = '\n';
  1516. set_quoting_style (0, escape_quoting_style);
  1517. /* Pre-allocate a few structures. */
  1518. allocated_archive_names = 10;
  1519. archive_name_array =
  1520. xmalloc (sizeof (const char *) * allocated_archive_names);
  1521. archive_names = 0;
  1522. obstack_init (&argv_stk);
  1523. #ifdef SIGCHLD
  1524. /* System V fork+wait does not work if SIGCHLD is ignored. */
  1525. signal (SIGCHLD, SIG_DFL);
  1526. #endif
  1527. init_names ();
  1528. /* Decode options. */
  1529. decode_options (argc, argv);
  1530. name_init ();
  1531. /* Main command execution. */
  1532. if (volno_file_option)
  1533. init_volume_number ();
  1534. switch (subcommand_option)
  1535. {
  1536. case UNKNOWN_SUBCOMMAND:
  1537. USAGE_ERROR ((0, 0,
  1538. _("You must specify one of the `-Acdtrux' options")));
  1539. case CAT_SUBCOMMAND:
  1540. case UPDATE_SUBCOMMAND:
  1541. case APPEND_SUBCOMMAND:
  1542. update_archive ();
  1543. break;
  1544. case DELETE_SUBCOMMAND:
  1545. delete_archive_members ();
  1546. break;
  1547. case CREATE_SUBCOMMAND:
  1548. create_archive ();
  1549. if (totals_option)
  1550. print_total_written ();
  1551. break;
  1552. case EXTRACT_SUBCOMMAND:
  1553. extr_init ();
  1554. read_and (extract_archive);
  1555. /* FIXME: should extract_finish () even if an ordinary signal is
  1556. received. */
  1557. extract_finish ();
  1558. break;
  1559. case LIST_SUBCOMMAND:
  1560. read_and (list_archive);
  1561. break;
  1562. case DIFF_SUBCOMMAND:
  1563. diff_init ();
  1564. read_and (diff_archive);
  1565. break;
  1566. }
  1567. if (check_links_option)
  1568. check_links ();
  1569. if (volno_file_option)
  1570. closeout_volume_number ();
  1571. /* Dispose of allocated memory, and return. */
  1572. free (archive_name_array);
  1573. name_term ();
  1574. if (stdlis != stderr && (ferror (stdlis) || fclose (stdlis) != 0))
  1575. FATAL_ERROR ((0, 0, _("Error in writing to standard output")));
  1576. if (exit_status == TAREXIT_FAILURE)
  1577. error (0, 0, _("Error exit delayed from previous errors"));
  1578. if (ferror (stderr) || fclose (stderr) != 0)
  1579. exit_status = TAREXIT_FAILURE;
  1580. return exit_status;
  1581. }
  1582. void
  1583. tar_stat_init (struct tar_stat_info *st)
  1584. {
  1585. memset (st, 0, sizeof (*st));
  1586. }
  1587. void
  1588. tar_stat_destroy (struct tar_stat_info *st)
  1589. {
  1590. free (st->orig_file_name);
  1591. free (st->file_name);
  1592. free (st->link_name);
  1593. free (st->uname);
  1594. free (st->gname);
  1595. free (st->sparse_map);
  1596. memset (st, 0, sizeof (*st));
  1597. }