1 /* ar.c - Archive modify and extract.
2 Copyright (C) 1991-2019 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"
36 #include "plugin-api.h"
41 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
43 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
46 /* Static declarations. */
48 static void mri_emul (void);
49 static const char *normalize (const char *, bfd
*);
50 static void remove_output (void);
51 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
52 static void print_contents (bfd
* member
);
53 static void delete_members (bfd
*, char **files_to_delete
);
55 static void move_members (bfd
*, char **files_to_move
);
56 static void replace_members
57 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
58 static void print_descr (bfd
* abfd
);
59 static void write_archive (bfd
*);
60 static int ranlib_only (const char *archname
);
61 static int ranlib_touch (const char *archname
);
62 static void usage (int);
64 /** Globals and flags. */
68 /* This flag distinguishes between ar and ranlib:
69 1 means this is 'ranlib'; 0 means this is 'ar'.
70 -1 means if we should use argv[0] to decide. */
73 /* Nonzero means don't warn about creating the archive file if necessary. */
74 int silent_create
= 0;
76 /* Nonzero means describe each action performed. */
79 /* Nonzero means display offsets of files in the archive. */
80 int display_offsets
= 0;
82 /* Nonzero means preserve dates of members when extracting them. */
83 int preserve_dates
= 0;
85 /* Nonzero means don't replace existing members whose dates are more recent
86 than the corresponding files. */
89 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
90 member). -1 means we've been explicitly asked to not write a symbol table;
91 +1 means we've been explicitly asked to write it;
93 Traditionally, the default in BSD has been to not write the table.
94 However, for POSIX.2 compliance the default is now to write a symbol table
95 if any of the members are object files. */
98 /* Operate in deterministic mode: write zero for timestamps, uids,
99 and gids for archive members and the archive symbol table, and write
100 consistent file modes. */
101 int deterministic
= -1; /* Determinism indeterminate. */
103 /* Nonzero means it's the name of an existing member; position new or moved
104 files with respect to this one. */
105 char *posname
= NULL
;
107 /* Sez how to use `posname': pos_before means position before that member.
108 pos_after means position after that member. pos_end means always at end.
109 pos_default means default appropriately. For the latter two, `posname'
110 should also be zero. */
113 pos_default
, pos_before
, pos_after
, pos_end
114 } postype
= pos_default
;
118 none
= 0, del
, replace
, print_table
,
119 print_files
, extract
, move
, quick_append
123 get_pos_bfd (bfd
**, enum pos
, const char *);
125 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
126 extract the COUNTED_NAME_COUNTER instance of that name. */
127 static bfd_boolean counted_name_mode
= 0;
128 static int counted_name_counter
= 0;
130 /* Whether to truncate names of files stored in the archive. */
131 static bfd_boolean ar_truncate
= FALSE
;
133 /* Whether to use a full file name match when searching an archive.
134 This is convenient for archives created by the Microsoft lib
136 static bfd_boolean full_pathname
= FALSE
;
138 /* Whether to create a "thin" archive (symbol index only -- no files). */
139 static bfd_boolean make_thin_archive
= FALSE
;
141 static int show_version
= 0;
143 static int show_help
= 0;
145 #if BFD_SUPPORTS_PLUGINS
146 static const char *plugin_target
= "plugin";
148 static const char *plugin_target
= NULL
;
151 static const char *target
= NULL
;
153 enum long_option_numbers
160 static const char * output_dir
= NULL
;
162 static struct option long_options
[] =
164 {"help", no_argument
, &show_help
, 1},
165 {"plugin", required_argument
, NULL
, OPTION_PLUGIN
},
166 {"target", required_argument
, NULL
, OPTION_TARGET
},
167 {"version", no_argument
, &show_version
, 1},
168 {"output", required_argument
, NULL
, OPTION_OUTPUT
},
169 {NULL
, no_argument
, NULL
, 0}
177 interactive
= isatty (fileno (stdin
));
181 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
182 COUNT is the length of the FILES chain; FUNCTION is called on each entry
183 whose name matches one in FILES. */
186 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
193 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
201 /* This may appear to be a baroque way of accomplishing what we want.
202 However we have to iterate over the filenames in order to notice where
203 a filename is requested but does not exist in the archive. Ditto
204 mapping over each file each time -- we want to hack multiple
207 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
208 head
->archive_pass
= 0;
210 for (; count
> 0; files
++, count
--)
212 bfd_boolean found
= FALSE
;
215 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
217 const char * filename
;
220 /* PR binutils/15796: Once an archive element has been matched
221 do not match it again. If the user provides multiple same-named
222 parameters on the command line their intent is to match multiple
223 same-named entries in the archive, not the same entry multiple
225 if (head
->archive_pass
)
228 filename
= head
->filename
;
229 if (filename
== NULL
)
231 /* Some archive formats don't get the filenames filled in
232 until the elements are opened. */
234 bfd_stat_arch_elt (head
, &buf
);
236 else if (bfd_is_thin_archive (arch
))
238 /* Thin archives store full pathnames. Need to normalize. */
239 filename
= normalize (filename
, arch
);
243 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
246 if (counted_name_mode
247 && match_count
!= counted_name_counter
)
249 /* Counting, and didn't match on count; go on to the
256 head
->archive_pass
= 1;
257 /* PR binutils/15796: Once a file has been matched, do not
258 match any more same-named files in the archive. If the
259 user does want to match multiple same-name files in an
260 archive they should provide multiple same-name parameters
261 to the ar command. */
267 /* xgettext:c-format */
268 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
272 bfd_boolean operation_alters_arch
= FALSE
;
279 #if BFD_SUPPORTS_PLUGINS
280 /* xgettext:c-format */
281 const char *command_line
282 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]"
283 " [--plugin <name>] [member-name] [count] archive-file file...\n");
286 /* xgettext:c-format */
287 const char *command_line
288 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]"
289 " [member-name] [count] archive-file file...\n");
291 s
= help
? stdout
: stderr
;
293 fprintf (s
, command_line
, program_name
);
295 /* xgettext:c-format */
296 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
297 fprintf (s
, _(" commands:\n"));
298 fprintf (s
, _(" d - delete file(s) from the archive\n"));
299 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
300 fprintf (s
, _(" p - print file(s) found in the archive\n"));
301 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
302 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
303 fprintf (s
, _(" s - act as ranlib\n"));
304 fprintf (s
, _(" t[O][v] - display contents of the archive\n"));
305 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
306 fprintf (s
, _(" command specific modifiers:\n"));
307 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
308 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
309 if (DEFAULT_AR_DETERMINISTIC
)
312 [D] - use zero for timestamps and uids/gids (default)\n"));
314 [U] - use actual timestamps and uids/gids\n"));
319 [D] - use zero for timestamps and uids/gids\n"));
321 [U] - use actual timestamps and uids/gids (default)\n"));
323 fprintf (s
, _(" [N] - use instance [count] of name\n"));
324 fprintf (s
, _(" [f] - truncate inserted file names\n"));
325 fprintf (s
, _(" [P] - use full path names when matching\n"));
326 fprintf (s
, _(" [o] - preserve original dates\n"));
327 fprintf (s
, _(" [O] - display offsets of files in the archive\n"));
328 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
329 fprintf (s
, _(" generic modifiers:\n"));
330 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
331 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
332 fprintf (s
, _(" [S] - do not build a symbol table\n"));
333 fprintf (s
, _(" [T] - make a thin archive\n"));
334 fprintf (s
, _(" [v] - be verbose\n"));
335 fprintf (s
, _(" [V] - display the version number\n"));
336 fprintf (s
, _(" @<file> - read options from <file>\n"));
337 fprintf (s
, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
338 fprintf (s
, _(" --output=DIRNAME - specify the output directory for extraction operations\n"));
339 #if BFD_SUPPORTS_PLUGINS
340 fprintf (s
, _(" optional:\n"));
341 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
346 list_supported_targets (program_name
, s
);
348 if (REPORT_BUGS_TO
[0] && help
)
349 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
351 xexit (help
? 0 : 1);
355 ranlib_usage (int help
)
359 s
= help
? stdout
: stderr
;
361 /* xgettext:c-format */
362 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
363 fprintf (s
, _(" Generate an index to speed access to archives\n"));
364 fprintf (s
, _(" The options are:\n\
365 @<file> Read options from <file>\n"));
366 #if BFD_SUPPORTS_PLUGINS
368 --plugin <name> Load the specified plugin\n"));
370 if (DEFAULT_AR_DETERMINISTIC
)
372 -D Use zero for symbol map timestamp (default)\n\
373 -U Use an actual symbol map timestamp\n"));
376 -D Use zero for symbol map timestamp\n\
377 -U Use actual symbol map timestamp (default)\n"));
379 -t Update the archive's symbol map timestamp\n\
380 -h --help Print this help message\n\
381 -v --version Print version information\n"));
383 list_supported_targets (program_name
, s
);
385 if (REPORT_BUGS_TO
[0] && help
)
386 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
388 xexit (help
? 0 : 1);
391 /* Normalize a file name specified on the command line into a file
392 name which we will use in an archive. */
395 normalize (const char *file
, bfd
*abfd
)
397 const char *filename
;
402 filename
= lbasename (file
);
406 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
411 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
412 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
413 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
420 /* Remove any output file. This is only called via xatexit. */
422 static const char *output_filename
= NULL
;
423 static FILE *output_file
= NULL
;
424 static bfd
*output_bfd
= NULL
;
429 if (output_filename
!= NULL
)
431 if (output_bfd
!= NULL
)
432 bfd_cache_close (output_bfd
);
433 if (output_file
!= NULL
)
434 fclose (output_file
);
435 unlink_if_ordinary (output_filename
);
440 decode_options (int argc
, char **argv
)
444 /* Convert old-style ar call by exploding option element and rearranging
445 options accordingly. */
448 if (argc
> 1 && argv
[1][0] != '-')
450 int new_argc
; /* argc value for rearranged arguments */
451 char **new_argv
; /* argv value for rearranged arguments */
452 char *const *in
; /* cursor into original argv */
453 char **out
; /* cursor into rearranged argv */
454 const char *letter
; /* cursor into old option letters */
455 char buffer
[3]; /* constructed option buffer */
457 /* Initialize a constructed option. */
462 /* Allocate a new argument array, and copy program name in it. */
464 new_argc
= argc
- 1 + strlen (argv
[1]);
465 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
470 /* Copy each old letter option as a separate option. */
472 for (letter
= *in
++; *letter
; letter
++)
475 *out
++ = xstrdup (buffer
);
478 /* Copy all remaining options. */
480 while (in
< argv
+ argc
)
484 /* Replace the old option list by the new one. */
490 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxlcoOVsSuvabiMNfPTDU",
491 long_options
, NULL
)) != EOF
)
502 if (operation
!= none
)
503 fatal (_("two different operation options specified"));
514 operation_alters_arch
= TRUE
;
518 operation_alters_arch
= TRUE
;
521 operation
= print_files
;
524 operation
= quick_append
;
525 operation_alters_arch
= TRUE
;
529 operation_alters_arch
= TRUE
;
532 operation
= print_table
;
567 postype
= pos_before
;
570 postype
= pos_before
;
576 counted_name_mode
= TRUE
;
582 full_pathname
= TRUE
;
585 make_thin_archive
= TRUE
;
588 deterministic
= TRUE
;
591 deterministic
= FALSE
;
594 #if BFD_SUPPORTS_PLUGINS
595 bfd_plugin_set_plugin (optarg
);
597 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
607 case 0: /* A long option that just sets a flag. */
614 /* PR 13256: Allow for the possibility that the first command line option
615 started with a dash (eg --plugin) but then the following option(s) are
616 old style, non-dash-prefixed versions. */
617 if (operation
== none
&& write_armap
!= 1 && !mri_mode
618 && optind
> 0 && optind
< argc
)
620 argv
+= (optind
- 1);
621 argc
-= (optind
- 1);
626 return &argv
[optind
];
629 /* If neither -D nor -U was specified explicitly,
630 then use the configured default. */
632 default_deterministic (void)
634 if (deterministic
< 0)
635 deterministic
= DEFAULT_AR_DETERMINISTIC
;
639 ranlib_main (int argc
, char **argv
)
641 int arg_index
, status
= 0;
642 bfd_boolean touch
= FALSE
;
645 while ((c
= getopt_long (argc
, argv
, "DhHUvVt", long_options
, NULL
)) != EOF
)
650 deterministic
= TRUE
;
653 deterministic
= FALSE
;
667 /* PR binutils/13493: Support plugins. */
669 #if BFD_SUPPORTS_PLUGINS
670 bfd_plugin_set_plugin (optarg
);
672 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
686 print_version ("ranlib");
688 default_deterministic ();
692 while (arg_index
< argc
)
695 status
|= ranlib_only (argv
[arg_index
]);
697 status
|= ranlib_touch (argv
[arg_index
]);
704 int main (int, char **);
707 main (int argc
, char **argv
)
712 char *inarch_filename
;
715 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
716 setlocale (LC_MESSAGES
, "");
718 #if defined (HAVE_SETLOCALE)
719 setlocale (LC_CTYPE
, "");
721 bindtextdomain (PACKAGE
, LOCALEDIR
);
722 textdomain (PACKAGE
);
724 program_name
= argv
[0];
725 xmalloc_set_program_name (program_name
);
726 bfd_set_error_program_name (program_name
);
727 #if BFD_SUPPORTS_PLUGINS
728 bfd_plugin_set_program_name (program_name
);
731 expandargv (&argc
, &argv
);
735 const char *temp
= lbasename (program_name
);
737 if (strlen (temp
) >= 6
738 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
744 START_PROGRESS (program_name
, 0);
746 if (bfd_init () != BFD_INIT_MAGIC
)
747 fatal (_("fatal error: libbfd ABI mismatch"));
748 set_default_bfd_target ();
750 xatexit (remove_output
);
752 for (i
= 1; i
< argc
; i
++)
753 if (! ar_emul_parse_arg (argv
[i
]))
759 ranlib_main (argc
, argv
);
764 argv
= decode_options (argc
, argv
);
770 print_version ("ar");
776 default_deterministic ();
783 /* Fail if no files are specified on the command line.
784 (But not for MRI mode which allows for reading arguments
785 and filenames from stdin). */
786 if (argv
[arg_index
] == NULL
)
789 /* We don't use do_quick_append any more. Too many systems
790 expect ar to always rebuild the symbol table even when q is
793 /* We can't write an armap when using ar q, so just do ar r
795 if (operation
== quick_append
&& write_armap
)
798 if ((operation
== none
|| operation
== print_table
)
800 xexit (ranlib_only (argv
[arg_index
]));
802 if (operation
== none
)
803 fatal (_("no operation specified"));
805 if (newer_only
&& operation
!= replace
)
806 fatal (_("`u' is only meaningful with the `r' option."));
808 if (newer_only
&& deterministic
> 0)
809 fatal (_("`u' is not meaningful with the `D' option."));
811 if (newer_only
&& deterministic
< 0 && DEFAULT_AR_DETERMINISTIC
)
813 `u' modifier ignored since `D' is the default (see `U')"));
815 default_deterministic ();
817 if (postype
!= pos_default
)
819 posname
= argv
[arg_index
++];
821 fatal (_("missing position arg."));
824 if (counted_name_mode
)
826 if (operation
!= extract
&& operation
!= del
)
827 fatal (_("`N' is only meaningful with the `x' and `d' options."));
828 if (argv
[arg_index
] == NULL
)
829 fatal (_("`N' missing value."));
830 counted_name_counter
= atoi (argv
[arg_index
++]);
831 if (counted_name_counter
<= 0)
832 fatal (_("Value for `N' must be positive."));
835 inarch_filename
= argv
[arg_index
++];
836 if (inarch_filename
== NULL
)
839 for (file_count
= 0; argv
[arg_index
+ file_count
] != NULL
; file_count
++)
842 files
= (file_count
> 0) ? argv
+ arg_index
: NULL
;
844 arch
= open_inarch (inarch_filename
,
845 files
== NULL
? (char *) NULL
: files
[0]);
847 if (operation
== extract
&& bfd_is_thin_archive (arch
))
848 fatal (_("`x' cannot be used on thin archives."));
853 map_over_members (arch
, print_descr
, files
, file_count
);
857 map_over_members (arch
, print_contents
, files
, file_count
);
861 map_over_members (arch
, extract_file
, files
, file_count
);
866 delete_members (arch
, files
);
868 output_filename
= NULL
;
872 /* PR 12558: Creating and moving at the same time does
873 not make sense. Just create the archive instead. */
877 move_members (arch
, files
);
879 output_filename
= NULL
;
886 if (files
!= NULL
|| write_armap
> 0)
887 replace_members (arch
, files
, operation
== quick_append
);
889 output_filename
= NULL
;
892 /* Shouldn't happen! */
894 /* xgettext:c-format */
895 fatal (_("internal error -- this option not implemented"));
899 END_PROGRESS (program_name
);
906 open_inarch (const char *archive_filename
, const char *file
)
914 bfd_set_error (bfd_error_no_error
);
917 target
= plugin_target
;
919 if (stat (archive_filename
, &sbuf
) != 0)
921 #if !defined(__GO32__) || defined(__DJGPP__)
923 /* FIXME: I don't understand why this fragment was ifndef'ed
924 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
925 stat() works just fine in v2.x, so I think this should be
926 removed. For now, I enable it for DJGPP v2. -- EZ. */
928 /* KLUDGE ALERT! Temporary fix until I figger why
929 stat() is wrong ... think it's buried in GO32's IDT - Jax */
931 bfd_fatal (archive_filename
);
934 if (!operation_alters_arch
)
936 fprintf (stderr
, "%s: ", program_name
);
937 perror (archive_filename
);
942 /* If the target isn't set, try to figure out the target to use
943 for the archive from the first object on the list. */
944 if (target
== NULL
&& file
!= NULL
)
948 obj
= bfd_openr (file
, target
);
951 if (bfd_check_format (obj
, bfd_object
))
952 target
= bfd_get_target (obj
);
953 (void) bfd_close (obj
);
957 /* Create an empty archive. */
958 arch
= bfd_openw (archive_filename
, target
);
960 || ! bfd_set_format (arch
, bfd_archive
)
961 || ! bfd_close (arch
))
962 bfd_fatal (archive_filename
);
963 else if (!silent_create
)
964 non_fatal (_("creating %s"), archive_filename
);
966 /* If we die creating a new archive, don't leave it around. */
967 output_filename
= archive_filename
;
970 arch
= bfd_openr (archive_filename
, target
);
974 bfd_fatal (archive_filename
);
977 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
979 bfd_nonfatal (archive_filename
);
980 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
982 list_matching_formats (matching
);
988 if ((operation
== replace
|| operation
== quick_append
)
989 && bfd_openr_next_archived_file (arch
, NULL
) != NULL
)
991 /* PR 15140: Catch attempts to convert a normal
992 archive into a thin archive or vice versa. */
993 if (make_thin_archive
&& ! bfd_is_thin_archive (arch
))
995 fatal (_("Cannot convert existing library %s to thin format"),
996 bfd_get_filename (arch
));
999 else if (! make_thin_archive
&& bfd_is_thin_archive (arch
))
1001 fatal (_("Cannot convert existing thin library %s to normal format"),
1002 bfd_get_filename (arch
));
1007 last_one
= &(arch
->archive_next
);
1008 /* Read all the contents right away, regardless. */
1009 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
1011 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
1014 *last_one
= next_one
;
1015 last_one
= &next_one
->archive_next
;
1017 *last_one
= (bfd
*) NULL
;
1018 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1024 print_contents (bfd
*abfd
)
1026 bfd_size_type ncopied
= 0;
1028 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1031 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1032 /* xgettext:c-format */
1033 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1036 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
1038 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1041 while (ncopied
< size
)
1043 bfd_size_type nread
;
1044 bfd_size_type tocopy
= size
- ncopied
;
1046 if (tocopy
> BUFSIZE
)
1049 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1050 if (nread
!= tocopy
)
1051 /* xgettext:c-format */
1052 fatal (_("%s is not a valid archive"),
1053 bfd_get_filename (abfd
->my_archive
));
1055 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
1056 return value to bfd_size_type to avoid comparison between signed and
1058 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
1059 fatal ("stdout: %s", strerror (errno
));
1066 static FILE * open_output_file (bfd
*) ATTRIBUTE_RETURNS_NONNULL
;
1069 open_output_file (bfd
* abfd
)
1071 output_filename
= bfd_get_filename (abfd
);
1073 /* PR binutils/17533: Do not allow directory traversal
1074 outside of the current directory tree - unless the
1075 user has explicitly specified an output directory. */
1076 if (! is_valid_archive_path (output_filename
))
1078 char * base
= (char *) lbasename (output_filename
);
1080 non_fatal (_("illegal output pathname for archive member: %s, using '%s' instead"),
1081 output_filename
, base
);
1082 output_filename
= base
;
1087 size_t len
= strlen (output_dir
);
1091 /* FIXME: There is a memory leak here, but it is not serious. */
1092 if (IS_DIR_SEPARATOR (output_dir
[len
- 1]))
1093 output_filename
= concat (output_dir
, output_filename
, NULL
);
1095 output_filename
= concat (output_dir
, "/", output_filename
, NULL
);
1100 printf ("x - %s\n", output_filename
);
1102 FILE * ostream
= fopen (output_filename
, FOPEN_WB
);
1103 if (ostream
== NULL
)
1105 perror (output_filename
);
1112 /* Extract a member of the archive into its own file.
1114 We defer opening the new file until after we have read a BUFSIZ chunk of the
1115 old one, since we know we have just read the archive header for the old
1116 one. Since most members are shorter than BUFSIZ, this means we will read
1117 the old header, read the old data, write a new inode for the new file, and
1118 write the new data, and be done. This 'optimization' is what comes from
1119 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1123 extract_file (bfd
*abfd
)
1128 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1129 /* xgettext:c-format */
1130 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1133 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1138 output_file
= open_output_file (abfd
);
1142 bfd_size_type ncopied
= 0;
1143 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1145 while (ncopied
< size
)
1147 bfd_size_type nread
, tocopy
;
1149 tocopy
= size
- ncopied
;
1150 if (tocopy
> BUFSIZE
)
1153 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1154 if (nread
!= tocopy
)
1155 /* xgettext:c-format */
1156 fatal (_("%s is not a valid archive"),
1157 bfd_get_filename (abfd
->my_archive
));
1159 /* See comment above; this saves disk arm motion. */
1160 if (output_file
== NULL
)
1161 output_file
= open_output_file (abfd
);
1163 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1164 the return value to bfd_size_type to avoid comparison between
1165 signed and unsigned values. */
1166 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, output_file
) != nread
)
1167 fatal ("%s: %s", output_filename
, strerror (errno
));
1175 fclose (output_file
);
1179 chmod (output_filename
, buf
.st_mode
);
1183 /* Set access time to modification time. Only st_mtime is
1184 initialized by bfd_stat_arch_elt. */
1185 buf
.st_atime
= buf
.st_mtime
;
1186 set_times (output_filename
, &buf
);
1189 output_filename
= NULL
;
1193 write_archive (bfd
*iarch
)
1196 char *old_name
, *new_name
;
1197 bfd
*contents_head
= iarch
->archive_next
;
1199 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1200 strcpy (old_name
, bfd_get_filename (iarch
));
1201 new_name
= make_tempname (old_name
);
1203 if (new_name
== NULL
)
1204 bfd_fatal (_("could not create temporary file whilst writing archive"));
1206 output_filename
= new_name
;
1208 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1211 bfd_fatal (old_name
);
1215 bfd_set_format (obfd
, bfd_archive
);
1217 /* Request writing the archive symbol table unless we've
1218 been explicitly requested not to. */
1219 obfd
->has_armap
= write_armap
>= 0;
1223 /* This should really use bfd_set_file_flags, but that rejects
1225 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1229 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1232 obfd
->flags
|= BFD_ARCHIVE_FULL_PATH
;
1234 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1235 bfd_set_thin_archive (obfd
, TRUE
);
1237 if (!bfd_set_archive_head (obfd
, contents_head
))
1238 bfd_fatal (old_name
);
1240 if (!bfd_close (obfd
))
1241 bfd_fatal (old_name
);
1244 output_filename
= NULL
;
1246 /* We don't care if this fails; we might be creating the archive. */
1249 if (smart_rename (new_name
, old_name
, 0) != 0)
1255 /* Return a pointer to the pointer to the entry which should be rplacd'd
1256 into when altering. DEFAULT_POS should be how to interpret pos_default,
1257 and should be a pos value. */
1260 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1262 bfd
**after_bfd
= contents
;
1264 const char *realposname
;
1266 if (postype
== pos_default
)
1268 realpos
= default_pos
;
1269 realposname
= default_posname
;
1274 realposname
= posname
;
1277 if (realpos
== pos_end
)
1280 after_bfd
= &((*after_bfd
)->archive_next
);
1284 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1285 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1287 if (realpos
== pos_after
)
1288 after_bfd
= &(*after_bfd
)->archive_next
;
1296 delete_members (bfd
*arch
, char **files_to_delete
)
1298 bfd
**current_ptr_ptr
;
1300 bfd_boolean something_changed
= FALSE
;
1303 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1305 /* In a.out systems, the armap is optional. It's also called
1306 __.SYMDEF. So if the user asked to delete it, we should remember
1307 that fact. This isn't quite right for COFF systems (where
1308 __.SYMDEF might be regular member), but it's very unlikely
1309 to be a problem. FIXME */
1311 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1313 arch
->has_armap
= FALSE
;
1320 current_ptr_ptr
= &(arch
->archive_next
);
1321 while (*current_ptr_ptr
)
1323 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1324 (*current_ptr_ptr
)->filename
) == 0)
1327 if (counted_name_mode
1328 && match_count
!= counted_name_counter
)
1330 /* Counting, and didn't match on count; go on to the
1336 something_changed
= TRUE
;
1340 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1345 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1348 if (verbose
&& !found
)
1350 /* xgettext:c-format */
1351 printf (_("No member named `%s'\n"), *files_to_delete
);
1357 if (something_changed
)
1358 write_archive (arch
);
1360 output_filename
= NULL
;
1364 /* Reposition existing members within an archive */
1367 move_members (bfd
*arch
, char **files_to_move
)
1369 bfd
**after_bfd
; /* New entries go after this one */
1370 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1372 for (; *files_to_move
; ++files_to_move
)
1374 current_ptr_ptr
= &(arch
->archive_next
);
1375 while (*current_ptr_ptr
)
1377 bfd
*current_ptr
= *current_ptr_ptr
;
1378 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1379 current_ptr
->filename
) == 0)
1381 /* Move this file to the end of the list - first cut from
1384 *current_ptr_ptr
= current_ptr
->archive_next
;
1386 /* Now glue to end */
1387 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1388 link_bfd
= *after_bfd
;
1389 *after_bfd
= current_ptr
;
1390 current_ptr
->archive_next
= link_bfd
;
1393 printf ("m - %s\n", *files_to_move
);
1398 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1400 /* xgettext:c-format */
1401 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1406 write_archive (arch
);
1409 /* Ought to default to replacing in place, but this is existing practice! */
1412 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1414 bfd_boolean changed
= FALSE
;
1415 bfd
**after_bfd
; /* New entries go after this one. */
1419 while (files_to_move
&& *files_to_move
)
1423 current_ptr
= &arch
->archive_next
;
1424 while (*current_ptr
)
1426 current
= *current_ptr
;
1428 /* For compatibility with existing ar programs, we
1429 permit the same file to be added multiple times. */
1430 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1431 normalize (current
->filename
, arch
)) == 0
1432 && current
->arelt_data
!= NULL
)
1436 struct stat fsbuf
, asbuf
;
1438 if (stat (*files_to_move
, &fsbuf
) != 0)
1440 if (errno
!= ENOENT
)
1441 bfd_fatal (*files_to_move
);
1444 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1445 /* xgettext:c-format */
1446 fatal (_("internal stat error on %s"),
1449 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1453 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1455 if (ar_emul_replace (after_bfd
, *files_to_move
,
1458 /* Snip out this entry from the chain. */
1459 *current_ptr
= (*current_ptr
)->archive_next
;
1465 current_ptr
= &(current
->archive_next
);
1469 /* Add to the end of the archive. */
1470 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1472 if (ar_emul_append (after_bfd
, *files_to_move
, target
,
1473 verbose
, make_thin_archive
))
1482 write_archive (arch
);
1484 output_filename
= NULL
;
1488 ranlib_only (const char *archname
)
1492 if (get_file_size (archname
) < 1)
1495 arch
= open_inarch (archname
, (char *) NULL
);
1498 write_archive (arch
);
1502 /* Update the timestamp of the symbol map of an archive. */
1505 ranlib_touch (const char *archname
)
1508 /* I don't think updating works on go32. */
1509 ranlib_only (archname
);
1515 if (get_file_size (archname
) < 1)
1517 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1520 bfd_set_error (bfd_error_system_call
);
1521 bfd_fatal (archname
);
1524 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1526 bfd_fatal (archname
);
1527 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1529 bfd_nonfatal (archname
);
1530 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1532 list_matching_formats (matching
);
1538 if (! bfd_has_map (arch
))
1539 /* xgettext:c-format */
1540 fatal (_("%s: no archive map to update"), archname
);
1543 arch
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1545 bfd_update_armap_timestamp (arch
);
1547 if (! bfd_close (arch
))
1548 bfd_fatal (archname
);
1553 /* Things which are interesting to map over all or some of the files: */
1556 print_descr (bfd
*abfd
)
1558 print_arelt_descr (stdout
, abfd
, verbose
, display_offsets
);