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