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
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: should use getopt the way tar does (complete w/optional -) and
25 should have long options too. GNU ar used to check file against filesystem
26 in quick_update and replace operations (would check mtime). Doesn't warn
27 when name truncated. No way to specify pos_end. Error messages should be
32 #include "libiberty.h"
38 #include "filenames.h"
44 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
46 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
49 /* Static declarations. */
51 static void mri_emul (void);
52 static const char *normalize (const char *, bfd
*);
53 static void remove_output (void);
54 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
55 static void print_contents (bfd
* member
);
56 static void delete_members (bfd
*, char **files_to_delete
);
58 static void move_members (bfd
*, char **files_to_move
);
59 static void replace_members
60 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
61 static void print_descr (bfd
* abfd
);
62 static void write_archive (bfd
*);
63 static int ranlib_only (const char *archname
);
64 static int ranlib_touch (const char *archname
);
65 static void usage (int);
67 /** Globals and flags. */
71 /* This flag distinguishes between ar and ranlib:
72 1 means this is 'ranlib'; 0 means this is 'ar'.
73 -1 means if we should use argv[0] to decide. */
76 /* Nonzero means don't warn about creating the archive file if necessary. */
77 int silent_create
= 0;
79 /* Nonzero means describe each action performed. */
82 /* Nonzero means preserve dates of members when extracting them. */
83 int preserve_dates
= 0;
85 /* Nonzero means don't replace existing members whose dates are more recent
86 than the corresponding files. */
89 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
90 member). -1 means we've been explicitly asked to not write a symbol table;
91 +1 means we've been explicitly asked to write it;
93 Traditionally, the default in BSD has been to not write the table.
94 However, for POSIX.2 compliance the default is now to write a symbol table
95 if any of the members are object files. */
98 /* Operate in deterministic mode: write zero for timestamps, uids,
99 and gids for archive members and the archive symbol table, and write
100 consistent file modes. */
101 int deterministic
= 0;
103 /* Nonzero means it's the name of an existing member; position new or moved
104 files with respect to this one. */
105 char *posname
= NULL
;
107 /* Sez how to use `posname': pos_before means position before that member.
108 pos_after means position after that member. pos_end means always at end.
109 pos_default means default appropriately. For the latter two, `posname'
110 should also be zero. */
113 pos_default
, pos_before
, pos_after
, pos_end
114 } postype
= pos_default
;
117 get_pos_bfd (bfd
**, enum pos
, const char *);
119 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
120 extract the COUNTED_NAME_COUNTER instance of that name. */
121 static bfd_boolean counted_name_mode
= 0;
122 static int counted_name_counter
= 0;
124 /* Whether to truncate names of files stored in the archive. */
125 static bfd_boolean ar_truncate
= FALSE
;
127 /* Whether to use a full file name match when searching an archive.
128 This is convenient for archives created by the Microsoft lib
130 static bfd_boolean full_pathname
= FALSE
;
132 /* Whether to create a "thin" archive (symbol index only -- no files). */
133 static bfd_boolean make_thin_archive
= FALSE
;
140 interactive
= isatty (fileno (stdin
));
144 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
145 COUNT is the length of the FILES chain; FUNCTION is called on each entry
146 whose name matches one in FILES. */
149 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
156 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
164 /* This may appear to be a baroque way of accomplishing what we want.
165 However we have to iterate over the filenames in order to notice where
166 a filename is requested but does not exist in the archive. Ditto
167 mapping over each file each time -- we want to hack multiple
170 for (; count
> 0; files
++, count
--)
172 bfd_boolean found
= FALSE
;
175 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
177 const char * filename
;
180 filename
= head
->filename
;
181 if (filename
== NULL
)
183 /* Some archive formats don't get the filenames filled in
184 until the elements are opened. */
186 bfd_stat_arch_elt (head
, &buf
);
188 else if (bfd_is_thin_archive (arch
))
190 /* Thin archives store full pathnames. Need to normalize. */
191 filename
= normalize (filename
, arch
);
194 if ((filename
!= NULL
) &&
195 (!FILENAME_CMP (normalize (*files
, arch
), filename
)))
198 if (counted_name_mode
199 && match_count
!= counted_name_counter
)
201 /* Counting, and didn't match on count; go on to the
212 /* xgettext:c-format */
213 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
217 bfd_boolean operation_alters_arch
= FALSE
;
224 s
= help
? stdout
: stderr
;
228 /* xgettext:c-format */
229 const char * command_line
=
230 #if BFD_SUPPORTS_PLUGINS
231 _("Usage: %s [emulation options] [--plugin <name>] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
233 _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
235 fprintf (s
, command_line
, program_name
);
237 /* xgettext:c-format */
238 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
239 fprintf (s
, _(" commands:\n"));
240 fprintf (s
, _(" d - delete file(s) from the archive\n"));
241 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
242 fprintf (s
, _(" p - print file(s) found in the archive\n"));
243 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
244 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
245 fprintf (s
, _(" t - display contents of archive\n"));
246 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
247 fprintf (s
, _(" command specific modifiers:\n"));
248 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
249 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
250 fprintf (s
, _(" [D] - use zero for timestamps and uids/gids\n"));
251 fprintf (s
, _(" [N] - use instance [count] of name\n"));
252 fprintf (s
, _(" [f] - truncate inserted file names\n"));
253 fprintf (s
, _(" [P] - use full path names when matching\n"));
254 fprintf (s
, _(" [o] - preserve original dates\n"));
255 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
256 fprintf (s
, _(" generic modifiers:\n"));
257 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
258 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
259 fprintf (s
, _(" [S] - do not build a symbol table\n"));
260 fprintf (s
, _(" [T] - make a thin archive\n"));
261 fprintf (s
, _(" [v] - be verbose\n"));
262 fprintf (s
, _(" [V] - display the version number\n"));
263 fprintf (s
, _(" @<file> - read options from <file>\n"));
264 #if BFD_SUPPORTS_PLUGINS
265 fprintf (s
, _(" optional:\n"));
266 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
272 /* xgettext:c-format */
273 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
274 fprintf (s
, _(" Generate an index to speed access to archives\n"));
275 fprintf (s
, _(" The options are:\n\
276 @<file> Read options from <file>\n"));
277 #if BFD_SUPPORTS_PLUGINS
279 --plugin <name> Load the specified plugin\n"));
282 -t Update the archive's symbol map timestamp\n\
283 -h --help Print this help message\n\
284 -v --version Print version information\n"));
287 list_supported_targets (program_name
, s
);
289 if (REPORT_BUGS_TO
[0] && help
)
290 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
292 xexit (help
? 0 : 1);
295 /* Normalize a file name specified on the command line into a file
296 name which we will use in an archive. */
299 normalize (const char *file
, bfd
*abfd
)
301 const char *filename
;
306 filename
= lbasename (file
);
310 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
315 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
316 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
317 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
324 /* Remove any output file. This is only called via xatexit. */
326 static const char *output_filename
= NULL
;
327 static FILE *output_file
= NULL
;
328 static bfd
*output_bfd
= NULL
;
333 if (output_filename
!= NULL
)
335 if (output_bfd
!= NULL
)
336 bfd_cache_close (output_bfd
);
337 if (output_file
!= NULL
)
338 fclose (output_file
);
339 unlink_if_ordinary (output_filename
);
343 /* The option parsing should be in its own function.
344 It will be when I have getopt working. */
346 int main (int, char **);
349 main (int argc
, char **argv
)
355 none
= 0, del
, replace
, print_table
,
356 print_files
, extract
, move
, quick_append
361 char *inarch_filename
;
366 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
367 setlocale (LC_MESSAGES
, "");
369 #if defined (HAVE_SETLOCALE)
370 setlocale (LC_CTYPE
, "");
372 bindtextdomain (PACKAGE
, LOCALEDIR
);
373 textdomain (PACKAGE
);
375 program_name
= argv
[0];
376 xmalloc_set_program_name (program_name
);
377 #if BFD_SUPPORTS_PLUGINS
378 bfd_plugin_set_program_name (program_name
);
381 expandargv (&argc
, &argv
);
385 const char *temp
= lbasename (program_name
);
387 if (strlen (temp
) >= 6
388 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
394 if (argc
> 1 && argv
[1][0] == '-')
396 if (strcmp (argv
[1], "--help") == 0)
398 else if (strcmp (argv
[1], "--version") == 0)
401 print_version ("ranlib");
403 print_version ("ar");
407 START_PROGRESS (program_name
, 0);
410 set_default_bfd_target ();
414 xatexit (remove_output
);
416 for (i
= 1; i
< argc
; i
++)
417 if (! ar_emul_parse_arg (argv
[i
]))
425 bfd_boolean touch
= FALSE
;
428 || strcmp (argv
[1], "--help") == 0
429 || strcmp (argv
[1], "-h") == 0
430 || strcmp (argv
[1], "-H") == 0)
432 if (strcmp (argv
[1], "-V") == 0
433 || strcmp (argv
[1], "-v") == 0
434 || CONST_STRNEQ (argv
[1], "--v"))
435 print_version ("ranlib");
437 if (strcmp (argv
[1], "-t") == 0)
442 while (arg_index
< argc
)
445 status
|= ranlib_only (argv
[arg_index
]);
447 status
|= ranlib_touch (argv
[arg_index
]);
453 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
463 arg_ptr
= argv
[arg_index
];
465 if (strcmp (arg_ptr
, "--plugin") == 0)
467 #if BFD_SUPPORTS_PLUGINS
471 bfd_plugin_set_plugin (argv
[2]);
474 arg_ptr
= argv
[arg_index
];
476 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
483 /* When the first option starts with '-' we support POSIX-compatible
486 ++arg_ptr
; /* compatibility */
491 while ((c
= *arg_ptr
++) != '\0')
502 if (operation
!= none
)
503 fatal (_("two different operation options specified"));
508 operation_alters_arch
= TRUE
;
512 operation_alters_arch
= TRUE
;
515 operation
= print_files
;
518 operation
= quick_append
;
519 operation_alters_arch
= TRUE
;
523 operation_alters_arch
= TRUE
;
526 operation
= print_table
;
559 postype
= pos_before
;
562 postype
= pos_before
;
568 counted_name_mode
= TRUE
;
574 full_pathname
= TRUE
;
577 make_thin_archive
= TRUE
;
580 deterministic
= TRUE
;
583 /* xgettext:c-format */
584 non_fatal (_("illegal option -- %c"), c
);
589 /* With POSIX-compatible option parsing continue with the next
590 argument if it starts with '-'. */
591 if (do_posix
&& arg_index
+ 1 < argc
&& argv
[arg_index
+ 1][0] == '-')
592 arg_ptr
= argv
[++arg_index
] + 1;
599 print_version ("ar");
602 if (arg_index
>= argc
)
613 /* We don't use do_quick_append any more. Too many systems
614 expect ar to always rebuild the symbol table even when q is
617 /* We can't write an armap when using ar q, so just do ar r
619 if (operation
== quick_append
&& write_armap
)
622 if ((operation
== none
|| operation
== print_table
)
624 xexit (ranlib_only (argv
[arg_index
]));
626 if (operation
== none
)
627 fatal (_("no operation specified"));
629 if (newer_only
&& operation
!= replace
)
630 fatal (_("`u' is only meaningful with the `r' option."));
632 if (newer_only
&& deterministic
)
633 fatal (_("`u' is not meaningful with the `D' option."));
635 if (postype
!= pos_default
)
636 posname
= argv
[arg_index
++];
638 if (counted_name_mode
)
640 if (operation
!= extract
&& operation
!= del
)
641 fatal (_("`N' is only meaningful with the `x' and `d' options."));
642 counted_name_counter
= atoi (argv
[arg_index
++]);
643 if (counted_name_counter
<= 0)
644 fatal (_("Value for `N' must be positive."));
647 inarch_filename
= argv
[arg_index
++];
649 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
650 file_count
= argc
- arg_index
;
652 arch
= open_inarch (inarch_filename
,
653 files
== NULL
? (char *) NULL
: files
[0]);
655 if (operation
== extract
&& bfd_is_thin_archive (arch
))
656 fatal (_("`x' cannot be used on thin archives."));
661 map_over_members (arch
, print_descr
, files
, file_count
);
665 map_over_members (arch
, print_contents
, files
, file_count
);
669 map_over_members (arch
, extract_file
, files
, file_count
);
674 delete_members (arch
, files
);
676 output_filename
= NULL
;
681 move_members (arch
, files
);
683 output_filename
= NULL
;
688 if (files
!= NULL
|| write_armap
> 0)
689 replace_members (arch
, files
, operation
== quick_append
);
691 output_filename
= NULL
;
694 /* Shouldn't happen! */
696 /* xgettext:c-format */
697 fatal (_("internal error -- this option not implemented"));
701 END_PROGRESS (program_name
);
708 open_inarch (const char *archive_filename
, const char *file
)
717 bfd_set_error (bfd_error_no_error
);
721 if (stat (archive_filename
, &sbuf
) != 0)
723 #if !defined(__GO32__) || defined(__DJGPP__)
725 /* FIXME: I don't understand why this fragment was ifndef'ed
726 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
727 stat() works just fine in v2.x, so I think this should be
728 removed. For now, I enable it for DJGPP v2. -- EZ. */
730 /* KLUDGE ALERT! Temporary fix until I figger why
731 stat() is wrong ... think it's buried in GO32's IDT - Jax */
733 bfd_fatal (archive_filename
);
736 if (!operation_alters_arch
)
738 fprintf (stderr
, "%s: ", program_name
);
739 perror (archive_filename
);
744 /* Try to figure out the target to use for the archive from the
745 first object on the list. */
750 obj
= bfd_openr (file
, NULL
);
753 if (bfd_check_format (obj
, bfd_object
))
754 target
= bfd_get_target (obj
);
755 (void) bfd_close (obj
);
759 /* Create an empty archive. */
760 arch
= bfd_openw (archive_filename
, target
);
762 || ! bfd_set_format (arch
, bfd_archive
)
763 || ! bfd_close (arch
))
764 bfd_fatal (archive_filename
);
765 else if (!silent_create
)
766 non_fatal (_("creating %s"), archive_filename
);
768 /* If we die creating a new archive, don't leave it around. */
769 output_filename
= archive_filename
;
772 arch
= bfd_openr (archive_filename
, target
);
776 bfd_fatal (archive_filename
);
779 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
781 bfd_nonfatal (archive_filename
);
782 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
784 list_matching_formats (matching
);
790 last_one
= &(arch
->archive_next
);
791 /* Read all the contents right away, regardless. */
792 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
794 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
797 *last_one
= next_one
;
798 last_one
= &next_one
->archive_next
;
800 *last_one
= (bfd
*) NULL
;
801 if (bfd_get_error () != bfd_error_no_more_archived_files
)
807 print_contents (bfd
*abfd
)
810 char *cbuf
= (char *) xmalloc (BUFSIZE
);
813 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
814 /* xgettext:c-format */
815 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
818 /* xgettext:c-format */
819 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd
));
821 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
824 while (ncopied
< size
)
828 size_t tocopy
= size
- ncopied
;
829 if (tocopy
> BUFSIZE
)
832 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
834 /* xgettext:c-format */
835 fatal (_("%s is not a valid archive"),
836 bfd_get_filename (bfd_my_archive (abfd
)));
838 /* fwrite in mingw32 may return int instead of size_t. Cast the
839 return value to size_t to avoid comparison between signed and
841 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
842 fatal ("stdout: %s", strerror (errno
));
848 /* Extract a member of the archive into its own file.
850 We defer opening the new file until after we have read a BUFSIZ chunk of the
851 old one, since we know we have just read the archive header for the old
852 one. Since most members are shorter than BUFSIZ, this means we will read
853 the old header, read the old data, write a new inode for the new file, and
854 write the new data, and be done. This 'optimization' is what comes from
855 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
859 extract_file (bfd
*abfd
)
862 char *cbuf
= (char *) xmalloc (BUFSIZE
);
863 size_t nread
, tocopy
;
868 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
869 /* xgettext:c-format */
870 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
874 printf ("x - %s\n", bfd_get_filename (abfd
));
876 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
881 /* Seems like an abstraction violation, eh? Well it's OK! */
882 output_filename
= bfd_get_filename (abfd
);
884 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
887 perror (bfd_get_filename (abfd
));
891 output_file
= ostream
;
894 while (ncopied
< size
)
896 tocopy
= size
- ncopied
;
897 if (tocopy
> BUFSIZE
)
900 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
902 /* xgettext:c-format */
903 fatal (_("%s is not a valid archive"),
904 bfd_get_filename (bfd_my_archive (abfd
)));
906 /* See comment above; this saves disk arm motion */
909 /* Seems like an abstraction violation, eh? Well it's OK! */
910 output_filename
= bfd_get_filename (abfd
);
912 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
915 perror (bfd_get_filename (abfd
));
919 output_file
= ostream
;
922 /* fwrite in mingw32 may return int instead of size_t. Cast
923 the return value to size_t to avoid comparison between
924 signed and unsigned values. */
925 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
926 fatal ("%s: %s", output_filename
, strerror (errno
));
934 output_filename
= NULL
;
936 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
940 /* Set access time to modification time. Only st_mtime is
941 initialized by bfd_stat_arch_elt. */
942 buf
.st_atime
= buf
.st_mtime
;
943 set_times (bfd_get_filename (abfd
), &buf
);
950 write_archive (bfd
*iarch
)
953 char *old_name
, *new_name
;
954 bfd
*contents_head
= iarch
->archive_next
;
956 old_name
= (char *) xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
957 strcpy (old_name
, bfd_get_filename (iarch
));
958 new_name
= make_tempname (old_name
);
960 if (new_name
== NULL
)
961 bfd_fatal ("could not create temporary file whilst writing archive");
963 output_filename
= new_name
;
965 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
968 bfd_fatal (old_name
);
972 bfd_set_format (obfd
, bfd_archive
);
974 /* Request writing the archive symbol table unless we've
975 been explicitly requested not to. */
976 obfd
->has_armap
= write_armap
>= 0;
980 /* This should really use bfd_set_file_flags, but that rejects
982 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
986 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
988 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
989 bfd_is_thin_archive (obfd
) = 1;
991 if (!bfd_set_archive_head (obfd
, contents_head
))
992 bfd_fatal (old_name
);
994 if (!bfd_close (obfd
))
995 bfd_fatal (old_name
);
998 output_filename
= NULL
;
1000 /* We don't care if this fails; we might be creating the archive. */
1003 if (smart_rename (new_name
, old_name
, 0) != 0)
1007 /* Return a pointer to the pointer to the entry which should be rplacd'd
1008 into when altering. DEFAULT_POS should be how to interpret pos_default,
1009 and should be a pos value. */
1012 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1014 bfd
**after_bfd
= contents
;
1016 const char *realposname
;
1018 if (postype
== pos_default
)
1020 realpos
= default_pos
;
1021 realposname
= default_posname
;
1026 realposname
= posname
;
1029 if (realpos
== pos_end
)
1032 after_bfd
= &((*after_bfd
)->archive_next
);
1036 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1037 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1039 if (realpos
== pos_after
)
1040 after_bfd
= &(*after_bfd
)->archive_next
;
1048 delete_members (bfd
*arch
, char **files_to_delete
)
1050 bfd
**current_ptr_ptr
;
1052 bfd_boolean something_changed
= FALSE
;
1055 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1057 /* In a.out systems, the armap is optional. It's also called
1058 __.SYMDEF. So if the user asked to delete it, we should remember
1059 that fact. This isn't quite right for COFF systems (where
1060 __.SYMDEF might be regular member), but it's very unlikely
1061 to be a problem. FIXME */
1063 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1065 arch
->has_armap
= FALSE
;
1072 current_ptr_ptr
= &(arch
->archive_next
);
1073 while (*current_ptr_ptr
)
1075 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1076 (*current_ptr_ptr
)->filename
) == 0)
1079 if (counted_name_mode
1080 && match_count
!= counted_name_counter
)
1082 /* Counting, and didn't match on count; go on to the
1088 something_changed
= TRUE
;
1092 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1097 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1100 if (verbose
&& !found
)
1102 /* xgettext:c-format */
1103 printf (_("No member named `%s'\n"), *files_to_delete
);
1109 if (something_changed
)
1110 write_archive (arch
);
1112 output_filename
= NULL
;
1116 /* Reposition existing members within an archive */
1119 move_members (bfd
*arch
, char **files_to_move
)
1121 bfd
**after_bfd
; /* New entries go after this one */
1122 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1124 for (; *files_to_move
; ++files_to_move
)
1126 current_ptr_ptr
= &(arch
->archive_next
);
1127 while (*current_ptr_ptr
)
1129 bfd
*current_ptr
= *current_ptr_ptr
;
1130 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1131 current_ptr
->filename
) == 0)
1133 /* Move this file to the end of the list - first cut from
1136 *current_ptr_ptr
= current_ptr
->archive_next
;
1138 /* Now glue to end */
1139 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1140 link_bfd
= *after_bfd
;
1141 *after_bfd
= current_ptr
;
1142 current_ptr
->archive_next
= link_bfd
;
1145 printf ("m - %s\n", *files_to_move
);
1150 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1152 /* xgettext:c-format */
1153 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1158 write_archive (arch
);
1161 /* Ought to default to replacing in place, but this is existing practice! */
1164 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1166 bfd_boolean changed
= FALSE
;
1167 bfd
**after_bfd
; /* New entries go after this one. */
1171 while (files_to_move
&& *files_to_move
)
1175 current_ptr
= &arch
->archive_next
;
1176 while (*current_ptr
)
1178 current
= *current_ptr
;
1180 /* For compatibility with existing ar programs, we
1181 permit the same file to be added multiple times. */
1182 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1183 normalize (current
->filename
, arch
)) == 0
1184 && current
->arelt_data
!= NULL
)
1188 struct stat fsbuf
, asbuf
;
1190 if (stat (*files_to_move
, &fsbuf
) != 0)
1192 if (errno
!= ENOENT
)
1193 bfd_fatal (*files_to_move
);
1196 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1197 /* xgettext:c-format */
1198 fatal (_("internal stat error on %s"),
1201 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1205 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1207 if (ar_emul_replace (after_bfd
, *files_to_move
,
1210 /* Snip out this entry from the chain. */
1211 *current_ptr
= (*current_ptr
)->archive_next
;
1217 current_ptr
= &(current
->archive_next
);
1221 /* Add to the end of the archive. */
1222 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1224 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
,
1234 write_archive (arch
);
1236 output_filename
= NULL
;
1240 ranlib_only (const char *archname
)
1244 if (get_file_size (archname
) < 1)
1247 arch
= open_inarch (archname
, (char *) NULL
);
1250 write_archive (arch
);
1254 /* Update the timestamp of the symbol map of an archive. */
1257 ranlib_touch (const char *archname
)
1260 /* I don't think updating works on go32. */
1261 ranlib_only (archname
);
1267 if (get_file_size (archname
) < 1)
1269 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1272 bfd_set_error (bfd_error_system_call
);
1273 bfd_fatal (archname
);
1276 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1278 bfd_fatal (archname
);
1279 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1281 bfd_nonfatal (archname
);
1282 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1284 list_matching_formats (matching
);
1290 if (! bfd_has_map (arch
))
1291 /* xgettext:c-format */
1292 fatal (_("%s: no archive map to update"), archname
);
1294 bfd_update_armap_timestamp (arch
);
1296 if (! bfd_close (arch
))
1297 bfd_fatal (archname
);
1302 /* Things which are interesting to map over all or some of the files: */
1305 print_descr (bfd
*abfd
)
1307 print_arelt_descr (stdout
, abfd
, verbose
);