1 /* ar.c - Archive modify and extract.
2 Copyright (C) 1991-2016 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"
34 #include "filenames.h"
39 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
41 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
44 /* Static declarations. */
46 static void mri_emul (void);
47 static const char *normalize (const char *, bfd *);
48 static void remove_output (void);
49 static void map_over_members (bfd *, void (*)(bfd *), char **, int);
50 static void print_contents (bfd * member);
51 static void delete_members (bfd *, char **files_to_delete);
53 static void move_members (bfd *, char **files_to_move);
54 static void replace_members
55 (bfd *, char **files_to_replace, bfd_boolean quick);
56 static void print_descr (bfd * abfd);
57 static void write_archive (bfd *);
58 static int ranlib_only (const char *archname);
59 static int ranlib_touch (const char *archname);
60 static void usage (int);
62 /** Globals and flags. */
66 /* This flag distinguishes between ar and ranlib:
67 1 means this is 'ranlib'; 0 means this is 'ar'.
68 -1 means if we should use argv[0] to decide. */
71 /* Nonzero means don't warn about creating the archive file if necessary. */
72 int silent_create = 0;
74 /* Nonzero means describe each action performed. */
77 /* Nonzero means preserve dates of members when extracting them. */
78 int preserve_dates = 0;
80 /* Nonzero means don't replace existing members whose dates are more recent
81 than the corresponding files. */
84 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
85 member). -1 means we've been explicitly asked to not write a symbol table;
86 +1 means we've been explicitly asked to write it;
88 Traditionally, the default in BSD has been to not write the table.
89 However, for POSIX.2 compliance the default is now to write a symbol table
90 if any of the members are object files. */
93 /* Operate in deterministic mode: write zero for timestamps, uids,
94 and gids for archive members and the archive symbol table, and write
95 consistent file modes. */
96 int deterministic = -1; /* Determinism indeterminate. */
98 /* Nonzero means it's the name of an existing member; position new or moved
99 files with respect to this one. */
100 char *posname = NULL;
102 /* Sez how to use `posname': pos_before means position before that member.
103 pos_after means position after that member. pos_end means always at end.
104 pos_default means default appropriately. For the latter two, `posname'
105 should also be zero. */
108 pos_default, pos_before, pos_after, pos_end
109 } postype = pos_default;
113 none = 0, del, replace, print_table,
114 print_files, extract, move, quick_append
118 get_pos_bfd (bfd **, enum pos, const char *);
120 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
121 extract the COUNTED_NAME_COUNTER instance of that name. */
122 static bfd_boolean counted_name_mode = 0;
123 static int counted_name_counter = 0;
125 /* Whether to truncate names of files stored in the archive. */
126 static bfd_boolean ar_truncate = FALSE;
128 /* Whether to use a full file name match when searching an archive.
129 This is convenient for archives created by the Microsoft lib
131 static bfd_boolean full_pathname = FALSE;
133 /* Whether to create a "thin" archive (symbol index only -- no files). */
134 static bfd_boolean make_thin_archive = FALSE;
136 static int show_version = 0;
138 static int show_help = 0;
140 #if BFD_SUPPORTS_PLUGINS
141 static const char *plugin_target = "plugin";
143 static const char *plugin_target = NULL;
146 static const char *target = NULL;
148 #define OPTION_PLUGIN 201
149 #define OPTION_TARGET 202
151 static struct option long_options[] =
153 {"help", no_argument, &show_help, 1},
154 {"plugin", required_argument, NULL, OPTION_PLUGIN},
155 {"target", required_argument, NULL, OPTION_TARGET},
156 {"version", no_argument, &show_version, 1},
157 {NULL, no_argument, NULL, 0}
165 interactive = isatty (fileno (stdin));
169 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
170 COUNT is the length of the FILES chain; FUNCTION is called on each entry
171 whose name matches one in FILES. */
174 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
181 for (head = arch->archive_next; head; head = head->archive_next)
189 /* This may appear to be a baroque way of accomplishing what we want.
190 However we have to iterate over the filenames in order to notice where
191 a filename is requested but does not exist in the archive. Ditto
192 mapping over each file each time -- we want to hack multiple
195 for (head = arch->archive_next; head; head = head->archive_next)
196 head->archive_pass = 0;
198 for (; count > 0; files++, count--)
200 bfd_boolean found = FALSE;
203 for (head = arch->archive_next; head; head = head->archive_next)
205 const char * filename;
208 /* PR binutils/15796: Once an archive element has been matched
209 do not match it again. If the user provides multiple same-named
210 parameters on the command line their intent is to match multiple
211 same-named entries in the archive, not the same entry multiple
213 if (head->archive_pass)
216 filename = head->filename;
217 if (filename == NULL)
219 /* Some archive formats don't get the filenames filled in
220 until the elements are opened. */
222 bfd_stat_arch_elt (head, &buf);
224 else if (bfd_is_thin_archive (arch))
226 /* Thin archives store full pathnames. Need to normalize. */
227 filename = normalize (filename, arch);
231 && !FILENAME_CMP (normalize (*files, arch), filename))
234 if (counted_name_mode
235 && match_count != counted_name_counter)
237 /* Counting, and didn't match on count; go on to the
244 head->archive_pass = 1;
245 /* PR binutils/15796: Once a file has been matched, do not
246 match any more same-named files in the archive. If the
247 user does want to match multiple same-name files in an
248 archive they should provide multiple same-name parameters
249 to the ar command. */
255 /* xgettext:c-format */
256 fprintf (stderr, _("no entry %s in archive\n"), *files);
260 bfd_boolean operation_alters_arch = FALSE;
267 #if BFD_SUPPORTS_PLUGINS
268 /* xgettext:c-format */
269 const char *command_line
270 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
271 " [--plugin <name>] [member-name] [count] archive-file file...\n");
274 /* xgettext:c-format */
275 const char *command_line
276 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
277 " [member-name] [count] archive-file file...\n");
279 s = help ? stdout : stderr;
281 fprintf (s, command_line, program_name);
283 /* xgettext:c-format */
284 fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
285 fprintf (s, _(" commands:\n"));
286 fprintf (s, _(" d - delete file(s) from the archive\n"));
287 fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
288 fprintf (s, _(" p - print file(s) found in the archive\n"));
289 fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
290 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
291 fprintf (s, _(" s - act as ranlib\n"));
292 fprintf (s, _(" t - display contents of archive\n"));
293 fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
294 fprintf (s, _(" command specific modifiers:\n"));
295 fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
296 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
297 if (DEFAULT_AR_DETERMINISTIC)
300 [D] - use zero for timestamps and uids/gids (default)\n"));
302 [U] - use actual timestamps and uids/gids\n"));
307 [D] - use zero for timestamps and uids/gids\n"));
309 [U] - use actual timestamps and uids/gids (default)\n"));
311 fprintf (s, _(" [N] - use instance [count] of name\n"));
312 fprintf (s, _(" [f] - truncate inserted file names\n"));
313 fprintf (s, _(" [P] - use full path names when matching\n"));
314 fprintf (s, _(" [o] - preserve original dates\n"));
315 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
316 fprintf (s, _(" generic modifiers:\n"));
317 fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
318 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
319 fprintf (s, _(" [S] - do not build a symbol table\n"));
320 fprintf (s, _(" [T] - make a thin archive\n"));
321 fprintf (s, _(" [v] - be verbose\n"));
322 fprintf (s, _(" [V] - display the version number\n"));
323 fprintf (s, _(" @<file> - read options from <file>\n"));
324 fprintf (s, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
325 #if BFD_SUPPORTS_PLUGINS
326 fprintf (s, _(" optional:\n"));
327 fprintf (s, _(" --plugin <p> - load the specified plugin\n"));
332 list_supported_targets (program_name, s);
334 if (REPORT_BUGS_TO[0] && help)
335 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
337 xexit (help ? 0 : 1);
341 ranlib_usage (int help)
345 s = help ? stdout : stderr;
347 /* xgettext:c-format */
348 fprintf (s, _("Usage: %s [options] archive\n"), program_name);
349 fprintf (s, _(" Generate an index to speed access to archives\n"));
350 fprintf (s, _(" The options are:\n\
351 @<file> Read options from <file>\n"));
352 #if BFD_SUPPORTS_PLUGINS
354 --plugin <name> Load the specified plugin\n"));
356 if (DEFAULT_AR_DETERMINISTIC)
358 -D Use zero for symbol map timestamp (default)\n\
359 -U Use an actual symbol map timestamp\n"));
362 -D Use zero for symbol map timestamp\n\
363 -U Use actual symbol map timestamp (default)\n"));
365 -t Update the archive's symbol map timestamp\n\
366 -h --help Print this help message\n\
367 -v --version Print version information\n"));
369 list_supported_targets (program_name, s);
371 if (REPORT_BUGS_TO[0] && help)
372 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
374 xexit (help ? 0 : 1);
377 /* Normalize a file name specified on the command line into a file
378 name which we will use in an archive. */
381 normalize (const char *file, bfd *abfd)
383 const char *filename;
388 filename = lbasename (file);
392 && strlen (filename) > abfd->xvec->ar_max_namelen)
397 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
398 memcpy (s, filename, abfd->xvec->ar_max_namelen);
399 s[abfd->xvec->ar_max_namelen] = '\0';
406 /* Remove any output file. This is only called via xatexit. */
408 static const char *output_filename = NULL;
409 static FILE *output_file = NULL;
410 static bfd *output_bfd = NULL;
415 if (output_filename != NULL)
417 if (output_bfd != NULL)
418 bfd_cache_close (output_bfd);
419 if (output_file != NULL)
420 fclose (output_file);
421 unlink_if_ordinary (output_filename);
426 decode_options (int argc, char **argv)
430 /* Convert old-style tar call by exploding option element and rearranging
431 options accordingly. */
433 if (argc > 1 && argv[1][0] != '-')
435 int new_argc; /* argc value for rearranged arguments */
436 char **new_argv; /* argv value for rearranged arguments */
437 char *const *in; /* cursor into original argv */
438 char **out; /* cursor into rearranged argv */
439 const char *letter; /* cursor into old option letters */
440 char buffer[3]; /* constructed option buffer */
442 /* Initialize a constructed option. */
447 /* Allocate a new argument array, and copy program name in it. */
449 new_argc = argc - 1 + strlen (argv[1]);
450 new_argv = xmalloc ((new_argc + 1) * sizeof (*argv));
455 /* Copy each old letter option as a separate option. */
457 for (letter = *in++; *letter; letter++)
460 *out++ = xstrdup (buffer);
463 /* Copy all remaining options. */
465 while (in < argv + argc)
469 /* Replace the old option list by the new one. */
475 while ((c = getopt_long (argc, argv, "hdmpqrtxlcoVsSuvabiMNfPTDU",
476 long_options, NULL)) != EOF)
487 if (operation != none)
488 fatal (_("two different operation options specified"));
499 operation_alters_arch = TRUE;
503 operation_alters_arch = TRUE;
506 operation = print_files;
509 operation = quick_append;
510 operation_alters_arch = TRUE;
514 operation_alters_arch = TRUE;
517 operation = print_table;
549 postype = pos_before;
552 postype = pos_before;
558 counted_name_mode = TRUE;
564 full_pathname = TRUE;
567 make_thin_archive = TRUE;
570 deterministic = TRUE;
573 deterministic = FALSE;
576 #if BFD_SUPPORTS_PLUGINS
577 bfd_plugin_set_plugin (optarg);
579 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
586 case 0: /* A long option that just sets a flag. */
593 return &argv[optind];
596 /* If neither -D nor -U was specified explicitly,
597 then use the configured default. */
599 default_deterministic (void)
601 if (deterministic < 0)
602 deterministic = DEFAULT_AR_DETERMINISTIC;
606 ranlib_main (int argc, char **argv)
608 int arg_index, status = 0;
609 bfd_boolean touch = FALSE;
612 while ((c = getopt_long (argc, argv, "DhHUvVt", long_options, NULL)) != EOF)
617 deterministic = TRUE;
620 deterministic = FALSE;
634 /* PR binutils/13493: Support plugins. */
636 #if BFD_SUPPORTS_PLUGINS
637 bfd_plugin_set_plugin (optarg);
639 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
653 print_version ("ranlib");
655 default_deterministic ();
659 while (arg_index < argc)
662 status |= ranlib_only (argv[arg_index]);
664 status |= ranlib_touch (argv[arg_index]);
671 int main (int, char **);
674 main (int argc, char **argv)
679 char *inarch_filename;
682 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
683 setlocale (LC_MESSAGES, "");
685 #if defined (HAVE_SETLOCALE)
686 setlocale (LC_CTYPE, "");
688 bindtextdomain (PACKAGE, LOCALEDIR);
689 textdomain (PACKAGE);
691 program_name = argv[0];
692 xmalloc_set_program_name (program_name);
693 bfd_set_error_program_name (program_name);
694 #if BFD_SUPPORTS_PLUGINS
695 bfd_plugin_set_program_name (program_name);
698 expandargv (&argc, &argv);
702 const char *temp = lbasename (program_name);
704 if (strlen (temp) >= 6
705 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
711 START_PROGRESS (program_name, 0);
714 set_default_bfd_target ();
716 xatexit (remove_output);
718 for (i = 1; i < argc; i++)
719 if (! ar_emul_parse_arg (argv[i]))
725 ranlib_main (argc, argv);
730 argv = decode_options (argc, argv);
736 print_version ("ar");
742 default_deterministic ();
749 /* We don't use do_quick_append any more. Too many systems
750 expect ar to always rebuild the symbol table even when q is
753 /* We can't write an armap when using ar q, so just do ar r
755 if (operation == quick_append && write_armap)
758 if ((operation == none || operation == print_table)
760 xexit (ranlib_only (argv[arg_index]));
762 if (operation == none)
763 fatal (_("no operation specified"));
765 if (newer_only && operation != replace)
766 fatal (_("`u' is only meaningful with the `r' option."));
768 if (newer_only && deterministic > 0)
769 fatal (_("`u' is not meaningful with the `D' option."));
771 if (newer_only && deterministic < 0 && DEFAULT_AR_DETERMINISTIC)
773 `u' modifier ignored since `D' is the default (see `U')"));
775 default_deterministic ();
777 if (postype != pos_default)
778 posname = argv[arg_index++];
780 if (counted_name_mode)
782 if (operation != extract && operation != del)
783 fatal (_("`N' is only meaningful with the `x' and `d' options."));
784 counted_name_counter = atoi (argv[arg_index++]);
785 if (counted_name_counter <= 0)
786 fatal (_("Value for `N' must be positive."));
789 inarch_filename = argv[arg_index++];
791 for (file_count = 0; argv[arg_index + file_count] != NULL; file_count++)
794 files = (file_count > 0) ? argv + arg_index : NULL;
796 arch = open_inarch (inarch_filename,
797 files == NULL ? (char *) NULL : files[0]);
799 if (operation == extract && bfd_is_thin_archive (arch))
800 fatal (_("`x' cannot be used on thin archives."));
805 map_over_members (arch, print_descr, files, file_count);
809 map_over_members (arch, print_contents, files, file_count);
813 map_over_members (arch, extract_file, files, file_count);
818 delete_members (arch, files);
820 output_filename = NULL;
824 /* PR 12558: Creating and moving at the same time does
825 not make sense. Just create the archive instead. */
829 move_members (arch, files);
831 output_filename = NULL;
838 if (files != NULL || write_armap > 0)
839 replace_members (arch, files, operation == quick_append);
841 output_filename = NULL;
844 /* Shouldn't happen! */
846 /* xgettext:c-format */
847 fatal (_("internal error -- this option not implemented"));
851 END_PROGRESS (program_name);
858 open_inarch (const char *archive_filename, const char *file)
866 bfd_set_error (bfd_error_no_error);
869 target = plugin_target;
871 if (stat (archive_filename, &sbuf) != 0)
873 #if !defined(__GO32__) || defined(__DJGPP__)
875 /* FIXME: I don't understand why this fragment was ifndef'ed
876 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
877 stat() works just fine in v2.x, so I think this should be
878 removed. For now, I enable it for DJGPP v2. -- EZ. */
880 /* KLUDGE ALERT! Temporary fix until I figger why
881 stat() is wrong ... think it's buried in GO32's IDT - Jax */
883 bfd_fatal (archive_filename);
886 if (!operation_alters_arch)
888 fprintf (stderr, "%s: ", program_name);
889 perror (archive_filename);
894 /* If the target isn't set, try to figure out the target to use
895 for the archive from the first object on the list. */
896 if (target == NULL && file != NULL)
900 obj = bfd_openr (file, target);
903 if (bfd_check_format (obj, bfd_object))
904 target = bfd_get_target (obj);
905 (void) bfd_close (obj);
909 /* Create an empty archive. */
910 arch = bfd_openw (archive_filename, target);
912 || ! bfd_set_format (arch, bfd_archive)
913 || ! bfd_close (arch))
914 bfd_fatal (archive_filename);
915 else if (!silent_create)
916 non_fatal (_("creating %s"), archive_filename);
918 /* If we die creating a new archive, don't leave it around. */
919 output_filename = archive_filename;
922 arch = bfd_openr (archive_filename, target);
926 bfd_fatal (archive_filename);
929 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
931 bfd_nonfatal (archive_filename);
932 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
934 list_matching_formats (matching);
940 if ((operation == replace || operation == quick_append)
941 && bfd_openr_next_archived_file (arch, NULL) != NULL)
943 /* PR 15140: Catch attempts to convert a normal
944 archive into a thin archive or vice versa. */
945 if (make_thin_archive && ! bfd_is_thin_archive (arch))
947 fatal (_("Cannot convert existing library %s to thin format"),
948 bfd_get_filename (arch));
951 else if (! make_thin_archive && bfd_is_thin_archive (arch))
953 fatal (_("Cannot convert existing thin library %s to normal format"),
954 bfd_get_filename (arch));
959 last_one = &(arch->archive_next);
960 /* Read all the contents right away, regardless. */
961 for (next_one = bfd_openr_next_archived_file (arch, NULL);
963 next_one = bfd_openr_next_archived_file (arch, next_one))
966 *last_one = next_one;
967 last_one = &next_one->archive_next;
969 *last_one = (bfd *) NULL;
970 if (bfd_get_error () != bfd_error_no_more_archived_files)
976 print_contents (bfd *abfd)
978 bfd_size_type ncopied = 0;
980 char *cbuf = (char *) xmalloc (BUFSIZE);
983 if (bfd_stat_arch_elt (abfd, &buf) != 0)
984 /* xgettext:c-format */
985 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
988 printf ("\n<%s>\n\n", bfd_get_filename (abfd));
990 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
993 while (ncopied < size)
996 bfd_size_type tocopy = size - ncopied;
998 if (tocopy > BUFSIZE)
1001 nread = bfd_bread (cbuf, tocopy, abfd);
1002 if (nread != tocopy)
1003 /* xgettext:c-format */
1004 fatal (_("%s is not a valid archive"),
1005 bfd_get_filename (abfd->my_archive));
1007 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
1008 return value to bfd_size_type to avoid comparison between signed and
1010 if ((bfd_size_type) fwrite (cbuf, 1, nread, stdout) != nread)
1011 fatal ("stdout: %s", strerror (errno));
1017 /* Extract a member of the archive into its own file.
1019 We defer opening the new file until after we have read a BUFSIZ chunk of the
1020 old one, since we know we have just read the archive header for the old
1021 one. Since most members are shorter than BUFSIZ, this means we will read
1022 the old header, read the old data, write a new inode for the new file, and
1023 write the new data, and be done. This 'optimization' is what comes from
1024 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1028 extract_file (bfd *abfd)
1031 char *cbuf = (char *) xmalloc (BUFSIZE);
1032 bfd_size_type nread, tocopy;
1033 bfd_size_type ncopied = 0;
1037 /* PR binutils/17533: Do not allow directory traversal
1038 outside of the current directory tree. */
1039 if (! is_valid_archive_path (bfd_get_filename (abfd)))
1041 non_fatal (_("illegal pathname found in archive member: %s"),
1042 bfd_get_filename (abfd));
1047 if (bfd_stat_arch_elt (abfd, &buf) != 0)
1048 /* xgettext:c-format */
1049 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
1053 printf ("x - %s\n", bfd_get_filename (abfd));
1055 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
1060 /* Seems like an abstraction violation, eh? Well it's OK! */
1061 output_filename = bfd_get_filename (abfd);
1063 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
1064 if (ostream == NULL)
1066 perror (bfd_get_filename (abfd));
1070 output_file = ostream;
1073 while (ncopied < size)
1075 tocopy = size - ncopied;
1076 if (tocopy > BUFSIZE)
1079 nread = bfd_bread (cbuf, tocopy, abfd);
1080 if (nread != tocopy)
1081 /* xgettext:c-format */
1082 fatal (_("%s is not a valid archive"),
1083 bfd_get_filename (abfd->my_archive));
1085 /* See comment above; this saves disk arm motion */
1086 if (ostream == NULL)
1088 /* Seems like an abstraction violation, eh? Well it's OK! */
1089 output_filename = bfd_get_filename (abfd);
1091 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
1092 if (ostream == NULL)
1094 perror (bfd_get_filename (abfd));
1098 output_file = ostream;
1101 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1102 the return value to bfd_size_type to avoid comparison between
1103 signed and unsigned values. */
1104 if ((bfd_size_type) fwrite (cbuf, 1, nread, ostream) != nread)
1105 fatal ("%s: %s", output_filename, strerror (errno));
1109 if (ostream != NULL)
1113 output_filename = NULL;
1115 chmod (bfd_get_filename (abfd), buf.st_mode);
1119 /* Set access time to modification time. Only st_mtime is
1120 initialized by bfd_stat_arch_elt. */
1121 buf.st_atime = buf.st_mtime;
1122 set_times (bfd_get_filename (abfd), &buf);
1129 write_archive (bfd *iarch)
1132 char *old_name, *new_name;
1133 bfd *contents_head = iarch->archive_next;
1135 old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
1136 strcpy (old_name, bfd_get_filename (iarch));
1137 new_name = make_tempname (old_name);
1139 if (new_name == NULL)
1140 bfd_fatal (_("could not create temporary file whilst writing archive"));
1142 output_filename = new_name;
1144 obfd = bfd_openw (new_name, bfd_get_target (iarch));
1147 bfd_fatal (old_name);
1151 bfd_set_format (obfd, bfd_archive);
1153 /* Request writing the archive symbol table unless we've
1154 been explicitly requested not to. */
1155 obfd->has_armap = write_armap >= 0;
1159 /* This should really use bfd_set_file_flags, but that rejects
1161 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1165 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
1167 if (make_thin_archive || bfd_is_thin_archive (iarch))
1168 bfd_is_thin_archive (obfd) = 1;
1170 if (!bfd_set_archive_head (obfd, contents_head))
1171 bfd_fatal (old_name);
1173 if (!bfd_close (obfd))
1174 bfd_fatal (old_name);
1177 output_filename = NULL;
1179 /* We don't care if this fails; we might be creating the archive. */
1182 if (smart_rename (new_name, old_name, 0) != 0)
1187 /* Return a pointer to the pointer to the entry which should be rplacd'd
1188 into when altering. DEFAULT_POS should be how to interpret pos_default,
1189 and should be a pos value. */
1192 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
1194 bfd **after_bfd = contents;
1196 const char *realposname;
1198 if (postype == pos_default)
1200 realpos = default_pos;
1201 realposname = default_posname;
1206 realposname = posname;
1209 if (realpos == pos_end)
1212 after_bfd = &((*after_bfd)->archive_next);
1216 for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
1217 if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1219 if (realpos == pos_after)
1220 after_bfd = &(*after_bfd)->archive_next;
1228 delete_members (bfd *arch, char **files_to_delete)
1230 bfd **current_ptr_ptr;
1232 bfd_boolean something_changed = FALSE;
1235 for (; *files_to_delete != NULL; ++files_to_delete)
1237 /* In a.out systems, the armap is optional. It's also called
1238 __.SYMDEF. So if the user asked to delete it, we should remember
1239 that fact. This isn't quite right for COFF systems (where
1240 __.SYMDEF might be regular member), but it's very unlikely
1241 to be a problem. FIXME */
1243 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1245 arch->has_armap = FALSE;
1252 current_ptr_ptr = &(arch->archive_next);
1253 while (*current_ptr_ptr)
1255 if (FILENAME_CMP (normalize (*files_to_delete, arch),
1256 (*current_ptr_ptr)->filename) == 0)
1259 if (counted_name_mode
1260 && match_count != counted_name_counter)
1262 /* Counting, and didn't match on count; go on to the
1268 something_changed = TRUE;
1272 *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
1277 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1280 if (verbose && !found)
1282 /* xgettext:c-format */
1283 printf (_("No member named `%s'\n"), *files_to_delete);
1289 if (something_changed)
1290 write_archive (arch);
1292 output_filename = NULL;
1296 /* Reposition existing members within an archive */
1299 move_members (bfd *arch, char **files_to_move)
1301 bfd **after_bfd; /* New entries go after this one */
1302 bfd **current_ptr_ptr; /* cdr pointer into contents */
1304 for (; *files_to_move; ++files_to_move)
1306 current_ptr_ptr = &(arch->archive_next);
1307 while (*current_ptr_ptr)
1309 bfd *current_ptr = *current_ptr_ptr;
1310 if (FILENAME_CMP (normalize (*files_to_move, arch),
1311 current_ptr->filename) == 0)
1313 /* Move this file to the end of the list - first cut from
1316 *current_ptr_ptr = current_ptr->archive_next;
1318 /* Now glue to end */
1319 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1320 link_bfd = *after_bfd;
1321 *after_bfd = current_ptr;
1322 current_ptr->archive_next = link_bfd;
1325 printf ("m - %s\n", *files_to_move);
1330 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1332 /* xgettext:c-format */
1333 fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1338 write_archive (arch);
1341 /* Ought to default to replacing in place, but this is existing practice! */
1344 replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
1346 bfd_boolean changed = FALSE;
1347 bfd **after_bfd; /* New entries go after this one. */
1351 while (files_to_move && *files_to_move)
1355 current_ptr = &arch->archive_next;
1356 while (*current_ptr)
1358 current = *current_ptr;
1360 /* For compatibility with existing ar programs, we
1361 permit the same file to be added multiple times. */
1362 if (FILENAME_CMP (normalize (*files_to_move, arch),
1363 normalize (current->filename, arch)) == 0
1364 && current->arelt_data != NULL)
1368 struct stat fsbuf, asbuf;
1370 if (stat (*files_to_move, &fsbuf) != 0)
1372 if (errno != ENOENT)
1373 bfd_fatal (*files_to_move);
1376 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1377 /* xgettext:c-format */
1378 fatal (_("internal stat error on %s"),
1381 if (fsbuf.st_mtime <= asbuf.st_mtime)
1385 after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
1387 if (ar_emul_replace (after_bfd, *files_to_move,
1390 /* Snip out this entry from the chain. */
1391 *current_ptr = (*current_ptr)->archive_next;
1397 current_ptr = &(current->archive_next);
1401 /* Add to the end of the archive. */
1402 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1404 if (ar_emul_append (after_bfd, *files_to_move, target,
1405 verbose, make_thin_archive))
1414 write_archive (arch);
1416 output_filename = NULL;
1420 ranlib_only (const char *archname)
1424 if (get_file_size (archname) < 1)
1427 arch = open_inarch (archname, (char *) NULL);
1430 write_archive (arch);
1434 /* Update the timestamp of the symbol map of an archive. */
1437 ranlib_touch (const char *archname)
1440 /* I don't think updating works on go32. */
1441 ranlib_only (archname);
1447 if (get_file_size (archname) < 1)
1449 f = open (archname, O_RDWR | O_BINARY, 0);
1452 bfd_set_error (bfd_error_system_call);
1453 bfd_fatal (archname);
1456 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1458 bfd_fatal (archname);
1459 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1461 bfd_nonfatal (archname);
1462 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1464 list_matching_formats (matching);
1470 if (! bfd_has_map (arch))
1471 /* xgettext:c-format */
1472 fatal (_("%s: no archive map to update"), archname);
1475 arch->flags |= BFD_DETERMINISTIC_OUTPUT;
1477 bfd_update_armap_timestamp (arch);
1479 if (! bfd_close (arch))
1480 bfd_fatal (archname);
1485 /* Things which are interesting to map over all or some of the files: */
1488 print_descr (bfd *abfd)
1490 print_arelt_descr (stdout, abfd, verbose);