1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 Bugs: should use getopt the way tar does (complete w/optional -) and
24 should have long options too. GNU ar used to check file against filesystem
25 in quick_update and replace operations (would check mtime). Doesn't warn
26 when name truncated. No way to specify pos_end. Error messages should be
30 #include "libiberty.h"
36 #include "filenames.h"
41 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
43 #define EXT_NAME_LEN 6 /* ditto for *NIX */
46 /* We need to open files in binary modes on system where that makes a
54 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
57 bfd_special_undocumented_glue
PARAMS ((bfd
* abfd
, const char *filename
));
59 /* Static declarations */
62 mri_emul
PARAMS ((void));
65 normalize
PARAMS ((const char *, bfd
*));
68 remove_output
PARAMS ((void));
71 map_over_members
PARAMS ((bfd
*, void (*)(bfd
*), char **, int));
74 print_contents
PARAMS ((bfd
* member
));
77 delete_members
PARAMS ((bfd
*, char **files_to_delete
));
81 do_quick_append
PARAMS ((const char *archive_filename
,
82 char **files_to_append
));
86 move_members
PARAMS ((bfd
*, char **files_to_move
));
89 replace_members
PARAMS ((bfd
*, char **files_to_replace
, bfd_boolean quick
));
92 print_descr
PARAMS ((bfd
* abfd
));
95 write_archive
PARAMS ((bfd
*));
98 ranlib_only
PARAMS ((const char *archname
));
101 ranlib_touch
PARAMS ((const char *archname
));
104 usage
PARAMS ((int));
106 /** Globals and flags */
110 /* This flag distinguishes between ar and ranlib:
111 1 means this is 'ranlib'; 0 means this is 'ar'.
112 -1 means if we should use argv[0] to decide. */
113 extern int is_ranlib
;
115 /* Nonzero means don't warn about creating the archive file if necessary. */
116 int silent_create
= 0;
118 /* Nonzero means describe each action performed. */
121 /* Nonzero means preserve dates of members when extracting them. */
122 int preserve_dates
= 0;
124 /* Nonzero means don't replace existing members whose dates are more recent
125 than the corresponding files. */
128 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
129 member). -1 means we've been explicitly asked to not write a symbol table;
130 +1 means we've been explictly asked to write it;
132 Traditionally, the default in BSD has been to not write the table.
133 However, for POSIX.2 compliance the default is now to write a symbol table
134 if any of the members are object files. */
137 /* Nonzero means it's the name of an existing member; position new or moved
138 files with respect to this one. */
139 char *posname
= NULL
;
141 /* Sez how to use `posname': pos_before means position before that member.
142 pos_after means position after that member. pos_end means always at end.
143 pos_default means default appropriately. For the latter two, `posname'
144 should also be zero. */
147 pos_default
, pos_before
, pos_after
, pos_end
148 } postype
= pos_default
;
151 get_pos_bfd
PARAMS ((bfd
**, enum pos
, const char *));
153 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
154 extract the COUNTED_NAME_COUNTER instance of that name. */
155 static bfd_boolean counted_name_mode
= 0;
156 static int counted_name_counter
= 0;
158 /* Whether to truncate names of files stored in the archive. */
159 static bfd_boolean ar_truncate
= FALSE
;
161 /* Whether to use a full file name match when searching an archive.
162 This is convenient for archives created by the Microsoft lib
164 static bfd_boolean full_pathname
= FALSE
;
171 interactive
= isatty (fileno (stdin
));
175 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
176 COUNT is the length of the FILES chain; FUNCTION is called on each entry
177 whose name matches one in FILES. */
180 map_over_members (arch
, function
, files
, count
)
182 void (*function
) PARAMS ((bfd
*));
191 for (head
= arch
->next
; head
; head
= head
->next
)
199 /* This may appear to be a baroque way of accomplishing what we want.
200 However we have to iterate over the filenames in order to notice where
201 a filename is requested but does not exist in the archive. Ditto
202 mapping over each file each time -- we want to hack multiple
205 for (; count
> 0; files
++, count
--)
207 bfd_boolean found
= FALSE
;
210 for (head
= arch
->next
; head
; head
= head
->next
)
213 if (head
->filename
== NULL
)
215 /* Some archive formats don't get the filenames filled in
216 until the elements are opened. */
218 bfd_stat_arch_elt (head
, &buf
);
220 if ((head
->filename
!= NULL
) &&
221 (!FILENAME_CMP (normalize (*files
, arch
), head
->filename
)))
224 if (counted_name_mode
225 && match_count
!= counted_name_counter
)
227 /* 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
;
250 s
= help
? stdout
: stderr
;
254 /* xgettext:c-format */
255 fprintf (s
, _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
257 /* xgettext:c-format */
258 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
259 fprintf (s
, _(" commands:\n"));
260 fprintf (s
, _(" d - delete file(s) from the archive\n"));
261 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
262 fprintf (s
, _(" p - print file(s) found in the archive\n"));
263 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
264 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
265 fprintf (s
, _(" t - display contents of archive\n"));
266 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
267 fprintf (s
, _(" command specific modifiers:\n"));
268 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
269 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
270 fprintf (s
, _(" [N] - use instance [count] of name\n"));
271 fprintf (s
, _(" [f] - truncate inserted file names\n"));
272 fprintf (s
, _(" [P] - use full path names when matching\n"));
273 fprintf (s
, _(" [o] - preserve original dates\n"));
274 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
275 fprintf (s
, _(" generic modifiers:\n"));
276 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
277 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
278 fprintf (s
, _(" [S] - do not build a symbol table\n"));
279 fprintf (s
, _(" [v] - be verbose\n"));
280 fprintf (s
, _(" [V] - display the version number\n"));
286 /* xgettext:c-format */
287 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
288 fprintf (s
, _(" Generate an index to speed access to archives\n"));
289 fprintf (s
, _(" The options are:\n\
290 -h --help Print this help message\n\
291 -V --version Print version information\n"));
294 list_supported_targets (program_name
, stderr
);
297 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
299 xexit (help
? 0 : 1);
302 /* Normalize a file name specified on the command line into a file
303 name which we will use in an archive. */
306 normalize (file
, abfd
)
310 const char *filename
;
315 filename
= strrchr (file
, '/');
316 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
318 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
319 char *bslash
= strrchr (file
, '\\');
320 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
322 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
326 if (filename
!= (char *) NULL
)
333 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
338 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
339 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
340 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
347 /* Remove any output file. This is only called via xatexit. */
349 static const char *output_filename
= NULL
;
350 static FILE *output_file
= NULL
;
351 static bfd
*output_bfd
= NULL
;
356 if (output_filename
!= NULL
)
358 if (output_bfd
!= NULL
&& output_bfd
->iostream
!= NULL
)
359 fclose ((FILE *) (output_bfd
->iostream
));
360 if (output_file
!= NULL
)
361 fclose (output_file
);
362 unlink (output_filename
);
366 /* The option parsing should be in its own function.
367 It will be when I have getopt working. */
369 int main
PARAMS ((int, char **));
380 none
= 0, delete, replace
, print_table
,
381 print_files
, extract
, move
, quick_append
386 char *inarch_filename
;
390 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
391 setlocale (LC_MESSAGES
, "");
393 #if defined (HAVE_SETLOCALE)
394 setlocale (LC_CTYPE
, "");
396 bindtextdomain (PACKAGE
, LOCALEDIR
);
397 textdomain (PACKAGE
);
399 program_name
= argv
[0];
400 xmalloc_set_program_name (program_name
);
406 temp
= strrchr (program_name
, '/');
407 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
409 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
410 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
]))
458 bfd_boolean touch
= FALSE
;
461 || strcmp (argv
[1], "--help") == 0
462 || strcmp (argv
[1], "-h") == 0
463 || strcmp (argv
[1], "-H") == 0)
465 if (strcmp (argv
[1], "-V") == 0
466 || strcmp (argv
[1], "-v") == 0
467 || strncmp (argv
[1], "--v", 3) == 0)
468 print_version ("ranlib");
470 if (strcmp (argv
[1], "-t") == 0)
475 while (arg_index
< argc
)
478 ranlib_only (argv
[arg_index
]);
480 ranlib_touch (argv
[arg_index
]);
486 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
498 ++arg_ptr
; /* compatibility */
500 while ((c
= *arg_ptr
++) != '\0')
511 if (operation
!= none
)
512 fatal (_("two different operation options specified"));
517 operation_alters_arch
= TRUE
;
521 operation_alters_arch
= TRUE
;
524 operation
= print_files
;
527 operation
= quick_append
;
528 operation_alters_arch
= TRUE
;
532 operation_alters_arch
= TRUE
;
535 operation
= print_table
;
568 postype
= pos_before
;
571 postype
= pos_before
;
577 counted_name_mode
= TRUE
;
583 full_pathname
= TRUE
;
586 /* xgettext:c-format */
587 non_fatal (_("illegal option -- %c"), c
);
593 print_version ("ar");
606 /* We can't write an armap when using ar q, so just do ar r
608 if (operation
== quick_append
&& write_armap
)
611 if ((operation
== none
|| operation
== print_table
)
614 ranlib_only (argv
[2]);
618 if (operation
== none
)
619 fatal (_("no operation specified"));
621 if (newer_only
&& operation
!= replace
)
622 fatal (_("`u' is only meaningful with the `r' option."));
626 if (postype
!= pos_default
)
627 posname
= argv
[arg_index
++];
629 if (counted_name_mode
)
631 if (operation
!= extract
&& operation
!= delete)
632 fatal (_("`N' is only meaningful with the `x' and `d' options."));
633 counted_name_counter
= atoi (argv
[arg_index
++]);
634 if (counted_name_counter
<= 0)
635 fatal (_("Value for `N' must be positive."));
638 inarch_filename
= argv
[arg_index
++];
640 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
641 file_count
= argc
- arg_index
;
644 /* We don't use do_quick_append any more. Too many systems
645 expect ar to always rebuild the symbol table even when q is
648 /* We can't do a quick append if we need to construct an
649 extended name table, because do_quick_append won't be able to
650 rebuild the name table. Unfortunately, at this point we
651 don't actually know the maximum name length permitted by this
652 object file format. So, we guess. FIXME. */
653 if (operation
== quick_append
&& ! ar_truncate
)
657 for (chk
= files
; chk
!= NULL
&& *chk
!= '\0'; chk
++)
659 if (strlen (normalize (*chk
, (bfd
*) NULL
)) > 14)
667 if (operation
== quick_append
)
669 /* Note that quick appending to a non-existent archive creates it,
670 even if there are no files to append. */
671 do_quick_append (inarch_filename
, files
);
676 arch
= open_inarch (inarch_filename
,
677 files
== NULL
? (char *) NULL
: files
[0]);
682 map_over_members (arch
, print_descr
, files
, file_count
);
686 map_over_members (arch
, print_contents
, files
, file_count
);
690 map_over_members (arch
, extract_file
, files
, file_count
);
695 delete_members (arch
, files
);
697 output_filename
= NULL
;
702 move_members (arch
, files
);
704 output_filename
= NULL
;
709 if (files
!= NULL
|| write_armap
> 0)
710 replace_members (arch
, files
, operation
== quick_append
);
712 output_filename
= NULL
;
715 /* Shouldn't happen! */
717 /* xgettext:c-format */
718 fatal (_("internal error -- this option not implemented"));
722 END_PROGRESS (program_name
);
729 open_inarch (archive_filename
, file
)
730 const char *archive_filename
;
740 bfd_set_error (bfd_error_no_error
);
744 if (stat (archive_filename
, &sbuf
) != 0)
746 #if !defined(__GO32__) || defined(__DJGPP__)
748 /* FIXME: I don't understand why this fragment was ifndef'ed
749 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
750 stat() works just fine in v2.x, so I think this should be
751 removed. For now, I enable it for DJGPP v2. -- EZ. */
753 /* KLUDGE ALERT! Temporary fix until I figger why
754 stat() is wrong ... think it's buried in GO32's IDT - Jax */
756 bfd_fatal (archive_filename
);
759 if (!operation_alters_arch
)
761 fprintf (stderr
, "%s: ", program_name
);
762 perror (archive_filename
);
767 /* Try to figure out the target to use for the archive from the
768 first object on the list. */
773 obj
= bfd_openr (file
, NULL
);
776 if (bfd_check_format (obj
, bfd_object
))
777 target
= bfd_get_target (obj
);
778 (void) bfd_close (obj
);
782 /* Create an empty archive. */
783 arch
= bfd_openw (archive_filename
, target
);
785 || ! bfd_set_format (arch
, bfd_archive
)
786 || ! bfd_close (arch
))
787 bfd_fatal (archive_filename
);
789 /* If we die creating a new archive, don't leave it around. */
790 output_filename
= archive_filename
;
793 arch
= bfd_openr (archive_filename
, target
);
797 bfd_fatal (archive_filename
);
800 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
802 bfd_nonfatal (archive_filename
);
803 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
805 list_matching_formats (matching
);
811 last_one
= &(arch
->next
);
812 /* Read all the contents right away, regardless. */
813 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
815 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
818 *last_one
= next_one
;
819 last_one
= &next_one
->next
;
821 *last_one
= (bfd
*) NULL
;
822 if (bfd_get_error () != bfd_error_no_more_archived_files
)
828 print_contents (abfd
)
832 char *cbuf
= xmalloc (BUFSIZE
);
835 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
836 /* xgettext:c-format */
837 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
840 /* xgettext:c-format */
841 printf (_("\n<member %s>\n\n"), bfd_get_filename (abfd
));
843 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
846 while (ncopied
< size
)
850 int tocopy
= size
- ncopied
;
851 if (tocopy
> BUFSIZE
)
854 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
856 /* xgettext:c-format */
857 fatal (_("%s is not a valid archive"),
858 bfd_get_filename (bfd_my_archive (abfd
)));
859 fwrite (cbuf
, 1, nread
, stdout
);
865 /* Extract a member of the archive into its own file.
867 We defer opening the new file until after we have read a BUFSIZ chunk of the
868 old one, since we know we have just read the archive header for the old
869 one. Since most members are shorter than BUFSIZ, this means we will read
870 the old header, read the old data, write a new inode for the new file, and
871 write the new data, and be done. This 'optimization' is what comes from
872 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
880 char *cbuf
= xmalloc (BUFSIZE
);
886 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
887 /* xgettext:c-format */
888 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
892 /* xgettext:c-format */
893 fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd
));
896 printf ("x - %s\n", bfd_get_filename (abfd
));
898 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
903 /* Seems like an abstraction violation, eh? Well it's OK! */
904 output_filename
= bfd_get_filename (abfd
);
906 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
909 perror (bfd_get_filename (abfd
));
913 output_file
= ostream
;
916 while (ncopied
< size
)
918 tocopy
= size
- ncopied
;
919 if (tocopy
> BUFSIZE
)
922 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
924 /* xgettext:c-format */
925 fatal (_("%s is not a valid archive"),
926 bfd_get_filename (bfd_my_archive (abfd
)));
928 /* See comment above; this saves disk arm motion */
931 /* Seems like an abstraction violation, eh? Well it's OK! */
932 output_filename
= bfd_get_filename (abfd
);
934 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
937 perror (bfd_get_filename (abfd
));
941 output_file
= ostream
;
943 fwrite (cbuf
, 1, nread
, ostream
);
951 output_filename
= NULL
;
953 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
956 set_times (bfd_get_filename (abfd
), &buf
);
963 /* We don't use this anymore. Too many systems expect ar to rebuild
964 the symbol table even when q is used. */
966 /* Just do it quickly; don't worry about dups, armap, or anything like that */
969 do_quick_append (archive_filename
, files_to_append
)
970 const char *archive_filename
;
971 char **files_to_append
;
974 char *buf
= xmalloc (BUFSIZE
);
975 long tocopy
, thistime
;
978 bfd_boolean newfile
= FALSE
;
979 bfd_set_error (bfd_error_no_error
);
981 if (stat (archive_filename
, &sbuf
) != 0)
984 #if !defined(__GO32__) || defined(__DJGPP__)
986 /* FIXME: I don't understand why this fragment was ifndef'ed
987 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
988 stat() works just fine in v2.x, so I think this should be
989 removed. For now, I enable it for DJGPP v2.
991 (And yes, I know this is all unused, but somebody, someday,
992 might wish to resurrect this again... -- EZ. */
994 /* KLUDGE ALERT! Temporary fix until I figger why
995 stat() is wrong ... think it's buried in GO32's IDT - Jax */
998 bfd_fatal (archive_filename
);
1004 ofile
= fopen (archive_filename
, FOPEN_AUB
);
1007 perror (program_name
);
1011 temp
= bfd_openr (archive_filename
, NULL
);
1014 bfd_fatal (archive_filename
);
1018 if (!bfd_check_format (temp
, bfd_archive
))
1019 /* xgettext:c-format */
1020 fatal (_("%s is not an archive"), archive_filename
);
1024 fwrite (ARMAG
, 1, SARMAG
, ofile
);
1026 /* xgettext:c-format */
1027 non_fatal (_("creating %s"), archive_filename
);
1031 temp
->flags
|= BFD_TRADITIONAL_FORMAT
;
1033 /* assume it's an achive, go straight to the end, sans $200 */
1034 fseek (ofile
, 0, 2);
1036 for (; files_to_append
&& *files_to_append
; ++files_to_append
)
1038 struct ar_hdr
*hdr
= bfd_special_undocumented_glue (temp
, *files_to_append
);
1041 bfd_fatal (*files_to_append
);
1044 BFD_SEND (temp
, _bfd_truncate_arname
, (temp
, *files_to_append
, (char *) hdr
));
1046 ifile
= fopen (*files_to_append
, FOPEN_RB
);
1049 bfd_nonfatal (*files_to_append
);
1052 if (stat (*files_to_append
, &sbuf
) != 0)
1054 bfd_nonfatal (*files_to_append
);
1057 tocopy
= sbuf
.st_size
;
1059 /* XXX should do error-checking! */
1060 fwrite (hdr
, 1, sizeof (struct ar_hdr
), ofile
);
1065 if (thistime
> BUFSIZE
)
1067 fread (buf
, 1, thistime
, ifile
);
1068 fwrite (buf
, 1, thistime
, ofile
);
1072 if ((sbuf
.st_size
% 2) == 1)
1073 putc ('\012', ofile
);
1083 write_archive (iarch
)
1087 char *old_name
, *new_name
;
1088 bfd
*contents_head
= iarch
->next
;
1090 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1091 strcpy (old_name
, bfd_get_filename (iarch
));
1092 new_name
= make_tempname (old_name
);
1094 output_filename
= new_name
;
1096 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1099 bfd_fatal (old_name
);
1103 bfd_set_format (obfd
, bfd_archive
);
1105 /* Request writing the archive symbol table unless we've
1106 been explicitly requested not to. */
1107 obfd
->has_armap
= write_armap
>= 0;
1111 /* This should really use bfd_set_file_flags, but that rejects
1113 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1116 if (!bfd_set_archive_head (obfd
, contents_head
))
1117 bfd_fatal (old_name
);
1119 if (!bfd_close (obfd
))
1120 bfd_fatal (old_name
);
1123 output_filename
= NULL
;
1125 /* We don't care if this fails; we might be creating the archive. */
1128 if (smart_rename (new_name
, old_name
, 0) != 0)
1132 /* Return a pointer to the pointer to the entry which should be rplacd'd
1133 into when altering. DEFAULT_POS should be how to interpret pos_default,
1134 and should be a pos value. */
1137 get_pos_bfd (contents
, default_pos
, default_posname
)
1139 enum pos default_pos
;
1140 const char *default_posname
;
1142 bfd
**after_bfd
= contents
;
1144 const char *realposname
;
1146 if (postype
== pos_default
)
1148 realpos
= default_pos
;
1149 realposname
= default_posname
;
1154 realposname
= posname
;
1157 if (realpos
== pos_end
)
1160 after_bfd
= &((*after_bfd
)->next
);
1164 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->next
)
1165 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1167 if (realpos
== pos_after
)
1168 after_bfd
= &(*after_bfd
)->next
;
1176 delete_members (arch
, files_to_delete
)
1178 char **files_to_delete
;
1180 bfd
**current_ptr_ptr
;
1182 bfd_boolean something_changed
= FALSE
;
1185 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1187 /* In a.out systems, the armap is optional. It's also called
1188 __.SYMDEF. So if the user asked to delete it, we should remember
1189 that fact. This isn't quite right for COFF systems (where
1190 __.SYMDEF might be regular member), but it's very unlikely
1191 to be a problem. FIXME */
1193 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1195 arch
->has_armap
= FALSE
;
1202 current_ptr_ptr
= &(arch
->next
);
1203 while (*current_ptr_ptr
)
1205 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1206 (*current_ptr_ptr
)->filename
) == 0)
1209 if (counted_name_mode
1210 && match_count
!= counted_name_counter
)
1212 /* Counting, and didn't match on count; go on to the
1218 something_changed
= TRUE
;
1222 *current_ptr_ptr
= ((*current_ptr_ptr
)->next
);
1227 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1230 if (verbose
&& !found
)
1232 /* xgettext:c-format */
1233 printf (_("No member named `%s'\n"), *files_to_delete
);
1239 if (something_changed
)
1240 write_archive (arch
);
1242 output_filename
= NULL
;
1246 /* Reposition existing members within an archive */
1249 move_members (arch
, files_to_move
)
1251 char **files_to_move
;
1253 bfd
**after_bfd
; /* New entries go after this one */
1254 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1256 for (; *files_to_move
; ++files_to_move
)
1258 current_ptr_ptr
= &(arch
->next
);
1259 while (*current_ptr_ptr
)
1261 bfd
*current_ptr
= *current_ptr_ptr
;
1262 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1263 current_ptr
->filename
) == 0)
1265 /* Move this file to the end of the list - first cut from
1268 *current_ptr_ptr
= current_ptr
->next
;
1270 /* Now glue to end */
1271 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1273 *after_bfd
= current_ptr
;
1274 current_ptr
->next
= link
;
1277 printf ("m - %s\n", *files_to_move
);
1282 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1284 /* xgettext:c-format */
1285 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1290 write_archive (arch
);
1293 /* Ought to default to replacing in place, but this is existing practice! */
1296 replace_members (arch
, files_to_move
, quick
)
1298 char **files_to_move
;
1301 bfd_boolean changed
= FALSE
;
1302 bfd
**after_bfd
; /* New entries go after this one */
1306 while (files_to_move
&& *files_to_move
)
1310 current_ptr
= &arch
->next
;
1311 while (*current_ptr
)
1313 current
= *current_ptr
;
1315 /* For compatibility with existing ar programs, we
1316 permit the same file to be added multiple times. */
1317 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1318 normalize (current
->filename
, arch
)) == 0
1319 && current
->arelt_data
!= NULL
)
1323 struct stat fsbuf
, asbuf
;
1325 if (stat (*files_to_move
, &fsbuf
) != 0)
1327 if (errno
!= ENOENT
)
1328 bfd_fatal (*files_to_move
);
1331 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1332 /* xgettext:c-format */
1333 fatal (_("internal stat error on %s"),
1336 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1340 after_bfd
= get_pos_bfd (&arch
->next
, pos_after
,
1342 if (ar_emul_replace (after_bfd
, *files_to_move
,
1345 /* Snip out this entry from the chain. */
1346 *current_ptr
= (*current_ptr
)->next
;
1352 current_ptr
= &(current
->next
);
1356 /* Add to the end of the archive. */
1357 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1358 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
))
1367 write_archive (arch
);
1369 output_filename
= NULL
;
1373 ranlib_only (archname
)
1374 const char *archname
;
1379 arch
= open_inarch (archname
, (char *) NULL
);
1382 write_archive (arch
);
1385 /* Update the timestamp of the symbol map of an archive. */
1388 ranlib_touch (archname
)
1389 const char *archname
;
1392 /* I don't think updating works on go32. */
1393 ranlib_only (archname
);
1399 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1402 bfd_set_error (bfd_error_system_call
);
1403 bfd_fatal (archname
);
1406 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1408 bfd_fatal (archname
);
1409 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1411 bfd_nonfatal (archname
);
1412 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1414 list_matching_formats (matching
);
1420 if (! bfd_has_map (arch
))
1421 /* xgettext:c-format */
1422 fatal (_("%s: no archive map to update"), archname
);
1424 bfd_update_armap_timestamp (arch
);
1426 if (! bfd_close (arch
))
1427 bfd_fatal (archname
);
1431 /* Things which are interesting to map over all or some of the files: */
1437 print_arelt_descr (stdout
, abfd
, verbose
);