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
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"
43 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
45 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
48 /* Static declarations. */
50 static void mri_emul (void);
51 static const char *normalize (const char *, bfd
*);
52 static void remove_output (void);
53 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
54 static void print_contents (bfd
* member
);
55 static void delete_members (bfd
*, char **files_to_delete
);
57 static void move_members (bfd
*, char **files_to_move
);
58 static void replace_members
59 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
60 static void print_descr (bfd
* abfd
);
61 static void write_archive (bfd
*);
62 static int ranlib_only (const char *archname
);
63 static int ranlib_touch (const char *archname
);
64 static void usage (int);
66 /** Globals and flags. */
70 /* This flag distinguishes between ar and ranlib:
71 1 means this is 'ranlib'; 0 means this is 'ar'.
72 -1 means if we should use argv[0] to decide. */
75 /* Nonzero means don't warn about creating the archive file if necessary. */
76 int silent_create
= 0;
78 /* Nonzero means describe each action performed. */
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
= 0;
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 static const char *plugin_target
= NULL
;
146 static const char *target
= NULL
;
148 #define OPTION_PLUGIN 201
149 #define OPTION_TARGET 202
151 static struct option long_options
[] =
153 {"help", no_argument
, &show_help
, 1},
154 {"plugin", required_argument
, NULL
, OPTION_PLUGIN
},
155 {"target", required_argument
, NULL
, OPTION_TARGET
},
156 {"version", no_argument
, &show_version
, 1},
157 {NULL
, no_argument
, NULL
, 0}
165 interactive
= isatty (fileno (stdin
));
169 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
170 COUNT is the length of the FILES chain; FUNCTION is called on each entry
171 whose name matches one in FILES. */
174 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
181 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
189 /* This may appear to be a baroque way of accomplishing what we want.
190 However we have to iterate over the filenames in order to notice where
191 a filename is requested but does not exist in the archive. Ditto
192 mapping over each file each time -- we want to hack multiple
195 for (; count
> 0; files
++, count
--)
197 bfd_boolean found
= FALSE
;
200 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
202 const char * filename
;
205 filename
= head
->filename
;
206 if (filename
== NULL
)
208 /* Some archive formats don't get the filenames filled in
209 until the elements are opened. */
211 bfd_stat_arch_elt (head
, &buf
);
213 else if (bfd_is_thin_archive (arch
))
215 /* Thin archives store full pathnames. Need to normalize. */
216 filename
= normalize (filename
, arch
);
220 && !FILENAME_CMP (normalize (*files
, arch
), filename
))
223 if (counted_name_mode
224 && match_count
!= counted_name_counter
)
226 /* Counting, and didn't match on count; go on to the
237 /* xgettext:c-format */
238 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
242 bfd_boolean operation_alters_arch
= FALSE
;
249 s
= help
? stdout
: stderr
;
251 #if BFD_SUPPORTS_PLUGINS
252 /* xgettext:c-format */
253 const char *command_line
254 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
255 " [--plugin <name>] [member-name] [count] archive-file file...\n");
258 /* xgettext:c-format */
259 const char *command_line
260 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
261 " [member-name] [count] archive-file file...\n");
263 fprintf (s
, command_line
, program_name
);
265 /* xgettext:c-format */
266 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
267 fprintf (s
, _(" commands:\n"));
268 fprintf (s
, _(" d - delete file(s) from the archive\n"));
269 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
270 fprintf (s
, _(" p - print file(s) found in the archive\n"));
271 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
272 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
273 fprintf (s
, _(" s - act as ranlib\n"));
274 fprintf (s
, _(" t - display contents of archive\n"));
275 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
276 fprintf (s
, _(" command specific modifiers:\n"));
277 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
278 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
279 fprintf (s
, _(" [D] - use zero for timestamps and uids/gids\n"));
280 fprintf (s
, _(" [N] - use instance [count] of name\n"));
281 fprintf (s
, _(" [f] - truncate inserted file names\n"));
282 fprintf (s
, _(" [P] - use full path names when matching\n"));
283 fprintf (s
, _(" [o] - preserve original dates\n"));
284 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
285 fprintf (s
, _(" generic modifiers:\n"));
286 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
287 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
288 fprintf (s
, _(" [S] - do not build a symbol table\n"));
289 fprintf (s
, _(" [T] - make a thin archive\n"));
290 fprintf (s
, _(" [v] - be verbose\n"));
291 fprintf (s
, _(" [V] - display the version number\n"));
292 fprintf (s
, _(" @<file> - read options from <file>\n"));
293 fprintf (s
, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
294 #if BFD_SUPPORTS_PLUGINS
295 fprintf (s
, _(" optional:\n"));
296 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
301 list_supported_targets (program_name
, s
);
303 if (REPORT_BUGS_TO
[0] && help
)
304 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
306 xexit (help
? 0 : 1);
310 ranlib_usage (int help
)
314 s
= help
? stdout
: stderr
;
316 /* xgettext:c-format */
317 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
318 fprintf (s
, _(" Generate an index to speed access to archives\n"));
319 fprintf (s
, _(" The options are:\n\
320 @<file> Read options from <file>\n"));
321 #if BFD_SUPPORTS_PLUGINS
323 --plugin <name> Load the specified plugin\n"));
326 -t Update the archive's symbol map timestamp\n\
327 -h --help Print this help message\n\
328 -v --version Print version information\n"));
330 list_supported_targets (program_name
, s
);
332 if (REPORT_BUGS_TO
[0] && help
)
333 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
335 xexit (help
? 0 : 1);
338 /* Normalize a file name specified on the command line into a file
339 name which we will use in an archive. */
342 normalize (const char *file
, bfd
*abfd
)
344 const char *filename
;
349 filename
= lbasename (file
);
353 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
358 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
359 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
360 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
367 /* Remove any output file. This is only called via xatexit. */
369 static const char *output_filename
= NULL
;
370 static FILE *output_file
= NULL
;
371 static bfd
*output_bfd
= NULL
;
376 if (output_filename
!= NULL
)
378 if (output_bfd
!= NULL
)
379 bfd_cache_close (output_bfd
);
380 if (output_file
!= NULL
)
381 fclose (output_file
);
382 unlink_if_ordinary (output_filename
);
387 decode_options (int argc
, char **argv
)
391 /* Convert old-style tar call by exploding option element and rearranging
392 options accordingly. */
394 if (argc
> 1 && argv
[1][0] != '-')
396 int new_argc
; /* argc value for rearranged arguments */
397 char **new_argv
; /* argv value for rearranged arguments */
398 char *const *in
; /* cursor into original argv */
399 char **out
; /* cursor into rearranged argv */
400 const char *letter
; /* cursor into old option letters */
401 char buffer
[3]; /* constructed option buffer */
403 /* Initialize a constructed option. */
408 /* Allocate a new argument array, and copy program name in it. */
410 new_argc
= argc
- 1 + strlen (argv
[1]);
411 new_argv
= xmalloc ((new_argc
+ 1) * sizeof (*argv
));
416 /* Copy each old letter option as a separate option. */
418 for (letter
= *in
++; *letter
; letter
++)
421 *out
++ = xstrdup (buffer
);
424 /* Copy all remaining options. */
426 while (in
< argv
+ argc
)
430 /* Replace the old option list by the new one. */
436 while ((c
= getopt_long (argc
, argv
, "hdmpqrtxlcoVsSuvabiMNfPTD",
437 long_options
, NULL
)) != EOF
)
448 if (operation
!= none
)
449 fatal (_("two different operation options specified"));
460 operation_alters_arch
= TRUE
;
464 operation_alters_arch
= TRUE
;
467 operation
= print_files
;
470 operation
= quick_append
;
471 operation_alters_arch
= TRUE
;
475 operation_alters_arch
= TRUE
;
478 operation
= print_table
;
510 postype
= pos_before
;
513 postype
= pos_before
;
519 counted_name_mode
= TRUE
;
525 full_pathname
= TRUE
;
528 make_thin_archive
= TRUE
;
531 deterministic
= TRUE
;
534 #if BFD_SUPPORTS_PLUGINS
535 plugin_target
= "plugin";
536 bfd_plugin_set_plugin (optarg
);
538 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
545 case 0: /* A long option that just sets a flag. */
552 return &argv
[optind
];
556 ranlib_main (int argc
, char **argv
)
558 int arg_index
, status
= 0;
559 bfd_boolean touch
= FALSE
;
562 while ((c
= getopt_long (argc
, argv
, "hHvVt", long_options
, NULL
)) != EOF
)
587 print_version ("ranlib");
591 while (arg_index
< argc
)
594 status
|= ranlib_only (argv
[arg_index
]);
596 status
|= ranlib_touch (argv
[arg_index
]);
603 int main (int, char **);
606 main (int argc
, char **argv
)
611 char *inarch_filename
;
614 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
615 setlocale (LC_MESSAGES
, "");
617 #if defined (HAVE_SETLOCALE)
618 setlocale (LC_CTYPE
, "");
620 bindtextdomain (PACKAGE
, LOCALEDIR
);
621 textdomain (PACKAGE
);
623 program_name
= argv
[0];
624 xmalloc_set_program_name (program_name
);
625 #if BFD_SUPPORTS_PLUGINS
626 bfd_plugin_set_program_name (program_name
);
629 expandargv (&argc
, &argv
);
633 const char *temp
= lbasename (program_name
);
635 if (strlen (temp
) >= 6
636 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
642 START_PROGRESS (program_name
, 0);
645 set_default_bfd_target ();
647 xatexit (remove_output
);
649 for (i
= 1; i
< argc
; i
++)
650 if (! ar_emul_parse_arg (argv
[i
]))
656 ranlib_main (argc
, argv
);
661 argv
= decode_options (argc
, argv
);
667 print_version ("ar");
679 /* We don't use do_quick_append any more. Too many systems
680 expect ar to always rebuild the symbol table even when q is
683 /* We can't write an armap when using ar q, so just do ar r
685 if (operation
== quick_append
&& write_armap
)
688 if ((operation
== none
|| operation
== print_table
)
690 xexit (ranlib_only (argv
[arg_index
]));
692 if (operation
== none
)
693 fatal (_("no operation specified"));
695 if (newer_only
&& operation
!= replace
)
696 fatal (_("`u' is only meaningful with the `r' option."));
698 if (newer_only
&& deterministic
)
699 fatal (_("`u' is not meaningful with the `D' option."));
701 if (postype
!= pos_default
)
702 posname
= argv
[arg_index
++];
704 if (counted_name_mode
)
706 if (operation
!= extract
&& operation
!= del
)
707 fatal (_("`N' is only meaningful with the `x' and `d' options."));
708 counted_name_counter
= atoi (argv
[arg_index
++]);
709 if (counted_name_counter
<= 0)
710 fatal (_("Value for `N' must be positive."));
713 inarch_filename
= argv
[arg_index
++];
715 for (file_count
= 0; argv
[arg_index
+ file_count
] != NULL
; file_count
++)
718 files
= (file_count
> 0) ? argv
+ arg_index
: NULL
;
720 arch
= open_inarch (inarch_filename
,
721 files
== NULL
? (char *) NULL
: files
[0]);
723 if (operation
== extract
&& bfd_is_thin_archive (arch
))
724 fatal (_("`x' cannot be used on thin archives."));
729 map_over_members (arch
, print_descr
, files
, file_count
);
733 map_over_members (arch
, print_contents
, files
, file_count
);
737 map_over_members (arch
, extract_file
, files
, file_count
);
742 delete_members (arch
, files
);
744 output_filename
= NULL
;
749 move_members (arch
, files
);
751 output_filename
= NULL
;
756 if (files
!= NULL
|| write_armap
> 0)
757 replace_members (arch
, files
, operation
== quick_append
);
759 output_filename
= NULL
;
762 /* Shouldn't happen! */
764 /* xgettext:c-format */
765 fatal (_("internal error -- this option not implemented"));
769 END_PROGRESS (program_name
);
776 open_inarch (const char *archive_filename
, const char *file
)
784 bfd_set_error (bfd_error_no_error
);
787 target
= plugin_target
;
789 if (stat (archive_filename
, &sbuf
) != 0)
791 #if !defined(__GO32__) || defined(__DJGPP__)
793 /* FIXME: I don't understand why this fragment was ifndef'ed
794 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
795 stat() works just fine in v2.x, so I think this should be
796 removed. For now, I enable it for DJGPP v2. -- EZ. */
798 /* KLUDGE ALERT! Temporary fix until I figger why
799 stat() is wrong ... think it's buried in GO32's IDT - Jax */
801 bfd_fatal (archive_filename
);
804 if (!operation_alters_arch
)
806 fprintf (stderr
, "%s: ", program_name
);
807 perror (archive_filename
);
812 /* Try to figure out the target to use for the archive from the
813 first object on the list. */
818 obj
= bfd_openr (file
, target
);
821 if (bfd_check_format (obj
, bfd_object
))
822 target
= bfd_get_target (obj
);
823 (void) bfd_close (obj
);
827 /* Create an empty archive. */
828 arch
= bfd_openw (archive_filename
, target
);
830 || ! bfd_set_format (arch
, bfd_archive
)
831 || ! bfd_close (arch
))
832 bfd_fatal (archive_filename
);
833 else if (!silent_create
)
834 non_fatal (_("creating %s"), archive_filename
);
836 /* If we die creating a new archive, don't leave it around. */
837 output_filename
= archive_filename
;
840 arch
= bfd_openr (archive_filename
, target
);
844 bfd_fatal (archive_filename
);
847 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
849 bfd_nonfatal (archive_filename
);
850 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
852 list_matching_formats (matching
);
858 last_one
= &(arch
->archive_next
);
859 /* Read all the contents right away, regardless. */
860 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
862 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
865 *last_one
= next_one
;
866 last_one
= &next_one
->archive_next
;
868 *last_one
= (bfd
*) NULL
;
869 if (bfd_get_error () != bfd_error_no_more_archived_files
)
875 print_contents (bfd
*abfd
)
878 char *cbuf
= (char *) xmalloc (BUFSIZE
);
881 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
882 /* xgettext:c-format */
883 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
886 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
888 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
891 while (ncopied
< size
)
895 size_t tocopy
= size
- ncopied
;
896 if (tocopy
> BUFSIZE
)
899 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
901 /* xgettext:c-format */
902 fatal (_("%s is not a valid archive"),
903 bfd_get_filename (bfd_my_archive (abfd
)));
905 /* fwrite in mingw32 may return int instead of size_t. Cast the
906 return value to size_t to avoid comparison between signed and
908 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
909 fatal ("stdout: %s", strerror (errno
));
915 /* Extract a member of the archive into its own file.
917 We defer opening the new file until after we have read a BUFSIZ chunk of the
918 old one, since we know we have just read the archive header for the old
919 one. Since most members are shorter than BUFSIZ, this means we will read
920 the old header, read the old data, write a new inode for the new file, and
921 write the new data, and be done. This 'optimization' is what comes from
922 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
926 extract_file (bfd
*abfd
)
929 char *cbuf
= (char *) xmalloc (BUFSIZE
);
930 size_t nread
, tocopy
;
935 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
936 /* xgettext:c-format */
937 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
941 printf ("x - %s\n", bfd_get_filename (abfd
));
943 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
948 /* Seems like an abstraction violation, eh? Well it's OK! */
949 output_filename
= bfd_get_filename (abfd
);
951 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
954 perror (bfd_get_filename (abfd
));
958 output_file
= ostream
;
961 while (ncopied
< size
)
963 tocopy
= size
- ncopied
;
964 if (tocopy
> BUFSIZE
)
967 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
969 /* xgettext:c-format */
970 fatal (_("%s is not a valid archive"),
971 bfd_get_filename (bfd_my_archive (abfd
)));
973 /* See comment above; this saves disk arm motion */
976 /* Seems like an abstraction violation, eh? Well it's OK! */
977 output_filename
= bfd_get_filename (abfd
);
979 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
982 perror (bfd_get_filename (abfd
));
986 output_file
= ostream
;
989 /* fwrite in mingw32 may return int instead of size_t. Cast
990 the return value to size_t to avoid comparison between
991 signed and unsigned values. */
992 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
993 fatal ("%s: %s", output_filename
, strerror (errno
));
1001 output_filename
= NULL
;
1003 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
1007 /* Set access time to modification time. Only st_mtime is
1008 initialized by bfd_stat_arch_elt. */
1009 buf
.st_atime
= buf
.st_mtime
;
1010 set_times (bfd_get_filename (abfd
), &buf
);
1017 write_archive (bfd
*iarch
)
1020 char *old_name
, *new_name
;
1021 bfd
*contents_head
= iarch
->archive_next
;
1023 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1024 strcpy (old_name
, bfd_get_filename (iarch
));
1025 new_name
= make_tempname (old_name
);
1027 if (new_name
== NULL
)
1028 bfd_fatal ("could not create temporary file whilst writing archive");
1030 output_filename
= new_name
;
1032 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1035 bfd_fatal (old_name
);
1039 bfd_set_format (obfd
, bfd_archive
);
1041 /* Request writing the archive symbol table unless we've
1042 been explicitly requested not to. */
1043 obfd
->has_armap
= write_armap
>= 0;
1047 /* This should really use bfd_set_file_flags, but that rejects
1049 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1053 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1055 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1056 bfd_is_thin_archive (obfd
) = 1;
1058 if (!bfd_set_archive_head (obfd
, contents_head
))
1059 bfd_fatal (old_name
);
1061 if (!bfd_close (obfd
))
1062 bfd_fatal (old_name
);
1065 output_filename
= NULL
;
1067 /* We don't care if this fails; we might be creating the archive. */
1070 if (smart_rename (new_name
, old_name
, 0) != 0)
1075 /* Return a pointer to the pointer to the entry which should be rplacd'd
1076 into when altering. DEFAULT_POS should be how to interpret pos_default,
1077 and should be a pos value. */
1080 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1082 bfd
**after_bfd
= contents
;
1084 const char *realposname
;
1086 if (postype
== pos_default
)
1088 realpos
= default_pos
;
1089 realposname
= default_posname
;
1094 realposname
= posname
;
1097 if (realpos
== pos_end
)
1100 after_bfd
= &((*after_bfd
)->archive_next
);
1104 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1105 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1107 if (realpos
== pos_after
)
1108 after_bfd
= &(*after_bfd
)->archive_next
;
1116 delete_members (bfd
*arch
, char **files_to_delete
)
1118 bfd
**current_ptr_ptr
;
1120 bfd_boolean something_changed
= FALSE
;
1123 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1125 /* In a.out systems, the armap is optional. It's also called
1126 __.SYMDEF. So if the user asked to delete it, we should remember
1127 that fact. This isn't quite right for COFF systems (where
1128 __.SYMDEF might be regular member), but it's very unlikely
1129 to be a problem. FIXME */
1131 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1133 arch
->has_armap
= FALSE
;
1140 current_ptr_ptr
= &(arch
->archive_next
);
1141 while (*current_ptr_ptr
)
1143 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1144 (*current_ptr_ptr
)->filename
) == 0)
1147 if (counted_name_mode
1148 && match_count
!= counted_name_counter
)
1150 /* Counting, and didn't match on count; go on to the
1156 something_changed
= TRUE
;
1160 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1165 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1168 if (verbose
&& !found
)
1170 /* xgettext:c-format */
1171 printf (_("No member named `%s'\n"), *files_to_delete
);
1177 if (something_changed
)
1178 write_archive (arch
);
1180 output_filename
= NULL
;
1184 /* Reposition existing members within an archive */
1187 move_members (bfd
*arch
, char **files_to_move
)
1189 bfd
**after_bfd
; /* New entries go after this one */
1190 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1192 for (; *files_to_move
; ++files_to_move
)
1194 current_ptr_ptr
= &(arch
->archive_next
);
1195 while (*current_ptr_ptr
)
1197 bfd
*current_ptr
= *current_ptr_ptr
;
1198 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1199 current_ptr
->filename
) == 0)
1201 /* Move this file to the end of the list - first cut from
1204 *current_ptr_ptr
= current_ptr
->archive_next
;
1206 /* Now glue to end */
1207 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1208 link_bfd
= *after_bfd
;
1209 *after_bfd
= current_ptr
;
1210 current_ptr
->archive_next
= link_bfd
;
1213 printf ("m - %s\n", *files_to_move
);
1218 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1220 /* xgettext:c-format */
1221 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1226 write_archive (arch
);
1229 /* Ought to default to replacing in place, but this is existing practice! */
1232 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1234 bfd_boolean changed
= FALSE
;
1235 bfd
**after_bfd
; /* New entries go after this one. */
1239 while (files_to_move
&& *files_to_move
)
1243 current_ptr
= &arch
->archive_next
;
1244 while (*current_ptr
)
1246 current
= *current_ptr
;
1248 /* For compatibility with existing ar programs, we
1249 permit the same file to be added multiple times. */
1250 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1251 normalize (current
->filename
, arch
)) == 0
1252 && current
->arelt_data
!= NULL
)
1256 struct stat fsbuf
, asbuf
;
1258 if (stat (*files_to_move
, &fsbuf
) != 0)
1260 if (errno
!= ENOENT
)
1261 bfd_fatal (*files_to_move
);
1264 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1265 /* xgettext:c-format */
1266 fatal (_("internal stat error on %s"),
1269 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1273 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1275 if (ar_emul_replace (after_bfd
, *files_to_move
,
1278 /* Snip out this entry from the chain. */
1279 *current_ptr
= (*current_ptr
)->archive_next
;
1285 current_ptr
= &(current
->archive_next
);
1289 /* Add to the end of the archive. */
1290 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1292 if (ar_emul_append (after_bfd
, *files_to_move
, target
,
1293 verbose
, make_thin_archive
))
1302 write_archive (arch
);
1304 output_filename
= NULL
;
1308 ranlib_only (const char *archname
)
1312 if (get_file_size (archname
) < 1)
1315 arch
= open_inarch (archname
, (char *) NULL
);
1318 write_archive (arch
);
1322 /* Update the timestamp of the symbol map of an archive. */
1325 ranlib_touch (const char *archname
)
1328 /* I don't think updating works on go32. */
1329 ranlib_only (archname
);
1335 if (get_file_size (archname
) < 1)
1337 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1340 bfd_set_error (bfd_error_system_call
);
1341 bfd_fatal (archname
);
1344 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1346 bfd_fatal (archname
);
1347 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1349 bfd_nonfatal (archname
);
1350 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1352 list_matching_formats (matching
);
1358 if (! bfd_has_map (arch
))
1359 /* xgettext:c-format */
1360 fatal (_("%s: no archive map to update"), archname
);
1362 bfd_update_armap_timestamp (arch
);
1364 if (! bfd_close (arch
))
1365 bfd_fatal (archname
);
1370 /* Things which are interesting to map over all or some of the files: */
1373 print_descr (bfd
*abfd
)
1375 print_arelt_descr (stdout
, abfd
, verbose
);