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 (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
193 head
->archive_pass
= 0;
195 for (; count
> 0; files
++, count
--)
197 bfd_boolean found
= FALSE
;
200 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
202 const char * filename
;
205 /* PR binutils/15796: Once an archive element has been matched
206 do not match it again. If the user provides multiple same-named
207 parameters on the command line their intent is to match multiple
208 same-named entries in the archive, not the same entry multiple
210 if (head
->archive_pass
)
213 filename
= head
->filename
;
214 if (filename
== NULL
)
216 /* Some archive formats don't get the filenames filled in
217 until the elements are opened. */
219 bfd_stat_arch_elt (head
, &buf
);
221 else if (bfd_is_thin_archive (arch
))
223 /* Thin archives store full pathnames. Need to normalize. */
224 filename
= normalize (filename
, arch
);
228 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
231 if (counted_name_mode
232 && match_count
!= counted_name_counter
)
234 /* Counting, and didn't match on count; go on to the
241 head
->archive_pass
= 1;
242 /* PR binutils/15796: Once a file has been matched, do not
243 match any more same-named files in the archive. If the
244 user does want to match multiple same-name files in an
245 archive they should provide multiple same-name parameters
246 to the ar command. */
252 /* xgettext:c-format */
253 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
257 bfd_boolean operation_alters_arch
= FALSE
;
264 #if BFD_SUPPORTS_PLUGINS
265 /* xgettext:c-format */
266 const char *command_line
267 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
268 " [--plugin <name>] [member-name] [count] archive-file file...\n");
271 /* xgettext:c-format */
272 const char *command_line
273 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
274 " [member-name] [count] archive-file file...\n");
276 s
= help
? stdout
: stderr
;
278 fprintf (s
, command_line
, program_name
);
280 /* xgettext:c-format */
281 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
282 fprintf (s
, _(" commands:\n"));
283 fprintf (s
, _(" d - delete file(s) from the archive\n"));
284 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
285 fprintf (s
, _(" p - print file(s) found in the archive\n"));
286 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
287 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
288 fprintf (s
, _(" s - act as ranlib\n"));
289 fprintf (s
, _(" t - display contents of archive\n"));
290 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
291 fprintf (s
, _(" command specific modifiers:\n"));
292 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
293 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
294 if (DEFAULT_AR_DETERMINISTIC
)
297 [D] - use zero for timestamps and uids/gids (default)\n"));
299 [U] - use actual timestamps and uids/gids\n"));
304 [D] - use zero for timestamps and uids/gids\n"));
306 [U] - use actual timestamps and uids/gids (default)\n"));
308 fprintf (s
, _(" [N] - use instance [count] of name\n"));
309 fprintf (s
, _(" [f] - truncate inserted file names\n"));
310 fprintf (s
, _(" [P] - use full path names when matching\n"));
311 fprintf (s
, _(" [o] - preserve original dates\n"));
312 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
313 fprintf (s
, _(" generic modifiers:\n"));
314 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
315 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
316 fprintf (s
, _(" [S] - do not build a symbol table\n"));
317 fprintf (s
, _(" [T] - make a thin archive\n"));
318 fprintf (s
, _(" [v] - be verbose\n"));
319 fprintf (s
, _(" [V] - display the version number\n"));
320 fprintf (s
, _(" @<file> - read options from <file>\n"));
321 fprintf (s
, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
322 #if BFD_SUPPORTS_PLUGINS
323 fprintf (s
, _(" optional:\n"));
324 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
329 list_supported_targets (program_name
, s
);
331 if (REPORT_BUGS_TO
[0] && help
)
332 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
334 xexit (help
? 0 : 1);
338 ranlib_usage (int help
)
342 s
= help
? stdout
: stderr
;
344 /* xgettext:c-format */
345 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
346 fprintf (s
, _(" Generate an index to speed access to archives\n"));
347 fprintf (s
, _(" The options are:\n\
348 @<file> Read options from <file>\n"));
349 #if BFD_SUPPORTS_PLUGINS
351 --plugin <name> Load the specified plugin\n"));
353 if (DEFAULT_AR_DETERMINISTIC
)
355 -D Use zero for symbol map timestamp (default)\n\
356 -U Use an actual symbol map timestamp\n"));
359 -D Use zero for symbol map timestamp\n\
360 -U Use actual symbol map timestamp (default)\n"));
362 -t Update the archive's symbol map timestamp\n\
363 -h --help Print this help message\n\
364 -v --version Print version information\n"));
366 list_supported_targets (program_name
, s
);
368 if (REPORT_BUGS_TO
[0] && help
)
369 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
371 xexit (help
? 0 : 1);
374 /* Normalize a file name specified on the command line into a file
375 name which we will use in an archive. */
378 normalize (const char *file
, bfd
*abfd
)
380 const char *filename
;
385 filename
= lbasename (file
);
389 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
394 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
395 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
396 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
403 /* Remove any output file. This is only called via xatexit. */
405 static const char *output_filename
= NULL
;
406 static FILE *output_file
= NULL
;
407 static bfd
*output_bfd
= NULL
;
412 if (output_filename
!= NULL
)
414 if (output_bfd
!= NULL
)
415 bfd_cache_close (output_bfd
);
416 if (output_file
!= NULL
)
417 fclose (output_file
);
418 unlink_if_ordinary (output_filename
);
423 decode_options (int argc
, char **argv
)
427 /* Convert old-style tar call by exploding option element and rearranging
428 options accordingly. */
430 if (argc
> 1 && argv
[1][0] != '-')
432 int new_argc
; /* argc value for rearranged arguments */
433 char **new_argv
; /* argv value for rearranged arguments */
434 char *const *in
; /* cursor into original argv */
435 char **out
; /* cursor into rearranged argv */
436 const char *letter
; /* cursor into old option letters */
437 char buffer
[3]; /* constructed option buffer */
439 /* Initialize a constructed option. */
444 /* Allocate a new argument array, and copy program name in it. */
446 new_argc
= argc
- 1 + strlen (argv
[1]);
447 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
452 /* Copy each old letter option as a separate option. */
454 for (letter
= *in
++; *letter
; letter
++)
457 *out
++ = xstrdup (buffer
);
460 /* Copy all remaining options. */
462 while (in
< argv
+ argc
)
466 /* Replace the old option list by the new one. */
472 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxlcoVsSuvabiMNfPTDU",
473 long_options
, NULL
)) != EOF
)
484 if (operation
!= none
)
485 fatal (_("two different operation options specified"));
496 operation_alters_arch
= TRUE
;
500 operation_alters_arch
= TRUE
;
503 operation
= print_files
;
506 operation
= quick_append
;
507 operation_alters_arch
= TRUE
;
511 operation_alters_arch
= TRUE
;
514 operation
= print_table
;
546 postype
= pos_before
;
549 postype
= pos_before
;
555 counted_name_mode
= TRUE
;
561 full_pathname
= TRUE
;
564 make_thin_archive
= TRUE
;
567 deterministic
= TRUE
;
570 deterministic
= FALSE
;
573 #if BFD_SUPPORTS_PLUGINS
574 plugin_target
= "plugin";
575 bfd_plugin_set_plugin (optarg
);
577 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
584 case 0: /* A long option that just sets a flag. */
591 return &argv
[optind
];
594 /* If neither -D nor -U was specified explicitly,
595 then use the configured default. */
597 default_deterministic (void)
599 if (deterministic
< 0)
600 deterministic
= DEFAULT_AR_DETERMINISTIC
;
604 ranlib_main (int argc
, char **argv
)
606 int arg_index
, status
= 0;
607 bfd_boolean touch
= FALSE
;
610 while ((c
= getopt_long (argc
, argv
, "DhHUvVt", long_options
, NULL
)) != EOF
)
615 deterministic
= TRUE
;
618 deterministic
= FALSE
;
632 /* PR binutils/13493: Support plugins. */
634 #if BFD_SUPPORTS_PLUGINS
635 plugin_target
= "plugin";
636 bfd_plugin_set_plugin (optarg
);
638 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
652 print_version ("ranlib");
654 default_deterministic ();
658 while (arg_index
< argc
)
661 status
|= ranlib_only (argv
[arg_index
]);
663 status
|= ranlib_touch (argv
[arg_index
]);
670 int main (int, char **);
673 main (int argc
, char **argv
)
678 char *inarch_filename
;
681 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
682 setlocale (LC_MESSAGES
, "");
684 #if defined (HAVE_SETLOCALE)
685 setlocale (LC_CTYPE
, "");
687 bindtextdomain (PACKAGE
, LOCALEDIR
);
688 textdomain (PACKAGE
);
690 program_name
= argv
[0];
691 xmalloc_set_program_name (program_name
);
692 #if BFD_SUPPORTS_PLUGINS
693 bfd_plugin_set_program_name (program_name
);
696 expandargv (&argc
, &argv
);
700 const char *temp
= lbasename (program_name
);
702 if (strlen (temp
) >= 6
703 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
709 START_PROGRESS (program_name
, 0);
712 set_default_bfd_target ();
714 xatexit (remove_output
);
716 for (i
= 1; i
< argc
; i
++)
717 if (! ar_emul_parse_arg (argv
[i
]))
723 ranlib_main (argc
, argv
);
728 argv
= decode_options (argc
, argv
);
734 print_version ("ar");
746 /* We don't use do_quick_append any more. Too many systems
747 expect ar to always rebuild the symbol table even when q is
750 /* We can't write an armap when using ar q, so just do ar r
752 if (operation
== quick_append
&& write_armap
)
755 if ((operation
== none
|| operation
== print_table
)
757 xexit (ranlib_only (argv
[arg_index
]));
759 if (operation
== none
)
760 fatal (_("no operation specified"));
762 if (newer_only
&& operation
!= replace
)
763 fatal (_("`u' is only meaningful with the `r' option."));
765 if (newer_only
&& deterministic
> 0)
766 fatal (_("`u' is not meaningful with the `D' option."));
768 if (newer_only
&& deterministic
< 0 && DEFAULT_AR_DETERMINISTIC
)
770 `u' modifier ignored since `D' is the default (see `U')"));
772 default_deterministic ();
774 if (postype
!= pos_default
)
775 posname
= argv
[arg_index
++];
777 if (counted_name_mode
)
779 if (operation
!= extract
&& operation
!= del
)
780 fatal (_("`N' is only meaningful with the `x' and `d' options."));
781 counted_name_counter
= atoi (argv
[arg_index
++]);
782 if (counted_name_counter
<= 0)
783 fatal (_("Value for `N' must be positive."));
786 inarch_filename
= argv
[arg_index
++];
788 for (file_count
= 0; argv
[arg_index
+ file_count
] != NULL
; file_count
++)
791 files
= (file_count
> 0) ? argv
+ arg_index
: NULL
;
793 arch
= open_inarch (inarch_filename
,
794 files
== NULL
? (char *) NULL
: files
[0]);
796 if (operation
== extract
&& bfd_is_thin_archive (arch
))
797 fatal (_("`x' cannot be used on thin archives."));
802 map_over_members (arch
, print_descr
, files
, file_count
);
806 map_over_members (arch
, print_contents
, files
, file_count
);
810 map_over_members (arch
, extract_file
, files
, file_count
);
815 delete_members (arch
, files
);
817 output_filename
= NULL
;
821 /* PR 12558: Creating and moving at the same time does
822 not make sense. Just create the archive instead. */
826 move_members (arch
, files
);
828 output_filename
= NULL
;
835 if (files
!= NULL
|| write_armap
> 0)
836 replace_members (arch
, files
, operation
== quick_append
);
838 output_filename
= NULL
;
841 /* Shouldn't happen! */
843 /* xgettext:c-format */
844 fatal (_("internal error -- this option not implemented"));
848 END_PROGRESS (program_name
);
855 open_inarch (const char *archive_filename
, const char *file
)
863 bfd_set_error (bfd_error_no_error
);
866 target
= plugin_target
;
868 if (stat (archive_filename
, &sbuf
) != 0)
870 #if !defined(__GO32__) || defined(__DJGPP__)
872 /* FIXME: I don't understand why this fragment was ifndef'ed
873 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
874 stat() works just fine in v2.x, so I think this should be
875 removed. For now, I enable it for DJGPP v2. -- EZ. */
877 /* KLUDGE ALERT! Temporary fix until I figger why
878 stat() is wrong ... think it's buried in GO32's IDT - Jax */
880 bfd_fatal (archive_filename
);
883 if (!operation_alters_arch
)
885 fprintf (stderr
, "%s: ", program_name
);
886 perror (archive_filename
);
891 /* If the target isn't set, try to figure out the target to use
892 for the archive from the first object on the list. */
893 if (target
== NULL
&& file
!= NULL
)
897 obj
= bfd_openr (file
, target
);
900 if (bfd_check_format (obj
, bfd_object
))
901 target
= bfd_get_target (obj
);
902 (void) bfd_close (obj
);
906 /* Create an empty archive. */
907 arch
= bfd_openw (archive_filename
, target
);
909 || ! bfd_set_format (arch
, bfd_archive
)
910 || ! bfd_close (arch
))
911 bfd_fatal (archive_filename
);
912 else if (!silent_create
)
913 non_fatal (_("creating %s"), archive_filename
);
915 /* If we die creating a new archive, don't leave it around. */
916 output_filename
= archive_filename
;
919 arch
= bfd_openr (archive_filename
, target
);
923 bfd_fatal (archive_filename
);
926 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
928 bfd_nonfatal (archive_filename
);
929 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
931 list_matching_formats (matching
);
937 if ((operation
== replace
|| operation
== quick_append
)
938 && bfd_openr_next_archived_file (arch
, NULL
) != NULL
)
940 /* PR 15140: Catch attempts to convert a normal
941 archive into a thin archive or vice versa. */
942 if (make_thin_archive
&& ! bfd_is_thin_archive (arch
))
944 fatal (_("Cannot convert existing library %s to thin format"),
945 bfd_get_filename (arch
));
948 else if (! make_thin_archive
&& bfd_is_thin_archive (arch
))
950 fatal (_("Cannot convert existing thin library %s to normal format"),
951 bfd_get_filename (arch
));
956 last_one
= &(arch
->archive_next
);
957 /* Read all the contents right away, regardless. */
958 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
960 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
963 *last_one
= next_one
;
964 last_one
= &next_one
->archive_next
;
966 *last_one
= (bfd
*) NULL
;
967 if (bfd_get_error () != bfd_error_no_more_archived_files
)
973 print_contents (bfd
*abfd
)
975 bfd_size_type ncopied
= 0;
977 char *cbuf
= (char *) xmalloc (BUFSIZE
);
980 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
981 /* xgettext:c-format */
982 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
985 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
987 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
990 while (ncopied
< size
)
993 bfd_size_type tocopy
= size
- ncopied
;
995 if (tocopy
> BUFSIZE
)
998 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1000 /* xgettext:c-format */
1001 fatal (_("%s is not a valid archive"),
1002 bfd_get_filename (bfd_my_archive (abfd
)));
1004 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
1005 return value to bfd_size_type to avoid comparison between signed and
1007 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
1008 fatal ("stdout: %s", strerror (errno
));
1014 /* Extract a member of the archive into its own file.
1016 We defer opening the new file until after we have read a BUFSIZ chunk of the
1017 old one, since we know we have just read the archive header for the old
1018 one. Since most members are shorter than BUFSIZ, this means we will read
1019 the old header, read the old data, write a new inode for the new file, and
1020 write the new data, and be done. This 'optimization' is what comes from
1021 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1025 extract_file (bfd
*abfd
)
1028 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1029 bfd_size_type nread
, tocopy
;
1030 bfd_size_type ncopied
= 0;
1034 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1035 /* xgettext:c-format */
1036 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1040 printf ("x - %s\n", bfd_get_filename (abfd
));
1042 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1047 /* Seems like an abstraction violation, eh? Well it's OK! */
1048 output_filename
= bfd_get_filename (abfd
);
1050 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
1051 if (ostream
== NULL
)
1053 perror (bfd_get_filename (abfd
));
1057 output_file
= ostream
;
1060 while (ncopied
< size
)
1062 tocopy
= size
- ncopied
;
1063 if (tocopy
> BUFSIZE
)
1066 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1067 if (nread
!= tocopy
)
1068 /* xgettext:c-format */
1069 fatal (_("%s is not a valid archive"),
1070 bfd_get_filename (bfd_my_archive (abfd
)));
1072 /* See comment above; this saves disk arm motion */
1073 if (ostream
== NULL
)
1075 /* Seems like an abstraction violation, eh? Well it's OK! */
1076 output_filename
= bfd_get_filename (abfd
);
1078 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
1079 if (ostream
== NULL
)
1081 perror (bfd_get_filename (abfd
));
1085 output_file
= ostream
;
1088 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1089 the return value to bfd_size_type to avoid comparison between
1090 signed and unsigned values. */
1091 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
1092 fatal ("%s: %s", output_filename
, strerror (errno
));
1096 if (ostream
!= NULL
)
1100 output_filename
= NULL
;
1102 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
1106 /* Set access time to modification time. Only st_mtime is
1107 initialized by bfd_stat_arch_elt. */
1108 buf
.st_atime
= buf
.st_mtime
;
1109 set_times (bfd_get_filename (abfd
), &buf
);
1116 write_archive (bfd
*iarch
)
1119 char *old_name
, *new_name
;
1120 bfd
*contents_head
= iarch
->archive_next
;
1122 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1123 strcpy (old_name
, bfd_get_filename (iarch
));
1124 new_name
= make_tempname (old_name
);
1126 if (new_name
== NULL
)
1127 bfd_fatal (_("could not create temporary file whilst writing archive"));
1129 output_filename
= new_name
;
1131 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1134 bfd_fatal (old_name
);
1138 bfd_set_format (obfd
, bfd_archive
);
1140 /* Request writing the archive symbol table unless we've
1141 been explicitly requested not to. */
1142 obfd
->has_armap
= write_armap
>= 0;
1146 /* This should really use bfd_set_file_flags, but that rejects
1148 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1152 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1154 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1155 bfd_is_thin_archive (obfd
) = 1;
1157 if (!bfd_set_archive_head (obfd
, contents_head
))
1158 bfd_fatal (old_name
);
1160 if (!bfd_close (obfd
))
1161 bfd_fatal (old_name
);
1164 output_filename
= NULL
;
1166 /* We don't care if this fails; we might be creating the archive. */
1169 if (smart_rename (new_name
, old_name
, 0) != 0)
1174 /* Return a pointer to the pointer to the entry which should be rplacd'd
1175 into when altering. DEFAULT_POS should be how to interpret pos_default,
1176 and should be a pos value. */
1179 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1181 bfd
**after_bfd
= contents
;
1183 const char *realposname
;
1185 if (postype
== pos_default
)
1187 realpos
= default_pos
;
1188 realposname
= default_posname
;
1193 realposname
= posname
;
1196 if (realpos
== pos_end
)
1199 after_bfd
= &((*after_bfd
)->archive_next
);
1203 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1204 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1206 if (realpos
== pos_after
)
1207 after_bfd
= &(*after_bfd
)->archive_next
;
1215 delete_members (bfd
*arch
, char **files_to_delete
)
1217 bfd
**current_ptr_ptr
;
1219 bfd_boolean something_changed
= FALSE
;
1222 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1224 /* In a.out systems, the armap is optional. It's also called
1225 __.SYMDEF. So if the user asked to delete it, we should remember
1226 that fact. This isn't quite right for COFF systems (where
1227 __.SYMDEF might be regular member), but it's very unlikely
1228 to be a problem. FIXME */
1230 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1232 arch
->has_armap
= FALSE
;
1239 current_ptr_ptr
= &(arch
->archive_next
);
1240 while (*current_ptr_ptr
)
1242 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1243 (*current_ptr_ptr
)->filename
) == 0)
1246 if (counted_name_mode
1247 && match_count
!= counted_name_counter
)
1249 /* Counting, and didn't match on count; go on to the
1255 something_changed
= TRUE
;
1259 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1264 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1267 if (verbose
&& !found
)
1269 /* xgettext:c-format */
1270 printf (_("No member named `%s'\n"), *files_to_delete
);
1276 if (something_changed
)
1277 write_archive (arch
);
1279 output_filename
= NULL
;
1283 /* Reposition existing members within an archive */
1286 move_members (bfd
*arch
, char **files_to_move
)
1288 bfd
**after_bfd
; /* New entries go after this one */
1289 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1291 for (; *files_to_move
; ++files_to_move
)
1293 current_ptr_ptr
= &(arch
->archive_next
);
1294 while (*current_ptr_ptr
)
1296 bfd
*current_ptr
= *current_ptr_ptr
;
1297 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1298 current_ptr
->filename
) == 0)
1300 /* Move this file to the end of the list - first cut from
1303 *current_ptr_ptr
= current_ptr
->archive_next
;
1305 /* Now glue to end */
1306 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1307 link_bfd
= *after_bfd
;
1308 *after_bfd
= current_ptr
;
1309 current_ptr
->archive_next
= link_bfd
;
1312 printf ("m - %s\n", *files_to_move
);
1317 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1319 /* xgettext:c-format */
1320 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1325 write_archive (arch
);
1328 /* Ought to default to replacing in place, but this is existing practice! */
1331 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1333 bfd_boolean changed
= FALSE
;
1334 bfd
**after_bfd
; /* New entries go after this one. */
1338 while (files_to_move
&& *files_to_move
)
1342 current_ptr
= &arch
->archive_next
;
1343 while (*current_ptr
)
1345 current
= *current_ptr
;
1347 /* For compatibility with existing ar programs, we
1348 permit the same file to be added multiple times. */
1349 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1350 normalize (current
->filename
, arch
)) == 0
1351 && current
->arelt_data
!= NULL
)
1355 struct stat fsbuf
, asbuf
;
1357 if (stat (*files_to_move
, &fsbuf
) != 0)
1359 if (errno
!= ENOENT
)
1360 bfd_fatal (*files_to_move
);
1363 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1364 /* xgettext:c-format */
1365 fatal (_("internal stat error on %s"),
1368 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1372 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1374 if (ar_emul_replace (after_bfd
, *files_to_move
,
1377 /* Snip out this entry from the chain. */
1378 *current_ptr
= (*current_ptr
)->archive_next
;
1384 current_ptr
= &(current
->archive_next
);
1388 /* Add to the end of the archive. */
1389 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1391 if (ar_emul_append (after_bfd
, *files_to_move
, target
,
1392 verbose
, make_thin_archive
))
1401 write_archive (arch
);
1403 output_filename
= NULL
;
1407 ranlib_only (const char *archname
)
1411 if (get_file_size (archname
) < 1)
1414 arch
= open_inarch (archname
, (char *) NULL
);
1417 write_archive (arch
);
1421 /* Update the timestamp of the symbol map of an archive. */
1424 ranlib_touch (const char *archname
)
1427 /* I don't think updating works on go32. */
1428 ranlib_only (archname
);
1434 if (get_file_size (archname
) < 1)
1436 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1439 bfd_set_error (bfd_error_system_call
);
1440 bfd_fatal (archname
);
1443 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1445 bfd_fatal (archname
);
1446 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1448 bfd_nonfatal (archname
);
1449 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1451 list_matching_formats (matching
);
1457 if (! bfd_has_map (arch
))
1458 /* xgettext:c-format */
1459 fatal (_("%s: no archive map to update"), archname
);
1462 arch
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1464 bfd_update_armap_timestamp (arch
);
1466 if (! bfd_close (arch
))
1467 bfd_fatal (archname
);
1472 /* Things which are interesting to map over all or some of the files: */
1475 print_descr (bfd
*abfd
)
1477 print_arelt_descr (stdout
, abfd
, verbose
);