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