1 /* ar.c - Archive modify and extract.
2 Copyright (C) 1991-2018 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 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 #if BFD_SUPPORTS_PLUGINS
142 static const char *plugin_target
= "plugin";
144 static const char *plugin_target
= NULL
;
147 static const char *target
= NULL
;
149 #define OPTION_PLUGIN 201
150 #define OPTION_TARGET 202
152 static struct option long_options
[] =
154 {"help", no_argument
, &show_help
, 1},
155 {"plugin", required_argument
, NULL
, OPTION_PLUGIN
},
156 {"target", required_argument
, NULL
, OPTION_TARGET
},
157 {"version", no_argument
, &show_version
, 1},
158 {NULL
, no_argument
, NULL
, 0}
166 interactive
= isatty (fileno (stdin
));
170 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
171 COUNT is the length of the FILES chain; FUNCTION is called on each entry
172 whose name matches one in FILES. */
175 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
182 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
190 /* This may appear to be a baroque way of accomplishing what we want.
191 However we have to iterate over the filenames in order to notice where
192 a filename is requested but does not exist in the archive. Ditto
193 mapping over each file each time -- we want to hack multiple
196 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
197 head
->archive_pass
= 0;
199 for (; count
> 0; files
++, count
--)
201 bfd_boolean found
= FALSE
;
204 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
206 const char * filename
;
209 /* PR binutils/15796: Once an archive element has been matched
210 do not match it again. If the user provides multiple same-named
211 parameters on the command line their intent is to match multiple
212 same-named entries in the archive, not the same entry multiple
214 if (head
->archive_pass
)
217 filename
= head
->filename
;
218 if (filename
== NULL
)
220 /* Some archive formats don't get the filenames filled in
221 until the elements are opened. */
223 bfd_stat_arch_elt (head
, &buf
);
225 else if (bfd_is_thin_archive (arch
))
227 /* Thin archives store full pathnames. Need to normalize. */
228 filename
= normalize (filename
, arch
);
232 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
235 if (counted_name_mode
236 && match_count
!= counted_name_counter
)
238 /* Counting, and didn't match on count; go on to the
245 head
->archive_pass
= 1;
246 /* PR binutils/15796: Once a file has been matched, do not
247 match any more same-named files in the archive. If the
248 user does want to match multiple same-name files in an
249 archive they should provide multiple same-name parameters
250 to the ar command. */
256 /* xgettext:c-format */
257 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
261 bfd_boolean operation_alters_arch
= FALSE
;
268 #if BFD_SUPPORTS_PLUGINS
269 /* xgettext:c-format */
270 const char *command_line
271 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
272 " [--plugin <name>] [member-name] [count] archive-file file...\n");
275 /* xgettext:c-format */
276 const char *command_line
277 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
278 " [member-name] [count] archive-file file...\n");
280 s
= help
? stdout
: stderr
;
282 fprintf (s
, command_line
, program_name
);
284 /* xgettext:c-format */
285 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
286 fprintf (s
, _(" commands:\n"));
287 fprintf (s
, _(" d - delete file(s) from the archive\n"));
288 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
289 fprintf (s
, _(" p - print file(s) found in the archive\n"));
290 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
291 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
292 fprintf (s
, _(" s - act as ranlib\n"));
293 fprintf (s
, _(" t - display contents of archive\n"));
294 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
295 fprintf (s
, _(" command specific modifiers:\n"));
296 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
297 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
298 if (DEFAULT_AR_DETERMINISTIC
)
301 [D] - use zero for timestamps and uids/gids (default)\n"));
303 [U] - use actual timestamps and uids/gids\n"));
308 [D] - use zero for timestamps and uids/gids\n"));
310 [U] - use actual timestamps and uids/gids (default)\n"));
312 fprintf (s
, _(" [N] - use instance [count] of name\n"));
313 fprintf (s
, _(" [f] - truncate inserted file names\n"));
314 fprintf (s
, _(" [P] - use full path names when matching\n"));
315 fprintf (s
, _(" [o] - preserve original dates\n"));
316 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
317 fprintf (s
, _(" generic modifiers:\n"));
318 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
319 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
320 fprintf (s
, _(" [S] - do not build a symbol table\n"));
321 fprintf (s
, _(" [T] - make a thin archive\n"));
322 fprintf (s
, _(" [v] - be verbose\n"));
323 fprintf (s
, _(" [V] - display the version number\n"));
324 fprintf (s
, _(" @<file> - read options from <file>\n"));
325 fprintf (s
, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
326 #if BFD_SUPPORTS_PLUGINS
327 fprintf (s
, _(" optional:\n"));
328 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
333 list_supported_targets (program_name
, s
);
335 if (REPORT_BUGS_TO
[0] && help
)
336 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
338 xexit (help
? 0 : 1);
342 ranlib_usage (int help
)
346 s
= help
? stdout
: stderr
;
348 /* xgettext:c-format */
349 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
350 fprintf (s
, _(" Generate an index to speed access to archives\n"));
351 fprintf (s
, _(" The options are:\n\
352 @<file> Read options from <file>\n"));
353 #if BFD_SUPPORTS_PLUGINS
355 --plugin <name> Load the specified plugin\n"));
357 if (DEFAULT_AR_DETERMINISTIC
)
359 -D Use zero for symbol map timestamp (default)\n\
360 -U Use an actual symbol map timestamp\n"));
363 -D Use zero for symbol map timestamp\n\
364 -U Use actual symbol map timestamp (default)\n"));
366 -t Update the archive's symbol map timestamp\n\
367 -h --help Print this help message\n\
368 -v --version Print version information\n"));
370 list_supported_targets (program_name
, s
);
372 if (REPORT_BUGS_TO
[0] && help
)
373 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
375 xexit (help
? 0 : 1);
378 /* Normalize a file name specified on the command line into a file
379 name which we will use in an archive. */
382 normalize (const char *file
, bfd
*abfd
)
384 const char *filename
;
389 filename
= lbasename (file
);
393 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
398 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
399 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
400 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
407 /* Remove any output file. This is only called via xatexit. */
409 static const char *output_filename
= NULL
;
410 static FILE *output_file
= NULL
;
411 static bfd
*output_bfd
= NULL
;
416 if (output_filename
!= NULL
)
418 if (output_bfd
!= NULL
)
419 bfd_cache_close (output_bfd
);
420 if (output_file
!= NULL
)
421 fclose (output_file
);
422 unlink_if_ordinary (output_filename
);
427 decode_options (int argc
, char **argv
)
431 /* Convert old-style tar call by exploding option element and rearranging
432 options accordingly. */
434 if (argc
> 1 && argv
[1][0] != '-')
436 int new_argc
; /* argc value for rearranged arguments */
437 char **new_argv
; /* argv value for rearranged arguments */
438 char *const *in
; /* cursor into original argv */
439 char **out
; /* cursor into rearranged argv */
440 const char *letter
; /* cursor into old option letters */
441 char buffer
[3]; /* constructed option buffer */
443 /* Initialize a constructed option. */
448 /* Allocate a new argument array, and copy program name in it. */
450 new_argc
= argc
- 1 + strlen (argv
[1]);
451 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
456 /* Copy each old letter option as a separate option. */
458 for (letter
= *in
++; *letter
; letter
++)
461 *out
++ = xstrdup (buffer
);
464 /* Copy all remaining options. */
466 while (in
< argv
+ argc
)
470 /* Replace the old option list by the new one. */
476 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxlcoVsSuvabiMNfPTDU",
477 long_options
, NULL
)) != EOF
)
488 if (operation
!= none
)
489 fatal (_("two different operation options specified"));
500 operation_alters_arch
= TRUE
;
504 operation_alters_arch
= TRUE
;
507 operation
= print_files
;
510 operation
= quick_append
;
511 operation_alters_arch
= TRUE
;
515 operation_alters_arch
= TRUE
;
518 operation
= print_table
;
550 postype
= pos_before
;
553 postype
= pos_before
;
559 counted_name_mode
= TRUE
;
565 full_pathname
= TRUE
;
568 make_thin_archive
= TRUE
;
571 deterministic
= TRUE
;
574 deterministic
= FALSE
;
577 #if BFD_SUPPORTS_PLUGINS
578 bfd_plugin_set_plugin (optarg
);
580 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
587 case 0: /* A long option that just sets a flag. */
594 return &argv
[optind
];
597 /* If neither -D nor -U was specified explicitly,
598 then use the configured default. */
600 default_deterministic (void)
602 if (deterministic
< 0)
603 deterministic
= DEFAULT_AR_DETERMINISTIC
;
607 ranlib_main (int argc
, char **argv
)
609 int arg_index
, status
= 0;
610 bfd_boolean touch
= FALSE
;
613 while ((c
= getopt_long (argc
, argv
, "DhHUvVt", long_options
, NULL
)) != EOF
)
618 deterministic
= TRUE
;
621 deterministic
= FALSE
;
635 /* PR binutils/13493: Support plugins. */
637 #if BFD_SUPPORTS_PLUGINS
638 bfd_plugin_set_plugin (optarg
);
640 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
654 print_version ("ranlib");
656 default_deterministic ();
660 while (arg_index
< argc
)
663 status
|= ranlib_only (argv
[arg_index
]);
665 status
|= ranlib_touch (argv
[arg_index
]);
672 int main (int, char **);
675 main (int argc
, char **argv
)
680 char *inarch_filename
;
683 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
684 setlocale (LC_MESSAGES
, "");
686 #if defined (HAVE_SETLOCALE)
687 setlocale (LC_CTYPE
, "");
689 bindtextdomain (PACKAGE
, LOCALEDIR
);
690 textdomain (PACKAGE
);
692 program_name
= argv
[0];
693 xmalloc_set_program_name (program_name
);
694 bfd_set_error_program_name (program_name
);
695 #if BFD_SUPPORTS_PLUGINS
696 bfd_plugin_set_program_name (program_name
);
699 expandargv (&argc
, &argv
);
703 const char *temp
= lbasename (program_name
);
705 if (strlen (temp
) >= 6
706 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
712 START_PROGRESS (program_name
, 0);
715 set_default_bfd_target ();
717 xatexit (remove_output
);
719 for (i
= 1; i
< argc
; i
++)
720 if (! ar_emul_parse_arg (argv
[i
]))
726 ranlib_main (argc
, argv
);
731 argv
= decode_options (argc
, argv
);
737 print_version ("ar");
743 default_deterministic ();
750 /* Fail if no files are specified on the command line.
751 (But not for MRI mode which allows for reading arguments
752 and filenames from stdin). */
753 if (argv
[arg_index
] == NULL
)
756 /* We don't use do_quick_append any more. Too many systems
757 expect ar to always rebuild the symbol table even when q is
760 /* We can't write an armap when using ar q, so just do ar r
762 if (operation
== quick_append
&& write_armap
)
765 if ((operation
== none
|| operation
== print_table
)
767 xexit (ranlib_only (argv
[arg_index
]));
769 if (operation
== none
)
770 fatal (_("no operation specified"));
772 if (newer_only
&& operation
!= replace
)
773 fatal (_("`u' is only meaningful with the `r' option."));
775 if (newer_only
&& deterministic
> 0)
776 fatal (_("`u' is not meaningful with the `D' option."));
778 if (newer_only
&& deterministic
< 0 && DEFAULT_AR_DETERMINISTIC
)
780 `u' modifier ignored since `D' is the default (see `U')"));
782 default_deterministic ();
784 if (postype
!= pos_default
)
786 posname
= argv
[arg_index
++];
788 fatal (_("missing position arg."));
791 if (counted_name_mode
)
793 if (operation
!= extract
&& operation
!= del
)
794 fatal (_("`N' is only meaningful with the `x' and `d' options."));
795 if (argv
[arg_index
] == NULL
)
796 fatal (_("`N' missing value."));
797 counted_name_counter
= atoi (argv
[arg_index
++]);
798 if (counted_name_counter
<= 0)
799 fatal (_("Value for `N' must be positive."));
802 inarch_filename
= argv
[arg_index
++];
803 if (inarch_filename
== NULL
)
806 for (file_count
= 0; argv
[arg_index
+ file_count
] != NULL
; file_count
++)
809 files
= (file_count
> 0) ? argv
+ arg_index
: NULL
;
811 arch
= open_inarch (inarch_filename
,
812 files
== NULL
? (char *) NULL
: files
[0]);
814 if (operation
== extract
&& bfd_is_thin_archive (arch
))
815 fatal (_("`x' cannot be used on thin archives."));
820 map_over_members (arch
, print_descr
, files
, file_count
);
824 map_over_members (arch
, print_contents
, files
, file_count
);
828 map_over_members (arch
, extract_file
, files
, file_count
);
833 delete_members (arch
, files
);
835 output_filename
= NULL
;
839 /* PR 12558: Creating and moving at the same time does
840 not make sense. Just create the archive instead. */
844 move_members (arch
, files
);
846 output_filename
= NULL
;
853 if (files
!= NULL
|| write_armap
> 0)
854 replace_members (arch
, files
, operation
== quick_append
);
856 output_filename
= NULL
;
859 /* Shouldn't happen! */
861 /* xgettext:c-format */
862 fatal (_("internal error -- this option not implemented"));
866 END_PROGRESS (program_name
);
873 open_inarch (const char *archive_filename
, const char *file
)
881 bfd_set_error (bfd_error_no_error
);
884 target
= plugin_target
;
886 if (stat (archive_filename
, &sbuf
) != 0)
888 #if !defined(__GO32__) || defined(__DJGPP__)
890 /* FIXME: I don't understand why this fragment was ifndef'ed
891 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
892 stat() works just fine in v2.x, so I think this should be
893 removed. For now, I enable it for DJGPP v2. -- EZ. */
895 /* KLUDGE ALERT! Temporary fix until I figger why
896 stat() is wrong ... think it's buried in GO32's IDT - Jax */
898 bfd_fatal (archive_filename
);
901 if (!operation_alters_arch
)
903 fprintf (stderr
, "%s: ", program_name
);
904 perror (archive_filename
);
909 /* If the target isn't set, try to figure out the target to use
910 for the archive from the first object on the list. */
911 if (target
== NULL
&& file
!= NULL
)
915 obj
= bfd_openr (file
, target
);
918 if (bfd_check_format (obj
, bfd_object
))
919 target
= bfd_get_target (obj
);
920 (void) bfd_close (obj
);
924 /* Create an empty archive. */
925 arch
= bfd_openw (archive_filename
, target
);
927 || ! bfd_set_format (arch
, bfd_archive
)
928 || ! bfd_close (arch
))
929 bfd_fatal (archive_filename
);
930 else if (!silent_create
)
931 non_fatal (_("creating %s"), archive_filename
);
933 /* If we die creating a new archive, don't leave it around. */
934 output_filename
= archive_filename
;
937 arch
= bfd_openr (archive_filename
, target
);
941 bfd_fatal (archive_filename
);
944 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
946 bfd_nonfatal (archive_filename
);
947 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
949 list_matching_formats (matching
);
955 if ((operation
== replace
|| operation
== quick_append
)
956 && bfd_openr_next_archived_file (arch
, NULL
) != NULL
)
958 /* PR 15140: Catch attempts to convert a normal
959 archive into a thin archive or vice versa. */
960 if (make_thin_archive
&& ! bfd_is_thin_archive (arch
))
962 fatal (_("Cannot convert existing library %s to thin format"),
963 bfd_get_filename (arch
));
966 else if (! make_thin_archive
&& bfd_is_thin_archive (arch
))
968 fatal (_("Cannot convert existing thin library %s to normal format"),
969 bfd_get_filename (arch
));
974 last_one
= &(arch
->archive_next
);
975 /* Read all the contents right away, regardless. */
976 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
978 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
981 *last_one
= next_one
;
982 last_one
= &next_one
->archive_next
;
984 *last_one
= (bfd
*) NULL
;
985 if (bfd_get_error () != bfd_error_no_more_archived_files
)
991 print_contents (bfd
*abfd
)
993 bfd_size_type ncopied
= 0;
995 char *cbuf
= (char *) xmalloc (BUFSIZE
);
998 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
999 /* xgettext:c-format */
1000 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1003 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
1005 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1008 while (ncopied
< size
)
1010 bfd_size_type nread
;
1011 bfd_size_type tocopy
= size
- ncopied
;
1013 if (tocopy
> BUFSIZE
)
1016 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1017 if (nread
!= tocopy
)
1018 /* xgettext:c-format */
1019 fatal (_("%s is not a valid archive"),
1020 bfd_get_filename (abfd
->my_archive
));
1022 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast the
1023 return value to bfd_size_type to avoid comparison between signed and
1025 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
1026 fatal ("stdout: %s", strerror (errno
));
1032 /* Extract a member of the archive into its own file.
1034 We defer opening the new file until after we have read a BUFSIZ chunk of the
1035 old one, since we know we have just read the archive header for the old
1036 one. Since most members are shorter than BUFSIZ, this means we will read
1037 the old header, read the old data, write a new inode for the new file, and
1038 write the new data, and be done. This 'optimization' is what comes from
1039 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
1043 extract_file (bfd
*abfd
)
1046 char *cbuf
= (char *) xmalloc (BUFSIZE
);
1047 bfd_size_type nread
, tocopy
;
1048 bfd_size_type ncopied
= 0;
1052 /* PR binutils/17533: Do not allow directory traversal
1053 outside of the current directory tree. */
1054 if (! is_valid_archive_path (bfd_get_filename (abfd
)))
1056 non_fatal (_("illegal pathname found in archive member: %s"),
1057 bfd_get_filename (abfd
));
1062 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
1063 /* xgettext:c-format */
1064 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1068 printf ("x - %s\n", bfd_get_filename (abfd
));
1070 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
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 while (ncopied
< size
)
1090 tocopy
= size
- ncopied
;
1091 if (tocopy
> BUFSIZE
)
1094 nread
= bfd_bread (cbuf
, tocopy
, abfd
);
1095 if (nread
!= tocopy
)
1096 /* xgettext:c-format */
1097 fatal (_("%s is not a valid archive"),
1098 bfd_get_filename (abfd
->my_archive
));
1100 /* See comment above; this saves disk arm motion */
1101 if (ostream
== NULL
)
1103 /* Seems like an abstraction violation, eh? Well it's OK! */
1104 output_filename
= bfd_get_filename (abfd
);
1106 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
1107 if (ostream
== NULL
)
1109 perror (bfd_get_filename (abfd
));
1113 output_file
= ostream
;
1116 /* fwrite in mingw32 may return int instead of bfd_size_type. Cast
1117 the return value to bfd_size_type to avoid comparison between
1118 signed and unsigned values. */
1119 if ((bfd_size_type
) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
1120 fatal ("%s: %s", output_filename
, strerror (errno
));
1124 if (ostream
!= NULL
)
1128 output_filename
= NULL
;
1130 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
1134 /* Set access time to modification time. Only st_mtime is
1135 initialized by bfd_stat_arch_elt. */
1136 buf
.st_atime
= buf
.st_mtime
;
1137 set_times (bfd_get_filename (abfd
), &buf
);
1144 write_archive (bfd
*iarch
)
1147 char *old_name
, *new_name
;
1148 bfd
*contents_head
= iarch
->archive_next
;
1150 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1151 strcpy (old_name
, bfd_get_filename (iarch
));
1152 new_name
= make_tempname (old_name
);
1154 if (new_name
== NULL
)
1155 bfd_fatal (_("could not create temporary file whilst writing archive"));
1157 output_filename
= new_name
;
1159 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1162 bfd_fatal (old_name
);
1166 bfd_set_format (obfd
, bfd_archive
);
1168 /* Request writing the archive symbol table unless we've
1169 been explicitly requested not to. */
1170 obfd
->has_armap
= write_armap
>= 0;
1174 /* This should really use bfd_set_file_flags, but that rejects
1176 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1180 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1182 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1183 bfd_is_thin_archive (obfd
) = 1;
1185 if (!bfd_set_archive_head (obfd
, contents_head
))
1186 bfd_fatal (old_name
);
1188 if (!bfd_close (obfd
))
1189 bfd_fatal (old_name
);
1192 output_filename
= NULL
;
1194 /* We don't care if this fails; we might be creating the archive. */
1197 if (smart_rename (new_name
, old_name
, 0) != 0)
1203 /* Return a pointer to the pointer to the entry which should be rplacd'd
1204 into when altering. DEFAULT_POS should be how to interpret pos_default,
1205 and should be a pos value. */
1208 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1210 bfd
**after_bfd
= contents
;
1212 const char *realposname
;
1214 if (postype
== pos_default
)
1216 realpos
= default_pos
;
1217 realposname
= default_posname
;
1222 realposname
= posname
;
1225 if (realpos
== pos_end
)
1228 after_bfd
= &((*after_bfd
)->archive_next
);
1232 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1233 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1235 if (realpos
== pos_after
)
1236 after_bfd
= &(*after_bfd
)->archive_next
;
1244 delete_members (bfd
*arch
, char **files_to_delete
)
1246 bfd
**current_ptr_ptr
;
1248 bfd_boolean something_changed
= FALSE
;
1251 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1253 /* In a.out systems, the armap is optional. It's also called
1254 __.SYMDEF. So if the user asked to delete it, we should remember
1255 that fact. This isn't quite right for COFF systems (where
1256 __.SYMDEF might be regular member), but it's very unlikely
1257 to be a problem. FIXME */
1259 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1261 arch
->has_armap
= FALSE
;
1268 current_ptr_ptr
= &(arch
->archive_next
);
1269 while (*current_ptr_ptr
)
1271 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1272 (*current_ptr_ptr
)->filename
) == 0)
1275 if (counted_name_mode
1276 && match_count
!= counted_name_counter
)
1278 /* Counting, and didn't match on count; go on to the
1284 something_changed
= TRUE
;
1288 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1293 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1296 if (verbose
&& !found
)
1298 /* xgettext:c-format */
1299 printf (_("No member named `%s'\n"), *files_to_delete
);
1305 if (something_changed
)
1306 write_archive (arch
);
1308 output_filename
= NULL
;
1312 /* Reposition existing members within an archive */
1315 move_members (bfd
*arch
, char **files_to_move
)
1317 bfd
**after_bfd
; /* New entries go after this one */
1318 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1320 for (; *files_to_move
; ++files_to_move
)
1322 current_ptr_ptr
= &(arch
->archive_next
);
1323 while (*current_ptr_ptr
)
1325 bfd
*current_ptr
= *current_ptr_ptr
;
1326 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1327 current_ptr
->filename
) == 0)
1329 /* Move this file to the end of the list - first cut from
1332 *current_ptr_ptr
= current_ptr
->archive_next
;
1334 /* Now glue to end */
1335 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1336 link_bfd
= *after_bfd
;
1337 *after_bfd
= current_ptr
;
1338 current_ptr
->archive_next
= link_bfd
;
1341 printf ("m - %s\n", *files_to_move
);
1346 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1348 /* xgettext:c-format */
1349 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1354 write_archive (arch
);
1357 /* Ought to default to replacing in place, but this is existing practice! */
1360 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1362 bfd_boolean changed
= FALSE
;
1363 bfd
**after_bfd
; /* New entries go after this one. */
1367 while (files_to_move
&& *files_to_move
)
1371 current_ptr
= &arch
->archive_next
;
1372 while (*current_ptr
)
1374 current
= *current_ptr
;
1376 /* For compatibility with existing ar programs, we
1377 permit the same file to be added multiple times. */
1378 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1379 normalize (current
->filename
, arch
)) == 0
1380 && current
->arelt_data
!= NULL
)
1384 struct stat fsbuf
, asbuf
;
1386 if (stat (*files_to_move
, &fsbuf
) != 0)
1388 if (errno
!= ENOENT
)
1389 bfd_fatal (*files_to_move
);
1392 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1393 /* xgettext:c-format */
1394 fatal (_("internal stat error on %s"),
1397 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1401 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1403 if (ar_emul_replace (after_bfd
, *files_to_move
,
1406 /* Snip out this entry from the chain. */
1407 *current_ptr
= (*current_ptr
)->archive_next
;
1413 current_ptr
= &(current
->archive_next
);
1417 /* Add to the end of the archive. */
1418 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1420 if (ar_emul_append (after_bfd
, *files_to_move
, target
,
1421 verbose
, make_thin_archive
))
1430 write_archive (arch
);
1432 output_filename
= NULL
;
1436 ranlib_only (const char *archname
)
1440 if (get_file_size (archname
) < 1)
1443 arch
= open_inarch (archname
, (char *) NULL
);
1446 write_archive (arch
);
1450 /* Update the timestamp of the symbol map of an archive. */
1453 ranlib_touch (const char *archname
)
1456 /* I don't think updating works on go32. */
1457 ranlib_only (archname
);
1463 if (get_file_size (archname
) < 1)
1465 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1468 bfd_set_error (bfd_error_system_call
);
1469 bfd_fatal (archname
);
1472 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1474 bfd_fatal (archname
);
1475 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1477 bfd_nonfatal (archname
);
1478 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1480 list_matching_formats (matching
);
1486 if (! bfd_has_map (arch
))
1487 /* xgettext:c-format */
1488 fatal (_("%s: no archive map to update"), archname
);
1491 arch
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1493 bfd_update_armap_timestamp (arch
);
1495 if (! bfd_close (arch
))
1496 bfd_fatal (archname
);
1501 /* Things which are interesting to map over all or some of the files: */
1504 print_descr (bfd
*abfd
)
1506 print_arelt_descr (stdout
, abfd
, verbose
);