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
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 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
52 bfd_special_undocumented_glue (bfd * abfd, const char *filename);
54 /* Static declarations. */
56 static void mri_emul (void);
57 static const char *normalize (const char *, bfd *);
58 static void remove_output (void);
59 static void map_over_members (bfd *, void (*)(bfd *), char **, int);
60 static void print_contents (bfd * member);
61 static void delete_members (bfd *, char **files_to_delete);
63 static void move_members (bfd *, char **files_to_move);
64 static void replace_members
65 (bfd *, char **files_to_replace, bfd_boolean quick);
66 static void print_descr (bfd * abfd);
67 static void write_archive (bfd *);
68 static int ranlib_only (const char *archname);
69 static int ranlib_touch (const char *archname);
70 static void usage (int);
72 /** Globals and flags. */
76 /* This flag distinguishes between ar and ranlib:
77 1 means this is 'ranlib'; 0 means this is 'ar'.
78 -1 means if we should use argv[0] to decide. */
81 /* Nonzero means don't warn about creating the archive file if necessary. */
82 int silent_create = 0;
84 /* Nonzero means describe each action performed. */
87 /* Nonzero means preserve dates of members when extracting them. */
88 int preserve_dates = 0;
90 /* Nonzero means don't replace existing members whose dates are more recent
91 than the corresponding files. */
94 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
95 member). -1 means we've been explicitly asked to not write a symbol table;
96 +1 means we've been explicitly asked to write it;
98 Traditionally, the default in BSD has been to not write the table.
99 However, for POSIX.2 compliance the default is now to write a symbol table
100 if any of the members are object files. */
103 /* Operate in deterministic mode: write zero for timestamps, uids,
104 and gids for archive members and the archive symbol table, and write
105 consistent file modes. */
106 int deterministic = 0;
108 /* Nonzero means it's the name of an existing member; position new or moved
109 files with respect to this one. */
110 char *posname = NULL;
112 /* Sez how to use `posname': pos_before means position before that member.
113 pos_after means position after that member. pos_end means always at end.
114 pos_default means default appropriately. For the latter two, `posname'
115 should also be zero. */
118 pos_default, pos_before, pos_after, pos_end
119 } postype = pos_default;
122 get_pos_bfd (bfd **, enum pos, const char *);
124 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
125 extract the COUNTED_NAME_COUNTER instance of that name. */
126 static bfd_boolean counted_name_mode = 0;
127 static int counted_name_counter = 0;
129 /* Whether to truncate names of files stored in the archive. */
130 static bfd_boolean ar_truncate = FALSE;
132 /* Whether to use a full file name match when searching an archive.
133 This is convenient for archives created by the Microsoft lib
135 static bfd_boolean full_pathname = FALSE;
137 /* Whether to create a "thin" archive (symbol index only -- no files). */
138 static bfd_boolean make_thin_archive = FALSE;
145 interactive = isatty (fileno (stdin));
149 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
150 COUNT is the length of the FILES chain; FUNCTION is called on each entry
151 whose name matches one in FILES. */
154 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
161 for (head = arch->archive_next; head; head = head->archive_next)
169 /* This may appear to be a baroque way of accomplishing what we want.
170 However we have to iterate over the filenames in order to notice where
171 a filename is requested but does not exist in the archive. Ditto
172 mapping over each file each time -- we want to hack multiple
175 for (; count > 0; files++, count--)
177 bfd_boolean found = FALSE;
180 for (head = arch->archive_next; head; head = head->archive_next)
182 const char * filename;
185 filename = head->filename;
186 if (filename == NULL)
188 /* Some archive formats don't get the filenames filled in
189 until the elements are opened. */
191 bfd_stat_arch_elt (head, &buf);
193 else if (bfd_is_thin_archive (arch))
195 /* Thin archives store full pathnames. Need to normalize. */
196 filename = normalize (filename, arch);
199 if ((filename != NULL) &&
200 (!FILENAME_CMP (normalize (*files, arch), filename)))
203 if (counted_name_mode
204 && match_count != counted_name_counter)
206 /* Counting, and didn't match on count; go on to the
217 /* xgettext:c-format */
218 fprintf (stderr, _("no entry %s in archive\n"), *files);
222 bfd_boolean operation_alters_arch = FALSE;
229 s = help ? stdout : stderr;
233 /* xgettext:c-format */
234 const char * command_line =
235 #if BFD_SUPPORTS_PLUGINS
236 _("Usage: %s [emulation options] [--plugin <name>] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
238 _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
240 fprintf (s, command_line, program_name);
242 /* xgettext:c-format */
243 fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
244 fprintf (s, _(" commands:\n"));
245 fprintf (s, _(" d - delete file(s) from the archive\n"));
246 fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
247 fprintf (s, _(" p - print file(s) found in the archive\n"));
248 fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
249 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
250 fprintf (s, _(" t - display contents of archive\n"));
251 fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
252 fprintf (s, _(" command specific modifiers:\n"));
253 fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
254 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
255 fprintf (s, _(" [D] - use zero for timestamps and uids/gids\n"));
256 fprintf (s, _(" [N] - use instance [count] of name\n"));
257 fprintf (s, _(" [f] - truncate inserted file names\n"));
258 fprintf (s, _(" [P] - use full path names when matching\n"));
259 fprintf (s, _(" [o] - preserve original dates\n"));
260 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
261 fprintf (s, _(" generic modifiers:\n"));
262 fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
263 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
264 fprintf (s, _(" [S] - do not build a symbol table\n"));
265 fprintf (s, _(" [T] - make a thin archive\n"));
266 fprintf (s, _(" [v] - be verbose\n"));
267 fprintf (s, _(" [V] - display the version number\n"));
268 fprintf (s, _(" @<file> - read options from <file>\n"));
269 #if BFD_SUPPORTS_PLUGINS
270 fprintf (s, _(" optional:\n"));
271 fprintf (s, _(" --plugin <p> - load the specified plugin\n"));
277 /* xgettext:c-format */
278 fprintf (s, _("Usage: %s [options] archive\n"), program_name);
279 fprintf (s, _(" Generate an index to speed access to archives\n"));
280 fprintf (s, _(" The options are:\n\
281 @<file> Read options from <file>\n"
282 #if BFD_SUPPORTS_PLUGINS
283 " --plugin <name> Load the specified plugin\n"
285 " -t Update the archive's symbol map timestamp\n\
286 -h --help Print this help message\n\
287 -v --version Print version information\n"));
290 list_supported_targets (program_name, s);
292 if (REPORT_BUGS_TO[0] && help)
293 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
295 xexit (help ? 0 : 1);
298 /* Normalize a file name specified on the command line into a file
299 name which we will use in an archive. */
302 normalize (const char *file, bfd *abfd)
304 const char *filename;
309 filename = strrchr (file, '/');
310 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
312 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
313 char *bslash = strrchr (file, '\\');
315 if (filename == NULL || (bslash != NULL && bslash > filename))
317 if (filename == NULL && file[0] != '\0' && file[1] == ':')
321 if (filename != (char *) NULL)
328 && strlen (filename) > abfd->xvec->ar_max_namelen)
333 s = xmalloc (abfd->xvec->ar_max_namelen + 1);
334 memcpy (s, filename, abfd->xvec->ar_max_namelen);
335 s[abfd->xvec->ar_max_namelen] = '\0';
342 /* Remove any output file. This is only called via xatexit. */
344 static const char *output_filename = NULL;
345 static FILE *output_file = NULL;
346 static bfd *output_bfd = NULL;
351 if (output_filename != NULL)
353 if (output_bfd != NULL)
354 bfd_cache_close (output_bfd);
355 if (output_file != NULL)
356 fclose (output_file);
357 unlink_if_ordinary (output_filename);
361 /* The option parsing should be in its own function.
362 It will be when I have getopt working. */
364 int main (int, char **);
367 main (int argc, char **argv)
373 none = 0, delete, replace, print_table,
374 print_files, extract, move, quick_append
379 char *inarch_filename;
384 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
385 setlocale (LC_MESSAGES, "");
387 #if defined (HAVE_SETLOCALE)
388 setlocale (LC_CTYPE, "");
390 bindtextdomain (PACKAGE, LOCALEDIR);
391 textdomain (PACKAGE);
393 program_name = argv[0];
394 xmalloc_set_program_name (program_name);
396 expandargv (&argc, &argv);
402 temp = strrchr (program_name, '/');
403 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
405 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
406 char *bslash = strrchr (program_name, '\\');
408 if (temp == NULL || (bslash != NULL && bslash > temp))
410 if (temp == NULL && program_name[0] != '\0' && program_name[1] == ':')
411 temp = program_name + 1;
418 if (strlen (temp) >= 6
419 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
425 if (argc > 1 && argv[1][0] == '-')
427 if (strcmp (argv[1], "--help") == 0)
429 else if (strcmp (argv[1], "--version") == 0)
432 print_version ("ranlib");
434 print_version ("ar");
438 START_PROGRESS (program_name, 0);
441 set_default_bfd_target ();
445 xatexit (remove_output);
447 for (i = 1; i < argc; i++)
448 if (! ar_emul_parse_arg (argv[i]))
456 bfd_boolean touch = FALSE;
459 || strcmp (argv[1], "--help") == 0
460 || strcmp (argv[1], "-h") == 0
461 || strcmp (argv[1], "-H") == 0)
463 if (strcmp (argv[1], "-V") == 0
464 || strcmp (argv[1], "-v") == 0
465 || CONST_STRNEQ (argv[1], "--v"))
466 print_version ("ranlib");
468 if (strcmp (argv[1], "-t") == 0)
473 while (arg_index < argc)
476 status |= ranlib_only (argv[arg_index]);
478 status |= ranlib_touch (argv[arg_index]);
484 if (argc == 2 && strcmp (argv[1], "-M") == 0)
494 arg_ptr = argv[arg_index];
496 if (strcmp (arg_ptr, "--plugin") == 0)
498 #if BFD_SUPPORTS_PLUGINS
502 bfd_plugin_set_plugin (argv[2]);
505 arg_ptr = argv[arg_index];
507 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
514 /* When the first option starts with '-' we support POSIX-compatible
517 ++arg_ptr; /* compatibility */
522 while ((c = *arg_ptr++) != '\0')
533 if (operation != none)
534 fatal (_("two different operation options specified"));
539 operation_alters_arch = TRUE;
543 operation_alters_arch = TRUE;
546 operation = print_files;
549 operation = quick_append;
550 operation_alters_arch = TRUE;
554 operation_alters_arch = TRUE;
557 operation = print_table;
590 postype = pos_before;
593 postype = pos_before;
599 counted_name_mode = TRUE;
605 full_pathname = TRUE;
608 make_thin_archive = TRUE;
611 deterministic = TRUE;
614 /* xgettext:c-format */
615 non_fatal (_("illegal option -- %c"), c);
620 /* With POSIX-compatible option parsing continue with the next
621 argument if it starts with '-'. */
622 if (do_posix && arg_index + 1 < argc && argv[arg_index + 1][0] == '-')
623 arg_ptr = argv[++arg_index] + 1;
630 print_version ("ar");
633 if (arg_index >= argc)
644 /* We don't use do_quick_append any more. Too many systems
645 expect ar to always rebuild the symbol table even when q is
648 /* We can't write an armap when using ar q, so just do ar r
650 if (operation == quick_append && write_armap)
653 if ((operation == none || operation == print_table)
655 xexit (ranlib_only (argv[arg_index]));
657 if (operation == none)
658 fatal (_("no operation specified"));
660 if (newer_only && operation != replace)
661 fatal (_("`u' is only meaningful with the `r' option."));
663 if (newer_only && deterministic)
664 fatal (_("`u' is not meaningful with the `D' option."));
666 if (postype != pos_default)
667 posname = argv[arg_index++];
669 if (counted_name_mode)
671 if (operation != extract && operation != delete)
672 fatal (_("`N' is only meaningful with the `x' and `d' options."));
673 counted_name_counter = atoi (argv[arg_index++]);
674 if (counted_name_counter <= 0)
675 fatal (_("Value for `N' must be positive."));
678 inarch_filename = argv[arg_index++];
680 files = arg_index < argc ? argv + arg_index : NULL;
681 file_count = argc - arg_index;
683 arch = open_inarch (inarch_filename,
684 files == NULL ? (char *) NULL : files[0]);
686 if (operation == extract && bfd_is_thin_archive (arch))
687 fatal (_("`x' cannot be used on thin archives."));
692 map_over_members (arch, print_descr, files, file_count);
696 map_over_members (arch, print_contents, files, file_count);
700 map_over_members (arch, extract_file, files, file_count);
705 delete_members (arch, files);
707 output_filename = NULL;
712 move_members (arch, files);
714 output_filename = NULL;
719 if (files != NULL || write_armap > 0)
720 replace_members (arch, files, operation == quick_append);
722 output_filename = NULL;
725 /* Shouldn't happen! */
727 /* xgettext:c-format */
728 fatal (_("internal error -- this option not implemented"));
732 END_PROGRESS (program_name);
739 open_inarch (const char *archive_filename, const char *file)
748 bfd_set_error (bfd_error_no_error);
752 if (stat (archive_filename, &sbuf) != 0)
754 #if !defined(__GO32__) || defined(__DJGPP__)
756 /* FIXME: I don't understand why this fragment was ifndef'ed
757 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
758 stat() works just fine in v2.x, so I think this should be
759 removed. For now, I enable it for DJGPP v2. -- EZ. */
761 /* KLUDGE ALERT! Temporary fix until I figger why
762 stat() is wrong ... think it's buried in GO32's IDT - Jax */
764 bfd_fatal (archive_filename);
767 if (!operation_alters_arch)
769 fprintf (stderr, "%s: ", program_name);
770 perror (archive_filename);
775 /* Try to figure out the target to use for the archive from the
776 first object on the list. */
781 obj = bfd_openr (file, NULL);
784 if (bfd_check_format (obj, bfd_object))
785 target = bfd_get_target (obj);
786 (void) bfd_close (obj);
790 /* Create an empty archive. */
791 arch = bfd_openw (archive_filename, target);
793 || ! bfd_set_format (arch, bfd_archive)
794 || ! bfd_close (arch))
795 bfd_fatal (archive_filename);
796 else if (!silent_create)
797 non_fatal (_("creating %s"), archive_filename);
799 /* If we die creating a new archive, don't leave it around. */
800 output_filename = archive_filename;
803 arch = bfd_openr (archive_filename, target);
807 bfd_fatal (archive_filename);
810 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
812 bfd_nonfatal (archive_filename);
813 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
815 list_matching_formats (matching);
821 last_one = &(arch->archive_next);
822 /* Read all the contents right away, regardless. */
823 for (next_one = bfd_openr_next_archived_file (arch, NULL);
825 next_one = bfd_openr_next_archived_file (arch, next_one))
828 *last_one = next_one;
829 last_one = &next_one->archive_next;
831 *last_one = (bfd *) NULL;
832 if (bfd_get_error () != bfd_error_no_more_archived_files)
838 print_contents (bfd *abfd)
841 char *cbuf = xmalloc (BUFSIZE);
844 if (bfd_stat_arch_elt (abfd, &buf) != 0)
845 /* xgettext:c-format */
846 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
849 /* xgettext:c-format */
850 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd));
852 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
855 while (ncopied < size)
859 size_t tocopy = size - ncopied;
860 if (tocopy > BUFSIZE)
863 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
865 /* xgettext:c-format */
866 fatal (_("%s is not a valid archive"),
867 bfd_get_filename (bfd_my_archive (abfd)));
869 /* fwrite in mingw32 may return int instead of size_t. Cast the
870 return value to size_t to avoid comparison between signed and
872 if ((size_t) fwrite (cbuf, 1, nread, stdout) != nread)
873 fatal ("stdout: %s", strerror (errno));
879 /* Extract a member of the archive into its own file.
881 We defer opening the new file until after we have read a BUFSIZ chunk of the
882 old one, since we know we have just read the archive header for the old
883 one. Since most members are shorter than BUFSIZ, this means we will read
884 the old header, read the old data, write a new inode for the new file, and
885 write the new data, and be done. This 'optimization' is what comes from
886 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
890 extract_file (bfd *abfd)
893 char *cbuf = xmalloc (BUFSIZE);
894 size_t nread, tocopy;
899 if (bfd_stat_arch_elt (abfd, &buf) != 0)
900 /* xgettext:c-format */
901 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
905 printf ("x - %s\n", bfd_get_filename (abfd));
907 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
912 /* Seems like an abstraction violation, eh? Well it's OK! */
913 output_filename = bfd_get_filename (abfd);
915 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
918 perror (bfd_get_filename (abfd));
922 output_file = ostream;
925 while (ncopied < size)
927 tocopy = size - ncopied;
928 if (tocopy > BUFSIZE)
931 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
933 /* xgettext:c-format */
934 fatal (_("%s is not a valid archive"),
935 bfd_get_filename (bfd_my_archive (abfd)));
937 /* See comment above; this saves disk arm motion */
940 /* Seems like an abstraction violation, eh? Well it's OK! */
941 output_filename = bfd_get_filename (abfd);
943 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
946 perror (bfd_get_filename (abfd));
950 output_file = ostream;
953 /* fwrite in mingw32 may return int instead of size_t. Cast
954 the return value to size_t to avoid comparison between
955 signed and unsigned values. */
956 if ((size_t) fwrite (cbuf, 1, nread, ostream) != nread)
957 fatal ("%s: %s", output_filename, strerror (errno));
965 output_filename = NULL;
967 chmod (bfd_get_filename (abfd), buf.st_mode);
971 /* Set access time to modification time. Only st_mtime is
972 initialized by bfd_stat_arch_elt. */
973 buf.st_atime = buf.st_mtime;
974 set_times (bfd_get_filename (abfd), &buf);
981 write_archive (bfd *iarch)
984 char *old_name, *new_name;
985 bfd *contents_head = iarch->archive_next;
987 old_name = xmalloc (strlen (bfd_get_filename (iarch)) + 1);
988 strcpy (old_name, bfd_get_filename (iarch));
989 new_name = make_tempname (old_name);
991 if (new_name == NULL)
992 bfd_fatal ("could not create temporary file whilst writing archive");
994 output_filename = new_name;
996 obfd = bfd_openw (new_name, bfd_get_target (iarch));
999 bfd_fatal (old_name);
1003 bfd_set_format (obfd, bfd_archive);
1005 /* Request writing the archive symbol table unless we've
1006 been explicitly requested not to. */
1007 obfd->has_armap = write_armap >= 0;
1011 /* This should really use bfd_set_file_flags, but that rejects
1013 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1017 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
1019 if (make_thin_archive || bfd_is_thin_archive (iarch))
1020 bfd_is_thin_archive (obfd) = 1;
1022 if (!bfd_set_archive_head (obfd, contents_head))
1023 bfd_fatal (old_name);
1025 if (!bfd_close (obfd))
1026 bfd_fatal (old_name);
1029 output_filename = NULL;
1031 /* We don't care if this fails; we might be creating the archive. */
1034 if (smart_rename (new_name, old_name, 0) != 0)
1038 /* Return a pointer to the pointer to the entry which should be rplacd'd
1039 into when altering. DEFAULT_POS should be how to interpret pos_default,
1040 and should be a pos value. */
1043 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
1045 bfd **after_bfd = contents;
1047 const char *realposname;
1049 if (postype == pos_default)
1051 realpos = default_pos;
1052 realposname = default_posname;
1057 realposname = posname;
1060 if (realpos == pos_end)
1063 after_bfd = &((*after_bfd)->archive_next);
1067 for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
1068 if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1070 if (realpos == pos_after)
1071 after_bfd = &(*after_bfd)->archive_next;
1079 delete_members (bfd *arch, char **files_to_delete)
1081 bfd **current_ptr_ptr;
1083 bfd_boolean something_changed = FALSE;
1086 for (; *files_to_delete != NULL; ++files_to_delete)
1088 /* In a.out systems, the armap is optional. It's also called
1089 __.SYMDEF. So if the user asked to delete it, we should remember
1090 that fact. This isn't quite right for COFF systems (where
1091 __.SYMDEF might be regular member), but it's very unlikely
1092 to be a problem. FIXME */
1094 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1096 arch->has_armap = FALSE;
1103 current_ptr_ptr = &(arch->archive_next);
1104 while (*current_ptr_ptr)
1106 if (FILENAME_CMP (normalize (*files_to_delete, arch),
1107 (*current_ptr_ptr)->filename) == 0)
1110 if (counted_name_mode
1111 && match_count != counted_name_counter)
1113 /* Counting, and didn't match on count; go on to the
1119 something_changed = TRUE;
1123 *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
1128 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1131 if (verbose && !found)
1133 /* xgettext:c-format */
1134 printf (_("No member named `%s'\n"), *files_to_delete);
1140 if (something_changed)
1141 write_archive (arch);
1143 output_filename = NULL;
1147 /* Reposition existing members within an archive */
1150 move_members (bfd *arch, char **files_to_move)
1152 bfd **after_bfd; /* New entries go after this one */
1153 bfd **current_ptr_ptr; /* cdr pointer into contents */
1155 for (; *files_to_move; ++files_to_move)
1157 current_ptr_ptr = &(arch->archive_next);
1158 while (*current_ptr_ptr)
1160 bfd *current_ptr = *current_ptr_ptr;
1161 if (FILENAME_CMP (normalize (*files_to_move, arch),
1162 current_ptr->filename) == 0)
1164 /* Move this file to the end of the list - first cut from
1167 *current_ptr_ptr = current_ptr->archive_next;
1169 /* Now glue to end */
1170 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1172 *after_bfd = current_ptr;
1173 current_ptr->archive_next = link;
1176 printf ("m - %s\n", *files_to_move);
1181 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1183 /* xgettext:c-format */
1184 fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1189 write_archive (arch);
1192 /* Ought to default to replacing in place, but this is existing practice! */
1195 replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
1197 bfd_boolean changed = FALSE;
1198 bfd **after_bfd; /* New entries go after this one. */
1202 while (files_to_move && *files_to_move)
1206 current_ptr = &arch->archive_next;
1207 while (*current_ptr)
1209 current = *current_ptr;
1211 /* For compatibility with existing ar programs, we
1212 permit the same file to be added multiple times. */
1213 if (FILENAME_CMP (normalize (*files_to_move, arch),
1214 normalize (current->filename, arch)) == 0
1215 && current->arelt_data != NULL)
1219 struct stat fsbuf, asbuf;
1221 if (stat (*files_to_move, &fsbuf) != 0)
1223 if (errno != ENOENT)
1224 bfd_fatal (*files_to_move);
1227 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1228 /* xgettext:c-format */
1229 fatal (_("internal stat error on %s"),
1232 if (fsbuf.st_mtime <= asbuf.st_mtime)
1236 after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
1238 if (ar_emul_replace (after_bfd, *files_to_move,
1241 /* Snip out this entry from the chain. */
1242 *current_ptr = (*current_ptr)->archive_next;
1248 current_ptr = &(current->archive_next);
1252 /* Add to the end of the archive. */
1253 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1255 if (ar_emul_append (after_bfd, *files_to_move, verbose,
1265 write_archive (arch);
1267 output_filename = NULL;
1271 ranlib_only (const char *archname)
1275 if (get_file_size (archname) < 1)
1278 arch = open_inarch (archname, (char *) NULL);
1281 write_archive (arch);
1285 /* Update the timestamp of the symbol map of an archive. */
1288 ranlib_touch (const char *archname)
1291 /* I don't think updating works on go32. */
1292 ranlib_only (archname);
1298 if (get_file_size (archname) < 1)
1300 f = open (archname, O_RDWR | O_BINARY, 0);
1303 bfd_set_error (bfd_error_system_call);
1304 bfd_fatal (archname);
1307 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1309 bfd_fatal (archname);
1310 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1312 bfd_nonfatal (archname);
1313 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1315 list_matching_formats (matching);
1321 if (! bfd_has_map (arch))
1322 /* xgettext:c-format */
1323 fatal (_("%s: no archive map to update"), archname);
1325 bfd_update_armap_timestamp (arch);
1327 if (! bfd_close (arch))
1328 bfd_fatal (archname);
1333 /* Things which are interesting to map over all or some of the files: */
1336 print_descr (bfd *abfd)
1338 print_arelt_descr (stdout, abfd, verbose);