1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 Bugs: GNU ar used to check file against filesystem in quick_update and
25 replace operations (would check mtime). Doesn't warn when name truncated.
26 No way to specify pos_end. Error messages should be more consistent. */
30 #include "libiberty.h"
37 #include "filenames.h"
42 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
44 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
47 /* Static declarations. */
49 static void mri_emul (void);
50 static const char *normalize (const char *, bfd
*);
51 static void remove_output (void);
52 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
53 static void print_contents (bfd
* member
);
54 static void delete_members (bfd
*, char **files_to_delete
);
56 static void move_members (bfd
*, char **files_to_move
);
57 static void replace_members
58 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
59 static void print_descr (bfd
* abfd
);
60 static void write_archive (bfd
*);
61 static int ranlib_only (const char *archname
);
62 static int ranlib_touch (const char *archname
);
63 static void usage (int);
65 /** Globals and flags. */
69 /* This flag distinguishes between ar and ranlib:
70 1 means this is 'ranlib'; 0 means this is 'ar'.
71 -1 means if we should use argv[0] to decide. */
74 /* Nonzero means don't warn about creating the archive file if necessary. */
75 int silent_create
= 0;
77 /* Nonzero means describe each action performed. */
80 /* Nonzero means preserve dates of members when extracting them. */
81 int preserve_dates
= 0;
83 /* Nonzero means don't replace existing members whose dates are more recent
84 than the corresponding files. */
87 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
88 member). -1 means we've been explicitly asked to not write a symbol table;
89 +1 means we've been explicitly asked to write it;
91 Traditionally, the default in BSD has been to not write the table.
92 However, for POSIX.2 compliance the default is now to write a symbol table
93 if any of the members are object files. */
96 /* Operate in deterministic mode: write zero for timestamps, uids,
97 and gids for archive members and the archive symbol table, and write
98 consistent file modes. */
99 int deterministic
= -1; /* Determinism indeterminate. */
101 /* Nonzero means it's the name of an existing member; position new or moved
102 files with respect to this one. */
103 char *posname
= NULL
;
105 /* Sez how to use `posname': pos_before means position before that member.
106 pos_after means position after that member. pos_end means always at end.
107 pos_default means default appropriately. For the latter two, `posname'
108 should also be zero. */
111 pos_default
, pos_before
, pos_after
, pos_end
112 } postype
= pos_default
;
116 none
= 0, del
, replace
, print_table
,
117 print_files
, extract
, move
, quick_append
121 get_pos_bfd (bfd
**, enum pos
, const char *);
123 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
124 extract the COUNTED_NAME_COUNTER instance of that name. */
125 static bfd_boolean counted_name_mode
= 0;
126 static int counted_name_counter
= 0;
128 /* Whether to truncate names of files stored in the archive. */
129 static bfd_boolean ar_truncate
= FALSE
;
131 /* Whether to use a full file name match when searching an archive.
132 This is convenient for archives created by the Microsoft lib
134 static bfd_boolean full_pathname
= FALSE
;
136 /* Whether to create a "thin" archive (symbol index only -- no files). */
137 static bfd_boolean make_thin_archive
= FALSE
;
139 static int show_version
= 0;
141 static int show_help
= 0;
143 static const char *plugin_target
= NULL
;
145 static const char *target
= NULL
;
147 #define OPTION_PLUGIN 201
148 #define OPTION_TARGET 202
150 static struct option long_options
[] =
152 {"help", no_argument
, &show_help
, 1},
153 {"plugin", required_argument
, NULL
, OPTION_PLUGIN
},
154 {"target", required_argument
, NULL
, OPTION_TARGET
},
155 {"version", no_argument
, &show_version
, 1},
156 {NULL
, no_argument
, NULL
, 0}
164 interactive
= isatty (fileno (stdin
));
168 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
169 COUNT is the length of the FILES chain; FUNCTION is called on each entry
170 whose name matches one in FILES. */
173 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
180 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
188 /* This may appear to be a baroque way of accomplishing what we want.
189 However we have to iterate over the filenames in order to notice where
190 a filename is requested but does not exist in the archive. Ditto
191 mapping over each file each time -- we want to hack multiple
194 for (; count
> 0; files
++, count
--)
196 bfd_boolean found
= FALSE
;
199 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
201 const char * filename
;
204 filename
= head
->filename
;
205 if (filename
== NULL
)
207 /* Some archive formats don't get the filenames filled in
208 until the elements are opened. */
210 bfd_stat_arch_elt (head
, &buf
);
212 else if (bfd_is_thin_archive (arch
))
214 /* Thin archives store full pathnames. Need to normalize. */
215 filename
= normalize (filename
, arch
);
219 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
222 if (counted_name_mode
223 && match_count
!= counted_name_counter
)
225 /* Counting, and didn't match on count; go on to the
236 /* xgettext:c-format */
237 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
241 bfd_boolean operation_alters_arch
= FALSE
;
248 s
= help
? stdout
: stderr
;
250 #if BFD_SUPPORTS_PLUGINS
251 /* xgettext:c-format */
252 const char *command_line
253 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
254 " [--plugin <name>] [member-name] [count] archive-file file...\n");
257 /* xgettext:c-format */
258 const char *command_line
259 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
260 " [member-name] [count] archive-file file...\n");
262 fprintf (s
, command_line
, program_name
);
264 /* xgettext:c-format */
265 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
266 fprintf (s
, _(" commands:\n"));
267 fprintf (s
, _(" d - delete file(s) from the archive\n"));
268 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
269 fprintf (s
, _(" p - print file(s) found in the archive\n"));
270 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
271 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
272 fprintf (s
, _(" s - act as ranlib\n"));
273 fprintf (s
, _(" t - display contents of archive\n"));
274 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
275 fprintf (s
, _(" command specific modifiers:\n"));
276 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
277 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
278 if (DEFAULT_AR_DETERMINISTIC
)
281 [D] - use zero for timestamps and uids/gids (default)\n"));
283 [U] - use actual timestamps and uids/gids\n"));
288 [D] - use zero for timestamps and uids/gids\n"));
290 [U] - use actual timestamps and uids/gids (default)\n"));
292 fprintf (s
, _(" [N] - use instance [count] of name\n"));
293 fprintf (s
, _(" [f] - truncate inserted file names\n"));
294 fprintf (s
, _(" [P] - use full path names when matching\n"));
295 fprintf (s
, _(" [o] - preserve original dates\n"));
296 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
297 fprintf (s
, _(" generic modifiers:\n"));
298 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
299 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
300 fprintf (s
, _(" [S] - do not build a symbol table\n"));
301 fprintf (s
, _(" [T] - make a thin archive\n"));
302 fprintf (s
, _(" [v] - be verbose\n"));
303 fprintf (s
, _(" [V] - display the version number\n"));
304 fprintf (s
, _(" @<file> - read options from <file>\n"));
305 fprintf (s
, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
306 #if BFD_SUPPORTS_PLUGINS
307 fprintf (s
, _(" optional:\n"));
308 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
313 list_supported_targets (program_name
, s
);
315 if (REPORT_BUGS_TO
[0] && help
)
316 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
318 xexit (help
? 0 : 1);
322 ranlib_usage (int help
)
326 s
= help
? stdout
: stderr
;
328 /* xgettext:c-format */
329 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
330 fprintf (s
, _(" Generate an index to speed access to archives\n"));
331 fprintf (s
, _(" The options are:\n\
332 @<file> Read options from <file>\n"));
333 #if BFD_SUPPORTS_PLUGINS
335 --plugin <name> Load the specified plugin\n"));
337 if (DEFAULT_AR_DETERMINISTIC
)
339 -D Use zero for symbol map timestamp (default)\n\
340 -U Use an actual symbol map timestamp\n"));
343 -D Use zero for symbol map timestamp\n\
344 -U Use actual symbol map timestamp (default)\n"));
346 -t Update the archive's symbol map timestamp\n\
347 -h --help Print this help message\n\
348 -v --version Print version information\n"));
350 list_supported_targets (program_name
, s
);
352 if (REPORT_BUGS_TO
[0] && help
)
353 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
355 xexit (help
? 0 : 1);
358 /* Normalize a file name specified on the command line into a file
359 name which we will use in an archive. */
362 normalize (const char *file
, bfd
*abfd
)
364 const char *filename
;
369 filename
= lbasename (file
);
373 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
378 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
379 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
380 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
387 /* Remove any output file. This is only called via xatexit. */
389 static const char *output_filename
= NULL
;
390 static FILE *output_file
= NULL
;
391 static bfd
*output_bfd
= NULL
;
396 if (output_filename
!= NULL
)
398 if (output_bfd
!= NULL
)
399 bfd_cache_close (output_bfd
);
400 if (output_file
!= NULL
)
401 fclose (output_file
);
402 unlink_if_ordinary (output_filename
);
407 decode_options (int argc
, char **argv
)
411 /* Convert old-style tar call by exploding option element and rearranging
412 options accordingly. */
414 if (argc
> 1 && argv
[1][0] != '-')
416 int new_argc
; /* argc value for rearranged arguments */
417 char **new_argv
; /* argv value for rearranged arguments */
418 char *const *in
; /* cursor into original argv */
419 char **out
; /* cursor into rearranged argv */
420 const char *letter
; /* cursor into old option letters */
421 char buffer
[3]; /* constructed option buffer */
423 /* Initialize a constructed option. */
428 /* Allocate a new argument array, and copy program name in it. */
430 new_argc
= argc
- 1 + strlen (argv
[1]);
431 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
436 /* Copy each old letter option as a separate option. */
438 for (letter
= *in
++; *letter
; letter
++)
441 *out
++ = xstrdup (buffer
);
444 /* Copy all remaining options. */
446 while (in
< argv
+ argc
)
450 /* Replace the old option list by the new one. */
456 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxlcoVsSuvabiMNfPTDU",
457 long_options
, NULL
)) != EOF
)
468 if (operation
!= none
)
469 fatal (_("two different operation options specified"));
480 operation_alters_arch
= TRUE
;
484 operation_alters_arch
= TRUE
;
487 operation
= print_files
;
490 operation
= quick_append
;
491 operation_alters_arch
= TRUE
;
495 operation_alters_arch
= TRUE
;
498 operation
= print_table
;
530 postype
= pos_before
;
533 postype
= pos_before
;
539 counted_name_mode
= TRUE
;
545 full_pathname
= TRUE
;
548 make_thin_archive
= TRUE
;
551 deterministic
= TRUE
;
554 deterministic
= FALSE
;
557 #if BFD_SUPPORTS_PLUGINS
558 plugin_target
= "plugin";
559 bfd_plugin_set_plugin (optarg
);
561 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
568 case 0: /* A long option that just sets a flag. */
575 return &argv
[optind
];
578 /* If neither -D nor -U was not specified explicitly,
579 then use the configured default. */
581 default_deterministic (void)
583 if (deterministic
< 0)
584 deterministic
= DEFAULT_AR_DETERMINISTIC
;
588 ranlib_main (int argc
, char **argv
)
590 int arg_index
, status
= 0;
591 bfd_boolean touch
= FALSE
;
594 while ((c
= getopt_long (argc
, argv
, "DhHUvVt", long_options
, NULL
)) != EOF
)
599 deterministic
= TRUE
;
602 deterministic
= FALSE
;
616 /* PR binutils/13493: Support plugins. */
618 #if BFD_SUPPORTS_PLUGINS
619 plugin_target
= "plugin";
620 bfd_plugin_set_plugin (optarg
);
622 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
636 print_version ("ranlib");
638 default_deterministic ();
642 while (arg_index
< argc
)
645 status
|= ranlib_only (argv
[arg_index
]);
647 status
|= ranlib_touch (argv
[arg_index
]);
654 int main (int, char **);
657 main (int argc
, char **argv
)
662 char *inarch_filename
;
665 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
666 setlocale (LC_MESSAGES
, "");
668 #if defined (HAVE_SETLOCALE)
669 setlocale (LC_CTYPE
, "");
671 bindtextdomain (PACKAGE
, LOCALEDIR
);
672 textdomain (PACKAGE
);
674 program_name
= argv
[0];
675 xmalloc_set_program_name (program_name
);
676 #if BFD_SUPPORTS_PLUGINS
677 bfd_plugin_set_program_name (program_name
);
680 expandargv (&argc
, &argv
);
684 const char *temp
= lbasename (program_name
);
686 if (strlen (temp
) >= 6
687 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
693 START_PROGRESS (program_name
, 0);
696 set_default_bfd_target ();
698 xatexit (remove_output
);
700 for (i
= 1; i
< argc
; i
++)
701 if (! ar_emul_parse_arg (argv
[i
]))
707 ranlib_main (argc
, argv
);
712 argv
= decode_options (argc
, argv
);
718 print_version ("ar");
730 /* We don't use do_quick_append any more. Too many systems
731 expect ar to always rebuild the symbol table even when q is
734 /* We can't write an armap when using ar q, so just do ar r
736 if (operation
== quick_append
&& write_armap
)
739 if ((operation
== none
|| operation
== print_table
)
741 xexit (ranlib_only (argv
[arg_index
]));
743 if (operation
== none
)
744 fatal (_("no operation specified"));
746 if (newer_only
&& operation
!= replace
)
747 fatal (_("`u' is only meaningful with the `r' option."));
749 if (newer_only
&& deterministic
> 0)
750 fatal (_("`u' is not meaningful with the `D' option."));
752 if (newer_only
&& deterministic
< 0 && DEFAULT_AR_DETERMINISTIC
)
754 `u' modifier ignored since `D' is the default (see `U')"));
756 default_deterministic ();
758 if (postype
!= pos_default
)
759 posname
= argv
[arg_index
++];
761 if (counted_name_mode
)
763 if (operation
!= extract
&& operation
!= del
)
764 fatal (_("`N' is only meaningful with the `x' and `d' options."));
765 counted_name_counter
= atoi (argv
[arg_index
++]);
766 if (counted_name_counter
<= 0)
767 fatal (_("Value for `N' must be positive."));
770 inarch_filename
= argv
[arg_index
++];
772 for (file_count
= 0; argv
[arg_index
+ file_count
] != NULL
; file_count
++)
775 files
= (file_count
> 0) ? argv
+ arg_index
: NULL
;
777 arch
= open_inarch (inarch_filename
,
778 files
== NULL
? (char *) NULL
: files
[0]);
780 if (operation
== extract
&& bfd_is_thin_archive (arch
))
781 fatal (_("`x' cannot be used on thin archives."));
786 map_over_members (arch
, print_descr
, files
, file_count
);
790 map_over_members (arch
, print_contents
, files
, file_count
);
794 map_over_members (arch
, extract_file
, files
, file_count
);
799 delete_members (arch
, files
);
801 output_filename
= NULL
;
805 /* PR 12558: Creating and moving at the same time does
806 not make sense. Just create the archive instead. */
810 move_members (arch
, files
);
812 output_filename
= NULL
;
819 if (files
!= NULL
|| write_armap
> 0)
820 replace_members (arch
, files
, operation
== quick_append
);
822 output_filename
= NULL
;
825 /* Shouldn't happen! */
827 /* xgettext:c-format */
828 fatal (_("internal error -- this option not implemented"));
832 END_PROGRESS (program_name
);
839 open_inarch (const char *archive_filename
, const char *file
)
847 bfd_set_error (bfd_error_no_error
);
850 target
= plugin_target
;
852 if (stat (archive_filename
, &sbuf
) != 0)
854 #if !defined(__GO32__) || defined(__DJGPP__)
856 /* FIXME: I don't understand why this fragment was ifndef'ed
857 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
858 stat() works just fine in v2.x, so I think this should be
859 removed. For now, I enable it for DJGPP v2. -- EZ. */
861 /* KLUDGE ALERT! Temporary fix until I figger why
862 stat() is wrong ... think it's buried in GO32's IDT - Jax */
864 bfd_fatal (archive_filename
);
867 if (!operation_alters_arch
)
869 fprintf (stderr
, "%s: ", program_name
);
870 perror (archive_filename
);
875 /* If the target isn't set, try to figure out the target to use
876 for the archive from the first object on the list. */
877 if (target
== NULL
&& file
!= NULL
)
881 obj
= bfd_openr (file
, target
);
884 if (bfd_check_format (obj
, bfd_object
))
885 target
= bfd_get_target (obj
);
886 (void) bfd_close (obj
);
890 /* Create an empty archive. */
891 arch
= bfd_openw (archive_filename
, target
);
893 || ! bfd_set_format (arch
, bfd_archive
)
894 || ! bfd_close (arch
))
895 bfd_fatal (archive_filename
);
896 else if (!silent_create
)
897 non_fatal (_("creating %s"), archive_filename
);
899 /* If we die creating a new archive, don't leave it around. */
900 output_filename
= archive_filename
;
903 arch
= bfd_openr (archive_filename
, target
);
907 bfd_fatal (archive_filename
);
910 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
912 bfd_nonfatal (archive_filename
);
913 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
915 list_matching_formats (matching
);
921 last_one
= &(arch
->archive_next
);
922 /* Read all the contents right away, regardless. */
923 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
925 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
928 *last_one
= next_one
;
929 last_one
= &next_one
->archive_next
;
931 *last_one
= (bfd
*) NULL
;
932 if (bfd_get_error () != bfd_error_no_more_archived_files
)
938 print_contents (bfd
*abfd
)
941 char *cbuf
= (char *) xmalloc (BUFSIZE
);
944 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
945 /* xgettext:c-format */
946 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
949 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
951 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
954 while (ncopied
< size
)
958 size_t tocopy
= size
- ncopied
;
959 if (tocopy
> BUFSIZE
)
962 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
964 /* xgettext:c-format */
965 fatal (_("%s is not a valid archive"),
966 bfd_get_filename (bfd_my_archive (abfd
)));
968 /* fwrite in mingw32 may return int instead of size_t. Cast the
969 return value to size_t to avoid comparison between signed and
971 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
972 fatal ("stdout: %s", strerror (errno
));
978 /* Extract a member of the archive into its own file.
980 We defer opening the new file until after we have read a BUFSIZ chunk of the
981 old one, since we know we have just read the archive header for the old
982 one. Since most members are shorter than BUFSIZ, this means we will read
983 the old header, read the old data, write a new inode for the new file, and
984 write the new data, and be done. This 'optimization' is what comes from
985 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
989 extract_file (bfd
*abfd
)
992 char *cbuf
= (char *) xmalloc (BUFSIZE
);
993 size_t nread
, tocopy
;
998 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
999 /* xgettext:c-format */
1000 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
1004 printf ("x - %s\n", bfd_get_filename (abfd
));
1006 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
1011 /* Seems like an abstraction violation, eh? Well it's OK! */
1012 output_filename
= bfd_get_filename (abfd
);
1014 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
1015 if (ostream
== NULL
)
1017 perror (bfd_get_filename (abfd
));
1021 output_file
= ostream
;
1024 while (ncopied
< size
)
1026 tocopy
= size
- ncopied
;
1027 if (tocopy
> BUFSIZE
)
1030 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
1031 if (nread
!= tocopy
)
1032 /* xgettext:c-format */
1033 fatal (_("%s is not a valid archive"),
1034 bfd_get_filename (bfd_my_archive (abfd
)));
1036 /* See comment above; this saves disk arm motion */
1037 if (ostream
== NULL
)
1039 /* Seems like an abstraction violation, eh? Well it's OK! */
1040 output_filename
= bfd_get_filename (abfd
);
1042 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
1043 if (ostream
== NULL
)
1045 perror (bfd_get_filename (abfd
));
1049 output_file
= ostream
;
1052 /* fwrite in mingw32 may return int instead of size_t. Cast
1053 the return value to size_t to avoid comparison between
1054 signed and unsigned values. */
1055 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
1056 fatal ("%s: %s", output_filename
, strerror (errno
));
1060 if (ostream
!= NULL
)
1064 output_filename
= NULL
;
1066 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
1070 /* Set access time to modification time. Only st_mtime is
1071 initialized by bfd_stat_arch_elt. */
1072 buf
.st_atime
= buf
.st_mtime
;
1073 set_times (bfd_get_filename (abfd
), &buf
);
1080 write_archive (bfd
*iarch
)
1083 char *old_name
, *new_name
;
1084 bfd
*contents_head
= iarch
->archive_next
;
1086 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1087 strcpy (old_name
, bfd_get_filename (iarch
));
1088 new_name
= make_tempname (old_name
);
1090 if (new_name
== NULL
)
1091 bfd_fatal (_("could not create temporary file whilst writing archive"));
1093 output_filename
= new_name
;
1095 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1098 bfd_fatal (old_name
);
1102 bfd_set_format (obfd
, bfd_archive
);
1104 /* Request writing the archive symbol table unless we've
1105 been explicitly requested not to. */
1106 obfd
->has_armap
= write_armap
>= 0;
1110 /* This should really use bfd_set_file_flags, but that rejects
1112 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1116 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1118 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1119 bfd_is_thin_archive (obfd
) = 1;
1121 if (!bfd_set_archive_head (obfd
, contents_head
))
1122 bfd_fatal (old_name
);
1124 if (!bfd_close (obfd
))
1125 bfd_fatal (old_name
);
1128 output_filename
= NULL
;
1130 /* We don't care if this fails; we might be creating the archive. */
1133 if (smart_rename (new_name
, old_name
, 0) != 0)
1138 /* Return a pointer to the pointer to the entry which should be rplacd'd
1139 into when altering. DEFAULT_POS should be how to interpret pos_default,
1140 and should be a pos value. */
1143 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1145 bfd
**after_bfd
= contents
;
1147 const char *realposname
;
1149 if (postype
== pos_default
)
1151 realpos
= default_pos
;
1152 realposname
= default_posname
;
1157 realposname
= posname
;
1160 if (realpos
== pos_end
)
1163 after_bfd
= &((*after_bfd
)->archive_next
);
1167 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1168 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1170 if (realpos
== pos_after
)
1171 after_bfd
= &(*after_bfd
)->archive_next
;
1179 delete_members (bfd
*arch
, char **files_to_delete
)
1181 bfd
**current_ptr_ptr
;
1183 bfd_boolean something_changed
= FALSE
;
1186 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1188 /* In a.out systems, the armap is optional. It's also called
1189 __.SYMDEF. So if the user asked to delete it, we should remember
1190 that fact. This isn't quite right for COFF systems (where
1191 __.SYMDEF might be regular member), but it's very unlikely
1192 to be a problem. FIXME */
1194 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1196 arch
->has_armap
= FALSE
;
1203 current_ptr_ptr
= &(arch
->archive_next
);
1204 while (*current_ptr_ptr
)
1206 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1207 (*current_ptr_ptr
)->filename
) == 0)
1210 if (counted_name_mode
1211 && match_count
!= counted_name_counter
)
1213 /* Counting, and didn't match on count; go on to the
1219 something_changed
= TRUE
;
1223 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1228 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1231 if (verbose
&& !found
)
1233 /* xgettext:c-format */
1234 printf (_("No member named `%s'\n"), *files_to_delete
);
1240 if (something_changed
)
1241 write_archive (arch
);
1243 output_filename
= NULL
;
1247 /* Reposition existing members within an archive */
1250 move_members (bfd
*arch
, char **files_to_move
)
1252 bfd
**after_bfd
; /* New entries go after this one */
1253 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1255 for (; *files_to_move
; ++files_to_move
)
1257 current_ptr_ptr
= &(arch
->archive_next
);
1258 while (*current_ptr_ptr
)
1260 bfd
*current_ptr
= *current_ptr_ptr
;
1261 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1262 current_ptr
->filename
) == 0)
1264 /* Move this file to the end of the list - first cut from
1267 *current_ptr_ptr
= current_ptr
->archive_next
;
1269 /* Now glue to end */
1270 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1271 link_bfd
= *after_bfd
;
1272 *after_bfd
= current_ptr
;
1273 current_ptr
->archive_next
= link_bfd
;
1276 printf ("m - %s\n", *files_to_move
);
1281 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1283 /* xgettext:c-format */
1284 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1289 write_archive (arch
);
1292 /* Ought to default to replacing in place, but this is existing practice! */
1295 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1297 bfd_boolean changed
= FALSE
;
1298 bfd
**after_bfd
; /* New entries go after this one. */
1302 while (files_to_move
&& *files_to_move
)
1306 current_ptr
= &arch
->archive_next
;
1307 while (*current_ptr
)
1309 current
= *current_ptr
;
1311 /* For compatibility with existing ar programs, we
1312 permit the same file to be added multiple times. */
1313 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1314 normalize (current
->filename
, arch
)) == 0
1315 && current
->arelt_data
!= NULL
)
1319 struct stat fsbuf
, asbuf
;
1321 if (stat (*files_to_move
, &fsbuf
) != 0)
1323 if (errno
!= ENOENT
)
1324 bfd_fatal (*files_to_move
);
1327 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1328 /* xgettext:c-format */
1329 fatal (_("internal stat error on %s"),
1332 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1336 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1338 if (ar_emul_replace (after_bfd
, *files_to_move
,
1341 /* Snip out this entry from the chain. */
1342 *current_ptr
= (*current_ptr
)->archive_next
;
1348 current_ptr
= &(current
->archive_next
);
1352 /* Add to the end of the archive. */
1353 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1355 if (ar_emul_append (after_bfd
, *files_to_move
, target
,
1356 verbose
, make_thin_archive
))
1365 write_archive (arch
);
1367 output_filename
= NULL
;
1371 ranlib_only (const char *archname
)
1375 if (get_file_size (archname
) < 1)
1378 arch
= open_inarch (archname
, (char *) NULL
);
1381 write_archive (arch
);
1385 /* Update the timestamp of the symbol map of an archive. */
1388 ranlib_touch (const char *archname
)
1391 /* I don't think updating works on go32. */
1392 ranlib_only (archname
);
1398 if (get_file_size (archname
) < 1)
1400 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1403 bfd_set_error (bfd_error_system_call
);
1404 bfd_fatal (archname
);
1407 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1409 bfd_fatal (archname
);
1410 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1412 bfd_nonfatal (archname
);
1413 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1415 list_matching_formats (matching
);
1421 if (! bfd_has_map (arch
))
1422 /* xgettext:c-format */
1423 fatal (_("%s: no archive map to update"), archname
);
1426 arch
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1428 bfd_update_armap_timestamp (arch
);
1430 if (! bfd_close (arch
))
1431 bfd_fatal (archname
);
1436 /* Things which are interesting to map over all or some of the files: */
1439 print_descr (bfd
*abfd
)
1441 print_arelt_descr (stdout
, abfd
, verbose
);