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