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