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