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