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: GNU ar used to check file against filesystem in quick_update and
25 replace operations (would check mtime). Doesn't warn when name truncated.
26 No way to specify pos_end. Error messages should be more consistent. */
30 #include "libiberty.h"
37 #include "filenames.h"
43 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
45 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
48 /* Static declarations. */
50 static void mri_emul (void);
51 static const char *normalize (const char *, bfd *);
52 static void remove_output (void);
53 static void map_over_members (bfd *, void (*)(bfd *), char **, int);
54 static void print_contents (bfd * member);
55 static void delete_members (bfd *, char **files_to_delete);
57 static void move_members (bfd *, char **files_to_move);
58 static void replace_members
59 (bfd *, char **files_to_replace, bfd_boolean quick);
60 static void print_descr (bfd * abfd);
61 static void write_archive (bfd *);
62 static int ranlib_only (const char *archname);
63 static int ranlib_touch (const char *archname);
64 static void usage (int);
66 /** Globals and flags. */
70 /* This flag distinguishes between ar and ranlib:
71 1 means this is 'ranlib'; 0 means this is 'ar'.
72 -1 means if we should use argv[0] to decide. */
75 /* Nonzero means don't warn about creating the archive file if necessary. */
76 int silent_create = 0;
78 /* Nonzero means describe each action performed. */
81 /* Nonzero means preserve dates of members when extracting them. */
82 int preserve_dates = 0;
84 /* Nonzero means don't replace existing members whose dates are more recent
85 than the corresponding files. */
88 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
89 member). -1 means we've been explicitly asked to not write a symbol table;
90 +1 means we've been explicitly asked to write it;
92 Traditionally, the default in BSD has been to not write the table.
93 However, for POSIX.2 compliance the default is now to write a symbol table
94 if any of the members are object files. */
97 /* Operate in deterministic mode: write zero for timestamps, uids,
98 and gids for archive members and the archive symbol table, and write
99 consistent file modes. */
100 int deterministic = 0;
102 /* Nonzero means it's the name of an existing member; position new or moved
103 files with respect to this one. */
104 char *posname = NULL;
106 /* Sez how to use `posname': pos_before means position before that member.
107 pos_after means position after that member. pos_end means always at end.
108 pos_default means default appropriately. For the latter two, `posname'
109 should also be zero. */
112 pos_default, pos_before, pos_after, pos_end
113 } postype = pos_default;
117 none = 0, del, replace, print_table,
118 print_files, extract, move, quick_append
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;
140 static int show_version = 0;
142 static int show_help = 0;
144 static const char *plugin_target = NULL;
146 #define OPTION_PLUGIN 201
148 static struct option long_options[] =
150 {"help", no_argument, &show_help, 1},
151 {"plugin", required_argument, NULL, OPTION_PLUGIN},
152 {"version", no_argument, &show_version, 1},
153 {NULL, no_argument, NULL, 0}
161 interactive = isatty (fileno (stdin));
165 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
166 COUNT is the length of the FILES chain; FUNCTION is called on each entry
167 whose name matches one in FILES. */
170 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
177 for (head = arch->archive_next; head; head = head->archive_next)
185 /* This may appear to be a baroque way of accomplishing what we want.
186 However we have to iterate over the filenames in order to notice where
187 a filename is requested but does not exist in the archive. Ditto
188 mapping over each file each time -- we want to hack multiple
191 for (; count > 0; files++, count--)
193 bfd_boolean found = FALSE;
196 for (head = arch->archive_next; head; head = head->archive_next)
198 const char * filename;
201 filename = head->filename;
202 if (filename == NULL)
204 /* Some archive formats don't get the filenames filled in
205 until the elements are opened. */
207 bfd_stat_arch_elt (head, &buf);
209 else if (bfd_is_thin_archive (arch))
211 /* Thin archives store full pathnames. Need to normalize. */
212 filename = normalize (filename, arch);
215 if ((filename != NULL) &&
216 (!FILENAME_CMP (normalize (*files, arch), filename)))
219 if (counted_name_mode
220 && match_count != counted_name_counter)
222 /* Counting, and didn't match on count; go on to the
233 /* xgettext:c-format */
234 fprintf (stderr, _("no entry %s in archive\n"), *files);
238 bfd_boolean operation_alters_arch = FALSE;
245 s = help ? stdout : stderr;
247 /* xgettext:c-format */
248 const char * command_line =
249 #if BFD_SUPPORTS_PLUGINS
250 _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [--plugin <name>] [member-name] [count] archive-file file...\n");
252 _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
254 fprintf (s, command_line, program_name);
256 /* xgettext:c-format */
257 fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
258 fprintf (s, _(" commands:\n"));
259 fprintf (s, _(" d - delete file(s) from the archive\n"));
260 fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
261 fprintf (s, _(" p - print file(s) found in the archive\n"));
262 fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
263 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
264 fprintf (s, _(" s - act as ranlib\n"));
265 fprintf (s, _(" t - display contents of archive\n"));
266 fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
267 fprintf (s, _(" command specific modifiers:\n"));
268 fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
269 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
270 fprintf (s, _(" [D] - use zero for timestamps and uids/gids\n"));
271 fprintf (s, _(" [N] - use instance [count] of name\n"));
272 fprintf (s, _(" [f] - truncate inserted file names\n"));
273 fprintf (s, _(" [P] - use full path names when matching\n"));
274 fprintf (s, _(" [o] - preserve original dates\n"));
275 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
276 fprintf (s, _(" generic modifiers:\n"));
277 fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
278 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
279 fprintf (s, _(" [S] - do not build a symbol table\n"));
280 fprintf (s, _(" [T] - make a thin archive\n"));
281 fprintf (s, _(" [v] - be verbose\n"));
282 fprintf (s, _(" [V] - display the version number\n"));
283 fprintf (s, _(" @<file> - read options from <file>\n"));
284 #if BFD_SUPPORTS_PLUGINS
285 fprintf (s, _(" optional:\n"));
286 fprintf (s, _(" --plugin <p> - load the specified plugin\n"));
291 list_supported_targets (program_name, s);
293 if (REPORT_BUGS_TO[0] && help)
294 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
296 xexit (help ? 0 : 1);
300 ranlib_usage(int help)
304 s = help ? stdout : stderr;
306 /* xgettext:c-format */
307 fprintf (s, _("Usage: %s [options] archive\n"), program_name);
308 fprintf (s, _(" Generate an index to speed access to archives\n"));
309 fprintf (s, _(" The options are:\n\
310 @<file> Read options from <file>\n"));
311 #if BFD_SUPPORTS_PLUGINS
313 --plugin <name> Load the specified plugin\n"));
316 -t Update the archive's symbol map timestamp\n\
317 -h --help Print this help message\n\
318 -v --version Print version information\n"));
320 list_supported_targets (program_name, s);
322 if (REPORT_BUGS_TO[0] && help)
323 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
325 xexit (help ? 0 : 1);
328 /* Normalize a file name specified on the command line into a file
329 name which we will use in an archive. */
332 normalize (const char *file, bfd *abfd)
334 const char *filename;
339 filename = lbasename (file);
343 && strlen (filename) > abfd->xvec->ar_max_namelen)
348 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
349 memcpy (s, filename, abfd->xvec->ar_max_namelen);
350 s[abfd->xvec->ar_max_namelen] = '\0';
357 /* Remove any output file. This is only called via xatexit. */
359 static const char *output_filename = NULL;
360 static FILE *output_file = NULL;
361 static bfd *output_bfd = NULL;
366 if (output_filename != NULL)
368 if (output_bfd != NULL)
369 bfd_cache_close (output_bfd);
370 if (output_file != NULL)
371 fclose (output_file);
372 unlink_if_ordinary (output_filename);
377 decode_options(int argc, char **argv)
381 /* Convert old-style tar call by exploding option element and rearranging
382 options accordingly. */
384 if (argc > 1 && argv[1][0] != '-')
386 int new_argc; /* argc value for rearranged arguments */
387 char **new_argv; /* argv value for rearranged arguments */
388 char *const *in; /* cursor into original argv */
389 char **out; /* cursor into rearranged argv */
390 const char *letter; /* cursor into old option letters */
391 char buffer[3]; /* constructed option buffer */
393 /* Initialize a constructed option. */
398 /* Allocate a new argument array, and copy program name in it. */
400 new_argc = argc - 1 + strlen (argv[1]);
401 new_argv = xmalloc ((new_argc + 1) * sizeof (*argv));
406 /* Copy each old letter option as a separate option. */
408 for (letter = *in++; *letter; letter++)
411 *out++ = xstrdup (buffer);
414 /* Copy all remaining options. */
416 while (in < argv + argc)
420 /* Replace the old option list by the new one. */
426 while ((c = getopt_long (argc, argv, "hdmpqrstxabcfilNoPsSuvV",
427 long_options, NULL)) != EOF)
438 if (operation != none)
439 fatal (_("two different operation options specified"));
450 operation_alters_arch = TRUE;
454 operation_alters_arch = TRUE;
457 operation = print_files;
460 operation = quick_append;
461 operation_alters_arch = TRUE;
465 operation_alters_arch = TRUE;
468 operation = print_table;
500 postype = pos_before;
503 postype = pos_before;
509 counted_name_mode = TRUE;
515 full_pathname = TRUE;
518 make_thin_archive = TRUE;
521 deterministic = TRUE;
524 #if BFD_SUPPORTS_PLUGINS
525 plugin_target = "plugin";
526 bfd_plugin_set_plugin (optarg);
528 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
532 case 0: /* A long option that just sets a flag. */
535 /* xgettext:c-format */
536 non_fatal (_("illegal option -- '%d'"), c);
541 return &argv[optind];
545 ranlib_main(int argc, char **argv)
547 int arg_index, status = 0;
548 bfd_boolean touch = FALSE;
551 while ((c = getopt_long (argc, argv, "hHvVt", long_options, NULL)) != EOF)
576 print_version ("ranlib");
580 while (arg_index < argc)
583 status |= ranlib_only (argv[arg_index]);
585 status |= ranlib_touch (argv[arg_index]);
592 /* The option parsing should be in its own function.
593 It will be when I have getopt working. */
595 int main (int, char **);
598 main (int argc, char **argv)
603 char *inarch_filename;
606 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
607 setlocale (LC_MESSAGES, "");
609 #if defined (HAVE_SETLOCALE)
610 setlocale (LC_CTYPE, "");
612 bindtextdomain (PACKAGE, LOCALEDIR);
613 textdomain (PACKAGE);
615 program_name = argv[0];
616 xmalloc_set_program_name (program_name);
617 #if BFD_SUPPORTS_PLUGINS
618 bfd_plugin_set_program_name (program_name);
621 expandargv (&argc, &argv);
625 const char *temp = lbasename (program_name);
627 if (strlen (temp) >= 6
628 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
634 START_PROGRESS (program_name, 0);
637 set_default_bfd_target ();
639 xatexit (remove_output);
641 for (i = 1; i < argc; i++)
642 if (! ar_emul_parse_arg (argv[i]))
648 ranlib_main(argc, argv);
650 if (argc == 2 && strcmp (argv[1], "-M") == 0)
659 argv = decode_options(argc, argv);
665 print_version ("ar");
677 /* We don't use do_quick_append any more. Too many systems
678 expect ar to always rebuild the symbol table even when q is
681 /* We can't write an armap when using ar q, so just do ar r
683 if (operation == quick_append && write_armap)
686 if ((operation == none || operation == print_table)
688 xexit (ranlib_only (argv[arg_index]));
690 if (operation == none)
691 fatal (_("no operation specified"));
693 if (newer_only && operation != replace)
694 fatal (_("`u' is only meaningful with the `r' option."));
696 if (newer_only && deterministic)
697 fatal (_("`u' is not meaningful with the `D' option."));
699 if (postype != pos_default)
700 posname = argv[arg_index++];
702 if (counted_name_mode)
704 if (operation != extract && operation != del)
705 fatal (_("`N' is only meaningful with the `x' and `d' options."));
706 counted_name_counter = atoi (argv[arg_index++]);
707 if (counted_name_counter <= 0)
708 fatal (_("Value for `N' must be positive."));
711 inarch_filename = argv[arg_index++];
713 for (file_count = 0; argv[arg_index + file_count] != NULL; file_count++)
716 files = (file_count > 0) ? argv + arg_index : NULL;
718 arch = open_inarch (inarch_filename,
719 files == NULL ? (char *) NULL : files[0]);
721 if (operation == extract && bfd_is_thin_archive (arch))
722 fatal (_("`x' cannot be used on thin archives."));
727 map_over_members (arch, print_descr, files, file_count);
731 map_over_members (arch, print_contents, files, file_count);
735 map_over_members (arch, extract_file, files, file_count);
740 delete_members (arch, files);
742 output_filename = NULL;
747 move_members (arch, files);
749 output_filename = NULL;
754 if (files != NULL || write_armap > 0)
755 replace_members (arch, files, operation == quick_append);
757 output_filename = NULL;
760 /* Shouldn't happen! */
762 /* xgettext:c-format */
763 fatal (_("internal error -- this option not implemented"));
767 END_PROGRESS (program_name);
774 open_inarch (const char *archive_filename, const char *file)
783 bfd_set_error (bfd_error_no_error);
785 target = plugin_target;
787 if (stat (archive_filename, &sbuf) != 0)
789 #if !defined(__GO32__) || defined(__DJGPP__)
791 /* FIXME: I don't understand why this fragment was ifndef'ed
792 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
793 stat() works just fine in v2.x, so I think this should be
794 removed. For now, I enable it for DJGPP v2. -- EZ. */
796 /* KLUDGE ALERT! Temporary fix until I figger why
797 stat() is wrong ... think it's buried in GO32's IDT - Jax */
799 bfd_fatal (archive_filename);
802 if (!operation_alters_arch)
804 fprintf (stderr, "%s: ", program_name);
805 perror (archive_filename);
810 /* Try to figure out the target to use for the archive from the
811 first object on the list. */
816 obj = bfd_openr (file, target);
819 if (bfd_check_format (obj, bfd_object))
820 target = bfd_get_target (obj);
821 (void) bfd_close (obj);
825 /* Create an empty archive. */
826 arch = bfd_openw (archive_filename, target);
828 || ! bfd_set_format (arch, bfd_archive)
829 || ! bfd_close (arch))
830 bfd_fatal (archive_filename);
831 else if (!silent_create)
832 non_fatal (_("creating %s"), archive_filename);
834 /* If we die creating a new archive, don't leave it around. */
835 output_filename = archive_filename;
838 arch = bfd_openr (archive_filename, target);
842 bfd_fatal (archive_filename);
845 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
847 bfd_nonfatal (archive_filename);
848 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
850 list_matching_formats (matching);
856 last_one = &(arch->archive_next);
857 /* Read all the contents right away, regardless. */
858 for (next_one = bfd_openr_next_archived_file (arch, NULL);
860 next_one = bfd_openr_next_archived_file (arch, next_one))
863 *last_one = next_one;
864 last_one = &next_one->archive_next;
866 *last_one = (bfd *) NULL;
867 if (bfd_get_error () != bfd_error_no_more_archived_files)
873 print_contents (bfd *abfd)
876 char *cbuf = (char *) xmalloc (BUFSIZE);
879 if (bfd_stat_arch_elt (abfd, &buf) != 0)
880 /* xgettext:c-format */
881 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
884 printf ("\n<%s>\n\n", bfd_get_filename (abfd));
886 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
889 while (ncopied < size)
893 size_t tocopy = size - ncopied;
894 if (tocopy > BUFSIZE)
897 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
899 /* xgettext:c-format */
900 fatal (_("%s is not a valid archive"),
901 bfd_get_filename (bfd_my_archive (abfd)));
903 /* fwrite in mingw32 may return int instead of size_t. Cast the
904 return value to size_t to avoid comparison between signed and
906 if ((size_t) fwrite (cbuf, 1, nread, stdout) != nread)
907 fatal ("stdout: %s", strerror (errno));
913 /* Extract a member of the archive into its own file.
915 We defer opening the new file until after we have read a BUFSIZ chunk of the
916 old one, since we know we have just read the archive header for the old
917 one. Since most members are shorter than BUFSIZ, this means we will read
918 the old header, read the old data, write a new inode for the new file, and
919 write the new data, and be done. This 'optimization' is what comes from
920 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
924 extract_file (bfd *abfd)
927 char *cbuf = (char *) xmalloc (BUFSIZE);
928 size_t nread, tocopy;
933 if (bfd_stat_arch_elt (abfd, &buf) != 0)
934 /* xgettext:c-format */
935 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
939 printf ("x - %s\n", bfd_get_filename (abfd));
941 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
946 /* Seems like an abstraction violation, eh? Well it's OK! */
947 output_filename = bfd_get_filename (abfd);
949 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
952 perror (bfd_get_filename (abfd));
956 output_file = ostream;
959 while (ncopied < size)
961 tocopy = size - ncopied;
962 if (tocopy > BUFSIZE)
965 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
967 /* xgettext:c-format */
968 fatal (_("%s is not a valid archive"),
969 bfd_get_filename (bfd_my_archive (abfd)));
971 /* See comment above; this saves disk arm motion */
974 /* Seems like an abstraction violation, eh? Well it's OK! */
975 output_filename = bfd_get_filename (abfd);
977 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
980 perror (bfd_get_filename (abfd));
984 output_file = ostream;
987 /* fwrite in mingw32 may return int instead of size_t. Cast
988 the return value to size_t to avoid comparison between
989 signed and unsigned values. */
990 if ((size_t) fwrite (cbuf, 1, nread, ostream) != nread)
991 fatal ("%s: %s", output_filename, strerror (errno));
999 output_filename = NULL;
1001 chmod (bfd_get_filename (abfd), buf.st_mode);
1005 /* Set access time to modification time. Only st_mtime is
1006 initialized by bfd_stat_arch_elt. */
1007 buf.st_atime = buf.st_mtime;
1008 set_times (bfd_get_filename (abfd), &buf);
1015 write_archive (bfd *iarch)
1018 char *old_name, *new_name;
1019 bfd *contents_head = iarch->archive_next;
1021 old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
1022 strcpy (old_name, bfd_get_filename (iarch));
1023 new_name = make_tempname (old_name);
1025 if (new_name == NULL)
1026 bfd_fatal ("could not create temporary file whilst writing archive");
1028 output_filename = new_name;
1030 obfd = bfd_openw (new_name, bfd_get_target (iarch));
1033 bfd_fatal (old_name);
1037 bfd_set_format (obfd, bfd_archive);
1039 /* Request writing the archive symbol table unless we've
1040 been explicitly requested not to. */
1041 obfd->has_armap = write_armap >= 0;
1045 /* This should really use bfd_set_file_flags, but that rejects
1047 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1051 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
1053 if (make_thin_archive || bfd_is_thin_archive (iarch))
1054 bfd_is_thin_archive (obfd) = 1;
1056 if (!bfd_set_archive_head (obfd, contents_head))
1057 bfd_fatal (old_name);
1059 if (!bfd_close (obfd))
1060 bfd_fatal (old_name);
1063 output_filename = NULL;
1065 /* We don't care if this fails; we might be creating the archive. */
1068 if (smart_rename (new_name, old_name, 0) != 0)
1072 /* Return a pointer to the pointer to the entry which should be rplacd'd
1073 into when altering. DEFAULT_POS should be how to interpret pos_default,
1074 and should be a pos value. */
1077 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
1079 bfd **after_bfd = contents;
1081 const char *realposname;
1083 if (postype == pos_default)
1085 realpos = default_pos;
1086 realposname = default_posname;
1091 realposname = posname;
1094 if (realpos == pos_end)
1097 after_bfd = &((*after_bfd)->archive_next);
1101 for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
1102 if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1104 if (realpos == pos_after)
1105 after_bfd = &(*after_bfd)->archive_next;
1113 delete_members (bfd *arch, char **files_to_delete)
1115 bfd **current_ptr_ptr;
1117 bfd_boolean something_changed = FALSE;
1120 for (; *files_to_delete != NULL; ++files_to_delete)
1122 /* In a.out systems, the armap is optional. It's also called
1123 __.SYMDEF. So if the user asked to delete it, we should remember
1124 that fact. This isn't quite right for COFF systems (where
1125 __.SYMDEF might be regular member), but it's very unlikely
1126 to be a problem. FIXME */
1128 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1130 arch->has_armap = FALSE;
1137 current_ptr_ptr = &(arch->archive_next);
1138 while (*current_ptr_ptr)
1140 if (FILENAME_CMP (normalize (*files_to_delete, arch),
1141 (*current_ptr_ptr)->filename) == 0)
1144 if (counted_name_mode
1145 && match_count != counted_name_counter)
1147 /* Counting, and didn't match on count; go on to the
1153 something_changed = TRUE;
1157 *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
1162 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1165 if (verbose && !found)
1167 /* xgettext:c-format */
1168 printf (_("No member named `%s'\n"), *files_to_delete);
1174 if (something_changed)
1175 write_archive (arch);
1177 output_filename = NULL;
1181 /* Reposition existing members within an archive */
1184 move_members (bfd *arch, char **files_to_move)
1186 bfd **after_bfd; /* New entries go after this one */
1187 bfd **current_ptr_ptr; /* cdr pointer into contents */
1189 for (; *files_to_move; ++files_to_move)
1191 current_ptr_ptr = &(arch->archive_next);
1192 while (*current_ptr_ptr)
1194 bfd *current_ptr = *current_ptr_ptr;
1195 if (FILENAME_CMP (normalize (*files_to_move, arch),
1196 current_ptr->filename) == 0)
1198 /* Move this file to the end of the list - first cut from
1201 *current_ptr_ptr = current_ptr->archive_next;
1203 /* Now glue to end */
1204 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1205 link_bfd = *after_bfd;
1206 *after_bfd = current_ptr;
1207 current_ptr->archive_next = link_bfd;
1210 printf ("m - %s\n", *files_to_move);
1215 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1217 /* xgettext:c-format */
1218 fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1223 write_archive (arch);
1226 /* Ought to default to replacing in place, but this is existing practice! */
1229 replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
1231 bfd_boolean changed = FALSE;
1232 bfd **after_bfd; /* New entries go after this one. */
1236 while (files_to_move && *files_to_move)
1240 current_ptr = &arch->archive_next;
1241 while (*current_ptr)
1243 current = *current_ptr;
1245 /* For compatibility with existing ar programs, we
1246 permit the same file to be added multiple times. */
1247 if (FILENAME_CMP (normalize (*files_to_move, arch),
1248 normalize (current->filename, arch)) == 0
1249 && current->arelt_data != NULL)
1253 struct stat fsbuf, asbuf;
1255 if (stat (*files_to_move, &fsbuf) != 0)
1257 if (errno != ENOENT)
1258 bfd_fatal (*files_to_move);
1261 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1262 /* xgettext:c-format */
1263 fatal (_("internal stat error on %s"),
1266 if (fsbuf.st_mtime <= asbuf.st_mtime)
1270 after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
1272 if (ar_emul_replace (after_bfd, *files_to_move,
1273 plugin_target, verbose))
1275 /* Snip out this entry from the chain. */
1276 *current_ptr = (*current_ptr)->archive_next;
1282 current_ptr = &(current->archive_next);
1286 /* Add to the end of the archive. */
1287 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1289 if (ar_emul_append (after_bfd, *files_to_move, plugin_target,
1290 verbose, make_thin_archive))
1299 write_archive (arch);
1301 output_filename = NULL;
1305 ranlib_only (const char *archname)
1309 if (get_file_size (archname) < 1)
1312 arch = open_inarch (archname, (char *) NULL);
1315 write_archive (arch);
1319 /* Update the timestamp of the symbol map of an archive. */
1322 ranlib_touch (const char *archname)
1325 /* I don't think updating works on go32. */
1326 ranlib_only (archname);
1332 if (get_file_size (archname) < 1)
1334 f = open (archname, O_RDWR | O_BINARY, 0);
1337 bfd_set_error (bfd_error_system_call);
1338 bfd_fatal (archname);
1341 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1343 bfd_fatal (archname);
1344 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1346 bfd_nonfatal (archname);
1347 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1349 list_matching_formats (matching);
1355 if (! bfd_has_map (arch))
1356 /* xgettext:c-format */
1357 fatal (_("%s: no archive map to update"), archname);
1359 bfd_update_armap_timestamp (arch);
1361 if (! bfd_close (arch))
1362 bfd_fatal (archname);
1367 /* Things which are interesting to map over all or some of the files: */
1370 print_descr (bfd *abfd)
1372 print_arelt_descr (stdout, abfd, verbose);