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
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 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
52 bfd_special_undocumented_glue (bfd
* abfd
, const char *filename
);
54 /* Static declarations. */
56 static void mri_emul (void);
57 static const char *normalize (const char *, bfd
*);
58 static void remove_output (void);
59 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
60 static void print_contents (bfd
* member
);
61 static void delete_members (bfd
*, char **files_to_delete
);
63 static void move_members (bfd
*, char **files_to_move
);
64 static void replace_members
65 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
66 static void print_descr (bfd
* abfd
);
67 static void write_archive (bfd
*);
68 static int ranlib_only (const char *archname
);
69 static int ranlib_touch (const char *archname
);
70 static void usage (int);
72 /** Globals and flags. */
76 /* This flag distinguishes between ar and ranlib:
77 1 means this is 'ranlib'; 0 means this is 'ar'.
78 -1 means if we should use argv[0] to decide. */
81 /* Nonzero means don't warn about creating the archive file if necessary. */
82 int silent_create
= 0;
84 /* Nonzero means describe each action performed. */
87 /* Nonzero means preserve dates of members when extracting them. */
88 int preserve_dates
= 0;
90 /* Nonzero means don't replace existing members whose dates are more recent
91 than the corresponding files. */
94 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
95 member). -1 means we've been explicitly asked to not write a symbol table;
96 +1 means we've been explicitly asked to write it;
98 Traditionally, the default in BSD has been to not write the table.
99 However, for POSIX.2 compliance the default is now to write a symbol table
100 if any of the members are object files. */
103 /* Operate in deterministic mode: write zero for timestamps, uids,
104 and gids for archive members and the archive symbol table, and write
105 consistent file modes. */
106 int deterministic
= 0;
108 /* Nonzero means it's the name of an existing member; position new or moved
109 files with respect to this one. */
110 char *posname
= NULL
;
112 /* Sez how to use `posname': pos_before means position before that member.
113 pos_after means position after that member. pos_end means always at end.
114 pos_default means default appropriately. For the latter two, `posname'
115 should also be zero. */
118 pos_default
, pos_before
, pos_after
, pos_end
119 } postype
= pos_default
;
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
;
145 interactive
= isatty (fileno (stdin
));
149 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
150 COUNT is the length of the FILES chain; FUNCTION is called on each entry
151 whose name matches one in FILES. */
154 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
161 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
169 /* This may appear to be a baroque way of accomplishing what we want.
170 However we have to iterate over the filenames in order to notice where
171 a filename is requested but does not exist in the archive. Ditto
172 mapping over each file each time -- we want to hack multiple
175 for (; count
> 0; files
++, count
--)
177 bfd_boolean found
= FALSE
;
180 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
182 const char * filename
;
185 filename
= head
->filename
;
186 if (filename
== NULL
)
188 /* Some archive formats don't get the filenames filled in
189 until the elements are opened. */
191 bfd_stat_arch_elt (head
, &buf
);
193 else if (bfd_is_thin_archive (arch
))
195 /* Thin archives store full pathnames. Need to normalize. */
196 filename
= normalize (filename
, arch
);
199 if ((filename
!= NULL
) &&
200 (!FILENAME_CMP (normalize (*files
, arch
), filename
)))
203 if (counted_name_mode
204 && match_count
!= counted_name_counter
)
206 /* Counting, and didn't match on count; go on to the
217 /* xgettext:c-format */
218 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
222 bfd_boolean operation_alters_arch
= FALSE
;
229 s
= help
? stdout
: stderr
;
233 /* xgettext:c-format */
234 const char * command_line
=
235 #if BFD_SUPPORTS_PLUGINS
236 _("Usage: %s [emulation options] [--plugin <name>] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
238 _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
240 fprintf (s
, command_line
, program_name
);
242 /* xgettext:c-format */
243 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
244 fprintf (s
, _(" commands:\n"));
245 fprintf (s
, _(" d - delete file(s) from the archive\n"));
246 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
247 fprintf (s
, _(" p - print file(s) found in the archive\n"));
248 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
249 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
250 fprintf (s
, _(" t - display contents of archive\n"));
251 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
252 fprintf (s
, _(" command specific modifiers:\n"));
253 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
254 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
255 fprintf (s
, _(" [D] - use zero for timestamps and uids/gids\n"));
256 fprintf (s
, _(" [N] - use instance [count] of name\n"));
257 fprintf (s
, _(" [f] - truncate inserted file names\n"));
258 fprintf (s
, _(" [P] - use full path names when matching\n"));
259 fprintf (s
, _(" [o] - preserve original dates\n"));
260 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
261 fprintf (s
, _(" generic modifiers:\n"));
262 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
263 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
264 fprintf (s
, _(" [S] - do not build a symbol table\n"));
265 fprintf (s
, _(" [T] - make a thin archive\n"));
266 fprintf (s
, _(" [v] - be verbose\n"));
267 fprintf (s
, _(" [V] - display the version number\n"));
268 fprintf (s
, _(" @<file> - read options from <file>\n"));
269 #if BFD_SUPPORTS_PLUGINS
270 fprintf (s
, _(" optional:\n"));
271 fprintf (s
, _(" --plugin <p> - load the specified plugin\n"));
277 /* xgettext:c-format */
278 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
279 fprintf (s
, _(" Generate an index to speed access to archives\n"));
280 fprintf (s
, _(" The options are:\n\
281 @<file> Read options from <file>\n"
282 #if BFD_SUPPORTS_PLUGINS
283 " --plugin <name> Load the specified plugin\n"
285 " -t Update the archive's symbol map timestamp\n\
286 -h --help Print this help message\n\
287 -v --version Print version information\n"));
290 list_supported_targets (program_name
, s
);
292 if (REPORT_BUGS_TO
[0] && help
)
293 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
295 xexit (help
? 0 : 1);
298 /* Normalize a file name specified on the command line into a file
299 name which we will use in an archive. */
302 normalize (const char *file
, bfd
*abfd
)
304 const char *filename
;
309 filename
= strrchr (file
, '/');
310 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
312 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
313 char *bslash
= strrchr (file
, '\\');
315 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
317 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
321 if (filename
!= (char *) NULL
)
328 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
333 s
= xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
334 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
335 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
342 /* Remove any output file. This is only called via xatexit. */
344 static const char *output_filename
= NULL
;
345 static FILE *output_file
= NULL
;
346 static bfd
*output_bfd
= NULL
;
351 if (output_filename
!= NULL
)
353 if (output_bfd
!= NULL
)
354 bfd_cache_close (output_bfd
);
355 if (output_file
!= NULL
)
356 fclose (output_file
);
357 unlink_if_ordinary (output_filename
);
361 /* The option parsing should be in its own function.
362 It will be when I have getopt working. */
364 int main (int, char **);
367 main (int argc
, char **argv
)
373 none
= 0, delete, replace
, print_table
,
374 print_files
, extract
, move
, quick_append
379 char *inarch_filename
;
384 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
385 setlocale (LC_MESSAGES
, "");
387 #if defined (HAVE_SETLOCALE)
388 setlocale (LC_CTYPE
, "");
390 bindtextdomain (PACKAGE
, LOCALEDIR
);
391 textdomain (PACKAGE
);
393 program_name
= argv
[0];
394 xmalloc_set_program_name (program_name
);
395 #if BFD_SUPPORTS_PLUGINS
396 bfd_plugin_set_program_name (program_name
);
399 expandargv (&argc
, &argv
);
405 temp
= strrchr (program_name
, '/');
406 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
408 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
409 char *bslash
= strrchr (program_name
, '\\');
411 if (temp
== NULL
|| (bslash
!= NULL
&& bslash
> temp
))
413 if (temp
== NULL
&& program_name
[0] != '\0' && program_name
[1] == ':')
414 temp
= program_name
+ 1;
421 if (strlen (temp
) >= 6
422 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
428 if (argc
> 1 && argv
[1][0] == '-')
430 if (strcmp (argv
[1], "--help") == 0)
432 else if (strcmp (argv
[1], "--version") == 0)
435 print_version ("ranlib");
437 print_version ("ar");
441 START_PROGRESS (program_name
, 0);
444 set_default_bfd_target ();
448 xatexit (remove_output
);
450 for (i
= 1; i
< argc
; i
++)
451 if (! ar_emul_parse_arg (argv
[i
]))
459 bfd_boolean touch
= FALSE
;
462 || strcmp (argv
[1], "--help") == 0
463 || strcmp (argv
[1], "-h") == 0
464 || strcmp (argv
[1], "-H") == 0)
466 if (strcmp (argv
[1], "-V") == 0
467 || strcmp (argv
[1], "-v") == 0
468 || CONST_STRNEQ (argv
[1], "--v"))
469 print_version ("ranlib");
471 if (strcmp (argv
[1], "-t") == 0)
476 while (arg_index
< argc
)
479 status
|= ranlib_only (argv
[arg_index
]);
481 status
|= ranlib_touch (argv
[arg_index
]);
487 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
497 arg_ptr
= argv
[arg_index
];
499 if (strcmp (arg_ptr
, "--plugin") == 0)
501 #if BFD_SUPPORTS_PLUGINS
505 bfd_plugin_set_plugin (argv
[2]);
508 arg_ptr
= argv
[arg_index
];
510 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
517 /* When the first option starts with '-' we support POSIX-compatible
520 ++arg_ptr
; /* compatibility */
525 while ((c
= *arg_ptr
++) != '\0')
536 if (operation
!= none
)
537 fatal (_("two different operation options specified"));
542 operation_alters_arch
= TRUE
;
546 operation_alters_arch
= TRUE
;
549 operation
= print_files
;
552 operation
= quick_append
;
553 operation_alters_arch
= TRUE
;
557 operation_alters_arch
= TRUE
;
560 operation
= print_table
;
593 postype
= pos_before
;
596 postype
= pos_before
;
602 counted_name_mode
= TRUE
;
608 full_pathname
= TRUE
;
611 make_thin_archive
= TRUE
;
614 deterministic
= TRUE
;
617 /* xgettext:c-format */
618 non_fatal (_("illegal option -- %c"), c
);
623 /* With POSIX-compatible option parsing continue with the next
624 argument if it starts with '-'. */
625 if (do_posix
&& arg_index
+ 1 < argc
&& argv
[arg_index
+ 1][0] == '-')
626 arg_ptr
= argv
[++arg_index
] + 1;
633 print_version ("ar");
636 if (arg_index
>= argc
)
647 /* We don't use do_quick_append any more. Too many systems
648 expect ar to always rebuild the symbol table even when q is
651 /* We can't write an armap when using ar q, so just do ar r
653 if (operation
== quick_append
&& write_armap
)
656 if ((operation
== none
|| operation
== print_table
)
658 xexit (ranlib_only (argv
[arg_index
]));
660 if (operation
== none
)
661 fatal (_("no operation specified"));
663 if (newer_only
&& operation
!= replace
)
664 fatal (_("`u' is only meaningful with the `r' option."));
666 if (newer_only
&& deterministic
)
667 fatal (_("`u' is not meaningful with the `D' option."));
669 if (postype
!= pos_default
)
670 posname
= argv
[arg_index
++];
672 if (counted_name_mode
)
674 if (operation
!= extract
&& operation
!= delete)
675 fatal (_("`N' is only meaningful with the `x' and `d' options."));
676 counted_name_counter
= atoi (argv
[arg_index
++]);
677 if (counted_name_counter
<= 0)
678 fatal (_("Value for `N' must be positive."));
681 inarch_filename
= argv
[arg_index
++];
683 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
684 file_count
= argc
- arg_index
;
686 arch
= open_inarch (inarch_filename
,
687 files
== NULL
? (char *) NULL
: files
[0]);
689 if (operation
== extract
&& bfd_is_thin_archive (arch
))
690 fatal (_("`x' cannot be used on thin archives."));
695 map_over_members (arch
, print_descr
, files
, file_count
);
699 map_over_members (arch
, print_contents
, files
, file_count
);
703 map_over_members (arch
, extract_file
, files
, file_count
);
708 delete_members (arch
, files
);
710 output_filename
= NULL
;
715 move_members (arch
, files
);
717 output_filename
= NULL
;
722 if (files
!= NULL
|| write_armap
> 0)
723 replace_members (arch
, files
, operation
== quick_append
);
725 output_filename
= NULL
;
728 /* Shouldn't happen! */
730 /* xgettext:c-format */
731 fatal (_("internal error -- this option not implemented"));
735 END_PROGRESS (program_name
);
742 open_inarch (const char *archive_filename
, const char *file
)
751 bfd_set_error (bfd_error_no_error
);
755 if (stat (archive_filename
, &sbuf
) != 0)
757 #if !defined(__GO32__) || defined(__DJGPP__)
759 /* FIXME: I don't understand why this fragment was ifndef'ed
760 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
761 stat() works just fine in v2.x, so I think this should be
762 removed. For now, I enable it for DJGPP v2. -- EZ. */
764 /* KLUDGE ALERT! Temporary fix until I figger why
765 stat() is wrong ... think it's buried in GO32's IDT - Jax */
767 bfd_fatal (archive_filename
);
770 if (!operation_alters_arch
)
772 fprintf (stderr
, "%s: ", program_name
);
773 perror (archive_filename
);
778 /* Try to figure out the target to use for the archive from the
779 first object on the list. */
784 obj
= bfd_openr (file
, NULL
);
787 if (bfd_check_format (obj
, bfd_object
))
788 target
= bfd_get_target (obj
);
789 (void) bfd_close (obj
);
793 /* Create an empty archive. */
794 arch
= bfd_openw (archive_filename
, target
);
796 || ! bfd_set_format (arch
, bfd_archive
)
797 || ! bfd_close (arch
))
798 bfd_fatal (archive_filename
);
799 else if (!silent_create
)
800 non_fatal (_("creating %s"), archive_filename
);
802 /* If we die creating a new archive, don't leave it around. */
803 output_filename
= archive_filename
;
806 arch
= bfd_openr (archive_filename
, target
);
810 bfd_fatal (archive_filename
);
813 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
815 bfd_nonfatal (archive_filename
);
816 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
818 list_matching_formats (matching
);
824 last_one
= &(arch
->archive_next
);
825 /* Read all the contents right away, regardless. */
826 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
828 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
831 *last_one
= next_one
;
832 last_one
= &next_one
->archive_next
;
834 *last_one
= (bfd
*) NULL
;
835 if (bfd_get_error () != bfd_error_no_more_archived_files
)
841 print_contents (bfd
*abfd
)
844 char *cbuf
= xmalloc (BUFSIZE
);
847 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
848 /* xgettext:c-format */
849 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
852 /* xgettext:c-format */
853 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd
));
855 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
858 while (ncopied
< size
)
862 size_t tocopy
= size
- ncopied
;
863 if (tocopy
> BUFSIZE
)
866 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
868 /* xgettext:c-format */
869 fatal (_("%s is not a valid archive"),
870 bfd_get_filename (bfd_my_archive (abfd
)));
872 /* fwrite in mingw32 may return int instead of size_t. Cast the
873 return value to size_t to avoid comparison between signed and
875 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
876 fatal ("stdout: %s", strerror (errno
));
882 /* Extract a member of the archive into its own file.
884 We defer opening the new file until after we have read a BUFSIZ chunk of the
885 old one, since we know we have just read the archive header for the old
886 one. Since most members are shorter than BUFSIZ, this means we will read
887 the old header, read the old data, write a new inode for the new file, and
888 write the new data, and be done. This 'optimization' is what comes from
889 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
893 extract_file (bfd
*abfd
)
896 char *cbuf
= xmalloc (BUFSIZE
);
897 size_t nread
, tocopy
;
902 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
903 /* xgettext:c-format */
904 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
908 printf ("x - %s\n", bfd_get_filename (abfd
));
910 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
915 /* Seems like an abstraction violation, eh? Well it's OK! */
916 output_filename
= bfd_get_filename (abfd
);
918 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
921 perror (bfd_get_filename (abfd
));
925 output_file
= ostream
;
928 while (ncopied
< size
)
930 tocopy
= size
- ncopied
;
931 if (tocopy
> BUFSIZE
)
934 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
936 /* xgettext:c-format */
937 fatal (_("%s is not a valid archive"),
938 bfd_get_filename (bfd_my_archive (abfd
)));
940 /* See comment above; this saves disk arm motion */
943 /* Seems like an abstraction violation, eh? Well it's OK! */
944 output_filename
= bfd_get_filename (abfd
);
946 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
949 perror (bfd_get_filename (abfd
));
953 output_file
= ostream
;
956 /* fwrite in mingw32 may return int instead of size_t. Cast
957 the return value to size_t to avoid comparison between
958 signed and unsigned values. */
959 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
960 fatal ("%s: %s", output_filename
, strerror (errno
));
968 output_filename
= NULL
;
970 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
974 /* Set access time to modification time. Only st_mtime is
975 initialized by bfd_stat_arch_elt. */
976 buf
.st_atime
= buf
.st_mtime
;
977 set_times (bfd_get_filename (abfd
), &buf
);
984 write_archive (bfd
*iarch
)
987 char *old_name
, *new_name
;
988 bfd
*contents_head
= iarch
->archive_next
;
990 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
991 strcpy (old_name
, bfd_get_filename (iarch
));
992 new_name
= make_tempname (old_name
);
994 if (new_name
== NULL
)
995 bfd_fatal ("could not create temporary file whilst writing archive");
997 output_filename
= new_name
;
999 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1002 bfd_fatal (old_name
);
1006 bfd_set_format (obfd
, bfd_archive
);
1008 /* Request writing the archive symbol table unless we've
1009 been explicitly requested not to. */
1010 obfd
->has_armap
= write_armap
>= 0;
1014 /* This should really use bfd_set_file_flags, but that rejects
1016 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1020 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1022 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1023 bfd_is_thin_archive (obfd
) = 1;
1025 if (!bfd_set_archive_head (obfd
, contents_head
))
1026 bfd_fatal (old_name
);
1028 if (!bfd_close (obfd
))
1029 bfd_fatal (old_name
);
1032 output_filename
= NULL
;
1034 /* We don't care if this fails; we might be creating the archive. */
1037 if (smart_rename (new_name
, old_name
, 0) != 0)
1041 /* Return a pointer to the pointer to the entry which should be rplacd'd
1042 into when altering. DEFAULT_POS should be how to interpret pos_default,
1043 and should be a pos value. */
1046 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1048 bfd
**after_bfd
= contents
;
1050 const char *realposname
;
1052 if (postype
== pos_default
)
1054 realpos
= default_pos
;
1055 realposname
= default_posname
;
1060 realposname
= posname
;
1063 if (realpos
== pos_end
)
1066 after_bfd
= &((*after_bfd
)->archive_next
);
1070 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1071 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1073 if (realpos
== pos_after
)
1074 after_bfd
= &(*after_bfd
)->archive_next
;
1082 delete_members (bfd
*arch
, char **files_to_delete
)
1084 bfd
**current_ptr_ptr
;
1086 bfd_boolean something_changed
= FALSE
;
1089 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1091 /* In a.out systems, the armap is optional. It's also called
1092 __.SYMDEF. So if the user asked to delete it, we should remember
1093 that fact. This isn't quite right for COFF systems (where
1094 __.SYMDEF might be regular member), but it's very unlikely
1095 to be a problem. FIXME */
1097 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1099 arch
->has_armap
= FALSE
;
1106 current_ptr_ptr
= &(arch
->archive_next
);
1107 while (*current_ptr_ptr
)
1109 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1110 (*current_ptr_ptr
)->filename
) == 0)
1113 if (counted_name_mode
1114 && match_count
!= counted_name_counter
)
1116 /* Counting, and didn't match on count; go on to the
1122 something_changed
= TRUE
;
1126 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1131 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1134 if (verbose
&& !found
)
1136 /* xgettext:c-format */
1137 printf (_("No member named `%s'\n"), *files_to_delete
);
1143 if (something_changed
)
1144 write_archive (arch
);
1146 output_filename
= NULL
;
1150 /* Reposition existing members within an archive */
1153 move_members (bfd
*arch
, char **files_to_move
)
1155 bfd
**after_bfd
; /* New entries go after this one */
1156 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1158 for (; *files_to_move
; ++files_to_move
)
1160 current_ptr_ptr
= &(arch
->archive_next
);
1161 while (*current_ptr_ptr
)
1163 bfd
*current_ptr
= *current_ptr_ptr
;
1164 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1165 current_ptr
->filename
) == 0)
1167 /* Move this file to the end of the list - first cut from
1170 *current_ptr_ptr
= current_ptr
->archive_next
;
1172 /* Now glue to end */
1173 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1175 *after_bfd
= current_ptr
;
1176 current_ptr
->archive_next
= link
;
1179 printf ("m - %s\n", *files_to_move
);
1184 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1186 /* xgettext:c-format */
1187 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1192 write_archive (arch
);
1195 /* Ought to default to replacing in place, but this is existing practice! */
1198 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1200 bfd_boolean changed
= FALSE
;
1201 bfd
**after_bfd
; /* New entries go after this one. */
1205 while (files_to_move
&& *files_to_move
)
1209 current_ptr
= &arch
->archive_next
;
1210 while (*current_ptr
)
1212 current
= *current_ptr
;
1214 /* For compatibility with existing ar programs, we
1215 permit the same file to be added multiple times. */
1216 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1217 normalize (current
->filename
, arch
)) == 0
1218 && current
->arelt_data
!= NULL
)
1222 struct stat fsbuf
, asbuf
;
1224 if (stat (*files_to_move
, &fsbuf
) != 0)
1226 if (errno
!= ENOENT
)
1227 bfd_fatal (*files_to_move
);
1230 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1231 /* xgettext:c-format */
1232 fatal (_("internal stat error on %s"),
1235 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1239 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1241 if (ar_emul_replace (after_bfd
, *files_to_move
,
1244 /* Snip out this entry from the chain. */
1245 *current_ptr
= (*current_ptr
)->archive_next
;
1251 current_ptr
= &(current
->archive_next
);
1255 /* Add to the end of the archive. */
1256 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1258 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
,
1268 write_archive (arch
);
1270 output_filename
= NULL
;
1274 ranlib_only (const char *archname
)
1278 if (get_file_size (archname
) < 1)
1281 arch
= open_inarch (archname
, (char *) NULL
);
1284 write_archive (arch
);
1288 /* Update the timestamp of the symbol map of an archive. */
1291 ranlib_touch (const char *archname
)
1294 /* I don't think updating works on go32. */
1295 ranlib_only (archname
);
1301 if (get_file_size (archname
) < 1)
1303 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1306 bfd_set_error (bfd_error_system_call
);
1307 bfd_fatal (archname
);
1310 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1312 bfd_fatal (archname
);
1313 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1315 bfd_nonfatal (archname
);
1316 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1318 list_matching_formats (matching
);
1324 if (! bfd_has_map (arch
))
1325 /* xgettext:c-format */
1326 fatal (_("%s: no archive map to update"), archname
);
1328 bfd_update_armap_timestamp (arch
);
1330 if (! bfd_close (arch
))
1331 bfd_fatal (archname
);
1336 /* Things which are interesting to map over all or some of the files: */
1339 print_descr (bfd
*abfd
)
1341 print_arelt_descr (stdout
, abfd
, verbose
);