ar.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /* ar.c - Archive modify and extract.
  2. Copyright (C) 1991-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. /*
  17. Bugs: GNU ar used to check file against filesystem in quick_update and
  18. replace operations (would check mtime). Doesn't warn when name truncated.
  19. No way to specify pos_end. Error messages should be more consistent. */
  20. #include "sysdep.h"
  21. #include "bfd.h"
  22. #include "libiberty.h"
  23. #include "progress.h"
  24. #include "getopt.h"
  25. #include "aout/ar.h"
  26. #include "libbfd.h"
  27. #include "bucomm.h"
  28. #include "arsup.h"
  29. #include "filenames.h"
  30. #include "binemul.h"
  31. #include "plugin.h"
  32. #ifdef __GO32___
  33. #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
  34. #else
  35. #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
  36. #endif
  37. /* Static declarations. */
  38. static void mri_emul (void);
  39. static const char *normalize (const char *, bfd *);
  40. static void remove_output (void);
  41. static void map_over_members (bfd *, void (*)(bfd *), char **, int);
  42. static void print_contents (bfd * member);
  43. static void delete_members (bfd *, char **files_to_delete);
  44. static void move_members (bfd *, char **files_to_move);
  45. static void replace_members
  46. (bfd *, char **files_to_replace, bfd_boolean quick);
  47. static void print_descr (bfd * abfd);
  48. static void write_archive (bfd *);
  49. static int ranlib_only (const char *archname);
  50. static int ranlib_touch (const char *archname);
  51. static void usage (int);
  52. /** Globals and flags. */
  53. static int mri_mode;
  54. /* This flag distinguishes between ar and ranlib:
  55. 1 means this is 'ranlib'; 0 means this is 'ar'.
  56. -1 means if we should use argv[0] to decide. */
  57. extern int is_ranlib;
  58. /* Nonzero means don't warn about creating the archive file if necessary. */
  59. int silent_create = 0;
  60. /* Nonzero means describe each action performed. */
  61. int verbose = 0;
  62. /* Nonzero means preserve dates of members when extracting them. */
  63. int preserve_dates = 0;
  64. /* Nonzero means don't replace existing members whose dates are more recent
  65. than the corresponding files. */
  66. int newer_only = 0;
  67. /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
  68. member). -1 means we've been explicitly asked to not write a symbol table;
  69. +1 means we've been explicitly asked to write it;
  70. 0 is the default.
  71. Traditionally, the default in BSD has been to not write the table.
  72. However, for POSIX.2 compliance the default is now to write a symbol table
  73. if any of the members are object files. */
  74. int write_armap = 0;
  75. /* Operate in deterministic mode: write zero for timestamps, uids,
  76. and gids for archive members and the archive symbol table, and write
  77. consistent file modes. */
  78. int deterministic = -1; /* Determinism indeterminate. */
  79. /* Nonzero means it's the name of an existing member; position new or moved
  80. files with respect to this one. */
  81. char *posname = NULL;
  82. /* Sez how to use `posname': pos_before means position before that member.
  83. pos_after means position after that member. pos_end means always at end.
  84. pos_default means default appropriately. For the latter two, `posname'
  85. should also be zero. */
  86. enum pos
  87. {
  88. pos_default, pos_before, pos_after, pos_end
  89. } postype = pos_default;
  90. enum operations
  91. {
  92. none = 0, del, replace, print_table,
  93. print_files, extract, move, quick_append
  94. } operation = none;
  95. static bfd **
  96. get_pos_bfd (bfd **, enum pos, const char *);
  97. /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
  98. extract the COUNTED_NAME_COUNTER instance of that name. */
  99. static bfd_boolean counted_name_mode = 0;
  100. static int counted_name_counter = 0;
  101. /* Whether to truncate names of files stored in the archive. */
  102. static bfd_boolean ar_truncate = FALSE;
  103. /* Whether to use a full file name match when searching an archive.
  104. This is convenient for archives created by the Microsoft lib
  105. program. */
  106. static bfd_boolean full_pathname = FALSE;
  107. /* Whether to create a "thin" archive (symbol index only -- no files). */
  108. static bfd_boolean make_thin_archive = FALSE;
  109. static int show_version = 0;
  110. static int show_help = 0;
  111. #if BFD_SUPPORTS_PLUGINS
  112. static const char *plugin_target = "plugin";
  113. #else
  114. static const char *plugin_target = NULL;
  115. #endif
  116. static const char *target = NULL;
  117. #define OPTION_PLUGIN 201
  118. #define OPTION_TARGET 202
  119. static struct option long_options[] =
  120. {
  121. {"help", no_argument, &show_help, 1},
  122. {"plugin", required_argument, NULL, OPTION_PLUGIN},
  123. {"target", required_argument, NULL, OPTION_TARGET},
  124. {"version", no_argument, &show_version, 1},
  125. {NULL, no_argument, NULL, 0}
  126. };
  127. int interactive = 0;
  128. static void
  129. mri_emul (void)
  130. {
  131. interactive = isatty (fileno (stdin));
  132. yyparse ();
  133. }
  134. /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
  135. COUNT is the length of the FILES chain; FUNCTION is called on each entry
  136. whose name matches one in FILES. */
  137. static void
  138. map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
  139. {
  140. bfd *head;
  141. int match_count;
  142. if (count == 0)
  143. {
  144. for (head = arch->archive_next; head; head = head->archive_next)
  145. {
  146. PROGRESS (1);
  147. function (head);
  148. }
  149. return;
  150. }
  151. /* This may appear to be a baroque way of accomplishing what we want.
  152. However we have to iterate over the filenames in order to notice where
  153. a filename is requested but does not exist in the archive. Ditto
  154. mapping over each file each time -- we want to hack multiple
  155. references. */
  156. for (head = arch->archive_next; head; head = head->archive_next)
  157. head->archive_pass = 0;
  158. for (; count > 0; files++, count--)
  159. {
  160. bfd_boolean found = FALSE;
  161. match_count = 0;
  162. for (head = arch->archive_next; head; head = head->archive_next)
  163. {
  164. const char * filename;
  165. PROGRESS (1);
  166. /* PR binutils/15796: Once an archive element has been matched
  167. do not match it again. If the user provides multiple same-named
  168. parameters on the command line their intent is to match multiple
  169. same-named entries in the archive, not the same entry multiple
  170. times. */
  171. if (head->archive_pass)
  172. continue;
  173. filename = head->filename;
  174. if (filename == NULL)
  175. {
  176. /* Some archive formats don't get the filenames filled in
  177. until the elements are opened. */
  178. struct stat buf;
  179. bfd_stat_arch_elt (head, &buf);
  180. }
  181. else if (bfd_is_thin_archive (arch))
  182. {
  183. /* Thin archives store full pathnames. Need to normalize. */
  184. filename = normalize (filename, arch);
  185. }
  186. if (filename != NULL
  187. && !FILENAME_CMP (normalize (*files, arch), filename))
  188. {
  189. ++match_count;
  190. if (counted_name_mode
  191. && match_count != counted_name_counter)
  192. {
  193. /* Counting, and didn't match on count; go on to the
  194. next one. */
  195. continue;
  196. }
  197. found = TRUE;
  198. function (head);
  199. head->archive_pass = 1;
  200. /* PR binutils/15796: Once a file has been matched, do not
  201. match any more same-named files in the archive. If the
  202. user does want to match multiple same-name files in an
  203. archive they should provide multiple same-name parameters
  204. to the ar command. */
  205. break;
  206. }
  207. }
  208. if (!found)
  209. /* xgettext:c-format */
  210. fprintf (stderr, _("no entry %s in archive\n"), *files);
  211. }
  212. }
  213. bfd_boolean operation_alters_arch = FALSE;
  214. static void
  215. usage (int help)
  216. {
  217. FILE *s;
  218. #if BFD_SUPPORTS_PLUGINS
  219. /* xgettext:c-format */
  220. const char *command_line
  221. = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
  222. " [--plugin <name>] [member-name] [count] archive-file file...\n");
  223. #else
  224. /* xgettext:c-format */
  225. const char *command_line
  226. = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
  227. " [member-name] [count] archive-file file...\n");
  228. #endif
  229. s = help ? stdout : stderr;
  230. fprintf (s, command_line, program_name);
  231. /* xgettext:c-format */
  232. fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
  233. fprintf (s, _(" commands:\n"));
  234. fprintf (s, _(" d - delete file(s) from the archive\n"));
  235. fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
  236. fprintf (s, _(" p - print file(s) found in the archive\n"));
  237. fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
  238. fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
  239. fprintf (s, _(" s - act as ranlib\n"));
  240. fprintf (s, _(" t - display contents of archive\n"));
  241. fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
  242. fprintf (s, _(" command specific modifiers:\n"));
  243. fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
  244. fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
  245. if (DEFAULT_AR_DETERMINISTIC)
  246. {
  247. fprintf (s, _("\
  248. [D] - use zero for timestamps and uids/gids (default)\n"));
  249. fprintf (s, _("\
  250. [U] - use actual timestamps and uids/gids\n"));
  251. }
  252. else
  253. {
  254. fprintf (s, _("\
  255. [D] - use zero for timestamps and uids/gids\n"));
  256. fprintf (s, _("\
  257. [U] - use actual timestamps and uids/gids (default)\n"));
  258. }
  259. fprintf (s, _(" [N] - use instance [count] of name\n"));
  260. fprintf (s, _(" [f] - truncate inserted file names\n"));
  261. fprintf (s, _(" [P] - use full path names when matching\n"));
  262. fprintf (s, _(" [o] - preserve original dates\n"));
  263. fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
  264. fprintf (s, _(" generic modifiers:\n"));
  265. fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
  266. fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
  267. fprintf (s, _(" [S] - do not build a symbol table\n"));
  268. fprintf (s, _(" [T] - make a thin archive\n"));
  269. fprintf (s, _(" [v] - be verbose\n"));
  270. fprintf (s, _(" [V] - display the version number\n"));
  271. fprintf (s, _(" @<file> - read options from <file>\n"));
  272. fprintf (s, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
  273. #if BFD_SUPPORTS_PLUGINS
  274. fprintf (s, _(" optional:\n"));
  275. fprintf (s, _(" --plugin <p> - load the specified plugin\n"));
  276. #endif
  277. ar_emul_usage (s);
  278. list_supported_targets (program_name, s);
  279. if (REPORT_BUGS_TO[0] && help)
  280. fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  281. xexit (help ? 0 : 1);
  282. }
  283. static void
  284. ranlib_usage (int help)
  285. {
  286. FILE *s;
  287. s = help ? stdout : stderr;
  288. /* xgettext:c-format */
  289. fprintf (s, _("Usage: %s [options] archive\n"), program_name);
  290. fprintf (s, _(" Generate an index to speed access to archives\n"));
  291. fprintf (s, _(" The options are:\n\
  292. @<file> Read options from <file>\n"));
  293. #if BFD_SUPPORTS_PLUGINS
  294. fprintf (s, _("\
  295. --plugin <name> Load the specified plugin\n"));
  296. #endif
  297. if (DEFAULT_AR_DETERMINISTIC)
  298. fprintf (s, _("\
  299. -D Use zero for symbol map timestamp (default)\n\
  300. -U Use an actual symbol map timestamp\n"));
  301. else
  302. fprintf (s, _("\
  303. -D Use zero for symbol map timestamp\n\
  304. -U Use actual symbol map timestamp (default)\n"));
  305. fprintf (s, _("\
  306. -t Update the archive's symbol map timestamp\n\
  307. -h --help Print this help message\n\
  308. -v --version Print version information\n"));
  309. list_supported_targets (program_name, s);
  310. if (REPORT_BUGS_TO[0] && help)
  311. fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  312. xexit (help ? 0 : 1);
  313. }
  314. /* Normalize a file name specified on the command line into a file
  315. name which we will use in an archive. */
  316. static const char *
  317. normalize (const char *file, bfd *abfd)
  318. {
  319. const char *filename;
  320. if (full_pathname)
  321. return file;
  322. filename = lbasename (file);
  323. if (ar_truncate
  324. && abfd != NULL
  325. && strlen (filename) > abfd->xvec->ar_max_namelen)
  326. {
  327. char *s;
  328. /* Space leak. */
  329. s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
  330. memcpy (s, filename, abfd->xvec->ar_max_namelen);
  331. s[abfd->xvec->ar_max_namelen] = '\0';
  332. filename = s;
  333. }
  334. return filename;
  335. }
  336. /* Remove any output file. This is only called via xatexit. */
  337. static const char *output_filename = NULL;
  338. static FILE *output_file = NULL;
  339. static bfd *output_bfd = NULL;
  340. static void
  341. remove_output (void)
  342. {
  343. if (output_filename != NULL)
  344. {
  345. if (output_bfd != NULL)
  346. bfd_cache_close (output_bfd);
  347. if (output_file != NULL)
  348. fclose (output_file);
  349. unlink_if_ordinary (output_filename);
  350. }
  351. }
  352. static char **
  353. decode_options (int argc, char **argv)
  354. {
  355. int c;
  356. /* Convert old-style tar call by exploding option element and rearranging
  357. options accordingly. */
  358. if (argc > 1 && argv[1][0] != '-')
  359. {
  360. int new_argc; /* argc value for rearranged arguments */
  361. char **new_argv; /* argv value for rearranged arguments */
  362. char *const *in; /* cursor into original argv */
  363. char **out; /* cursor into rearranged argv */
  364. const char *letter; /* cursor into old option letters */
  365. char buffer[3]; /* constructed option buffer */
  366. /* Initialize a constructed option. */
  367. buffer[0] = '-';
  368. buffer[2] = '\0';
  369. /* Allocate a new argument array, and copy program name in it. */
  370. new_argc = argc - 1 + strlen (argv[1]);
  371. new_argv = xmalloc ((new_argc + 1) * sizeof (*argv));
  372. in = argv;
  373. out = new_argv;
  374. *out++ = *in++;
  375. /* Copy each old letter option as a separate option. */
  376. for (letter = *in++; *letter; letter++)
  377. {
  378. buffer[1] = *letter;
  379. *out++ = xstrdup (buffer);
  380. }
  381. /* Copy all remaining options. */
  382. while (in < argv + argc)
  383. *out++ = *in++;
  384. *out = NULL;
  385. /* Replace the old option list by the new one. */
  386. argc = new_argc;
  387. argv = new_argv;
  388. }
  389. while ((c = getopt_long (argc, argv, "hdmpqrtxlcoVsSuvabiMNfPTDU",
  390. long_options, NULL)) != EOF)
  391. {
  392. switch (c)
  393. {
  394. case 'd':
  395. case 'm':
  396. case 'p':
  397. case 'q':
  398. case 'r':
  399. case 't':
  400. case 'x':
  401. if (operation != none)
  402. fatal (_("two different operation options specified"));
  403. break;
  404. }
  405. switch (c)
  406. {
  407. case 'h':
  408. show_help = 1;
  409. break;
  410. case 'd':
  411. operation = del;
  412. operation_alters_arch = TRUE;
  413. break;
  414. case 'm':
  415. operation = move;
  416. operation_alters_arch = TRUE;
  417. break;
  418. case 'p':
  419. operation = print_files;
  420. break;
  421. case 'q':
  422. operation = quick_append;
  423. operation_alters_arch = TRUE;
  424. break;
  425. case 'r':
  426. operation = replace;
  427. operation_alters_arch = TRUE;
  428. break;
  429. case 't':
  430. operation = print_table;
  431. break;
  432. case 'x':
  433. operation = extract;
  434. break;
  435. case 'l':
  436. break;
  437. case 'c':
  438. silent_create = 1;
  439. break;
  440. case 'o':
  441. preserve_dates = 1;
  442. break;
  443. case 'V':
  444. show_version = TRUE;
  445. break;
  446. case 's':
  447. write_armap = 1;
  448. break;
  449. case 'S':
  450. write_armap = -1;
  451. break;
  452. case 'u':
  453. newer_only = 1;
  454. break;
  455. case 'v':
  456. verbose = 1;
  457. break;
  458. case 'a':
  459. postype = pos_after;
  460. break;
  461. case 'b':
  462. postype = pos_before;
  463. break;
  464. case 'i':
  465. postype = pos_before;
  466. break;
  467. case 'M':
  468. mri_mode = 1;
  469. break;
  470. case 'N':
  471. counted_name_mode = TRUE;
  472. break;
  473. case 'f':
  474. ar_truncate = TRUE;
  475. break;
  476. case 'P':
  477. full_pathname = TRUE;
  478. break;
  479. case 'T':
  480. make_thin_archive = TRUE;
  481. break;
  482. case 'D':
  483. deterministic = TRUE;
  484. break;
  485. case 'U':
  486. deterministic = FALSE;
  487. break;
  488. case OPTION_PLUGIN:
  489. #if BFD_SUPPORTS_PLUGINS
  490. bfd_plugin_set_plugin (optarg);
  491. #else
  492. fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
  493. xexit (1);
  494. #endif
  495. break;
  496. case OPTION_TARGET:
  497. target = optarg;
  498. break;
  499. case 0: /* A long option that just sets a flag. */
  500. break;
  501. default:
  502. usage (0);
  503. }
  504. }
  505. return &argv[optind];
  506. }
  507. /* If neither -D nor -U was specified explicitly,
  508. then use the configured default. */
  509. static void
  510. default_deterministic (void)
  511. {
  512. if (deterministic < 0)
  513. deterministic = DEFAULT_AR_DETERMINISTIC;
  514. }
  515. static void
  516. ranlib_main (int argc, char **argv)
  517. {
  518. int arg_index, status = 0;
  519. bfd_boolean touch = FALSE;
  520. int c;
  521. while ((c = getopt_long (argc, argv, "DhHUvVt", long_options, NULL)) != EOF)
  522. {
  523. switch (c)
  524. {
  525. case 'D':
  526. deterministic = TRUE;
  527. break;
  528. case 'U':
  529. deterministic = FALSE;
  530. break;
  531. case 'h':
  532. case 'H':
  533. show_help = 1;
  534. break;
  535. case 't':
  536. touch = TRUE;
  537. break;
  538. case 'v':
  539. case 'V':
  540. show_version = 1;
  541. break;
  542. /* PR binutils/13493: Support plugins. */
  543. case OPTION_PLUGIN:
  544. #if BFD_SUPPORTS_PLUGINS
  545. bfd_plugin_set_plugin (optarg);
  546. #else
  547. fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
  548. xexit (1);
  549. #endif
  550. break;
  551. }
  552. }
  553. if (argc < 2)
  554. ranlib_usage (0);
  555. if (show_help)
  556. ranlib_usage (1);
  557. if (show_version)
  558. print_version ("ranlib");
  559. default_deterministic ();
  560. arg_index = optind;
  561. while (arg_index < argc)
  562. {
  563. if (! touch)
  564. status |= ranlib_only (argv[arg_index]);
  565. else
  566. status |= ranlib_touch (argv[arg_index]);
  567. ++arg_index;
  568. }
  569. xexit (status);
  570. }
  571. int main (int, char **);
  572. int
  573. main (int argc, char **argv)
  574. {
  575. int arg_index;
  576. char **files;
  577. int file_count;
  578. char *inarch_filename;
  579. int i;
  580. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  581. setlocale (LC_MESSAGES, "");
  582. #endif
  583. #if defined (HAVE_SETLOCALE)
  584. setlocale (LC_CTYPE, "");
  585. #endif
  586. bindtextdomain (PACKAGE, LOCALEDIR);
  587. textdomain (PACKAGE);
  588. program_name = argv[0];
  589. xmalloc_set_program_name (program_name);
  590. bfd_set_error_program_name (program_name);
  591. #if BFD_SUPPORTS_PLUGINS
  592. bfd_plugin_set_program_name (program_name);
  593. #endif
  594. expandargv (&argc, &argv);
  595. if (is_ranlib < 0)
  596. {
  597. const char *temp = lbasename (program_name);
  598. if (strlen (temp) >= 6
  599. && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
  600. is_ranlib = 1;
  601. else
  602. is_ranlib = 0;
  603. }
  604. START_PROGRESS (program_name, 0);
  605. bfd_init ();
  606. set_default_bfd_target ();
  607. xatexit (remove_output);
  608. for (i = 1; i < argc; i++)
  609. if (! ar_emul_parse_arg (argv[i]))
  610. break;
  611. argv += (i - 1);
  612. argc -= (i - 1);
  613. if (is_ranlib)
  614. ranlib_main (argc, argv);
  615. if (argc < 2)
  616. usage (0);
  617. argv = decode_options (argc, argv);
  618. if (show_help)
  619. usage (1);
  620. if (show_version)
  621. print_version ("ar");
  622. arg_index = 0;
  623. if (mri_mode)
  624. {
  625. default_deterministic ();
  626. mri_emul ();
  627. }
  628. else
  629. {
  630. bfd *arch;
  631. /* We don't use do_quick_append any more. Too many systems
  632. expect ar to always rebuild the symbol table even when q is
  633. used. */
  634. /* We can't write an armap when using ar q, so just do ar r
  635. instead. */
  636. if (operation == quick_append && write_armap)
  637. operation = replace;
  638. if ((operation == none || operation == print_table)
  639. && write_armap == 1)
  640. xexit (ranlib_only (argv[arg_index]));
  641. if (operation == none)
  642. fatal (_("no operation specified"));
  643. if (newer_only && operation != replace)
  644. fatal (_("`u' is only meaningful with the `r' option."));
  645. if (newer_only && deterministic > 0)
  646. fatal (_("`u' is not meaningful with the `D' option."));
  647. if (newer_only && deterministic < 0 && DEFAULT_AR_DETERMINISTIC)
  648. non_fatal (_("\
  649. `u' modifier ignored since `D' is the default (see `U')"));
  650. default_deterministic ();
  651. if (postype != pos_default)
  652. posname = argv[arg_index++];
  653. if (counted_name_mode)
  654. {
  655. if (operation != extract && operation != del)
  656. fatal (_("`N' is only meaningful with the `x' and `d' options."));
  657. counted_name_counter = atoi (argv[arg_index++]);
  658. if (counted_name_counter <= 0)
  659. fatal (_("Value for `N' must be positive."));
  660. }
  661. inarch_filename = argv[arg_index++];
  662. for (file_count = 0; argv[arg_index + file_count] != NULL; file_count++)
  663. continue;
  664. files = (file_count > 0) ? argv + arg_index : NULL;
  665. arch = open_inarch (inarch_filename,
  666. files == NULL ? (char *) NULL : files[0]);
  667. if (operation == extract && bfd_is_thin_archive (arch))
  668. fatal (_("`x' cannot be used on thin archives."));
  669. switch (operation)
  670. {
  671. case print_table:
  672. map_over_members (arch, print_descr, files, file_count);
  673. break;
  674. case print_files:
  675. map_over_members (arch, print_contents, files, file_count);
  676. break;
  677. case extract:
  678. map_over_members (arch, extract_file, files, file_count);
  679. break;
  680. case del:
  681. if (files != NULL)
  682. delete_members (arch, files);
  683. else
  684. output_filename = NULL;
  685. break;
  686. case move:
  687. /* PR 12558: Creating and moving at the same time does
  688. not make sense. Just create the archive instead. */
  689. if (! silent_create)
  690. {
  691. if (files != NULL)
  692. move_members (arch, files);
  693. else
  694. output_filename = NULL;
  695. break;
  696. }
  697. /* Fall through. */
  698. case replace:
  699. case quick_append:
  700. if (files != NULL || write_armap > 0)
  701. replace_members (arch, files, operation == quick_append);
  702. else
  703. output_filename = NULL;
  704. break;
  705. /* Shouldn't happen! */
  706. default:
  707. /* xgettext:c-format */
  708. fatal (_("internal error -- this option not implemented"));
  709. }
  710. }
  711. END_PROGRESS (program_name);
  712. xexit (0);
  713. return 0;
  714. }
  715. bfd *
  716. open_inarch (const char *archive_filename, const char *file)
  717. {
  718. bfd **last_one;
  719. bfd *next_one;
  720. struct stat sbuf;
  721. bfd *arch;
  722. char **matching;
  723. bfd_set_error (bfd_error_no_error);
  724. if (target == NULL)
  725. target = plugin_target;
  726. if (stat (archive_filename, &sbuf) != 0)
  727. {
  728. #if !defined(__GO32__) || defined(__DJGPP__)
  729. /* FIXME: I don't understand why this fragment was ifndef'ed
  730. away for __GO32__; perhaps it was in the days of DJGPP v1.x.
  731. stat() works just fine in v2.x, so I think this should be
  732. removed. For now, I enable it for DJGPP v2. -- EZ. */
  733. /* KLUDGE ALERT! Temporary fix until I figger why
  734. stat() is wrong ... think it's buried in GO32's IDT - Jax */
  735. if (errno != ENOENT)
  736. bfd_fatal (archive_filename);
  737. #endif
  738. if (!operation_alters_arch)
  739. {
  740. fprintf (stderr, "%s: ", program_name);
  741. perror (archive_filename);
  742. maybequit ();
  743. return NULL;
  744. }
  745. /* If the target isn't set, try to figure out the target to use
  746. for the archive from the first object on the list. */
  747. if (target == NULL && file != NULL)
  748. {
  749. bfd *obj;
  750. obj = bfd_openr (file, target);
  751. if (obj != NULL)
  752. {
  753. if (bfd_check_format (obj, bfd_object))
  754. target = bfd_get_target (obj);
  755. (void) bfd_close (obj);
  756. }
  757. }
  758. /* Create an empty archive. */
  759. arch = bfd_openw (archive_filename, target);
  760. if (arch == NULL
  761. || ! bfd_set_format (arch, bfd_archive)
  762. || ! bfd_close (arch))
  763. bfd_fatal (archive_filename);
  764. else if (!silent_create)
  765. non_fatal (_("creating %s"), archive_filename);
  766. /* If we die creating a new archive, don't leave it around. */
  767. output_filename = archive_filename;
  768. }
  769. arch = bfd_openr (archive_filename, target);
  770. if (arch == NULL)
  771. {
  772. bloser:
  773. bfd_fatal (archive_filename);
  774. }
  775. if (! bfd_check_format_matches (arch, bfd_archive, &matching))
  776. {
  777. bfd_nonfatal (archive_filename);
  778. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  779. {
  780. list_matching_formats (matching);
  781. free (matching);
  782. }
  783. xexit (1);
  784. }
  785. if ((operation == replace || operation == quick_append)
  786. && bfd_openr_next_archived_file (arch, NULL) != NULL)
  787. {
  788. /* PR 15140: Catch attempts to convert a normal
  789. archive into a thin archive or vice versa. */
  790. if (make_thin_archive && ! bfd_is_thin_archive (arch))
  791. {
  792. fatal (_("Cannot convert existing library %s to thin format"),
  793. bfd_get_filename (arch));
  794. goto bloser;
  795. }
  796. else if (! make_thin_archive && bfd_is_thin_archive (arch))
  797. {
  798. fatal (_("Cannot convert existing thin library %s to normal format"),
  799. bfd_get_filename (arch));
  800. goto bloser;
  801. }
  802. }
  803. last_one = &(arch->archive_next);
  804. /* Read all the contents right away, regardless. */
  805. for (next_one = bfd_openr_next_archived_file (arch, NULL);
  806. next_one;
  807. next_one = bfd_openr_next_archived_file (arch, next_one))
  808. {
  809. PROGRESS (1);
  810. *last_one = next_one;
  811. last_one = &next_one->archive_next;
  812. }
  813. *last_one = (bfd *) NULL;
  814. if (bfd_get_error () != bfd_error_no_more_archived_files)
  815. goto bloser;
  816. return arch;
  817. }
  818. static void
  819. print_contents (bfd *abfd)
  820. {
  821. bfd_size_type ncopied = 0;
  822. bfd_size_type size;
  823. char *cbuf = (char *) xmalloc (BUFSIZE);
  824. struct stat buf;
  825. if (bfd_stat_arch_elt (abfd, &buf) != 0)
  826. /* xgettext:c-format */
  827. fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
  828. if (verbose)
  829. printf ("\n<%s>\n\n", bfd_get_filename (abfd));
  830. bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
  831. size = buf.st_size;
  832. while (ncopied < size)
  833. {
  834. bfd_size_type nread;
  835. bfd_size_type tocopy = size - ncopied;
  836. if (tocopy > BUFSIZE)
  837. tocopy = BUFSIZE;
  838. nread = bfd_bread (cbuf, tocopy, abfd);
  839. if (nread != tocopy)
  840. /* xgettext:c-format */
  841. fatal (_("%s is not a valid archive"),
  842. bfd_get_filename (bfd_my_archive (abfd)));
  843. /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
  844. return value to bfd_size_type to avoid comparison between signed and
  845. unsigned values. */
  846. if ((bfd_size_type) fwrite (cbuf, 1, nread, stdout) != nread)
  847. fatal ("stdout: %s", strerror (errno));
  848. ncopied += tocopy;
  849. }
  850. free (cbuf);
  851. }
  852. /* Extract a member of the archive into its own file.
  853. We defer opening the new file until after we have read a BUFSIZ chunk of the
  854. old one, since we know we have just read the archive header for the old
  855. one. Since most members are shorter than BUFSIZ, this means we will read
  856. the old header, read the old data, write a new inode for the new file, and
  857. write the new data, and be done. This 'optimization' is what comes from
  858. sitting next to a bare disk and hearing it every time it seeks. -- Gnu
  859. Gilmore */
  860. void
  861. extract_file (bfd *abfd)
  862. {
  863. FILE *ostream;
  864. char *cbuf = (char *) xmalloc (BUFSIZE);
  865. bfd_size_type nread, tocopy;
  866. bfd_size_type ncopied = 0;
  867. bfd_size_type size;
  868. struct stat buf;
  869. /* PR binutils/17533: Do not allow directory traversal
  870. outside of the current directory tree. */
  871. if (! is_valid_archive_path (bfd_get_filename (abfd)))
  872. {
  873. non_fatal (_("illegal pathname found in archive member: %s"),
  874. bfd_get_filename (abfd));
  875. free (cbuf);
  876. return;
  877. }
  878. if (bfd_stat_arch_elt (abfd, &buf) != 0)
  879. /* xgettext:c-format */
  880. fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
  881. size = buf.st_size;
  882. if (verbose)
  883. printf ("x - %s\n", bfd_get_filename (abfd));
  884. bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
  885. ostream = NULL;
  886. if (size == 0)
  887. {
  888. /* Seems like an abstraction violation, eh? Well it's OK! */
  889. output_filename = bfd_get_filename (abfd);
  890. ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
  891. if (ostream == NULL)
  892. {
  893. perror (bfd_get_filename (abfd));
  894. xexit (1);
  895. }
  896. output_file = ostream;
  897. }
  898. else
  899. while (ncopied < size)
  900. {
  901. tocopy = size - ncopied;
  902. if (tocopy > BUFSIZE)
  903. tocopy = BUFSIZE;
  904. nread = bfd_bread (cbuf, tocopy, abfd);
  905. if (nread != tocopy)
  906. /* xgettext:c-format */
  907. fatal (_("%s is not a valid archive"),
  908. bfd_get_filename (bfd_my_archive (abfd)));
  909. /* See comment above; this saves disk arm motion */
  910. if (ostream == NULL)
  911. {
  912. /* Seems like an abstraction violation, eh? Well it's OK! */
  913. output_filename = bfd_get_filename (abfd);
  914. ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
  915. if (ostream == NULL)
  916. {
  917. perror (bfd_get_filename (abfd));
  918. xexit (1);
  919. }
  920. output_file = ostream;
  921. }
  922. /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
  923. the return value to bfd_size_type to avoid comparison between
  924. signed and unsigned values. */
  925. if ((bfd_size_type) fwrite (cbuf, 1, nread, ostream) != nread)
  926. fatal ("%s: %s", output_filename, strerror (errno));
  927. ncopied += tocopy;
  928. }
  929. if (ostream != NULL)
  930. fclose (ostream);
  931. output_file = NULL;
  932. output_filename = NULL;
  933. chmod (bfd_get_filename (abfd), buf.st_mode);
  934. if (preserve_dates)
  935. {
  936. /* Set access time to modification time. Only st_mtime is
  937. initialized by bfd_stat_arch_elt. */
  938. buf.st_atime = buf.st_mtime;
  939. set_times (bfd_get_filename (abfd), &buf);
  940. }
  941. free (cbuf);
  942. }
  943. static void
  944. write_archive (bfd *iarch)
  945. {
  946. bfd *obfd;
  947. char *old_name, *new_name;
  948. bfd *contents_head = iarch->archive_next;
  949. old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
  950. strcpy (old_name, bfd_get_filename (iarch));
  951. new_name = make_tempname (old_name);
  952. if (new_name == NULL)
  953. bfd_fatal (_("could not create temporary file whilst writing archive"));
  954. output_filename = new_name;
  955. obfd = bfd_openw (new_name, bfd_get_target (iarch));
  956. if (obfd == NULL)
  957. bfd_fatal (old_name);
  958. output_bfd = obfd;
  959. bfd_set_format (obfd, bfd_archive);
  960. /* Request writing the archive symbol table unless we've
  961. been explicitly requested not to. */
  962. obfd->has_armap = write_armap >= 0;
  963. if (ar_truncate)
  964. {
  965. /* This should really use bfd_set_file_flags, but that rejects
  966. archives. */
  967. obfd->flags |= BFD_TRADITIONAL_FORMAT;
  968. }
  969. if (deterministic)
  970. obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
  971. if (make_thin_archive || bfd_is_thin_archive (iarch))
  972. bfd_is_thin_archive (obfd) = 1;
  973. if (!bfd_set_archive_head (obfd, contents_head))
  974. bfd_fatal (old_name);
  975. if (!bfd_close (obfd))
  976. bfd_fatal (old_name);
  977. output_bfd = NULL;
  978. output_filename = NULL;
  979. /* We don't care if this fails; we might be creating the archive. */
  980. bfd_close (iarch);
  981. if (smart_rename (new_name, old_name, 0) != 0)
  982. xexit (1);
  983. free (old_name);
  984. }
  985. /* Return a pointer to the pointer to the entry which should be rplacd'd
  986. into when altering. DEFAULT_POS should be how to interpret pos_default,
  987. and should be a pos value. */
  988. static bfd **
  989. get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
  990. {
  991. bfd **after_bfd = contents;
  992. enum pos realpos;
  993. const char *realposname;
  994. if (postype == pos_default)
  995. {
  996. realpos = default_pos;
  997. realposname = default_posname;
  998. }
  999. else
  1000. {
  1001. realpos = postype;
  1002. realposname = posname;
  1003. }
  1004. if (realpos == pos_end)
  1005. {
  1006. while (*after_bfd)
  1007. after_bfd = &((*after_bfd)->archive_next);
  1008. }
  1009. else
  1010. {
  1011. for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
  1012. if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
  1013. {
  1014. if (realpos == pos_after)
  1015. after_bfd = &(*after_bfd)->archive_next;
  1016. break;
  1017. }
  1018. }
  1019. return after_bfd;
  1020. }
  1021. static void
  1022. delete_members (bfd *arch, char **files_to_delete)
  1023. {
  1024. bfd **current_ptr_ptr;
  1025. bfd_boolean found;
  1026. bfd_boolean something_changed = FALSE;
  1027. int match_count;
  1028. for (; *files_to_delete != NULL; ++files_to_delete)
  1029. {
  1030. /* In a.out systems, the armap is optional. It's also called
  1031. __.SYMDEF. So if the user asked to delete it, we should remember
  1032. that fact. This isn't quite right for COFF systems (where
  1033. __.SYMDEF might be regular member), but it's very unlikely
  1034. to be a problem. FIXME */
  1035. if (!strcmp (*files_to_delete, "__.SYMDEF"))
  1036. {
  1037. arch->has_armap = FALSE;
  1038. write_armap = -1;
  1039. continue;
  1040. }
  1041. found = FALSE;
  1042. match_count = 0;
  1043. current_ptr_ptr = &(arch->archive_next);
  1044. while (*current_ptr_ptr)
  1045. {
  1046. if (FILENAME_CMP (normalize (*files_to_delete, arch),
  1047. (*current_ptr_ptr)->filename) == 0)
  1048. {
  1049. ++match_count;
  1050. if (counted_name_mode
  1051. && match_count != counted_name_counter)
  1052. {
  1053. /* Counting, and didn't match on count; go on to the
  1054. next one. */
  1055. }
  1056. else
  1057. {
  1058. found = TRUE;
  1059. something_changed = TRUE;
  1060. if (verbose)
  1061. printf ("d - %s\n",
  1062. *files_to_delete);
  1063. *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
  1064. goto next_file;
  1065. }
  1066. }
  1067. current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
  1068. }
  1069. if (verbose && !found)
  1070. {
  1071. /* xgettext:c-format */
  1072. printf (_("No member named `%s'\n"), *files_to_delete);
  1073. }
  1074. next_file:
  1075. ;
  1076. }
  1077. if (something_changed)
  1078. write_archive (arch);
  1079. else
  1080. output_filename = NULL;
  1081. }
  1082. /* Reposition existing members within an archive */
  1083. static void
  1084. move_members (bfd *arch, char **files_to_move)
  1085. {
  1086. bfd **after_bfd; /* New entries go after this one */
  1087. bfd **current_ptr_ptr; /* cdr pointer into contents */
  1088. for (; *files_to_move; ++files_to_move)
  1089. {
  1090. current_ptr_ptr = &(arch->archive_next);
  1091. while (*current_ptr_ptr)
  1092. {
  1093. bfd *current_ptr = *current_ptr_ptr;
  1094. if (FILENAME_CMP (normalize (*files_to_move, arch),
  1095. current_ptr->filename) == 0)
  1096. {
  1097. /* Move this file to the end of the list - first cut from
  1098. where it is. */
  1099. bfd *link_bfd;
  1100. *current_ptr_ptr = current_ptr->archive_next;
  1101. /* Now glue to end */
  1102. after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
  1103. link_bfd = *after_bfd;
  1104. *after_bfd = current_ptr;
  1105. current_ptr->archive_next = link_bfd;
  1106. if (verbose)
  1107. printf ("m - %s\n", *files_to_move);
  1108. goto next_file;
  1109. }
  1110. current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
  1111. }
  1112. /* xgettext:c-format */
  1113. fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
  1114. next_file:;
  1115. }
  1116. write_archive (arch);
  1117. }
  1118. /* Ought to default to replacing in place, but this is existing practice! */
  1119. static void
  1120. replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
  1121. {
  1122. bfd_boolean changed = FALSE;
  1123. bfd **after_bfd; /* New entries go after this one. */
  1124. bfd *current;
  1125. bfd **current_ptr;
  1126. while (files_to_move && *files_to_move)
  1127. {
  1128. if (! quick)
  1129. {
  1130. current_ptr = &arch->archive_next;
  1131. while (*current_ptr)
  1132. {
  1133. current = *current_ptr;
  1134. /* For compatibility with existing ar programs, we
  1135. permit the same file to be added multiple times. */
  1136. if (FILENAME_CMP (normalize (*files_to_move, arch),
  1137. normalize (current->filename, arch)) == 0
  1138. && current->arelt_data != NULL)
  1139. {
  1140. if (newer_only)
  1141. {
  1142. struct stat fsbuf, asbuf;
  1143. if (stat (*files_to_move, &fsbuf) != 0)
  1144. {
  1145. if (errno != ENOENT)
  1146. bfd_fatal (*files_to_move);
  1147. goto next_file;
  1148. }
  1149. if (bfd_stat_arch_elt (current, &asbuf) != 0)
  1150. /* xgettext:c-format */
  1151. fatal (_("internal stat error on %s"),
  1152. current->filename);
  1153. if (fsbuf.st_mtime <= asbuf.st_mtime)
  1154. goto next_file;
  1155. }
  1156. after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
  1157. current->filename);
  1158. if (ar_emul_replace (after_bfd, *files_to_move,
  1159. target, verbose))
  1160. {
  1161. /* Snip out this entry from the chain. */
  1162. *current_ptr = (*current_ptr)->archive_next;
  1163. changed = TRUE;
  1164. }
  1165. goto next_file;
  1166. }
  1167. current_ptr = &(current->archive_next);
  1168. }
  1169. }
  1170. /* Add to the end of the archive. */
  1171. after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
  1172. if (ar_emul_append (after_bfd, *files_to_move, target,
  1173. verbose, make_thin_archive))
  1174. changed = TRUE;
  1175. next_file:;
  1176. files_to_move++;
  1177. }
  1178. if (changed)
  1179. write_archive (arch);
  1180. else
  1181. output_filename = NULL;
  1182. }
  1183. static int
  1184. ranlib_only (const char *archname)
  1185. {
  1186. bfd *arch;
  1187. if (get_file_size (archname) < 1)
  1188. return 1;
  1189. write_armap = 1;
  1190. arch = open_inarch (archname, (char *) NULL);
  1191. if (arch == NULL)
  1192. xexit (1);
  1193. write_archive (arch);
  1194. return 0;
  1195. }
  1196. /* Update the timestamp of the symbol map of an archive. */
  1197. static int
  1198. ranlib_touch (const char *archname)
  1199. {
  1200. #ifdef __GO32__
  1201. /* I don't think updating works on go32. */
  1202. ranlib_only (archname);
  1203. #else
  1204. int f;
  1205. bfd *arch;
  1206. char **matching;
  1207. if (get_file_size (archname) < 1)
  1208. return 1;
  1209. f = open (archname, O_RDWR | O_BINARY, 0);
  1210. if (f < 0)
  1211. {
  1212. bfd_set_error (bfd_error_system_call);
  1213. bfd_fatal (archname);
  1214. }
  1215. arch = bfd_fdopenr (archname, (const char *) NULL, f);
  1216. if (arch == NULL)
  1217. bfd_fatal (archname);
  1218. if (! bfd_check_format_matches (arch, bfd_archive, &matching))
  1219. {
  1220. bfd_nonfatal (archname);
  1221. if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
  1222. {
  1223. list_matching_formats (matching);
  1224. free (matching);
  1225. }
  1226. xexit (1);
  1227. }
  1228. if (! bfd_has_map (arch))
  1229. /* xgettext:c-format */
  1230. fatal (_("%s: no archive map to update"), archname);
  1231. if (deterministic)
  1232. arch->flags |= BFD_DETERMINISTIC_OUTPUT;
  1233. bfd_update_armap_timestamp (arch);
  1234. if (! bfd_close (arch))
  1235. bfd_fatal (archname);
  1236. #endif
  1237. return 0;
  1238. }
  1239. /* Things which are interesting to map over all or some of the files: */
  1240. static void
  1241. print_descr (bfd *abfd)
  1242. {
  1243. print_arelt_descr (stdout, abfd, verbose);
  1244. }