1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 Bugs: should use getopt the way tar does (complete w/optional -) and
25 should have long options too. GNU ar used to check file against filesystem
26 in quick_update and replace operations (would check mtime). Doesn't warn
27 when name truncated. No way to specify pos_end. Error messages should be
32 #include "libiberty.h"
38 #include "filenames.h"
44 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
46 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
49 /* Static declarations. */
51 static void mri_emul (void);
52 static const char *normalize (const char *, bfd *);
53 static void remove_output (void);
54 static void map_over_members (bfd *, void (*)(bfd *), char **, int);
55 static void print_contents (bfd * member);
56 static void delete_members (bfd *, char **files_to_delete);
58 static void move_members (bfd *, char **files_to_move);
59 static void replace_members
60 (bfd *, char **files_to_replace, bfd_boolean quick);
61 static void print_descr (bfd * abfd);
62 static void write_archive (bfd *);
63 static int ranlib_only (const char *archname);
64 static int ranlib_touch (const char *archname);
65 static void usage (int);
67 /** Globals and flags. */
71 /* This flag distinguishes between ar and ranlib:
72 1 means this is 'ranlib'; 0 means this is 'ar'.
73 -1 means if we should use argv[0] to decide. */
76 /* Nonzero means don't warn about creating the archive file if necessary. */
77 int silent_create = 0;
79 /* Nonzero means describe each action performed. */
82 /* Nonzero means preserve dates of members when extracting them. */
83 int preserve_dates = 0;
85 /* Nonzero means don't replace existing members whose dates are more recent
86 than the corresponding files. */
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;
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. */
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 = 0;
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;
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. */
113 pos_default, pos_before, pos_after, pos_end
114 } postype = pos_default;
117 get_pos_bfd (bfd **, enum pos, const char *);
119 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
120 extract the COUNTED_NAME_COUNTER instance of that name. */
121 static bfd_boolean counted_name_mode = 0;
122 static int counted_name_counter = 0;
124 /* Whether to truncate names of files stored in the archive. */
125 static bfd_boolean ar_truncate = FALSE;
127 /* Whether to use a full file name match when searching an archive.
128 This is convenient for archives created by the Microsoft lib
130 static bfd_boolean full_pathname = FALSE;
132 /* Whether to create a "thin" archive (symbol index only -- no files). */
133 static bfd_boolean make_thin_archive = FALSE;
135 static const char *plugin_target = NULL;
142 interactive = isatty (fileno (stdin));
146 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
147 COUNT is the length of the FILES chain; FUNCTION is called on each entry
148 whose name matches one in FILES. */
151 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
158 for (head = arch->archive_next; head; head = head->archive_next)
166 /* This may appear to be a baroque way of accomplishing what we want.
167 However we have to iterate over the filenames in order to notice where
168 a filename is requested but does not exist in the archive. Ditto
169 mapping over each file each time -- we want to hack multiple
172 for (; count > 0; files++, count--)
174 bfd_boolean found = FALSE;
177 for (head = arch->archive_next; head; head = head->archive_next)
179 const char * filename;
182 filename = head->filename;
183 if (filename == NULL)
185 /* Some archive formats don't get the filenames filled in
186 until the elements are opened. */
188 bfd_stat_arch_elt (head, &buf);
190 else if (bfd_is_thin_archive (arch))
192 /* Thin archives store full pathnames. Need to normalize. */
193 filename = normalize (filename, arch);
196 if ((filename != NULL) &&
197 (!FILENAME_CMP (normalize (*files, arch), filename)))
200 if (counted_name_mode
201 && match_count != counted_name_counter)
203 /* Counting, and didn't match on count; go on to the
214 /* xgettext:c-format */
215 fprintf (stderr, _("no entry %s in archive\n"), *files);
219 bfd_boolean operation_alters_arch = FALSE;
226 s = help ? stdout : stderr;
228 /* xgettext:c-format */
229 const char * command_line =
230 #if BFD_SUPPORTS_PLUGINS
231 _("Usage: %s [emulation options] [--plugin <name>] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
233 _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
235 fprintf (s, command_line, program_name);
237 /* xgettext:c-format */
238 fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
239 fprintf (s, _(" commands:\n"));
240 fprintf (s, _(" d - delete file(s) from the archive\n"));
241 fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
242 fprintf (s, _(" p - print file(s) found in the archive\n"));
243 fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
244 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
245 fprintf (s, _(" s - act as ranlib\n"));
246 fprintf (s, _(" t - display contents of archive\n"));
247 fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
248 fprintf (s, _(" command specific modifiers:\n"));
249 fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
250 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
251 fprintf (s, _(" [D] - use zero for timestamps and uids/gids\n"));
252 fprintf (s, _(" [N] - use instance [count] of name\n"));
253 fprintf (s, _(" [f] - truncate inserted file names\n"));
254 fprintf (s, _(" [P] - use full path names when matching\n"));
255 fprintf (s, _(" [o] - preserve original dates\n"));
256 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
257 fprintf (s, _(" generic modifiers:\n"));
258 fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
259 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
260 fprintf (s, _(" [S] - do not build a symbol table\n"));
261 fprintf (s, _(" [T] - make a thin archive\n"));
262 fprintf (s, _(" [v] - be verbose\n"));
263 fprintf (s, _(" [V] - display the version number\n"));
264 fprintf (s, _(" @<file> - read options from <file>\n"));
265 #if BFD_SUPPORTS_PLUGINS
266 fprintf (s, _(" optional:\n"));
267 fprintf (s, _(" --plugin <p> - load the specified plugin\n"));
272 list_supported_targets (program_name, s);
274 if (REPORT_BUGS_TO[0] && help)
275 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
277 xexit (help ? 0 : 1);
281 ranlib_usage(int help)
285 s = help ? stdout : stderr;
287 /* xgettext:c-format */
288 fprintf (s, _("Usage: %s [options] archive\n"), program_name);
289 fprintf (s, _(" Generate an index to speed access to archives\n"));
290 fprintf (s, _(" The options are:\n\
291 @<file> Read options from <file>\n"));
292 #if BFD_SUPPORTS_PLUGINS
294 --plugin <name> Load the specified plugin\n"));
297 -t Update the archive's symbol map timestamp\n\
298 -h --help Print this help message\n\
299 -v --version Print version information\n"));
301 list_supported_targets (program_name, s);
303 if (REPORT_BUGS_TO[0] && help)
304 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
306 xexit (help ? 0 : 1);
309 /* Normalize a file name specified on the command line into a file
310 name which we will use in an archive. */
313 normalize (const char *file, bfd *abfd)
315 const char *filename;
320 filename = lbasename (file);
324 && strlen (filename) > abfd->xvec->ar_max_namelen)
329 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
330 memcpy (s, filename, abfd->xvec->ar_max_namelen);
331 s[abfd->xvec->ar_max_namelen] = '\0';
338 /* Remove any output file. This is only called via xatexit. */
340 static const char *output_filename = NULL;
341 static FILE *output_file = NULL;
342 static bfd *output_bfd = NULL;
347 if (output_filename != NULL)
349 if (output_bfd != NULL)
350 bfd_cache_close (output_bfd);
351 if (output_file != NULL)
352 fclose (output_file);
353 unlink_if_ordinary (output_filename);
358 ranlib_main(int argc, char **argv)
360 int arg_index, status = 0;
361 bfd_boolean touch = FALSE;
363 if (argc > 1 && argv[1][0] == '-')
365 if (strcmp (argv[1], "--help") == 0)
367 else if (strcmp (argv[1], "--version") == 0)
369 print_version ("ranlib");
374 || strcmp (argv[1], "--help") == 0
375 || strcmp (argv[1], "-h") == 0
376 || strcmp (argv[1], "-H") == 0)
379 if (strcmp (argv[1], "-V") == 0
380 || strcmp (argv[1], "-v") == 0
381 || CONST_STRNEQ (argv[1], "--v"))
382 print_version ("ranlib");
385 if (strcmp (argv[1], "-t") == 0)
391 while (arg_index < argc)
394 status |= ranlib_only (argv[arg_index]);
396 status |= ranlib_touch (argv[arg_index]);
403 /* The option parsing should be in its own function.
404 It will be when I have getopt working. */
406 int main (int, char **);
409 main (int argc, char **argv)
415 none = 0, del, replace, print_table,
416 print_files, extract, move, quick_append
421 char *inarch_filename;
426 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
427 setlocale (LC_MESSAGES, "");
429 #if defined (HAVE_SETLOCALE)
430 setlocale (LC_CTYPE, "");
432 bindtextdomain (PACKAGE, LOCALEDIR);
433 textdomain (PACKAGE);
435 program_name = argv[0];
436 xmalloc_set_program_name (program_name);
437 #if BFD_SUPPORTS_PLUGINS
438 bfd_plugin_set_program_name (program_name);
441 expandargv (&argc, &argv);
445 const char *temp = lbasename (program_name);
447 if (strlen (temp) >= 6
448 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
455 START_PROGRESS (program_name, 0);
458 set_default_bfd_target ();
462 xatexit (remove_output);
464 for (i = 1; i < argc; i++)
465 if (! ar_emul_parse_arg (argv[i]))
471 ranlib_main(argc, argv);
473 if (argc > 1 && argv[1][0] == '-')
475 if (strcmp (argv[1], "--help") == 0)
477 else if (strcmp (argv[1], "--version") == 0)
478 print_version ("ar");
481 if (argc == 2 && strcmp (argv[1], "-M") == 0)
491 arg_ptr = argv[arg_index];
493 if (strcmp (arg_ptr, "--plugin") == 0)
495 #if BFD_SUPPORTS_PLUGINS
499 bfd_plugin_set_plugin (argv[2]);
502 arg_ptr = argv[arg_index];
504 plugin_target = "plugin";
506 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
513 /* When the first option starts with '-' we support POSIX-compatible
516 ++arg_ptr; /* compatibility */
521 while ((c = *arg_ptr++) != '\0')
532 if (operation != none)
533 fatal (_("two different operation options specified"));
538 operation_alters_arch = TRUE;
542 operation_alters_arch = TRUE;
545 operation = print_files;
548 operation = quick_append;
549 operation_alters_arch = TRUE;
553 operation_alters_arch = TRUE;
556 operation = print_table;
589 postype = pos_before;
592 postype = pos_before;
598 counted_name_mode = TRUE;
604 full_pathname = TRUE;
607 make_thin_archive = TRUE;
610 deterministic = TRUE;
613 /* xgettext:c-format */
614 non_fatal (_("illegal option -- %c"), c);
619 /* With POSIX-compatible option parsing continue with the next
620 argument if it starts with '-'. */
621 if (do_posix && arg_index + 1 < argc && argv[arg_index + 1][0] == '-')
622 arg_ptr = argv[++arg_index] + 1;
629 print_version ("ar");
632 if (arg_index >= argc)
643 /* We don't use do_quick_append any more. Too many systems
644 expect ar to always rebuild the symbol table even when q is
647 /* We can't write an armap when using ar q, so just do ar r
649 if (operation == quick_append && write_armap)
652 if ((operation == none || operation == print_table)
654 xexit (ranlib_only (argv[arg_index]));
656 if (operation == none)
657 fatal (_("no operation specified"));
659 if (newer_only && operation != replace)
660 fatal (_("`u' is only meaningful with the `r' option."));
662 if (newer_only && deterministic)
663 fatal (_("`u' is not meaningful with the `D' option."));
665 if (postype != pos_default)
666 posname = argv[arg_index++];
668 if (counted_name_mode)
670 if (operation != extract && operation != del)
671 fatal (_("`N' is only meaningful with the `x' and `d' options."));
672 counted_name_counter = atoi (argv[arg_index++]);
673 if (counted_name_counter <= 0)
674 fatal (_("Value for `N' must be positive."));
677 inarch_filename = argv[arg_index++];
679 files = arg_index < argc ? argv + arg_index : NULL;
680 file_count = argc - arg_index;
682 arch = open_inarch (inarch_filename,
683 files == NULL ? (char *) NULL : files[0]);
685 if (operation == extract && bfd_is_thin_archive (arch))
686 fatal (_("`x' cannot be used on thin archives."));
691 map_over_members (arch, print_descr, files, file_count);
695 map_over_members (arch, print_contents, files, file_count);
699 map_over_members (arch, extract_file, files, file_count);
704 delete_members (arch, files);
706 output_filename = NULL;
711 move_members (arch, files);
713 output_filename = NULL;
718 if (files != NULL || write_armap > 0)
719 replace_members (arch, files, operation == quick_append);
721 output_filename = NULL;
724 /* Shouldn't happen! */
726 /* xgettext:c-format */
727 fatal (_("internal error -- this option not implemented"));
731 END_PROGRESS (program_name);
738 open_inarch (const char *archive_filename, const char *file)
747 bfd_set_error (bfd_error_no_error);
749 target = plugin_target;
751 if (stat (archive_filename, &sbuf) != 0)
753 #if !defined(__GO32__) || defined(__DJGPP__)
755 /* FIXME: I don't understand why this fragment was ifndef'ed
756 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
757 stat() works just fine in v2.x, so I think this should be
758 removed. For now, I enable it for DJGPP v2. -- EZ. */
760 /* KLUDGE ALERT! Temporary fix until I figger why
761 stat() is wrong ... think it's buried in GO32's IDT - Jax */
763 bfd_fatal (archive_filename);
766 if (!operation_alters_arch)
768 fprintf (stderr, "%s: ", program_name);
769 perror (archive_filename);
774 /* Try to figure out the target to use for the archive from the
775 first object on the list. */
780 obj = bfd_openr (file, target);
783 if (bfd_check_format (obj, bfd_object))
784 target = bfd_get_target (obj);
785 (void) bfd_close (obj);
789 /* Create an empty archive. */
790 arch = bfd_openw (archive_filename, target);
792 || ! bfd_set_format (arch, bfd_archive)
793 || ! bfd_close (arch))
794 bfd_fatal (archive_filename);
795 else if (!silent_create)
796 non_fatal (_("creating %s"), archive_filename);
798 /* If we die creating a new archive, don't leave it around. */
799 output_filename = archive_filename;
802 arch = bfd_openr (archive_filename, target);
806 bfd_fatal (archive_filename);
809 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
811 bfd_nonfatal (archive_filename);
812 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
814 list_matching_formats (matching);
820 last_one = &(arch->archive_next);
821 /* Read all the contents right away, regardless. */
822 for (next_one = bfd_openr_next_archived_file (arch, NULL);
824 next_one = bfd_openr_next_archived_file (arch, next_one))
827 *last_one = next_one;
828 last_one = &next_one->archive_next;
830 *last_one = (bfd *) NULL;
831 if (bfd_get_error () != bfd_error_no_more_archived_files)
837 print_contents (bfd *abfd)
840 char *cbuf = (char *) xmalloc (BUFSIZE);
843 if (bfd_stat_arch_elt (abfd, &buf) != 0)
844 /* xgettext:c-format */
845 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
848 printf ("\n<%s>\n\n", bfd_get_filename (abfd));
850 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
853 while (ncopied < size)
857 size_t tocopy = size - ncopied;
858 if (tocopy > BUFSIZE)
861 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
863 /* xgettext:c-format */
864 fatal (_("%s is not a valid archive"),
865 bfd_get_filename (bfd_my_archive (abfd)));
867 /* fwrite in mingw32 may return int instead of size_t. Cast the
868 return value to size_t to avoid comparison between signed and
870 if ((size_t) fwrite (cbuf, 1, nread, stdout) != nread)
871 fatal ("stdout: %s", strerror (errno));
877 /* Extract a member of the archive into its own file.
879 We defer opening the new file until after we have read a BUFSIZ chunk of the
880 old one, since we know we have just read the archive header for the old
881 one. Since most members are shorter than BUFSIZ, this means we will read
882 the old header, read the old data, write a new inode for the new file, and
883 write the new data, and be done. This 'optimization' is what comes from
884 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
888 extract_file (bfd *abfd)
891 char *cbuf = (char *) xmalloc (BUFSIZE);
892 size_t nread, tocopy;
897 if (bfd_stat_arch_elt (abfd, &buf) != 0)
898 /* xgettext:c-format */
899 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
903 printf ("x - %s\n", bfd_get_filename (abfd));
905 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
910 /* Seems like an abstraction violation, eh? Well it's OK! */
911 output_filename = bfd_get_filename (abfd);
913 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
916 perror (bfd_get_filename (abfd));
920 output_file = ostream;
923 while (ncopied < size)
925 tocopy = size - ncopied;
926 if (tocopy > BUFSIZE)
929 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
931 /* xgettext:c-format */
932 fatal (_("%s is not a valid archive"),
933 bfd_get_filename (bfd_my_archive (abfd)));
935 /* See comment above; this saves disk arm motion */
938 /* Seems like an abstraction violation, eh? Well it's OK! */
939 output_filename = bfd_get_filename (abfd);
941 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
944 perror (bfd_get_filename (abfd));
948 output_file = ostream;
951 /* fwrite in mingw32 may return int instead of size_t. Cast
952 the return value to size_t to avoid comparison between
953 signed and unsigned values. */
954 if ((size_t) fwrite (cbuf, 1, nread, ostream) != nread)
955 fatal ("%s: %s", output_filename, strerror (errno));
963 output_filename = NULL;
965 chmod (bfd_get_filename (abfd), buf.st_mode);
969 /* Set access time to modification time. Only st_mtime is
970 initialized by bfd_stat_arch_elt. */
971 buf.st_atime = buf.st_mtime;
972 set_times (bfd_get_filename (abfd), &buf);
979 write_archive (bfd *iarch)
982 char *old_name, *new_name;
983 bfd *contents_head = iarch->archive_next;
985 old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
986 strcpy (old_name, bfd_get_filename (iarch));
987 new_name = make_tempname (old_name);
989 if (new_name == NULL)
990 bfd_fatal ("could not create temporary file whilst writing archive");
992 output_filename = new_name;
994 obfd = bfd_openw (new_name, bfd_get_target (iarch));
997 bfd_fatal (old_name);
1001 bfd_set_format (obfd, bfd_archive);
1003 /* Request writing the archive symbol table unless we've
1004 been explicitly requested not to. */
1005 obfd->has_armap = write_armap >= 0;
1009 /* This should really use bfd_set_file_flags, but that rejects
1011 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1015 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
1017 if (make_thin_archive || bfd_is_thin_archive (iarch))
1018 bfd_is_thin_archive (obfd) = 1;
1020 if (!bfd_set_archive_head (obfd, contents_head))
1021 bfd_fatal (old_name);
1023 if (!bfd_close (obfd))
1024 bfd_fatal (old_name);
1027 output_filename = NULL;
1029 /* We don't care if this fails; we might be creating the archive. */
1032 if (smart_rename (new_name, old_name, 0) != 0)
1036 /* Return a pointer to the pointer to the entry which should be rplacd'd
1037 into when altering. DEFAULT_POS should be how to interpret pos_default,
1038 and should be a pos value. */
1041 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
1043 bfd **after_bfd = contents;
1045 const char *realposname;
1047 if (postype == pos_default)
1049 realpos = default_pos;
1050 realposname = default_posname;
1055 realposname = posname;
1058 if (realpos == pos_end)
1061 after_bfd = &((*after_bfd)->archive_next);
1065 for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
1066 if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1068 if (realpos == pos_after)
1069 after_bfd = &(*after_bfd)->archive_next;
1077 delete_members (bfd *arch, char **files_to_delete)
1079 bfd **current_ptr_ptr;
1081 bfd_boolean something_changed = FALSE;
1084 for (; *files_to_delete != NULL; ++files_to_delete)
1086 /* In a.out systems, the armap is optional. It's also called
1087 __.SYMDEF. So if the user asked to delete it, we should remember
1088 that fact. This isn't quite right for COFF systems (where
1089 __.SYMDEF might be regular member), but it's very unlikely
1090 to be a problem. FIXME */
1092 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1094 arch->has_armap = FALSE;
1101 current_ptr_ptr = &(arch->archive_next);
1102 while (*current_ptr_ptr)
1104 if (FILENAME_CMP (normalize (*files_to_delete, arch),
1105 (*current_ptr_ptr)->filename) == 0)
1108 if (counted_name_mode
1109 && match_count != counted_name_counter)
1111 /* Counting, and didn't match on count; go on to the
1117 something_changed = TRUE;
1121 *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
1126 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1129 if (verbose && !found)
1131 /* xgettext:c-format */
1132 printf (_("No member named `%s'\n"), *files_to_delete);
1138 if (something_changed)
1139 write_archive (arch);
1141 output_filename = NULL;
1145 /* Reposition existing members within an archive */
1148 move_members (bfd *arch, char **files_to_move)
1150 bfd **after_bfd; /* New entries go after this one */
1151 bfd **current_ptr_ptr; /* cdr pointer into contents */
1153 for (; *files_to_move; ++files_to_move)
1155 current_ptr_ptr = &(arch->archive_next);
1156 while (*current_ptr_ptr)
1158 bfd *current_ptr = *current_ptr_ptr;
1159 if (FILENAME_CMP (normalize (*files_to_move, arch),
1160 current_ptr->filename) == 0)
1162 /* Move this file to the end of the list - first cut from
1165 *current_ptr_ptr = current_ptr->archive_next;
1167 /* Now glue to end */
1168 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1169 link_bfd = *after_bfd;
1170 *after_bfd = current_ptr;
1171 current_ptr->archive_next = link_bfd;
1174 printf ("m - %s\n", *files_to_move);
1179 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1181 /* xgettext:c-format */
1182 fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1187 write_archive (arch);
1190 /* Ought to default to replacing in place, but this is existing practice! */
1193 replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
1195 bfd_boolean changed = FALSE;
1196 bfd **after_bfd; /* New entries go after this one. */
1200 while (files_to_move && *files_to_move)
1204 current_ptr = &arch->archive_next;
1205 while (*current_ptr)
1207 current = *current_ptr;
1209 /* For compatibility with existing ar programs, we
1210 permit the same file to be added multiple times. */
1211 if (FILENAME_CMP (normalize (*files_to_move, arch),
1212 normalize (current->filename, arch)) == 0
1213 && current->arelt_data != NULL)
1217 struct stat fsbuf, asbuf;
1219 if (stat (*files_to_move, &fsbuf) != 0)
1221 if (errno != ENOENT)
1222 bfd_fatal (*files_to_move);
1225 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1226 /* xgettext:c-format */
1227 fatal (_("internal stat error on %s"),
1230 if (fsbuf.st_mtime <= asbuf.st_mtime)
1234 after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
1236 if (ar_emul_replace (after_bfd, *files_to_move,
1237 plugin_target, verbose))
1239 /* Snip out this entry from the chain. */
1240 *current_ptr = (*current_ptr)->archive_next;
1246 current_ptr = &(current->archive_next);
1250 /* Add to the end of the archive. */
1251 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1253 if (ar_emul_append (after_bfd, *files_to_move, plugin_target,
1254 verbose, make_thin_archive))
1263 write_archive (arch);
1265 output_filename = NULL;
1269 ranlib_only (const char *archname)
1273 if (get_file_size (archname) < 1)
1276 arch = open_inarch (archname, (char *) NULL);
1279 write_archive (arch);
1283 /* Update the timestamp of the symbol map of an archive. */
1286 ranlib_touch (const char *archname)
1289 /* I don't think updating works on go32. */
1290 ranlib_only (archname);
1296 if (get_file_size (archname) < 1)
1298 f = open (archname, O_RDWR | O_BINARY, 0);
1301 bfd_set_error (bfd_error_system_call);
1302 bfd_fatal (archname);
1305 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1307 bfd_fatal (archname);
1308 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1310 bfd_nonfatal (archname);
1311 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1313 list_matching_formats (matching);
1319 if (! bfd_has_map (arch))
1320 /* xgettext:c-format */
1321 fatal (_("%s: no archive map to update"), archname);
1323 bfd_update_armap_timestamp (arch);
1325 if (! bfd_close (arch))
1326 bfd_fatal (archname);
1331 /* Things which are interesting to map over all or some of the files: */
1334 print_descr (bfd *abfd)
1336 print_arelt_descr (stdout, abfd, verbose);