1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 Bugs: should use getopt the way tar does (complete w/optional -) and
23 should have long options too. GNU ar used to check file against filesystem
24 in quick_update and replace operations (would check mtime). Doesn't warn
25 when name truncated. No way to specify pos_end. Error messages should be
29 #include "libiberty.h"
35 #include "filenames.h"
39 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
41 #define EXT_NAME_LEN 6 /* ditto for *NIX */
44 /* We need to open files in binary modes on system where that makes a
52 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
55 bfd_special_undocumented_glue
PARAMS ((bfd
* abfd
, const char *filename
));
57 /* Static declarations */
60 mri_emul
PARAMS ((void));
63 normalize
PARAMS ((const char *, bfd
*));
66 remove_output
PARAMS ((void));
69 map_over_members
PARAMS ((bfd
*, void (*)(bfd
*), char **, int));
72 print_contents
PARAMS ((bfd
* member
));
75 delete_members
PARAMS ((bfd
*, char **files_to_delete
));
79 do_quick_append
PARAMS ((const char *archive_filename
,
80 char **files_to_append
));
84 move_members
PARAMS ((bfd
*, char **files_to_move
));
87 replace_members
PARAMS ((bfd
*, char **files_to_replace
, boolean quick
));
90 print_descr
PARAMS ((bfd
* abfd
));
93 write_archive
PARAMS ((bfd
*));
96 ranlib_only
PARAMS ((const char *archname
));
99 ranlib_touch
PARAMS ((const char *archname
));
102 usage
PARAMS ((int));
104 /** Globals and flags */
108 /* This flag distinguishes between ar and ranlib:
109 1 means this is 'ranlib'; 0 means this is 'ar'.
110 -1 means if we should use argv[0] to decide. */
111 extern int is_ranlib
;
113 /* Nonzero means don't warn about creating the archive file if necessary. */
114 int silent_create
= 0;
116 /* Nonzero means describe each action performed. */
119 /* Nonzero means preserve dates of members when extracting them. */
120 int preserve_dates
= 0;
122 /* Nonzero means don't replace existing members whose dates are more recent
123 than the corresponding files. */
126 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
127 member). -1 means we've been explicitly asked to not write a symbol table;
128 +1 means we've been explictly asked to write it;
130 Traditionally, the default in BSD has been to not write the table.
131 However, for POSIX.2 compliance the default is now to write a symbol table
132 if any of the members are object files. */
135 /* Nonzero means it's the name of an existing member; position new or moved
136 files with respect to this one. */
137 char *posname
= NULL
;
139 /* Sez how to use `posname': pos_before means position before that member.
140 pos_after means position after that member. pos_end means always at end.
141 pos_default means default appropriately. For the latter two, `posname'
142 should also be zero. */
145 pos_default
, pos_before
, pos_after
, pos_end
146 } postype
= pos_default
;
149 get_pos_bfd
PARAMS ((bfd
**, enum pos
, const char *));
151 /* For extract/delete only. If COUNTED_NAME_MODE is true, we only
152 extract the COUNTED_NAME_COUNTER instance of that name. */
153 static boolean counted_name_mode
= 0;
154 static int counted_name_counter
= 0;
156 /* Whether to truncate names of files stored in the archive. */
157 static boolean ar_truncate
= false;
159 /* Whether to use a full file name match when searching an archive.
160 This is convenient for archives created by the Microsoft lib
162 static boolean full_pathname
= false;
169 interactive
= isatty (fileno (stdin
));
173 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
174 COUNT is the length of the FILES chain; FUNCTION is called on each entry
175 whose name matches one in FILES. */
178 map_over_members (arch
, function
, files
, count
)
180 void (*function
) PARAMS ((bfd
*));
189 for (head
= arch
->next
; head
; head
= head
->next
)
197 /* This may appear to be a baroque way of accomplishing what we want.
198 However we have to iterate over the filenames in order to notice where
199 a filename is requested but does not exist in the archive. Ditto
200 mapping over each file each time -- we want to hack multiple
203 for (; count
> 0; files
++, count
--)
205 boolean found
= false;
208 for (head
= arch
->next
; head
; head
= head
->next
)
211 if (head
->filename
== NULL
)
213 /* Some archive formats don't get the filenames filled in
214 until the elements are opened. */
216 bfd_stat_arch_elt (head
, &buf
);
218 if ((head
->filename
!= NULL
) &&
219 (!FILENAME_CMP (normalize (*files
, arch
), head
->filename
)))
222 if (counted_name_mode
223 && match_count
!= counted_name_counter
)
225 /* Counting, and didn't match on count; go on to the
235 /* xgettext:c-format */
236 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
240 boolean operation_alters_arch
= false;
248 s
= help
? stdout
: stderr
;
252 /* xgettext:c-format */
253 fprintf (s
, _("Usage: %s [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
255 /* xgettext:c-format */
256 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
257 fprintf (s
, _(" commands:\n"));
258 fprintf (s
, _(" d - delete file(s) from the archive\n"));
259 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
260 fprintf (s
, _(" p - print file(s) found in the archive\n"));
261 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
262 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
263 fprintf (s
, _(" t - display contents of archive\n"));
264 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
265 fprintf (s
, _(" command specific modifiers:\n"));
266 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
267 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
268 fprintf (s
, _(" [N] - use instance [count] of name\n"));
269 fprintf (s
, _(" [f] - truncate inserted file names\n"));
270 fprintf (s
, _(" [P] - use full path names when matching\n"));
271 fprintf (s
, _(" [o] - preserve original dates\n"));
272 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
273 fprintf (s
, _(" generic modifiers:\n"));
274 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
275 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
276 fprintf (s
, _(" [S] - do not build a symbol table\n"));
277 fprintf (s
, _(" [v] - be verbose\n"));
278 fprintf (s
, _(" [V] - display the version number\n"));
281 /* xgettext:c-format */
282 fprintf (s
, _("Usage: %s [-vV] archive\n"), program_name
);
284 list_supported_targets (program_name
, stderr
);
287 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
289 xexit (help
? 0 : 1);
292 /* Normalize a file name specified on the command line into a file
293 name which we will use in an archive. */
296 normalize (file
, abfd
)
300 const char *filename
;
305 filename
= strrchr (file
, '/');
306 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
308 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
309 char *bslash
= strrchr (file
, '\\');
310 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
312 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
316 if (filename
!= (char *) NULL
)
323 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
328 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
329 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
330 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
337 /* Remove any output file. This is only called via xatexit. */
339 static const char *output_filename
= NULL
;
340 static FILE *output_file
= NULL
;
341 static bfd
*output_bfd
= NULL
;
346 if (output_filename
!= NULL
)
348 if (output_bfd
!= NULL
&& output_bfd
->iostream
!= NULL
)
349 fclose ((FILE *) (output_bfd
->iostream
));
350 if (output_file
!= NULL
)
351 fclose (output_file
);
352 unlink (output_filename
);
356 /* The option parsing should be in its own function.
357 It will be when I have getopt working. */
368 none
= 0, delete, replace
, print_table
,
369 print_files
, extract
, move
, quick_append
374 char *inarch_filename
;
377 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
378 setlocale (LC_MESSAGES
, "");
380 bindtextdomain (PACKAGE
, LOCALEDIR
);
381 textdomain (PACKAGE
);
383 program_name
= argv
[0];
384 xmalloc_set_program_name (program_name
);
390 temp
= strrchr (program_name
, '/');
391 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
393 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
394 char *bslash
= strrchr (program_name
, '\\');
395 if (temp
== NULL
|| (bslash
!= NULL
&& bslash
> temp
))
397 if (temp
== NULL
&& program_name
[0] != '\0' && program_name
[1] == ':')
398 temp
= program_name
+ 1;
405 if (strlen (temp
) >= 6
406 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
412 if (argc
> 1 && argv
[1][0] == '-')
414 if (strcmp (argv
[1], "--help") == 0)
416 else if (strcmp (argv
[1], "--version") == 0)
419 print_version ("ranlib");
421 print_version ("ar");
425 START_PROGRESS (program_name
, 0);
428 set_default_bfd_target ();
432 xatexit (remove_output
);
436 boolean touch
= false;
438 if (argc
< 2 || strcmp (argv
[1], "--help") == 0)
440 if (strcmp (argv
[1], "-V") == 0
441 || strcmp (argv
[1], "-v") == 0
442 || strncmp (argv
[1], "--v", 3) == 0)
443 print_version ("ranlib");
445 if (strcmp (argv
[1], "-t") == 0)
450 while (arg_index
< argc
)
453 ranlib_only (argv
[arg_index
]);
455 ranlib_touch (argv
[arg_index
]);
461 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
473 ++arg_ptr
; /* compatibility */
475 while ((c
= *arg_ptr
++) != '\0')
486 if (operation
!= none
)
487 fatal (_("two different operation options specified"));
492 operation_alters_arch
= true;
496 operation_alters_arch
= true;
499 operation
= print_files
;
502 operation
= quick_append
;
503 operation_alters_arch
= true;
507 operation_alters_arch
= true;
510 operation
= print_table
;
543 postype
= pos_before
;
546 postype
= pos_before
;
552 counted_name_mode
= true;
558 full_pathname
= true;
561 /* xgettext:c-format */
562 non_fatal (_("illegal option -- %c"), c
);
568 print_version ("ar");
581 /* We can't write an armap when using ar q, so just do ar r
583 if (operation
== quick_append
&& write_armap
)
586 if ((operation
== none
|| operation
== print_table
)
589 ranlib_only (argv
[2]);
593 if (operation
== none
)
594 fatal (_("no operation specified"));
596 if (newer_only
&& operation
!= replace
)
597 fatal (_("`u' is only meaningful with the `r' option."));
601 if (postype
!= pos_default
)
602 posname
= argv
[arg_index
++];
604 if (counted_name_mode
)
606 if (operation
!= extract
&& operation
!= delete)
607 fatal (_("`N' is only meaningful with the `x' and `d' options."));
608 counted_name_counter
= atoi (argv
[arg_index
++]);
609 if (counted_name_counter
<= 0)
610 fatal (_("Value for `N' must be positive."));
613 inarch_filename
= argv
[arg_index
++];
615 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
616 file_count
= argc
- arg_index
;
619 /* We don't use do_quick_append any more. Too many systems
620 expect ar to always rebuild the symbol table even when q is
623 /* We can't do a quick append if we need to construct an
624 extended name table, because do_quick_append won't be able to
625 rebuild the name table. Unfortunately, at this point we
626 don't actually know the maximum name length permitted by this
627 object file format. So, we guess. FIXME. */
628 if (operation
== quick_append
&& ! ar_truncate
)
632 for (chk
= files
; chk
!= NULL
&& *chk
!= '\0'; chk
++)
634 if (strlen (normalize (*chk
, (bfd
*) NULL
)) > 14)
642 if (operation
== quick_append
)
644 /* Note that quick appending to a non-existent archive creates it,
645 even if there are no files to append. */
646 do_quick_append (inarch_filename
, files
);
651 arch
= open_inarch (inarch_filename
,
652 files
== NULL
? (char *) NULL
: files
[0]);
657 map_over_members (arch
, print_descr
, files
, file_count
);
661 map_over_members (arch
, print_contents
, files
, file_count
);
665 map_over_members (arch
, extract_file
, files
, file_count
);
670 delete_members (arch
, files
);
672 output_filename
= NULL
;
677 move_members (arch
, files
);
679 output_filename
= NULL
;
684 if (files
!= NULL
|| write_armap
> 0)
685 replace_members (arch
, files
, operation
== quick_append
);
687 output_filename
= NULL
;
690 /* Shouldn't happen! */
692 /* xgettext:c-format */
693 fatal (_("internal error -- this option not implemented"));
697 END_PROGRESS (program_name
);
704 open_inarch (archive_filename
, file
)
705 const char *archive_filename
;
715 bfd_set_error (bfd_error_no_error
);
719 if (stat (archive_filename
, &sbuf
) != 0)
721 #if !defined(__GO32__) || defined(__DJGPP__)
723 /* FIXME: I don't understand why this fragment was ifndef'ed
724 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
725 stat() works just fine in v2.x, so I think this should be
726 removed. For now, I enable it for DJGPP v2. -- EZ. */
728 /* KLUDGE ALERT! Temporary fix until I figger why
729 stat() is wrong ... think it's buried in GO32's IDT - Jax */
731 bfd_fatal (archive_filename
);
734 if (!operation_alters_arch
)
736 fprintf (stderr
, "%s: ", program_name
);
737 perror (archive_filename
);
742 /* Try to figure out the target to use for the archive from the
743 first object on the list. */
748 obj
= bfd_openr (file
, NULL
);
751 if (bfd_check_format (obj
, bfd_object
))
752 target
= bfd_get_target (obj
);
753 (void) bfd_close (obj
);
757 /* Create an empty archive. */
758 arch
= bfd_openw (archive_filename
, target
);
760 || ! bfd_set_format (arch
, bfd_archive
)
761 || ! bfd_close (arch
))
762 bfd_fatal (archive_filename
);
764 /* If we die creating a new archive, don't leave it around. */
765 output_filename
= archive_filename
;
768 arch
= bfd_openr (archive_filename
, target
);
772 bfd_fatal (archive_filename
);
775 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
777 bfd_nonfatal (archive_filename
);
778 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
780 list_matching_formats (matching
);
786 last_one
= &(arch
->next
);
787 /* Read all the contents right away, regardless. */
788 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
790 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
793 *last_one
= next_one
;
794 last_one
= &next_one
->next
;
796 *last_one
= (bfd
*) NULL
;
797 if (bfd_get_error () != bfd_error_no_more_archived_files
)
803 print_contents (abfd
)
807 char *cbuf
= xmalloc (BUFSIZE
);
810 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
811 /* xgettext:c-format */
812 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
815 /* xgettext:c-format */
816 printf (_("\n<member %s>\n\n"), bfd_get_filename (abfd
));
818 bfd_seek (abfd
, 0, SEEK_SET
);
821 while (ncopied
< size
)
825 int tocopy
= size
- ncopied
;
826 if (tocopy
> BUFSIZE
)
829 nread
= bfd_read (cbuf
, 1, tocopy
, abfd
); /* oops -- broke
832 /* xgettext:c-format */
833 fatal (_("%s is not a valid archive"),
834 bfd_get_filename (bfd_my_archive (abfd
)));
835 fwrite (cbuf
, 1, nread
, stdout
);
841 /* Extract a member of the archive into its own file.
843 We defer opening the new file until after we have read a BUFSIZ chunk of the
844 old one, since we know we have just read the archive header for the old
845 one. Since most members are shorter than BUFSIZ, this means we will read
846 the old header, read the old data, write a new inode for the new file, and
847 write the new data, and be done. This 'optimization' is what comes from
848 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
856 char *cbuf
= xmalloc (BUFSIZE
);
862 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
863 /* xgettext:c-format */
864 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
868 /* xgettext:c-format */
869 fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd
));
872 printf ("x - %s\n", bfd_get_filename (abfd
));
874 bfd_seek (abfd
, 0, SEEK_SET
);
879 /* Seems like an abstraction violation, eh? Well it's OK! */
880 output_filename
= bfd_get_filename (abfd
);
882 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
885 perror (bfd_get_filename (abfd
));
889 output_file
= ostream
;
892 while (ncopied
< size
)
894 tocopy
= size
- ncopied
;
895 if (tocopy
> BUFSIZE
)
898 nread
= bfd_read (cbuf
, 1, tocopy
, abfd
);
900 /* xgettext:c-format */
901 fatal (_("%s is not a valid archive"),
902 bfd_get_filename (bfd_my_archive (abfd
)));
904 /* See comment above; this saves disk arm motion */
907 /* Seems like an abstraction violation, eh? Well it's OK! */
908 output_filename
= bfd_get_filename (abfd
);
910 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
913 perror (bfd_get_filename (abfd
));
917 output_file
= ostream
;
919 fwrite (cbuf
, 1, nread
, ostream
);
927 output_filename
= NULL
;
929 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
932 set_times (bfd_get_filename (abfd
), &buf
);
939 /* We don't use this anymore. Too many systems expect ar to rebuild
940 the symbol table even when q is used. */
942 /* Just do it quickly; don't worry about dups, armap, or anything like that */
945 do_quick_append (archive_filename
, files_to_append
)
946 const char *archive_filename
;
947 char **files_to_append
;
950 char *buf
= xmalloc (BUFSIZE
);
951 long tocopy
, thistime
;
954 boolean newfile
= false;
955 bfd_set_error (bfd_error_no_error
);
957 if (stat (archive_filename
, &sbuf
) != 0)
960 #if !defined(__GO32__) || defined(__DJGPP__)
962 /* FIXME: I don't understand why this fragment was ifndef'ed
963 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
964 stat() works just fine in v2.x, so I think this should be
965 removed. For now, I enable it for DJGPP v2.
967 (And yes, I know this is all unused, but somebody, someday,
968 might wish to resurrect this again... -- EZ. */
970 /* KLUDGE ALERT! Temporary fix until I figger why
971 stat() is wrong ... think it's buried in GO32's IDT - Jax */
974 bfd_fatal (archive_filename
);
980 ofile
= fopen (archive_filename
, FOPEN_AUB
);
983 perror (program_name
);
987 temp
= bfd_openr (archive_filename
, NULL
);
990 bfd_fatal (archive_filename
);
992 if (newfile
== false)
994 if (bfd_check_format (temp
, bfd_archive
) != true)
995 /* xgettext:c-format */
996 fatal (_("%s is not an archive"), archive_filename
);
1000 fwrite (ARMAG
, 1, SARMAG
, ofile
);
1002 /* xgettext:c-format */
1003 non_fatal (_("creating %s"), archive_filename
);
1007 temp
->flags
|= BFD_TRADITIONAL_FORMAT
;
1009 /* assume it's an achive, go straight to the end, sans $200 */
1010 fseek (ofile
, 0, 2);
1012 for (; files_to_append
&& *files_to_append
; ++files_to_append
)
1014 struct ar_hdr
*hdr
= bfd_special_undocumented_glue (temp
, *files_to_append
);
1017 bfd_fatal (*files_to_append
);
1020 BFD_SEND (temp
, _bfd_truncate_arname
, (temp
, *files_to_append
, (char *) hdr
));
1022 ifile
= fopen (*files_to_append
, FOPEN_RB
);
1025 bfd_nonfatal (*files_to_append
);
1028 if (stat (*files_to_append
, &sbuf
) != 0)
1030 bfd_nonfatal (*files_to_append
);
1033 tocopy
= sbuf
.st_size
;
1035 /* XXX should do error-checking! */
1036 fwrite (hdr
, 1, sizeof (struct ar_hdr
), ofile
);
1041 if (thistime
> BUFSIZE
)
1043 fread (buf
, 1, thistime
, ifile
);
1044 fwrite (buf
, 1, thistime
, ofile
);
1048 if ((sbuf
.st_size
% 2) == 1)
1049 putc ('\012', ofile
);
1059 write_archive (iarch
)
1063 char *old_name
, *new_name
;
1064 bfd
*contents_head
= iarch
->next
;
1066 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1067 strcpy (old_name
, bfd_get_filename (iarch
));
1068 new_name
= make_tempname (old_name
);
1070 output_filename
= new_name
;
1072 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1075 bfd_fatal (old_name
);
1079 bfd_set_format (obfd
, bfd_archive
);
1081 /* Request writing the archive symbol table unless we've
1082 been explicitly requested not to. */
1083 obfd
->has_armap
= write_armap
>= 0;
1087 /* This should really use bfd_set_file_flags, but that rejects
1089 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1092 if (bfd_set_archive_head (obfd
, contents_head
) != true)
1093 bfd_fatal (old_name
);
1095 if (!bfd_close (obfd
))
1096 bfd_fatal (old_name
);
1099 output_filename
= NULL
;
1101 /* We don't care if this fails; we might be creating the archive. */
1104 if (smart_rename (new_name
, old_name
, 0) != 0)
1108 /* Return a pointer to the pointer to the entry which should be rplacd'd
1109 into when altering. DEFAULT_POS should be how to interpret pos_default,
1110 and should be a pos value. */
1113 get_pos_bfd (contents
, default_pos
, default_posname
)
1115 enum pos default_pos
;
1116 const char *default_posname
;
1118 bfd
**after_bfd
= contents
;
1120 const char *realposname
;
1122 if (postype
== pos_default
)
1124 realpos
= default_pos
;
1125 realposname
= default_posname
;
1130 realposname
= posname
;
1133 if (realpos
== pos_end
)
1136 after_bfd
= &((*after_bfd
)->next
);
1140 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->next
)
1141 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1143 if (realpos
== pos_after
)
1144 after_bfd
= &(*after_bfd
)->next
;
1152 delete_members (arch
, files_to_delete
)
1154 char **files_to_delete
;
1156 bfd
**current_ptr_ptr
;
1158 boolean something_changed
= false;
1161 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1163 /* In a.out systems, the armap is optional. It's also called
1164 __.SYMDEF. So if the user asked to delete it, we should remember
1165 that fact. This isn't quite right for COFF systems (where
1166 __.SYMDEF might be regular member), but it's very unlikely
1167 to be a problem. FIXME */
1169 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1171 arch
->has_armap
= false;
1178 current_ptr_ptr
= &(arch
->next
);
1179 while (*current_ptr_ptr
)
1181 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1182 (*current_ptr_ptr
)->filename
) == 0)
1185 if (counted_name_mode
1186 && match_count
!= counted_name_counter
)
1188 /* Counting, and didn't match on count; go on to the
1194 something_changed
= true;
1198 *current_ptr_ptr
= ((*current_ptr_ptr
)->next
);
1203 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1206 if (verbose
&& found
== false)
1208 /* xgettext:c-format */
1209 printf (_("No member named `%s'\n"), *files_to_delete
);
1215 if (something_changed
== true)
1216 write_archive (arch
);
1218 output_filename
= NULL
;
1222 /* Reposition existing members within an archive */
1225 move_members (arch
, files_to_move
)
1227 char **files_to_move
;
1229 bfd
**after_bfd
; /* New entries go after this one */
1230 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1232 for (; *files_to_move
; ++files_to_move
)
1234 current_ptr_ptr
= &(arch
->next
);
1235 while (*current_ptr_ptr
)
1237 bfd
*current_ptr
= *current_ptr_ptr
;
1238 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1239 current_ptr
->filename
) == 0)
1241 /* Move this file to the end of the list - first cut from
1244 *current_ptr_ptr
= current_ptr
->next
;
1246 /* Now glue to end */
1247 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1249 *after_bfd
= current_ptr
;
1250 current_ptr
->next
= link
;
1253 printf ("m - %s\n", *files_to_move
);
1258 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1260 /* xgettext:c-format */
1261 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1266 write_archive (arch
);
1269 /* Ought to default to replacing in place, but this is existing practice! */
1272 replace_members (arch
, files_to_move
, quick
)
1274 char **files_to_move
;
1277 boolean changed
= false;
1278 bfd
**after_bfd
; /* New entries go after this one */
1283 while (files_to_move
&& *files_to_move
)
1287 current_ptr
= &arch
->next
;
1288 while (*current_ptr
)
1290 current
= *current_ptr
;
1292 /* For compatibility with existing ar programs, we
1293 permit the same file to be added multiple times. */
1294 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1295 normalize (current
->filename
, arch
)) == 0
1296 && current
->arelt_data
!= NULL
)
1300 struct stat fsbuf
, asbuf
;
1302 if (stat (*files_to_move
, &fsbuf
) != 0)
1304 if (errno
!= ENOENT
)
1305 bfd_fatal (*files_to_move
);
1308 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1309 /* xgettext:c-format */
1310 fatal (_("internal stat error on %s"), current
->filename
);
1312 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1316 after_bfd
= get_pos_bfd (&arch
->next
, pos_after
,
1320 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1321 if (*after_bfd
== (bfd
*) NULL
)
1323 bfd_fatal (*files_to_move
);
1325 (*after_bfd
)->next
= temp
;
1327 /* snip out this entry from the chain */
1328 *current_ptr
= (*current_ptr
)->next
;
1332 printf ("r - %s\n", *files_to_move
);
1339 current_ptr
= &(current
->next
);
1343 /* Add to the end of the archive. */
1345 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1347 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1348 if (*after_bfd
== (bfd
*) NULL
)
1350 bfd_fatal (*files_to_move
);
1354 printf ("a - %s\n", *files_to_move
);
1357 (*after_bfd
)->next
= temp
;
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
);