1 /* ar.c - Archive modify and extract.
2 Copyright 1991-2013 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
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.
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.
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. */
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. */
28 #include "libiberty.h"
35 #include "filenames.h"
40 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
42 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
45 /* Static declarations. */
47 static void mri_emul (void);
48 static const char *normalize (const char *, bfd *);
49 static void remove_output (void);
50 static void map_over_members (bfd *, void (*)(bfd *), char **, int);
51 static void print_contents (bfd * member);
52 static void delete_members (bfd *, char **files_to_delete);
54 static void move_members (bfd *, char **files_to_move);
55 static void replace_members
56 (bfd *, char **files_to_replace, bfd_boolean quick);
57 static void print_descr (bfd * abfd);
58 static void write_archive (bfd *);
59 static int ranlib_only (const char *archname);
60 static int ranlib_touch (const char *archname);
61 static void usage (int);
63 /** Globals and flags. */
67 /* This flag distinguishes between ar and ranlib:
68 1 means this is 'ranlib'; 0 means this is 'ar'.
69 -1 means if we should use argv[0] to decide. */
72 /* Nonzero means don't warn about creating the archive file if necessary. */
73 int silent_create = 0;
75 /* Nonzero means describe each action performed. */
78 /* Nonzero means preserve dates of members when extracting them. */
79 int preserve_dates = 0;
81 /* Nonzero means don't replace existing members whose dates are more recent
82 than the corresponding files. */
85 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
86 member). -1 means we've been explicitly asked to not write a symbol table;
87 +1 means we've been explicitly asked to write it;
89 Traditionally, the default in BSD has been to not write the table.
90 However, for POSIX.2 compliance the default is now to write a symbol table
91 if any of the members are object files. */
94 /* Operate in deterministic mode: write zero for timestamps, uids,
95 and gids for archive members and the archive symbol table, and write
96 consistent file modes. */
97 int deterministic = -1; /* Determinism indeterminate. */
99 /* Nonzero means it's the name of an existing member; position new or moved
100 files with respect to this one. */
101 char *posname = NULL;
103 /* Sez how to use `posname': pos_before means position before that member.
104 pos_after means position after that member. pos_end means always at end.
105 pos_default means default appropriately. For the latter two, `posname'
106 should also be zero. */
109 pos_default, pos_before, pos_after, pos_end
110 } postype = pos_default;
114 none = 0, del, replace, print_table,
115 print_files, extract, move, quick_append
119 get_pos_bfd (bfd **, enum pos, const char *);
121 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
122 extract the COUNTED_NAME_COUNTER instance of that name. */
123 static bfd_boolean counted_name_mode = 0;
124 static int counted_name_counter = 0;
126 /* Whether to truncate names of files stored in the archive. */
127 static bfd_boolean ar_truncate = FALSE;
129 /* Whether to use a full file name match when searching an archive.
130 This is convenient for archives created by the Microsoft lib
132 static bfd_boolean full_pathname = FALSE;
134 /* Whether to create a "thin" archive (symbol index only -- no files). */
135 static bfd_boolean make_thin_archive = FALSE;
137 static int show_version = 0;
139 static int show_help = 0;
141 static const char *plugin_target = NULL;
143 static const char *target = NULL;
145 #define OPTION_PLUGIN 201
146 #define OPTION_TARGET 202
148 static struct option long_options[] =
150 {"help", no_argument, &show_help, 1},
151 {"plugin", required_argument, NULL, OPTION_PLUGIN},
152 {"target", required_argument, NULL, OPTION_TARGET},
153 {"version", no_argument, &show_version, 1},
154 {NULL, no_argument, NULL, 0}
162 interactive = isatty (fileno (stdin));
166 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
167 COUNT is the length of the FILES chain; FUNCTION is called on each entry
168 whose name matches one in FILES. */
171 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
178 for (head = arch->archive_next; head; head = head->archive_next)
186 /* This may appear to be a baroque way of accomplishing what we want.
187 However we have to iterate over the filenames in order to notice where
188 a filename is requested but does not exist in the archive. Ditto
189 mapping over each file each time -- we want to hack multiple
192 for (; count > 0; files++, count--)
194 bfd_boolean found = FALSE;
197 for (head = arch->archive_next; head; head = head->archive_next)
199 const char * filename;
202 filename = head->filename;
203 if (filename == NULL)
205 /* Some archive formats don't get the filenames filled in
206 until the elements are opened. */
208 bfd_stat_arch_elt (head, &buf);
210 else if (bfd_is_thin_archive (arch))
212 /* Thin archives store full pathnames. Need to normalize. */
213 filename = normalize (filename, arch);
217 && !FILENAME_CMP (normalize (*files, arch), filename))
220 if (counted_name_mode
221 && match_count != counted_name_counter)
223 /* Counting, and didn't match on count; go on to the
234 /* xgettext:c-format */
235 fprintf (stderr, _("no entry %s in archive\n"), *files);
239 bfd_boolean operation_alters_arch = FALSE;
246 s = help ? stdout : stderr;
248 #if BFD_SUPPORTS_PLUGINS
249 /* xgettext:c-format */
250 const char *command_line
251 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
252 " [--plugin <name>] [member-name] [count] archive-file file...\n");
255 /* xgettext:c-format */
256 const char *command_line
257 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
258 " [member-name] [count] archive-file file...\n");
260 fprintf (s, command_line, program_name);
262 /* xgettext:c-format */
263 fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
264 fprintf (s, _(" commands:\n"));
265 fprintf (s, _(" d - delete file(s) from the archive\n"));
266 fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
267 fprintf (s, _(" p - print file(s) found in the archive\n"));
268 fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
269 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
270 fprintf (s, _(" s - act as ranlib\n"));
271 fprintf (s, _(" t - display contents of archive\n"));
272 fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
273 fprintf (s, _(" command specific modifiers:\n"));
274 fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
275 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
276 if (DEFAULT_AR_DETERMINISTIC)
279 [D] - use zero for timestamps and uids/gids (default)\n"));
281 [U] - use actual timestamps and uids/gids\n"));
286 [D] - use zero for timestamps and uids/gids\n"));
288 [U] - use actual timestamps and uids/gids (default)\n"));
290 fprintf (s, _(" [N] - use instance [count] of name\n"));
291 fprintf (s, _(" [f] - truncate inserted file names\n"));
292 fprintf (s, _(" [P] - use full path names when matching\n"));
293 fprintf (s, _(" [o] - preserve original dates\n"));
294 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
295 fprintf (s, _(" generic modifiers:\n"));
296 fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
297 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
298 fprintf (s, _(" [S] - do not build a symbol table\n"));
299 fprintf (s, _(" [T] - make a thin archive\n"));
300 fprintf (s, _(" [v] - be verbose\n"));
301 fprintf (s, _(" [V] - display the version number\n"));
302 fprintf (s, _(" @<file> - read options from <file>\n"));
303 fprintf (s, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
304 #if BFD_SUPPORTS_PLUGINS
305 fprintf (s, _(" optional:\n"));
306 fprintf (s, _(" --plugin <p> - load the specified plugin\n"));
311 list_supported_targets (program_name, s);
313 if (REPORT_BUGS_TO[0] && help)
314 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
316 xexit (help ? 0 : 1);
320 ranlib_usage (int help)
324 s = help ? stdout : stderr;
326 /* xgettext:c-format */
327 fprintf (s, _("Usage: %s [options] archive\n"), program_name);
328 fprintf (s, _(" Generate an index to speed access to archives\n"));
329 fprintf (s, _(" The options are:\n\
330 @<file> Read options from <file>\n"));
331 #if BFD_SUPPORTS_PLUGINS
333 --plugin <name> Load the specified plugin\n"));
335 if (DEFAULT_AR_DETERMINISTIC)
337 -D Use zero for symbol map timestamp (default)\n\
338 -U Use an actual symbol map timestamp\n"));
341 -D Use zero for symbol map timestamp\n\
342 -U Use actual symbol map timestamp (default)\n"));
344 -t Update the archive's symbol map timestamp\n\
345 -h --help Print this help message\n\
346 -v --version Print version information\n"));
348 list_supported_targets (program_name, s);
350 if (REPORT_BUGS_TO[0] && help)
351 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
353 xexit (help ? 0 : 1);
356 /* Normalize a file name specified on the command line into a file
357 name which we will use in an archive. */
360 normalize (const char *file, bfd *abfd)
362 const char *filename;
367 filename = lbasename (file);
371 && strlen (filename) > abfd->xvec->ar_max_namelen)
376 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
377 memcpy (s, filename, abfd->xvec->ar_max_namelen);
378 s[abfd->xvec->ar_max_namelen] = '\0';
385 /* Remove any output file. This is only called via xatexit. */
387 static const char *output_filename = NULL;
388 static FILE *output_file = NULL;
389 static bfd *output_bfd = NULL;
394 if (output_filename != NULL)
396 if (output_bfd != NULL)
397 bfd_cache_close (output_bfd);
398 if (output_file != NULL)
399 fclose (output_file);
400 unlink_if_ordinary (output_filename);
405 decode_options (int argc, char **argv)
409 /* Convert old-style tar call by exploding option element and rearranging
410 options accordingly. */
412 if (argc > 1 && argv[1][0] != '-')
414 int new_argc; /* argc value for rearranged arguments */
415 char **new_argv; /* argv value for rearranged arguments */
416 char *const *in; /* cursor into original argv */
417 char **out; /* cursor into rearranged argv */
418 const char *letter; /* cursor into old option letters */
419 char buffer[3]; /* constructed option buffer */
421 /* Initialize a constructed option. */
426 /* Allocate a new argument array, and copy program name in it. */
428 new_argc = argc - 1 + strlen (argv[1]);
429 new_argv = xmalloc ((new_argc + 1) * sizeof (*argv));
434 /* Copy each old letter option as a separate option. */
436 for (letter = *in++; *letter; letter++)
439 *out++ = xstrdup (buffer);
442 /* Copy all remaining options. */
444 while (in < argv + argc)
448 /* Replace the old option list by the new one. */
454 while ((c = getopt_long (argc, argv, "hdmpqrtxlcoVsSuvabiMNfPTDU",
455 long_options, NULL)) != EOF)
466 if (operation != none)
467 fatal (_("two different operation options specified"));
478 operation_alters_arch = TRUE;
482 operation_alters_arch = TRUE;
485 operation = print_files;
488 operation = quick_append;
489 operation_alters_arch = TRUE;
493 operation_alters_arch = TRUE;
496 operation = print_table;
528 postype = pos_before;
531 postype = pos_before;
537 counted_name_mode = TRUE;
543 full_pathname = TRUE;
546 make_thin_archive = TRUE;
549 deterministic = TRUE;
552 deterministic = FALSE;
555 #if BFD_SUPPORTS_PLUGINS
556 plugin_target = "plugin";
557 bfd_plugin_set_plugin (optarg);
559 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
566 case 0: /* A long option that just sets a flag. */
573 return &argv[optind];
576 /* If neither -D nor -U was specified explicitly,
577 then use the configured default. */
579 default_deterministic (void)
581 if (deterministic < 0)
582 deterministic = DEFAULT_AR_DETERMINISTIC;
586 ranlib_main (int argc, char **argv)
588 int arg_index, status = 0;
589 bfd_boolean touch = FALSE;
592 while ((c = getopt_long (argc, argv, "DhHUvVt", long_options, NULL)) != EOF)
597 deterministic = TRUE;
600 deterministic = FALSE;
614 /* PR binutils/13493: Support plugins. */
616 #if BFD_SUPPORTS_PLUGINS
617 plugin_target = "plugin";
618 bfd_plugin_set_plugin (optarg);
620 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
634 print_version ("ranlib");
636 default_deterministic ();
640 while (arg_index < argc)
643 status |= ranlib_only (argv[arg_index]);
645 status |= ranlib_touch (argv[arg_index]);
652 int main (int, char **);
655 main (int argc, char **argv)
660 char *inarch_filename;
663 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
664 setlocale (LC_MESSAGES, "");
666 #if defined (HAVE_SETLOCALE)
667 setlocale (LC_CTYPE, "");
669 bindtextdomain (PACKAGE, LOCALEDIR);
670 textdomain (PACKAGE);
672 program_name = argv[0];
673 xmalloc_set_program_name (program_name);
674 #if BFD_SUPPORTS_PLUGINS
675 bfd_plugin_set_program_name (program_name);
678 expandargv (&argc, &argv);
682 const char *temp = lbasename (program_name);
684 if (strlen (temp) >= 6
685 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
691 START_PROGRESS (program_name, 0);
694 set_default_bfd_target ();
696 xatexit (remove_output);
698 for (i = 1; i < argc; i++)
699 if (! ar_emul_parse_arg (argv[i]))
705 ranlib_main (argc, argv);
710 argv = decode_options (argc, argv);
716 print_version ("ar");
728 /* We don't use do_quick_append any more. Too many systems
729 expect ar to always rebuild the symbol table even when q is
732 /* We can't write an armap when using ar q, so just do ar r
734 if (operation == quick_append && write_armap)
737 if ((operation == none || operation == print_table)
739 xexit (ranlib_only (argv[arg_index]));
741 if (operation == none)
742 fatal (_("no operation specified"));
744 if (newer_only && operation != replace)
745 fatal (_("`u' is only meaningful with the `r' option."));
747 if (newer_only && deterministic > 0)
748 fatal (_("`u' is not meaningful with the `D' option."));
750 if (newer_only && deterministic < 0 && DEFAULT_AR_DETERMINISTIC)
752 `u' modifier ignored since `D' is the default (see `U')"));
754 default_deterministic ();
756 if (postype != pos_default)
757 posname = argv[arg_index++];
759 if (counted_name_mode)
761 if (operation != extract && operation != del)
762 fatal (_("`N' is only meaningful with the `x' and `d' options."));
763 counted_name_counter = atoi (argv[arg_index++]);
764 if (counted_name_counter <= 0)
765 fatal (_("Value for `N' must be positive."));
768 inarch_filename = argv[arg_index++];
770 for (file_count = 0; argv[arg_index + file_count] != NULL; file_count++)
773 files = (file_count > 0) ? argv + arg_index : NULL;
775 arch = open_inarch (inarch_filename,
776 files == NULL ? (char *) NULL : files[0]);
778 if (operation == extract && bfd_is_thin_archive (arch))
779 fatal (_("`x' cannot be used on thin archives."));
784 map_over_members (arch, print_descr, files, file_count);
788 map_over_members (arch, print_contents, files, file_count);
792 map_over_members (arch, extract_file, files, file_count);
797 delete_members (arch, files);
799 output_filename = NULL;
803 /* PR 12558: Creating and moving at the same time does
804 not make sense. Just create the archive instead. */
808 move_members (arch, files);
810 output_filename = NULL;
817 if (files != NULL || write_armap > 0)
818 replace_members (arch, files, operation == quick_append);
820 output_filename = NULL;
823 /* Shouldn't happen! */
825 /* xgettext:c-format */
826 fatal (_("internal error -- this option not implemented"));
830 END_PROGRESS (program_name);
837 open_inarch (const char *archive_filename, const char *file)
845 bfd_set_error (bfd_error_no_error);
848 target = plugin_target;
850 if (stat (archive_filename, &sbuf) != 0)
852 #if !defined(__GO32__) || defined(__DJGPP__)
854 /* FIXME: I don't understand why this fragment was ifndef'ed
855 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
856 stat() works just fine in v2.x, so I think this should be
857 removed. For now, I enable it for DJGPP v2. -- EZ. */
859 /* KLUDGE ALERT! Temporary fix until I figger why
860 stat() is wrong ... think it's buried in GO32's IDT - Jax */
862 bfd_fatal (archive_filename);
865 if (!operation_alters_arch)
867 fprintf (stderr, "%s: ", program_name);
868 perror (archive_filename);
873 /* If the target isn't set, try to figure out the target to use
874 for the archive from the first object on the list. */
875 if (target == NULL && file != NULL)
879 obj = bfd_openr (file, target);
882 if (bfd_check_format (obj, bfd_object))
883 target = bfd_get_target (obj);
884 (void) bfd_close (obj);
888 /* Create an empty archive. */
889 arch = bfd_openw (archive_filename, target);
891 || ! bfd_set_format (arch, bfd_archive)
892 || ! bfd_close (arch))
893 bfd_fatal (archive_filename);
894 else if (!silent_create)
895 non_fatal (_("creating %s"), archive_filename);
897 /* If we die creating a new archive, don't leave it around. */
898 output_filename = archive_filename;
901 arch = bfd_openr (archive_filename, target);
905 bfd_fatal (archive_filename);
908 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
910 bfd_nonfatal (archive_filename);
911 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
913 list_matching_formats (matching);
919 if ((operation == replace || operation == quick_append)
920 && bfd_openr_next_archived_file (arch, NULL) != NULL)
922 /* PR 15140: Catch attempts to convert a normal
923 archive into a thin archive or vice versa. */
924 if (make_thin_archive && ! bfd_is_thin_archive (arch))
926 fatal (_("Cannot convert existing library %s to thin format"),
927 bfd_get_filename (arch));
930 else if (! make_thin_archive && bfd_is_thin_archive (arch))
932 fatal (_("Cannot convert existing thin library %s to normal format"),
933 bfd_get_filename (arch));
938 last_one = &(arch->archive_next);
939 /* Read all the contents right away, regardless. */
940 for (next_one = bfd_openr_next_archived_file (arch, NULL);
942 next_one = bfd_openr_next_archived_file (arch, next_one))
945 *last_one = next_one;
946 last_one = &next_one->archive_next;
948 *last_one = (bfd *) NULL;
949 if (bfd_get_error () != bfd_error_no_more_archived_files)
955 print_contents (bfd *abfd)
957 bfd_size_type ncopied = 0;
959 char *cbuf = (char *) xmalloc (BUFSIZE);
962 if (bfd_stat_arch_elt (abfd, &buf) != 0)
963 /* xgettext:c-format */
964 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
967 printf ("\n<%s>\n\n", bfd_get_filename (abfd));
969 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
972 while (ncopied < size)
975 bfd_size_type tocopy = size - ncopied;
977 if (tocopy > BUFSIZE)
980 nread = bfd_bread (cbuf, tocopy, abfd);
982 /* xgettext:c-format */
983 fatal (_("%s is not a valid archive"),
984 bfd_get_filename (bfd_my_archive (abfd)));
986 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
987 return value to bfd_size_type to avoid comparison between signed and
989 if ((bfd_size_type) fwrite (cbuf, 1, nread, stdout) != nread)
990 fatal ("stdout: %s", strerror (errno));
996 /* Extract a member of the archive into its own file.
998 We defer opening the new file until after we have read a BUFSIZ chunk of the
999 old one, since we know we have just read the archive header for the old
1000 one. Since most members are shorter than BUFSIZ, this means we will read
1001 the old header, read the old data, write a new inode for the new file, and
1002 write the new data, and be done. This 'optimization' is what comes from
1003 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1007 extract_file (bfd *abfd)
1010 char *cbuf = (char *) xmalloc (BUFSIZE);
1011 bfd_size_type nread, tocopy;
1012 bfd_size_type ncopied = 0;
1016 if (bfd_stat_arch_elt (abfd, &buf) != 0)
1017 /* xgettext:c-format */
1018 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
1022 printf ("x - %s\n", bfd_get_filename (abfd));
1024 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
1029 /* Seems like an abstraction violation, eh? Well it's OK! */
1030 output_filename = bfd_get_filename (abfd);
1032 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
1033 if (ostream == NULL)
1035 perror (bfd_get_filename (abfd));
1039 output_file = ostream;
1042 while (ncopied < size)
1044 tocopy = size - ncopied;
1045 if (tocopy > BUFSIZE)
1048 nread = bfd_bread (cbuf, tocopy, abfd);
1049 if (nread != tocopy)
1050 /* xgettext:c-format */
1051 fatal (_("%s is not a valid archive"),
1052 bfd_get_filename (bfd_my_archive (abfd)));
1054 /* See comment above; this saves disk arm motion */
1055 if (ostream == NULL)
1057 /* Seems like an abstraction violation, eh? Well it's OK! */
1058 output_filename = bfd_get_filename (abfd);
1060 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
1061 if (ostream == NULL)
1063 perror (bfd_get_filename (abfd));
1067 output_file = ostream;
1070 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1071 the return value to bfd_size_type to avoid comparison between
1072 signed and unsigned values. */
1073 if ((bfd_size_type) fwrite (cbuf, 1, nread, ostream) != nread)
1074 fatal ("%s: %s", output_filename, strerror (errno));
1078 if (ostream != NULL)
1082 output_filename = NULL;
1084 chmod (bfd_get_filename (abfd), buf.st_mode);
1088 /* Set access time to modification time. Only st_mtime is
1089 initialized by bfd_stat_arch_elt. */
1090 buf.st_atime = buf.st_mtime;
1091 set_times (bfd_get_filename (abfd), &buf);
1098 write_archive (bfd *iarch)
1101 char *old_name, *new_name;
1102 bfd *contents_head = iarch->archive_next;
1104 old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
1105 strcpy (old_name, bfd_get_filename (iarch));
1106 new_name = make_tempname (old_name);
1108 if (new_name == NULL)
1109 bfd_fatal (_("could not create temporary file whilst writing archive"));
1111 output_filename = new_name;
1113 obfd = bfd_openw (new_name, bfd_get_target (iarch));
1116 bfd_fatal (old_name);
1120 bfd_set_format (obfd, bfd_archive);
1122 /* Request writing the archive symbol table unless we've
1123 been explicitly requested not to. */
1124 obfd->has_armap = write_armap >= 0;
1128 /* This should really use bfd_set_file_flags, but that rejects
1130 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1134 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
1136 if (make_thin_archive || bfd_is_thin_archive (iarch))
1137 bfd_is_thin_archive (obfd) = 1;
1139 if (!bfd_set_archive_head (obfd, contents_head))
1140 bfd_fatal (old_name);
1142 if (!bfd_close (obfd))
1143 bfd_fatal (old_name);
1146 output_filename = NULL;
1148 /* We don't care if this fails; we might be creating the archive. */
1151 if (smart_rename (new_name, old_name, 0) != 0)
1156 /* Return a pointer to the pointer to the entry which should be rplacd'd
1157 into when altering. DEFAULT_POS should be how to interpret pos_default,
1158 and should be a pos value. */
1161 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
1163 bfd **after_bfd = contents;
1165 const char *realposname;
1167 if (postype == pos_default)
1169 realpos = default_pos;
1170 realposname = default_posname;
1175 realposname = posname;
1178 if (realpos == pos_end)
1181 after_bfd = &((*after_bfd)->archive_next);
1185 for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
1186 if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1188 if (realpos == pos_after)
1189 after_bfd = &(*after_bfd)->archive_next;
1197 delete_members (bfd *arch, char **files_to_delete)
1199 bfd **current_ptr_ptr;
1201 bfd_boolean something_changed = FALSE;
1204 for (; *files_to_delete != NULL; ++files_to_delete)
1206 /* In a.out systems, the armap is optional. It's also called
1207 __.SYMDEF. So if the user asked to delete it, we should remember
1208 that fact. This isn't quite right for COFF systems (where
1209 __.SYMDEF might be regular member), but it's very unlikely
1210 to be a problem. FIXME */
1212 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1214 arch->has_armap = FALSE;
1221 current_ptr_ptr = &(arch->archive_next);
1222 while (*current_ptr_ptr)
1224 if (FILENAME_CMP (normalize (*files_to_delete, arch),
1225 (*current_ptr_ptr)->filename) == 0)
1228 if (counted_name_mode
1229 && match_count != counted_name_counter)
1231 /* Counting, and didn't match on count; go on to the
1237 something_changed = TRUE;
1241 *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
1246 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1249 if (verbose && !found)
1251 /* xgettext:c-format */
1252 printf (_("No member named `%s'\n"), *files_to_delete);
1258 if (something_changed)
1259 write_archive (arch);
1261 output_filename = NULL;
1265 /* Reposition existing members within an archive */
1268 move_members (bfd *arch, char **files_to_move)
1270 bfd **after_bfd; /* New entries go after this one */
1271 bfd **current_ptr_ptr; /* cdr pointer into contents */
1273 for (; *files_to_move; ++files_to_move)
1275 current_ptr_ptr = &(arch->archive_next);
1276 while (*current_ptr_ptr)
1278 bfd *current_ptr = *current_ptr_ptr;
1279 if (FILENAME_CMP (normalize (*files_to_move, arch),
1280 current_ptr->filename) == 0)
1282 /* Move this file to the end of the list - first cut from
1285 *current_ptr_ptr = current_ptr->archive_next;
1287 /* Now glue to end */
1288 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1289 link_bfd = *after_bfd;
1290 *after_bfd = current_ptr;
1291 current_ptr->archive_next = link_bfd;
1294 printf ("m - %s\n", *files_to_move);
1299 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1301 /* xgettext:c-format */
1302 fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1307 write_archive (arch);
1310 /* Ought to default to replacing in place, but this is existing practice! */
1313 replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
1315 bfd_boolean changed = FALSE;
1316 bfd **after_bfd; /* New entries go after this one. */
1320 while (files_to_move && *files_to_move)
1324 current_ptr = &arch->archive_next;
1325 while (*current_ptr)
1327 current = *current_ptr;
1329 /* For compatibility with existing ar programs, we
1330 permit the same file to be added multiple times. */
1331 if (FILENAME_CMP (normalize (*files_to_move, arch),
1332 normalize (current->filename, arch)) == 0
1333 && current->arelt_data != NULL)
1337 struct stat fsbuf, asbuf;
1339 if (stat (*files_to_move, &fsbuf) != 0)
1341 if (errno != ENOENT)
1342 bfd_fatal (*files_to_move);
1345 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1346 /* xgettext:c-format */
1347 fatal (_("internal stat error on %s"),
1350 if (fsbuf.st_mtime <= asbuf.st_mtime)
1354 after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
1356 if (ar_emul_replace (after_bfd, *files_to_move,
1359 /* Snip out this entry from the chain. */
1360 *current_ptr = (*current_ptr)->archive_next;
1366 current_ptr = &(current->archive_next);
1370 /* Add to the end of the archive. */
1371 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1373 if (ar_emul_append (after_bfd, *files_to_move, target,
1374 verbose, make_thin_archive))
1383 write_archive (arch);
1385 output_filename = NULL;
1389 ranlib_only (const char *archname)
1393 if (get_file_size (archname) < 1)
1396 arch = open_inarch (archname, (char *) NULL);
1399 write_archive (arch);
1403 /* Update the timestamp of the symbol map of an archive. */
1406 ranlib_touch (const char *archname)
1409 /* I don't think updating works on go32. */
1410 ranlib_only (archname);
1416 if (get_file_size (archname) < 1)
1418 f = open (archname, O_RDWR | O_BINARY, 0);
1421 bfd_set_error (bfd_error_system_call);
1422 bfd_fatal (archname);
1425 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1427 bfd_fatal (archname);
1428 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1430 bfd_nonfatal (archname);
1431 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1433 list_matching_formats (matching);
1439 if (! bfd_has_map (arch))
1440 /* xgettext:c-format */
1441 fatal (_("%s: no archive map to update"), archname);
1444 arch->flags |= BFD_DETERMINISTIC_OUTPUT;
1446 bfd_update_armap_timestamp (arch);
1448 if (! bfd_close (arch))
1449 bfd_fatal (archname);
1454 /* Things which are interesting to map over all or some of the files: */
1457 print_descr (bfd *abfd)
1459 print_arelt_descr (stdout, abfd, verbose);