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"
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 display offsets of files in the archive. */
79 int display_offsets
= 0;
81 /* Nonzero means preserve dates of members when extracting them. */
82 int preserve_dates
= 0;
84 /* Nonzero means don't replace existing members whose dates are more recent
85 than the corresponding files. */
88 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
89 member). -1 means we've been explicitly asked to not write a symbol table;
90 +1 means we've been explicitly asked to write it;
92 Traditionally, the default in BSD has been to not write the table.
93 However, for POSIX.2 compliance the default is now to write a symbol table
94 if any of the members are object files. */
97 /* Operate in deterministic mode: write zero for timestamps, uids,
98 and gids for archive members and the archive symbol table, and write
99 consistent file modes. */
100 int deterministic
= -1; /* Determinism indeterminate. */
102 /* Nonzero means it's the name of an existing member; position new or moved
103 files with respect to this one. */
104 char *posname
= NULL
;
106 /* Sez how to use `posname': pos_before means position before that member.
107 pos_after means position after that member. pos_end means always at end.
108 pos_default means default appropriately. For the latter two, `posname'
109 should also be zero. */
112 pos_default
, pos_before
, pos_after
, pos_end
113 } postype
= pos_default
;
117 none
= 0, del
, replace
, print_table
,
118 print_files
, extract
, move
, quick_append
122 get_pos_bfd (bfd
**, enum pos
, const char *);
124 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
125 extract the COUNTED_NAME_COUNTER instance of that name. */
126 static bfd_boolean counted_name_mode
= 0;
127 static int counted_name_counter
= 0;
129 /* Whether to truncate names of files stored in the archive. */
130 static bfd_boolean ar_truncate
= FALSE
;
132 /* Whether to use a full file name match when searching an archive.
133 This is convenient for archives created by the Microsoft lib
135 static bfd_boolean full_pathname
= FALSE
;
137 /* Whether to create a "thin" archive (symbol index only -- no files). */
138 static bfd_boolean make_thin_archive
= FALSE
;
140 static int show_version
= 0;
142 static int show_help
= 0;
144 #if BFD_SUPPORTS_PLUGINS
145 static const char *plugin_target
= "plugin";
147 static const char *plugin_target
= NULL
;
150 static const char *target
= NULL
;
152 #define OPTION_PLUGIN 201
153 #define OPTION_TARGET 202
155 static struct option long_options
[] =
157 {"help", no_argument
, &show_help
, 1},
158 {"plugin", required_argument
, NULL
, OPTION_PLUGIN
},
159 {"target", required_argument
, NULL
, OPTION_TARGET
},
160 {"version", no_argument
, &show_version
, 1},
161 {NULL
, no_argument
, NULL
, 0}
169 interactive
= isatty (fileno (stdin
));
173 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
174 COUNT is the length of the FILES chain; FUNCTION is called on each entry
175 whose name matches one in FILES. */
178 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
185 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
193 /* This may appear to be a baroque way of accomplishing what we want.
194 However we have to iterate over the filenames in order to notice where
195 a filename is requested but does not exist in the archive. Ditto
196 mapping over each file each time -- we want to hack multiple
199 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
200 head
->archive_pass
= 0;
202 for (; count
> 0; files
++, count
--)
204 bfd_boolean found
= FALSE
;
207 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
209 const char * filename
;
212 /* PR binutils/15796: Once an archive element has been matched
213 do not match it again. If the user provides multiple same-named
214 parameters on the command line their intent is to match multiple
215 same-named entries in the archive, not the same entry multiple
217 if (head
->archive_pass
)
220 filename
= head
->filename
;
221 if (filename
== NULL
)
223 /* Some archive formats don't get the filenames filled in
224 until the elements are opened. */
226 bfd_stat_arch_elt (head
, &buf
);
228 else if (bfd_is_thin_archive (arch
))
230 /* Thin archives store full pathnames. Need to normalize. */
231 filename
= normalize (filename
, arch
);
235 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
238 if (counted_name_mode
239 && match_count
!= counted_name_counter
)
241 /* Counting, and didn't match on count; go on to the
248 head
->archive_pass
= 1;
249 /* PR binutils/15796: Once a file has been matched, do not
250 match any more same-named files in the archive. If the
251 user does want to match multiple same-name files in an
252 archive they should provide multiple same-name parameters
253 to the ar command. */
259 /* xgettext:c-format */
260 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
264 bfd_boolean operation_alters_arch
= FALSE
;
271 #if BFD_SUPPORTS_PLUGINS
272 /* xgettext:c-format */
273 const char *command_line
274 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]"
275 " [--plugin <name>] [member-name] [count] archive-file file...\n");
278 /* xgettext:c-format */
279 const char *command_line
280 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoOPsSTuvV]"
281 " [member-name] [count] archive-file file...\n");
283 s
= help
? stdout
: stderr
;
285 fprintf (s
, command_line
, program_name
);
287 /* xgettext:c-format */
288 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
289 fprintf (s
, _(" commands:\n"));
290 fprintf (s
, _(" d - delete file(s) from the archive\n"));
291 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
292 fprintf (s
, _(" p - print file(s) found in the archive\n"));
293 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
294 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
295 fprintf (s
, _(" s - act as ranlib\n"));
296 fprintf (s
, _(" t[O][v] - display contents of the archive\n"));
297 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
298 fprintf (s
, _(" command specific modifiers:\n"));
299 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
300 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
301 if (DEFAULT_AR_DETERMINISTIC
)
304 [D] - use zero for timestamps and uids/gids (default)\n"));
306 [U] - use actual timestamps and uids/gids\n"));
311 [D] - use zero for timestamps and uids/gids\n"));
313 [U] - use actual timestamps and uids/gids (default)\n"));
315 fprintf (s
, _(" [N] - use instance [count] of name\n"));
316 fprintf (s
, _(" [f] - truncate inserted file names\n"));
317 fprintf (s
, _(" [P] - use full path names when matching\n"));
318 fprintf (s
, _(" [o] - preserve original dates\n"));
319 fprintf (s
, _(" [O] - display offsets of files in the archive\n"));
320 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
321 fprintf (s
, _(" generic modifiers:\n"));
322 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
323 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
324 fprintf (s
, _(" [S] - do not build a symbol table\n"));
325 fprintf (s
, _(" [T] - make a thin archive\n"));
326 fprintf (s
, _(" [v] - be verbose\n"));
327 fprintf (s
, _(" [V] - display the version number\n"));
328 fprintf (s
, _(" @<file> - read options from <file>\n"));
329 fprintf (s
, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
330 #if BFD_SUPPORTS_PLUGINS
331 fprintf (s
, _(" optional:\n"));
332 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
337 list_supported_targets (program_name
, s
);
339 if (REPORT_BUGS_TO
[0] && help
)
340 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
342 xexit (help
? 0 : 1);
346 ranlib_usage (int help
)
350 s
= help
? stdout
: stderr
;
352 /* xgettext:c-format */
353 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
354 fprintf (s
, _(" Generate an index to speed access to archives\n"));
355 fprintf (s
, _(" The options are:\n\
356 @<file> Read options from <file>\n"));
357 #if BFD_SUPPORTS_PLUGINS
359 --plugin <name> Load the specified plugin\n"));
361 if (DEFAULT_AR_DETERMINISTIC
)
363 -D Use zero for symbol map timestamp (default)\n\
364 -U Use an actual symbol map timestamp\n"));
367 -D Use zero for symbol map timestamp\n\
368 -U Use actual symbol map timestamp (default)\n"));
370 -t Update the archive's symbol map timestamp\n\
371 -h --help Print this help message\n\
372 -v --version Print version information\n"));
374 list_supported_targets (program_name
, s
);
376 if (REPORT_BUGS_TO
[0] && help
)
377 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
379 xexit (help
? 0 : 1);
382 /* Normalize a file name specified on the command line into a file
383 name which we will use in an archive. */
386 normalize (const char *file
, bfd
*abfd
)
388 const char *filename
;
393 filename
= lbasename (file
);
397 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
402 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
403 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
404 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
411 /* Remove any output file. This is only called via xatexit. */
413 static const char *output_filename
= NULL
;
414 static FILE *output_file
= NULL
;
415 static bfd
*output_bfd
= NULL
;
420 if (output_filename
!= NULL
)
422 if (output_bfd
!= NULL
)
423 bfd_cache_close (output_bfd
);
424 if (output_file
!= NULL
)
425 fclose (output_file
);
426 unlink_if_ordinary (output_filename
);
431 decode_options (int argc
, char **argv
)
435 /* Convert old-style ar call by exploding option element and rearranging
436 options accordingly. */
439 if (argc
> 1 && argv
[1][0] != '-')
441 int new_argc
; /* argc value for rearranged arguments */
442 char **new_argv
; /* argv value for rearranged arguments */
443 char *const *in
; /* cursor into original argv */
444 char **out
; /* cursor into rearranged argv */
445 const char *letter
; /* cursor into old option letters */
446 char buffer
[3]; /* constructed option buffer */
448 /* Initialize a constructed option. */
453 /* Allocate a new argument array, and copy program name in it. */
455 new_argc
= argc
- 1 + strlen (argv
[1]);
456 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
461 /* Copy each old letter option as a separate option. */
463 for (letter
= *in
++; *letter
; letter
++)
466 *out
++ = xstrdup (buffer
);
469 /* Copy all remaining options. */
471 while (in
< argv
+ argc
)
475 /* Replace the old option list by the new one. */
481 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxlcoOVsSuvabiMNfPTDU",
482 long_options
, NULL
)) != EOF
)
493 if (operation
!= none
)
494 fatal (_("two different operation options specified"));
505 operation_alters_arch
= TRUE
;
509 operation_alters_arch
= TRUE
;
512 operation
= print_files
;
515 operation
= quick_append
;
516 operation_alters_arch
= TRUE
;
520 operation_alters_arch
= TRUE
;
523 operation
= print_table
;
558 postype
= pos_before
;
561 postype
= pos_before
;
567 counted_name_mode
= TRUE
;
573 full_pathname
= TRUE
;
576 make_thin_archive
= TRUE
;
579 deterministic
= TRUE
;
582 deterministic
= FALSE
;
585 #if BFD_SUPPORTS_PLUGINS
586 bfd_plugin_set_plugin (optarg
);
588 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
595 case 0: /* A long option that just sets a flag. */
602 /* PR 13256: Allow for the possibility that the first command line option
603 started with a dash (eg --plugin) but then the following option(s) are
604 old style, non-dash-prefixed versions. */
605 if (operation
== none
&& write_armap
!= 1 && !mri_mode
606 && optind
> 0 && optind
< argc
)
608 argv
+= (optind
- 1);
609 argc
-= (optind
- 1);
614 return &argv
[optind
];
617 /* If neither -D nor -U was specified explicitly,
618 then use the configured default. */
620 default_deterministic (void)
622 if (deterministic
< 0)
623 deterministic
= DEFAULT_AR_DETERMINISTIC
;
627 ranlib_main (int argc
, char **argv
)
629 int arg_index
, status
= 0;
630 bfd_boolean touch
= FALSE
;
633 while ((c
= getopt_long (argc
, argv
, "DhHUvVt", long_options
, NULL
)) != EOF
)
638 deterministic
= TRUE
;
641 deterministic
= FALSE
;
655 /* PR binutils/13493: Support plugins. */
657 #if BFD_SUPPORTS_PLUGINS
658 bfd_plugin_set_plugin (optarg
);
660 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
674 print_version ("ranlib");
676 default_deterministic ();
680 while (arg_index
< argc
)
683 status
|= ranlib_only (argv
[arg_index
]);
685 status
|= ranlib_touch (argv
[arg_index
]);
692 int main (int, char **);
695 main (int argc
, char **argv
)
700 char *inarch_filename
;
703 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
704 setlocale (LC_MESSAGES
, "");
706 #if defined (HAVE_SETLOCALE)
707 setlocale (LC_CTYPE
, "");
709 bindtextdomain (PACKAGE
, LOCALEDIR
);
710 textdomain (PACKAGE
);
712 program_name
= argv
[0];
713 xmalloc_set_program_name (program_name
);
714 bfd_set_error_program_name (program_name
);
715 #if BFD_SUPPORTS_PLUGINS
716 bfd_plugin_set_program_name (program_name
);
719 expandargv (&argc
, &argv
);
723 const char *temp
= lbasename (program_name
);
725 if (strlen (temp
) >= 6
726 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
732 START_PROGRESS (program_name
, 0);
734 if (bfd_init () != BFD_INIT_MAGIC
)
735 fatal (_("fatal error: libbfd ABI mismatch"));
736 set_default_bfd_target ();
738 xatexit (remove_output
);
740 for (i
= 1; i
< argc
; i
++)
741 if (! ar_emul_parse_arg (argv
[i
]))
747 ranlib_main (argc
, argv
);
752 argv
= decode_options (argc
, argv
);
758 print_version ("ar");
764 default_deterministic ();
771 /* Fail if no files are specified on the command line.
772 (But not for MRI mode which allows for reading arguments
773 and filenames from stdin). */
774 if (argv
[arg_index
] == NULL
)
777 /* We don't use do_quick_append any more. Too many systems
778 expect ar to always rebuild the symbol table even when q is
781 /* We can't write an armap when using ar q, so just do ar r
783 if (operation
== quick_append
&& write_armap
)
786 if ((operation
== none
|| operation
== print_table
)
788 xexit (ranlib_only (argv
[arg_index
]));
790 if (operation
== none
)
791 fatal (_("no operation specified"));
793 if (newer_only
&& operation
!= replace
)
794 fatal (_("`u' is only meaningful with the `r' option."));
796 if (newer_only
&& deterministic
> 0)
797 fatal (_("`u' is not meaningful with the `D' option."));
799 if (newer_only
&& deterministic
< 0 && DEFAULT_AR_DETERMINISTIC
)
801 `u' modifier ignored since `D' is the default (see `U')"));
803 default_deterministic ();
805 if (postype
!= pos_default
)
807 posname
= argv
[arg_index
++];
809 fatal (_("missing position arg."));
812 if (counted_name_mode
)
814 if (operation
!= extract
&& operation
!= del
)
815 fatal (_("`N' is only meaningful with the `x' and `d' options."));
816 if (argv
[arg_index
] == NULL
)
817 fatal (_("`N' missing value."));
818 counted_name_counter
= atoi (argv
[arg_index
++]);
819 if (counted_name_counter
<= 0)
820 fatal (_("Value for `N' must be positive."));
823 inarch_filename
= argv
[arg_index
++];
824 if (inarch_filename
== NULL
)
827 for (file_count
= 0; argv
[arg_index
+ file_count
] != NULL
; file_count
++)
830 files
= (file_count
> 0) ? argv
+ arg_index
: NULL
;
832 arch
= open_inarch (inarch_filename
,
833 files
== NULL
? (char *) NULL
: files
[0]);
835 if (operation
== extract
&& bfd_is_thin_archive (arch
))
836 fatal (_("`x' cannot be used on thin archives."));
841 map_over_members (arch
, print_descr
, files
, file_count
);
845 map_over_members (arch
, print_contents
, files
, file_count
);
849 map_over_members (arch
, extract_file
, files
, file_count
);
854 delete_members (arch
, files
);
856 output_filename
= NULL
;
860 /* PR 12558: Creating and moving at the same time does
861 not make sense. Just create the archive instead. */
865 move_members (arch
, files
);
867 output_filename
= NULL
;
874 if (files
!= NULL
|| write_armap
> 0)
875 replace_members (arch
, files
, operation
== quick_append
);
877 output_filename
= NULL
;
880 /* Shouldn't happen! */
882 /* xgettext:c-format */
883 fatal (_("internal error -- this option not implemented"));
887 END_PROGRESS (program_name
);
894 open_inarch (const char *archive_filename
, const char *file
)
902 bfd_set_error (bfd_error_no_error
);
905 target
= plugin_target
;
907 if (stat (archive_filename
, &sbuf
) != 0)
909 #if !defined(__GO32__) || defined(__DJGPP__)
911 /* FIXME: I don't understand why this fragment was ifndef'ed
912 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
913 stat() works just fine in v2.x, so I think this should be
914 removed. For now, I enable it for DJGPP v2. -- EZ. */
916 /* KLUDGE ALERT! Temporary fix until I figger why
917 stat() is wrong ... think it's buried in GO32's IDT - Jax */
919 bfd_fatal (archive_filename
);
922 if (!operation_alters_arch
)
924 fprintf (stderr
, "%s: ", program_name
);
925 perror (archive_filename
);
930 /* If the target isn't set, try to figure out the target to use
931 for the archive from the first object on the list. */
932 if (target
== NULL
&& file
!= NULL
)
936 obj
= bfd_openr (file
, target
);
939 if (bfd_check_format (obj
, bfd_object
))
940 target
= bfd_get_target (obj
);
941 (void) bfd_close (obj
);
945 /* Create an empty archive. */
946 arch
= bfd_openw (archive_filename
, target
);
948 || ! bfd_set_format (arch
, bfd_archive
)
949 || ! bfd_close (arch
))
950 bfd_fatal (archive_filename
);
951 else if (!silent_create
)
952 non_fatal (_("creating %s"), archive_filename
);
954 /* If we die creating a new archive, don't leave it around. */
955 output_filename
= archive_filename
;
958 arch
= bfd_openr (archive_filename
, target
);
962 bfd_fatal (archive_filename
);
965 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
967 bfd_nonfatal (archive_filename
);
968 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
970 list_matching_formats (matching
);
976 if ((operation
== replace
|| operation
== quick_append
)
977 && bfd_openr_next_archived_file (arch
, NULL
) != NULL
)
979 /* PR 15140: Catch attempts to convert a normal
980 archive into a thin archive or vice versa. */
981 if (make_thin_archive
&& ! bfd_is_thin_archive (arch
))
983 fatal (_("Cannot convert existing library %s to thin format"),
984 bfd_get_filename (arch
));
987 else if (! make_thin_archive
&& bfd_is_thin_archive (arch
))
989 fatal (_("Cannot convert existing thin library %s to normal format"),
990 bfd_get_filename (arch
));
995 last_one
= &(arch
->archive_next
);
996 /* Read all the contents right away, regardless. */
997 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
999 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
1002 *last_one
= next_one
;
1003 last_one
= &next_one
->archive_next
;
1005 *last_one
= (bfd
*) NULL
;
1006 if (bfd_get_error () != bfd_error_no_more_archived_files
)
1012 print_contents (bfd
*abfd
)
1014 bfd_size_type ncopied
= 0;
1016 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1019 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1020 /* xgettext:c-format */
1021 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1024 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
1026 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1029 while (ncopied
< size
)
1031 bfd_size_type nread
;
1032 bfd_size_type tocopy
= size
- ncopied
;
1034 if (tocopy
> BUFSIZE
)
1037 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1038 if (nread
!= tocopy
)
1039 /* xgettext:c-format */
1040 fatal (_("%s is not a valid archive"),
1041 bfd_get_filename (abfd
->my_archive
));
1043 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
1044 return value to bfd_size_type to avoid comparison between signed and
1046 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
1047 fatal ("stdout: %s", strerror (errno
));
1053 /* Extract a member of the archive into its own file.
1055 We defer opening the new file until after we have read a BUFSIZ chunk of the
1056 old one, since we know we have just read the archive header for the old
1057 one. Since most members are shorter than BUFSIZ, this means we will read
1058 the old header, read the old data, write a new inode for the new file, and
1059 write the new data, and be done. This 'optimization' is what comes from
1060 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1064 extract_file (bfd
*abfd
)
1067 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1068 bfd_size_type nread
, tocopy
;
1069 bfd_size_type ncopied
= 0;
1073 /* PR binutils/17533: Do not allow directory traversal
1074 outside of the current directory tree. */
1075 if (! is_valid_archive_path (bfd_get_filename (abfd
)))
1077 non_fatal (_("illegal pathname found in archive member: %s"),
1078 bfd_get_filename (abfd
));
1083 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1084 /* xgettext:c-format */
1085 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1089 printf ("x - %s\n", bfd_get_filename (abfd
));
1091 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1096 /* Seems like an abstraction violation, eh? Well it's OK! */
1097 output_filename
= bfd_get_filename (abfd
);
1099 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
1100 if (ostream
== NULL
)
1102 perror (bfd_get_filename (abfd
));
1106 output_file
= ostream
;
1109 while (ncopied
< size
)
1111 tocopy
= size
- ncopied
;
1112 if (tocopy
> BUFSIZE
)
1115 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1116 if (nread
!= tocopy
)
1117 /* xgettext:c-format */
1118 fatal (_("%s is not a valid archive"),
1119 bfd_get_filename (abfd
->my_archive
));
1121 /* See comment above; this saves disk arm motion */
1122 if (ostream
== NULL
)
1124 /* Seems like an abstraction violation, eh? Well it's OK! */
1125 output_filename
= bfd_get_filename (abfd
);
1127 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
1128 if (ostream
== NULL
)
1130 perror (bfd_get_filename (abfd
));
1134 output_file
= ostream
;
1137 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1138 the return value to bfd_size_type to avoid comparison between
1139 signed and unsigned values. */
1140 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
1141 fatal ("%s: %s", output_filename
, strerror (errno
));
1145 if (ostream
!= NULL
)
1149 output_filename
= NULL
;
1151 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
1155 /* Set access time to modification time. Only st_mtime is
1156 initialized by bfd_stat_arch_elt. */
1157 buf
.st_atime
= buf
.st_mtime
;
1158 set_times (bfd_get_filename (abfd
), &buf
);
1165 write_archive (bfd
*iarch
)
1168 char *old_name
, *new_name
;
1169 bfd
*contents_head
= iarch
->archive_next
;
1171 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1172 strcpy (old_name
, bfd_get_filename (iarch
));
1173 new_name
= make_tempname (old_name
);
1175 if (new_name
== NULL
)
1176 bfd_fatal (_("could not create temporary file whilst writing archive"));
1178 output_filename
= new_name
;
1180 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1183 bfd_fatal (old_name
);
1187 bfd_set_format (obfd
, bfd_archive
);
1189 /* Request writing the archive symbol table unless we've
1190 been explicitly requested not to. */
1191 obfd
->has_armap
= write_armap
>= 0;
1195 /* This should really use bfd_set_file_flags, but that rejects
1197 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1201 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1204 obfd
->flags
|= BFD_ARCHIVE_FULL_PATH
;
1206 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1207 bfd_set_thin_archive (obfd
, TRUE
);
1209 if (!bfd_set_archive_head (obfd
, contents_head
))
1210 bfd_fatal (old_name
);
1212 if (!bfd_close (obfd
))
1213 bfd_fatal (old_name
);
1216 output_filename
= NULL
;
1218 /* We don't care if this fails; we might be creating the archive. */
1221 if (smart_rename (new_name
, old_name
, 0) != 0)
1227 /* Return a pointer to the pointer to the entry which should be rplacd'd
1228 into when altering. DEFAULT_POS should be how to interpret pos_default,
1229 and should be a pos value. */
1232 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1234 bfd
**after_bfd
= contents
;
1236 const char *realposname
;
1238 if (postype
== pos_default
)
1240 realpos
= default_pos
;
1241 realposname
= default_posname
;
1246 realposname
= posname
;
1249 if (realpos
== pos_end
)
1252 after_bfd
= &((*after_bfd
)->archive_next
);
1256 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1257 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1259 if (realpos
== pos_after
)
1260 after_bfd
= &(*after_bfd
)->archive_next
;
1268 delete_members (bfd
*arch
, char **files_to_delete
)
1270 bfd
**current_ptr_ptr
;
1272 bfd_boolean something_changed
= FALSE
;
1275 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1277 /* In a.out systems, the armap is optional. It's also called
1278 __.SYMDEF. So if the user asked to delete it, we should remember
1279 that fact. This isn't quite right for COFF systems (where
1280 __.SYMDEF might be regular member), but it's very unlikely
1281 to be a problem. FIXME */
1283 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1285 arch
->has_armap
= FALSE
;
1292 current_ptr_ptr
= &(arch
->archive_next
);
1293 while (*current_ptr_ptr
)
1295 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1296 (*current_ptr_ptr
)->filename
) == 0)
1299 if (counted_name_mode
1300 && match_count
!= counted_name_counter
)
1302 /* Counting, and didn't match on count; go on to the
1308 something_changed
= TRUE
;
1312 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1317 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1320 if (verbose
&& !found
)
1322 /* xgettext:c-format */
1323 printf (_("No member named `%s'\n"), *files_to_delete
);
1329 if (something_changed
)
1330 write_archive (arch
);
1332 output_filename
= NULL
;
1336 /* Reposition existing members within an archive */
1339 move_members (bfd
*arch
, char **files_to_move
)
1341 bfd
**after_bfd
; /* New entries go after this one */
1342 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1344 for (; *files_to_move
; ++files_to_move
)
1346 current_ptr_ptr
= &(arch
->archive_next
);
1347 while (*current_ptr_ptr
)
1349 bfd
*current_ptr
= *current_ptr_ptr
;
1350 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1351 current_ptr
->filename
) == 0)
1353 /* Move this file to the end of the list - first cut from
1356 *current_ptr_ptr
= current_ptr
->archive_next
;
1358 /* Now glue to end */
1359 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1360 link_bfd
= *after_bfd
;
1361 *after_bfd
= current_ptr
;
1362 current_ptr
->archive_next
= link_bfd
;
1365 printf ("m - %s\n", *files_to_move
);
1370 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1372 /* xgettext:c-format */
1373 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1378 write_archive (arch
);
1381 /* Ought to default to replacing in place, but this is existing practice! */
1384 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1386 bfd_boolean changed
= FALSE
;
1387 bfd
**after_bfd
; /* New entries go after this one. */
1391 while (files_to_move
&& *files_to_move
)
1395 current_ptr
= &arch
->archive_next
;
1396 while (*current_ptr
)
1398 current
= *current_ptr
;
1400 /* For compatibility with existing ar programs, we
1401 permit the same file to be added multiple times. */
1402 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1403 normalize (current
->filename
, arch
)) == 0
1404 && current
->arelt_data
!= NULL
)
1408 struct stat fsbuf
, asbuf
;
1410 if (stat (*files_to_move
, &fsbuf
) != 0)
1412 if (errno
!= ENOENT
)
1413 bfd_fatal (*files_to_move
);
1416 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1417 /* xgettext:c-format */
1418 fatal (_("internal stat error on %s"),
1421 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1425 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1427 if (ar_emul_replace (after_bfd
, *files_to_move
,
1430 /* Snip out this entry from the chain. */
1431 *current_ptr
= (*current_ptr
)->archive_next
;
1437 current_ptr
= &(current
->archive_next
);
1441 /* Add to the end of the archive. */
1442 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1444 if (ar_emul_append (after_bfd
, *files_to_move
, target
,
1445 verbose
, make_thin_archive
))
1454 write_archive (arch
);
1456 output_filename
= NULL
;
1460 ranlib_only (const char *archname
)
1464 if (get_file_size (archname
) < 1)
1467 arch
= open_inarch (archname
, (char *) NULL
);
1470 write_archive (arch
);
1474 /* Update the timestamp of the symbol map of an archive. */
1477 ranlib_touch (const char *archname
)
1480 /* I don't think updating works on go32. */
1481 ranlib_only (archname
);
1487 if (get_file_size (archname
) < 1)
1489 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1492 bfd_set_error (bfd_error_system_call
);
1493 bfd_fatal (archname
);
1496 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1498 bfd_fatal (archname
);
1499 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1501 bfd_nonfatal (archname
);
1502 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1504 list_matching_formats (matching
);
1510 if (! bfd_has_map (arch
))
1511 /* xgettext:c-format */
1512 fatal (_("%s: no archive map to update"), archname
);
1515 arch
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1517 bfd_update_armap_timestamp (arch
);
1519 if (! bfd_close (arch
))
1520 bfd_fatal (archname
);
1525 /* Things which are interesting to map over all or some of the files: */
1528 print_descr (bfd
*abfd
)
1530 print_arelt_descr (stdout
, abfd
, verbose
, display_offsets
);