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
284 --plugin <name> Load the specified plugin\n"));
287 -t Update the archive's symbol map timestamp\n\
288 -h --help Print this help message\n\
289 -v --version Print version information\n"));
292 list_supported_targets (program_name
, s
);
294 if (REPORT_BUGS_TO
[0] && help
)
295 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
297 xexit (help
? 0 : 1);
300 /* Normalize a file name specified on the command line into a file
301 name which we will use in an archive. */
304 normalize (const char *file
, bfd
*abfd
)
306 const char *filename
;
311 filename
= strrchr (file
, '/');
312 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
314 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
315 char *bslash
= strrchr (file
, '\\');
317 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
319 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
323 if (filename
!= (char *) NULL
)
330 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
335 s
= xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
336 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
337 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
344 /* Remove any output file. This is only called via xatexit. */
346 static const char *output_filename
= NULL
;
347 static FILE *output_file
= NULL
;
348 static bfd
*output_bfd
= NULL
;
353 if (output_filename
!= NULL
)
355 if (output_bfd
!= NULL
)
356 bfd_cache_close (output_bfd
);
357 if (output_file
!= NULL
)
358 fclose (output_file
);
359 unlink_if_ordinary (output_filename
);
363 /* The option parsing should be in its own function.
364 It will be when I have getopt working. */
366 int main (int, char **);
369 main (int argc
, char **argv
)
375 none
= 0, delete, replace
, print_table
,
376 print_files
, extract
, move
, quick_append
381 char *inarch_filename
;
386 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
387 setlocale (LC_MESSAGES
, "");
389 #if defined (HAVE_SETLOCALE)
390 setlocale (LC_CTYPE
, "");
392 bindtextdomain (PACKAGE
, LOCALEDIR
);
393 textdomain (PACKAGE
);
395 program_name
= argv
[0];
396 xmalloc_set_program_name (program_name
);
397 #if BFD_SUPPORTS_PLUGINS
398 bfd_plugin_set_program_name (program_name
);
401 expandargv (&argc
, &argv
);
407 temp
= strrchr (program_name
, '/');
408 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
410 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
411 char *bslash
= strrchr (program_name
, '\\');
413 if (temp
== NULL
|| (bslash
!= NULL
&& bslash
> temp
))
415 if (temp
== NULL
&& program_name
[0] != '\0' && program_name
[1] == ':')
416 temp
= program_name
+ 1;
423 if (strlen (temp
) >= 6
424 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
430 if (argc
> 1 && argv
[1][0] == '-')
432 if (strcmp (argv
[1], "--help") == 0)
434 else if (strcmp (argv
[1], "--version") == 0)
437 print_version ("ranlib");
439 print_version ("ar");
443 START_PROGRESS (program_name
, 0);
446 set_default_bfd_target ();
450 xatexit (remove_output
);
452 for (i
= 1; i
< argc
; i
++)
453 if (! ar_emul_parse_arg (argv
[i
]))
461 bfd_boolean touch
= FALSE
;
464 || strcmp (argv
[1], "--help") == 0
465 || strcmp (argv
[1], "-h") == 0
466 || strcmp (argv
[1], "-H") == 0)
468 if (strcmp (argv
[1], "-V") == 0
469 || strcmp (argv
[1], "-v") == 0
470 || CONST_STRNEQ (argv
[1], "--v"))
471 print_version ("ranlib");
473 if (strcmp (argv
[1], "-t") == 0)
478 while (arg_index
< argc
)
481 status
|= ranlib_only (argv
[arg_index
]);
483 status
|= ranlib_touch (argv
[arg_index
]);
489 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
499 arg_ptr
= argv
[arg_index
];
501 if (strcmp (arg_ptr
, "--plugin") == 0)
503 #if BFD_SUPPORTS_PLUGINS
507 bfd_plugin_set_plugin (argv
[2]);
510 arg_ptr
= argv
[arg_index
];
512 fprintf (stderr
, _("sorry - this program has been built without plugin support\n"));
519 /* When the first option starts with '-' we support POSIX-compatible
522 ++arg_ptr
; /* compatibility */
527 while ((c
= *arg_ptr
++) != '\0')
538 if (operation
!= none
)
539 fatal (_("two different operation options specified"));
544 operation_alters_arch
= TRUE
;
548 operation_alters_arch
= TRUE
;
551 operation
= print_files
;
554 operation
= quick_append
;
555 operation_alters_arch
= TRUE
;
559 operation_alters_arch
= TRUE
;
562 operation
= print_table
;
595 postype
= pos_before
;
598 postype
= pos_before
;
604 counted_name_mode
= TRUE
;
610 full_pathname
= TRUE
;
613 make_thin_archive
= TRUE
;
616 deterministic
= TRUE
;
619 /* xgettext:c-format */
620 non_fatal (_("illegal option -- %c"), c
);
625 /* With POSIX-compatible option parsing continue with the next
626 argument if it starts with '-'. */
627 if (do_posix
&& arg_index
+ 1 < argc
&& argv
[arg_index
+ 1][0] == '-')
628 arg_ptr
= argv
[++arg_index
] + 1;
635 print_version ("ar");
638 if (arg_index
>= argc
)
649 /* We don't use do_quick_append any more. Too many systems
650 expect ar to always rebuild the symbol table even when q is
653 /* We can't write an armap when using ar q, so just do ar r
655 if (operation
== quick_append
&& write_armap
)
658 if ((operation
== none
|| operation
== print_table
)
660 xexit (ranlib_only (argv
[arg_index
]));
662 if (operation
== none
)
663 fatal (_("no operation specified"));
665 if (newer_only
&& operation
!= replace
)
666 fatal (_("`u' is only meaningful with the `r' option."));
668 if (newer_only
&& deterministic
)
669 fatal (_("`u' is not meaningful with the `D' option."));
671 if (postype
!= pos_default
)
672 posname
= argv
[arg_index
++];
674 if (counted_name_mode
)
676 if (operation
!= extract
&& operation
!= delete)
677 fatal (_("`N' is only meaningful with the `x' and `d' options."));
678 counted_name_counter
= atoi (argv
[arg_index
++]);
679 if (counted_name_counter
<= 0)
680 fatal (_("Value for `N' must be positive."));
683 inarch_filename
= argv
[arg_index
++];
685 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
686 file_count
= argc
- arg_index
;
688 arch
= open_inarch (inarch_filename
,
689 files
== NULL
? (char *) NULL
: files
[0]);
691 if (operation
== extract
&& bfd_is_thin_archive (arch
))
692 fatal (_("`x' cannot be used on thin archives."));
697 map_over_members (arch
, print_descr
, files
, file_count
);
701 map_over_members (arch
, print_contents
, files
, file_count
);
705 map_over_members (arch
, extract_file
, files
, file_count
);
710 delete_members (arch
, files
);
712 output_filename
= NULL
;
717 move_members (arch
, files
);
719 output_filename
= NULL
;
724 if (files
!= NULL
|| write_armap
> 0)
725 replace_members (arch
, files
, operation
== quick_append
);
727 output_filename
= NULL
;
730 /* Shouldn't happen! */
732 /* xgettext:c-format */
733 fatal (_("internal error -- this option not implemented"));
737 END_PROGRESS (program_name
);
744 open_inarch (const char *archive_filename
, const char *file
)
753 bfd_set_error (bfd_error_no_error
);
757 if (stat (archive_filename
, &sbuf
) != 0)
759 #if !defined(__GO32__) || defined(__DJGPP__)
761 /* FIXME: I don't understand why this fragment was ifndef'ed
762 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
763 stat() works just fine in v2.x, so I think this should be
764 removed. For now, I enable it for DJGPP v2. -- EZ. */
766 /* KLUDGE ALERT! Temporary fix until I figger why
767 stat() is wrong ... think it's buried in GO32's IDT - Jax */
769 bfd_fatal (archive_filename
);
772 if (!operation_alters_arch
)
774 fprintf (stderr
, "%s: ", program_name
);
775 perror (archive_filename
);
780 /* Try to figure out the target to use for the archive from the
781 first object on the list. */
786 obj
= bfd_openr (file
, NULL
);
789 if (bfd_check_format (obj
, bfd_object
))
790 target
= bfd_get_target (obj
);
791 (void) bfd_close (obj
);
795 /* Create an empty archive. */
796 arch
= bfd_openw (archive_filename
, target
);
798 || ! bfd_set_format (arch
, bfd_archive
)
799 || ! bfd_close (arch
))
800 bfd_fatal (archive_filename
);
801 else if (!silent_create
)
802 non_fatal (_("creating %s"), archive_filename
);
804 /* If we die creating a new archive, don't leave it around. */
805 output_filename
= archive_filename
;
808 arch
= bfd_openr (archive_filename
, target
);
812 bfd_fatal (archive_filename
);
815 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
817 bfd_nonfatal (archive_filename
);
818 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
820 list_matching_formats (matching
);
826 last_one
= &(arch
->archive_next
);
827 /* Read all the contents right away, regardless. */
828 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
830 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
833 *last_one
= next_one
;
834 last_one
= &next_one
->archive_next
;
836 *last_one
= (bfd
*) NULL
;
837 if (bfd_get_error () != bfd_error_no_more_archived_files
)
843 print_contents (bfd
*abfd
)
846 char *cbuf
= xmalloc (BUFSIZE
);
849 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
850 /* xgettext:c-format */
851 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
854 /* xgettext:c-format */
855 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd
));
857 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
860 while (ncopied
< size
)
864 size_t tocopy
= size
- ncopied
;
865 if (tocopy
> BUFSIZE
)
868 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
870 /* xgettext:c-format */
871 fatal (_("%s is not a valid archive"),
872 bfd_get_filename (bfd_my_archive (abfd
)));
874 /* fwrite in mingw32 may return int instead of size_t. Cast the
875 return value to size_t to avoid comparison between signed and
877 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
878 fatal ("stdout: %s", strerror (errno
));
884 /* Extract a member of the archive into its own file.
886 We defer opening the new file until after we have read a BUFSIZ chunk of the
887 old one, since we know we have just read the archive header for the old
888 one. Since most members are shorter than BUFSIZ, this means we will read
889 the old header, read the old data, write a new inode for the new file, and
890 write the new data, and be done. This 'optimization' is what comes from
891 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
895 extract_file (bfd
*abfd
)
898 char *cbuf
= xmalloc (BUFSIZE
);
899 size_t nread
, tocopy
;
904 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
905 /* xgettext:c-format */
906 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
910 printf ("x - %s\n", bfd_get_filename (abfd
));
912 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
917 /* Seems like an abstraction violation, eh? Well it's OK! */
918 output_filename
= bfd_get_filename (abfd
);
920 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
923 perror (bfd_get_filename (abfd
));
927 output_file
= ostream
;
930 while (ncopied
< size
)
932 tocopy
= size
- ncopied
;
933 if (tocopy
> BUFSIZE
)
936 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
938 /* xgettext:c-format */
939 fatal (_("%s is not a valid archive"),
940 bfd_get_filename (bfd_my_archive (abfd
)));
942 /* See comment above; this saves disk arm motion */
945 /* Seems like an abstraction violation, eh? Well it's OK! */
946 output_filename
= bfd_get_filename (abfd
);
948 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
951 perror (bfd_get_filename (abfd
));
955 output_file
= ostream
;
958 /* fwrite in mingw32 may return int instead of size_t. Cast
959 the return value to size_t to avoid comparison between
960 signed and unsigned values. */
961 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
962 fatal ("%s: %s", output_filename
, strerror (errno
));
970 output_filename
= NULL
;
972 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
976 /* Set access time to modification time. Only st_mtime is
977 initialized by bfd_stat_arch_elt. */
978 buf
.st_atime
= buf
.st_mtime
;
979 set_times (bfd_get_filename (abfd
), &buf
);
986 write_archive (bfd
*iarch
)
989 char *old_name
, *new_name
;
990 bfd
*contents_head
= iarch
->archive_next
;
992 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
993 strcpy (old_name
, bfd_get_filename (iarch
));
994 new_name
= make_tempname (old_name
);
996 if (new_name
== NULL
)
997 bfd_fatal ("could not create temporary file whilst writing archive");
999 output_filename
= new_name
;
1001 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1004 bfd_fatal (old_name
);
1008 bfd_set_format (obfd
, bfd_archive
);
1010 /* Request writing the archive symbol table unless we've
1011 been explicitly requested not to. */
1012 obfd
->has_armap
= write_armap
>= 0;
1016 /* This should really use bfd_set_file_flags, but that rejects
1018 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1022 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
1024 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
1025 bfd_is_thin_archive (obfd
) = 1;
1027 if (!bfd_set_archive_head (obfd
, contents_head
))
1028 bfd_fatal (old_name
);
1030 if (!bfd_close (obfd
))
1031 bfd_fatal (old_name
);
1034 output_filename
= NULL
;
1036 /* We don't care if this fails; we might be creating the archive. */
1039 if (smart_rename (new_name
, old_name
, 0) != 0)
1043 /* Return a pointer to the pointer to the entry which should be rplacd'd
1044 into when altering. DEFAULT_POS should be how to interpret pos_default,
1045 and should be a pos value. */
1048 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1050 bfd
**after_bfd
= contents
;
1052 const char *realposname
;
1054 if (postype
== pos_default
)
1056 realpos
= default_pos
;
1057 realposname
= default_posname
;
1062 realposname
= posname
;
1065 if (realpos
== pos_end
)
1068 after_bfd
= &((*after_bfd
)->archive_next
);
1072 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1073 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1075 if (realpos
== pos_after
)
1076 after_bfd
= &(*after_bfd
)->archive_next
;
1084 delete_members (bfd
*arch
, char **files_to_delete
)
1086 bfd
**current_ptr_ptr
;
1088 bfd_boolean something_changed
= FALSE
;
1091 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1093 /* In a.out systems, the armap is optional. It's also called
1094 __.SYMDEF. So if the user asked to delete it, we should remember
1095 that fact. This isn't quite right for COFF systems (where
1096 __.SYMDEF might be regular member), but it's very unlikely
1097 to be a problem. FIXME */
1099 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1101 arch
->has_armap
= FALSE
;
1108 current_ptr_ptr
= &(arch
->archive_next
);
1109 while (*current_ptr_ptr
)
1111 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1112 (*current_ptr_ptr
)->filename
) == 0)
1115 if (counted_name_mode
1116 && match_count
!= counted_name_counter
)
1118 /* Counting, and didn't match on count; go on to the
1124 something_changed
= TRUE
;
1128 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1133 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1136 if (verbose
&& !found
)
1138 /* xgettext:c-format */
1139 printf (_("No member named `%s'\n"), *files_to_delete
);
1145 if (something_changed
)
1146 write_archive (arch
);
1148 output_filename
= NULL
;
1152 /* Reposition existing members within an archive */
1155 move_members (bfd
*arch
, char **files_to_move
)
1157 bfd
**after_bfd
; /* New entries go after this one */
1158 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1160 for (; *files_to_move
; ++files_to_move
)
1162 current_ptr_ptr
= &(arch
->archive_next
);
1163 while (*current_ptr_ptr
)
1165 bfd
*current_ptr
= *current_ptr_ptr
;
1166 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1167 current_ptr
->filename
) == 0)
1169 /* Move this file to the end of the list - first cut from
1172 *current_ptr_ptr
= current_ptr
->archive_next
;
1174 /* Now glue to end */
1175 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1177 *after_bfd
= current_ptr
;
1178 current_ptr
->archive_next
= link
;
1181 printf ("m - %s\n", *files_to_move
);
1186 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1188 /* xgettext:c-format */
1189 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1194 write_archive (arch
);
1197 /* Ought to default to replacing in place, but this is existing practice! */
1200 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1202 bfd_boolean changed
= FALSE
;
1203 bfd
**after_bfd
; /* New entries go after this one. */
1207 while (files_to_move
&& *files_to_move
)
1211 current_ptr
= &arch
->archive_next
;
1212 while (*current_ptr
)
1214 current
= *current_ptr
;
1216 /* For compatibility with existing ar programs, we
1217 permit the same file to be added multiple times. */
1218 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1219 normalize (current
->filename
, arch
)) == 0
1220 && current
->arelt_data
!= NULL
)
1224 struct stat fsbuf
, asbuf
;
1226 if (stat (*files_to_move
, &fsbuf
) != 0)
1228 if (errno
!= ENOENT
)
1229 bfd_fatal (*files_to_move
);
1232 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1233 /* xgettext:c-format */
1234 fatal (_("internal stat error on %s"),
1237 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1241 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1243 if (ar_emul_replace (after_bfd
, *files_to_move
,
1246 /* Snip out this entry from the chain. */
1247 *current_ptr
= (*current_ptr
)->archive_next
;
1253 current_ptr
= &(current
->archive_next
);
1257 /* Add to the end of the archive. */
1258 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1260 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
,
1270 write_archive (arch
);
1272 output_filename
= NULL
;
1276 ranlib_only (const char *archname
)
1280 if (get_file_size (archname
) < 1)
1283 arch
= open_inarch (archname
, (char *) NULL
);
1286 write_archive (arch
);
1290 /* Update the timestamp of the symbol map of an archive. */
1293 ranlib_touch (const char *archname
)
1296 /* I don't think updating works on go32. */
1297 ranlib_only (archname
);
1303 if (get_file_size (archname
) < 1)
1305 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1308 bfd_set_error (bfd_error_system_call
);
1309 bfd_fatal (archname
);
1312 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1314 bfd_fatal (archname
);
1315 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1317 bfd_nonfatal (archname
);
1318 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1320 list_matching_formats (matching
);
1326 if (! bfd_has_map (arch
))
1327 /* xgettext:c-format */
1328 fatal (_("%s: no archive map to update"), archname
);
1330 bfd_update_armap_timestamp (arch
);
1332 if (! bfd_close (arch
))
1333 bfd_fatal (archname
);
1338 /* Things which are interesting to map over all or some of the files: */
1341 print_descr (bfd
*abfd
)
1343 print_arelt_descr (stdout
, abfd
, verbose
);