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