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"
40 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
42 #define EXT_NAME_LEN 6 /* ditto for *NIX */
45 /* We need to open files in binary modes on system where that makes a
53 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
56 bfd_special_undocumented_glue
PARAMS ((bfd
* abfd
, const char *filename
));
58 /* Static declarations */
61 mri_emul
PARAMS ((void));
64 normalize
PARAMS ((const char *, bfd
*));
67 remove_output
PARAMS ((void));
70 map_over_members
PARAMS ((bfd
*, void (*)(bfd
*), char **, int));
73 print_contents
PARAMS ((bfd
* member
));
76 delete_members
PARAMS ((bfd
*, char **files_to_delete
));
80 do_quick_append
PARAMS ((const char *archive_filename
,
81 char **files_to_append
));
85 move_members
PARAMS ((bfd
*, char **files_to_move
));
88 replace_members
PARAMS ((bfd
*, char **files_to_replace
, boolean quick
));
91 print_descr
PARAMS ((bfd
* abfd
));
94 write_archive
PARAMS ((bfd
*));
97 ranlib_only
PARAMS ((const char *archname
));
100 ranlib_touch
PARAMS ((const char *archname
));
103 usage
PARAMS ((int));
105 /** Globals and flags */
109 /* This flag distinguishes between ar and ranlib:
110 1 means this is 'ranlib'; 0 means this is 'ar'.
111 -1 means if we should use argv[0] to decide. */
112 extern int is_ranlib
;
114 /* Nonzero means don't warn about creating the archive file if necessary. */
115 int silent_create
= 0;
117 /* Nonzero means describe each action performed. */
120 /* Nonzero means preserve dates of members when extracting them. */
121 int preserve_dates
= 0;
123 /* Nonzero means don't replace existing members whose dates are more recent
124 than the corresponding files. */
127 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
128 member). -1 means we've been explicitly asked to not write a symbol table;
129 +1 means we've been explictly asked to write it;
131 Traditionally, the default in BSD has been to not write the table.
132 However, for POSIX.2 compliance the default is now to write a symbol table
133 if any of the members are object files. */
136 /* Nonzero means it's the name of an existing member; position new or moved
137 files with respect to this one. */
138 char *posname
= NULL
;
140 /* Sez how to use `posname': pos_before means position before that member.
141 pos_after means position after that member. pos_end means always at end.
142 pos_default means default appropriately. For the latter two, `posname'
143 should also be zero. */
146 pos_default
, pos_before
, pos_after
, pos_end
147 } postype
= pos_default
;
150 get_pos_bfd
PARAMS ((bfd
**, enum pos
, const char *));
152 /* For extract/delete only. If COUNTED_NAME_MODE is true, we only
153 extract the COUNTED_NAME_COUNTER instance of that name. */
154 static boolean counted_name_mode
= 0;
155 static int counted_name_counter
= 0;
157 /* Whether to truncate names of files stored in the archive. */
158 static boolean ar_truncate
= false;
160 /* Whether to use a full file name match when searching an archive.
161 This is convenient for archives created by the Microsoft lib
163 static boolean full_pathname
= false;
170 interactive
= isatty (fileno (stdin
));
174 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
175 COUNT is the length of the FILES chain; FUNCTION is called on each entry
176 whose name matches one in FILES. */
179 map_over_members (arch
, function
, files
, count
)
181 void (*function
) PARAMS ((bfd
*));
190 for (head
= arch
->next
; head
; head
= head
->next
)
198 /* This may appear to be a baroque way of accomplishing what we want.
199 However we have to iterate over the filenames in order to notice where
200 a filename is requested but does not exist in the archive. Ditto
201 mapping over each file each time -- we want to hack multiple
204 for (; count
> 0; files
++, count
--)
206 boolean found
= false;
209 for (head
= arch
->next
; head
; head
= head
->next
)
212 if (head
->filename
== NULL
)
214 /* Some archive formats don't get the filenames filled in
215 until the elements are opened. */
217 bfd_stat_arch_elt (head
, &buf
);
219 if ((head
->filename
!= NULL
) &&
220 (!FILENAME_CMP (normalize (*files
, arch
), head
->filename
)))
223 if (counted_name_mode
224 && match_count
!= counted_name_counter
)
226 /* Counting, and didn't match on count; go on to the
236 /* xgettext:c-format */
237 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
241 boolean operation_alters_arch
= false;
249 s
= help
? stdout
: stderr
;
253 /* xgettext:c-format */
254 fprintf (s
, _("Usage: %s [-X32_64] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
256 /* xgettext:c-format */
257 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
258 fprintf (s
, _(" commands:\n"));
259 fprintf (s
, _(" d - delete file(s) from the archive\n"));
260 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
261 fprintf (s
, _(" p - print file(s) found in the archive\n"));
262 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
263 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
264 fprintf (s
, _(" t - display contents of archive\n"));
265 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
266 fprintf (s
, _(" command specific modifiers:\n"));
267 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
268 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
269 fprintf (s
, _(" [N] - use instance [count] of name\n"));
270 fprintf (s
, _(" [f] - truncate inserted file names\n"));
271 fprintf (s
, _(" [P] - use full path names when matching\n"));
272 fprintf (s
, _(" [o] - preserve original dates\n"));
273 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
274 fprintf (s
, _(" generic modifiers:\n"));
275 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
276 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
277 fprintf (s
, _(" [S] - do not build a symbol table\n"));
278 fprintf (s
, _(" [v] - be verbose\n"));
279 fprintf (s
, _(" [V] - display the version number\n"));
280 fprintf (s
, _(" [-X32_64] - (ignored)\n"));
284 /* xgettext:c-format */
285 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
286 fprintf (s
, _(" Generate an index to speed access to archives\n"));
287 fprintf (s
, _(" The options are:\n\
288 -h --help Print this help message\n\
289 -V --version Print version information\n"));
292 list_supported_targets (program_name
, stderr
);
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 (file
, abfd
)
308 const char *filename
;
313 filename
= strrchr (file
, '/');
314 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
316 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
317 char *bslash
= strrchr (file
, '\\');
318 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
320 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
324 if (filename
!= (char *) NULL
)
331 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
336 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
337 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
338 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
345 /* Remove any output file. This is only called via xatexit. */
347 static const char *output_filename
= NULL
;
348 static FILE *output_file
= NULL
;
349 static bfd
*output_bfd
= NULL
;
354 if (output_filename
!= NULL
)
356 if (output_bfd
!= NULL
&& output_bfd
->iostream
!= NULL
)
357 fclose ((FILE *) (output_bfd
->iostream
));
358 if (output_file
!= NULL
)
359 fclose (output_file
);
360 unlink (output_filename
);
364 /* The option parsing should be in its own function.
365 It will be when I have getopt working. */
367 int main
PARAMS ((int, char **));
378 none
= 0, delete, replace
, print_table
,
379 print_files
, extract
, move
, quick_append
384 char *inarch_filename
;
387 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
388 setlocale (LC_MESSAGES
, "");
390 #if defined (HAVE_SETLOCALE)
391 setlocale (LC_CTYPE
, "");
393 bindtextdomain (PACKAGE
, LOCALEDIR
);
394 textdomain (PACKAGE
);
396 program_name
= argv
[0];
397 xmalloc_set_program_name (program_name
);
403 temp
= strrchr (program_name
, '/');
404 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
406 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
407 char *bslash
= strrchr (program_name
, '\\');
408 if (temp
== NULL
|| (bslash
!= NULL
&& bslash
> temp
))
410 if (temp
== NULL
&& program_name
[0] != '\0' && program_name
[1] == ':')
411 temp
= program_name
+ 1;
418 if (strlen (temp
) >= 6
419 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
425 if (argc
> 1 && argv
[1][0] == '-')
427 if (strcmp (argv
[1], "--help") == 0)
429 else if (strcmp (argv
[1], "--version") == 0)
432 print_version ("ranlib");
434 print_version ("ar");
438 START_PROGRESS (program_name
, 0);
441 set_default_bfd_target ();
445 xatexit (remove_output
);
447 /* Ignored for (partial) AIX compatibility. On AIX,
448 the -X option can be used to ignore certain kinds
449 of object files in the archive (the 64-bit objects
450 or the 32-bit objects). GNU ar always looks at all
451 kinds of objects in an archive. */
452 while (argc
> 1 && strcmp (argv
[1], "-X32_64") == 0)
460 boolean touch
= false;
463 || strcmp (argv
[1], "--help") == 0
464 || strcmp (argv
[1], "-h") == 0
465 || strcmp (argv
[1], "-H") == 0)
467 if (strcmp (argv
[1], "-V") == 0
468 || strcmp (argv
[1], "-v") == 0
469 || strncmp (argv
[1], "--v", 3) == 0)
470 print_version ("ranlib");
472 if (strcmp (argv
[1], "-t") == 0)
477 while (arg_index
< argc
)
480 ranlib_only (argv
[arg_index
]);
482 ranlib_touch (argv
[arg_index
]);
488 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
500 ++arg_ptr
; /* compatibility */
502 while ((c
= *arg_ptr
++) != '\0')
513 if (operation
!= none
)
514 fatal (_("two different operation options specified"));
519 operation_alters_arch
= true;
523 operation_alters_arch
= true;
526 operation
= print_files
;
529 operation
= quick_append
;
530 operation_alters_arch
= true;
534 operation_alters_arch
= true;
537 operation
= print_table
;
570 postype
= pos_before
;
573 postype
= pos_before
;
579 counted_name_mode
= true;
585 full_pathname
= true;
588 /* xgettext:c-format */
589 non_fatal (_("illegal option -- %c"), c
);
595 print_version ("ar");
608 /* We can't write an armap when using ar q, so just do ar r
610 if (operation
== quick_append
&& write_armap
)
613 if ((operation
== none
|| operation
== print_table
)
616 ranlib_only (argv
[2]);
620 if (operation
== none
)
621 fatal (_("no operation specified"));
623 if (newer_only
&& operation
!= replace
)
624 fatal (_("`u' is only meaningful with the `r' option."));
628 if (postype
!= pos_default
)
629 posname
= argv
[arg_index
++];
631 if (counted_name_mode
)
633 if (operation
!= extract
&& operation
!= delete)
634 fatal (_("`N' is only meaningful with the `x' and `d' options."));
635 counted_name_counter
= atoi (argv
[arg_index
++]);
636 if (counted_name_counter
<= 0)
637 fatal (_("Value for `N' must be positive."));
640 inarch_filename
= argv
[arg_index
++];
642 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
643 file_count
= argc
- arg_index
;
646 /* We don't use do_quick_append any more. Too many systems
647 expect ar to always rebuild the symbol table even when q is
650 /* We can't do a quick append if we need to construct an
651 extended name table, because do_quick_append won't be able to
652 rebuild the name table. Unfortunately, at this point we
653 don't actually know the maximum name length permitted by this
654 object file format. So, we guess. FIXME. */
655 if (operation
== quick_append
&& ! ar_truncate
)
659 for (chk
= files
; chk
!= NULL
&& *chk
!= '\0'; chk
++)
661 if (strlen (normalize (*chk
, (bfd
*) NULL
)) > 14)
669 if (operation
== quick_append
)
671 /* Note that quick appending to a non-existent archive creates it,
672 even if there are no files to append. */
673 do_quick_append (inarch_filename
, files
);
678 arch
= open_inarch (inarch_filename
,
679 files
== NULL
? (char *) NULL
: files
[0]);
684 map_over_members (arch
, print_descr
, files
, file_count
);
688 map_over_members (arch
, print_contents
, files
, file_count
);
692 map_over_members (arch
, extract_file
, files
, file_count
);
697 delete_members (arch
, files
);
699 output_filename
= NULL
;
704 move_members (arch
, files
);
706 output_filename
= NULL
;
711 if (files
!= NULL
|| write_armap
> 0)
712 replace_members (arch
, files
, operation
== quick_append
);
714 output_filename
= NULL
;
717 /* Shouldn't happen! */
719 /* xgettext:c-format */
720 fatal (_("internal error -- this option not implemented"));
724 END_PROGRESS (program_name
);
731 open_inarch (archive_filename
, file
)
732 const char *archive_filename
;
742 bfd_set_error (bfd_error_no_error
);
746 if (stat (archive_filename
, &sbuf
) != 0)
748 #if !defined(__GO32__) || defined(__DJGPP__)
750 /* FIXME: I don't understand why this fragment was ifndef'ed
751 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
752 stat() works just fine in v2.x, so I think this should be
753 removed. For now, I enable it for DJGPP v2. -- EZ. */
755 /* KLUDGE ALERT! Temporary fix until I figger why
756 stat() is wrong ... think it's buried in GO32's IDT - Jax */
758 bfd_fatal (archive_filename
);
761 if (!operation_alters_arch
)
763 fprintf (stderr
, "%s: ", program_name
);
764 perror (archive_filename
);
769 /* Try to figure out the target to use for the archive from the
770 first object on the list. */
775 obj
= bfd_openr (file
, NULL
);
778 if (bfd_check_format (obj
, bfd_object
))
779 target
= bfd_get_target (obj
);
780 (void) bfd_close (obj
);
784 /* Create an empty archive. */
785 arch
= bfd_openw (archive_filename
, target
);
787 || ! bfd_set_format (arch
, bfd_archive
)
788 || ! bfd_close (arch
))
789 bfd_fatal (archive_filename
);
791 /* If we die creating a new archive, don't leave it around. */
792 output_filename
= archive_filename
;
795 arch
= bfd_openr (archive_filename
, target
);
799 bfd_fatal (archive_filename
);
802 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
804 bfd_nonfatal (archive_filename
);
805 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
807 list_matching_formats (matching
);
813 last_one
= &(arch
->next
);
814 /* Read all the contents right away, regardless. */
815 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
817 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
820 *last_one
= next_one
;
821 last_one
= &next_one
->next
;
823 *last_one
= (bfd
*) NULL
;
824 if (bfd_get_error () != bfd_error_no_more_archived_files
)
830 print_contents (abfd
)
834 char *cbuf
= xmalloc (BUFSIZE
);
837 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
838 /* xgettext:c-format */
839 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
842 /* xgettext:c-format */
843 printf (_("\n<member %s>\n\n"), bfd_get_filename (abfd
));
845 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
848 while (ncopied
< size
)
852 int tocopy
= size
- ncopied
;
853 if (tocopy
> BUFSIZE
)
856 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
858 /* xgettext:c-format */
859 fatal (_("%s is not a valid archive"),
860 bfd_get_filename (bfd_my_archive (abfd
)));
861 fwrite (cbuf
, 1, nread
, stdout
);
867 /* Extract a member of the archive into its own file.
869 We defer opening the new file until after we have read a BUFSIZ chunk of the
870 old one, since we know we have just read the archive header for the old
871 one. Since most members are shorter than BUFSIZ, this means we will read
872 the old header, read the old data, write a new inode for the new file, and
873 write the new data, and be done. This 'optimization' is what comes from
874 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
882 char *cbuf
= xmalloc (BUFSIZE
);
888 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
889 /* xgettext:c-format */
890 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
894 /* xgettext:c-format */
895 fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd
));
898 printf ("x - %s\n", bfd_get_filename (abfd
));
900 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
905 /* Seems like an abstraction violation, eh? Well it's OK! */
906 output_filename
= bfd_get_filename (abfd
);
908 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
911 perror (bfd_get_filename (abfd
));
915 output_file
= ostream
;
918 while (ncopied
< size
)
920 tocopy
= size
- ncopied
;
921 if (tocopy
> BUFSIZE
)
924 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
926 /* xgettext:c-format */
927 fatal (_("%s is not a valid archive"),
928 bfd_get_filename (bfd_my_archive (abfd
)));
930 /* See comment above; this saves disk arm motion */
933 /* Seems like an abstraction violation, eh? Well it's OK! */
934 output_filename
= bfd_get_filename (abfd
);
936 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
939 perror (bfd_get_filename (abfd
));
943 output_file
= ostream
;
945 fwrite (cbuf
, 1, nread
, ostream
);
953 output_filename
= NULL
;
955 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
958 set_times (bfd_get_filename (abfd
), &buf
);
965 /* We don't use this anymore. Too many systems expect ar to rebuild
966 the symbol table even when q is used. */
968 /* Just do it quickly; don't worry about dups, armap, or anything like that */
971 do_quick_append (archive_filename
, files_to_append
)
972 const char *archive_filename
;
973 char **files_to_append
;
976 char *buf
= xmalloc (BUFSIZE
);
977 long tocopy
, thistime
;
980 boolean newfile
= false;
981 bfd_set_error (bfd_error_no_error
);
983 if (stat (archive_filename
, &sbuf
) != 0)
986 #if !defined(__GO32__) || defined(__DJGPP__)
988 /* FIXME: I don't understand why this fragment was ifndef'ed
989 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
990 stat() works just fine in v2.x, so I think this should be
991 removed. For now, I enable it for DJGPP v2.
993 (And yes, I know this is all unused, but somebody, someday,
994 might wish to resurrect this again... -- EZ. */
996 /* KLUDGE ALERT! Temporary fix until I figger why
997 stat() is wrong ... think it's buried in GO32's IDT - Jax */
1000 bfd_fatal (archive_filename
);
1006 ofile
= fopen (archive_filename
, FOPEN_AUB
);
1009 perror (program_name
);
1013 temp
= bfd_openr (archive_filename
, NULL
);
1016 bfd_fatal (archive_filename
);
1018 if (newfile
== false)
1020 if (bfd_check_format (temp
, bfd_archive
) != true)
1021 /* xgettext:c-format */
1022 fatal (_("%s is not an archive"), archive_filename
);
1026 fwrite (ARMAG
, 1, SARMAG
, ofile
);
1028 /* xgettext:c-format */
1029 non_fatal (_("creating %s"), archive_filename
);
1033 temp
->flags
|= BFD_TRADITIONAL_FORMAT
;
1035 /* assume it's an achive, go straight to the end, sans $200 */
1036 fseek (ofile
, 0, 2);
1038 for (; files_to_append
&& *files_to_append
; ++files_to_append
)
1040 struct ar_hdr
*hdr
= bfd_special_undocumented_glue (temp
, *files_to_append
);
1043 bfd_fatal (*files_to_append
);
1046 BFD_SEND (temp
, _bfd_truncate_arname
, (temp
, *files_to_append
, (char *) hdr
));
1048 ifile
= fopen (*files_to_append
, FOPEN_RB
);
1051 bfd_nonfatal (*files_to_append
);
1054 if (stat (*files_to_append
, &sbuf
) != 0)
1056 bfd_nonfatal (*files_to_append
);
1059 tocopy
= sbuf
.st_size
;
1061 /* XXX should do error-checking! */
1062 fwrite (hdr
, 1, sizeof (struct ar_hdr
), ofile
);
1067 if (thistime
> BUFSIZE
)
1069 fread (buf
, 1, thistime
, ifile
);
1070 fwrite (buf
, 1, thistime
, ofile
);
1074 if ((sbuf
.st_size
% 2) == 1)
1075 putc ('\012', ofile
);
1085 write_archive (iarch
)
1089 char *old_name
, *new_name
;
1090 bfd
*contents_head
= iarch
->next
;
1092 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1093 strcpy (old_name
, bfd_get_filename (iarch
));
1094 new_name
= make_tempname (old_name
);
1096 output_filename
= new_name
;
1098 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1101 bfd_fatal (old_name
);
1105 bfd_set_format (obfd
, bfd_archive
);
1107 /* Request writing the archive symbol table unless we've
1108 been explicitly requested not to. */
1109 obfd
->has_armap
= write_armap
>= 0;
1113 /* This should really use bfd_set_file_flags, but that rejects
1115 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1118 if (bfd_set_archive_head (obfd
, contents_head
) != true)
1119 bfd_fatal (old_name
);
1121 if (!bfd_close (obfd
))
1122 bfd_fatal (old_name
);
1125 output_filename
= NULL
;
1127 /* We don't care if this fails; we might be creating the archive. */
1130 if (smart_rename (new_name
, old_name
, 0) != 0)
1134 /* Return a pointer to the pointer to the entry which should be rplacd'd
1135 into when altering. DEFAULT_POS should be how to interpret pos_default,
1136 and should be a pos value. */
1139 get_pos_bfd (contents
, default_pos
, default_posname
)
1141 enum pos default_pos
;
1142 const char *default_posname
;
1144 bfd
**after_bfd
= contents
;
1146 const char *realposname
;
1148 if (postype
== pos_default
)
1150 realpos
= default_pos
;
1151 realposname
= default_posname
;
1156 realposname
= posname
;
1159 if (realpos
== pos_end
)
1162 after_bfd
= &((*after_bfd
)->next
);
1166 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->next
)
1167 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1169 if (realpos
== pos_after
)
1170 after_bfd
= &(*after_bfd
)->next
;
1178 delete_members (arch
, files_to_delete
)
1180 char **files_to_delete
;
1182 bfd
**current_ptr_ptr
;
1184 boolean something_changed
= false;
1187 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1189 /* In a.out systems, the armap is optional. It's also called
1190 __.SYMDEF. So if the user asked to delete it, we should remember
1191 that fact. This isn't quite right for COFF systems (where
1192 __.SYMDEF might be regular member), but it's very unlikely
1193 to be a problem. FIXME */
1195 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1197 arch
->has_armap
= false;
1204 current_ptr_ptr
= &(arch
->next
);
1205 while (*current_ptr_ptr
)
1207 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1208 (*current_ptr_ptr
)->filename
) == 0)
1211 if (counted_name_mode
1212 && match_count
!= counted_name_counter
)
1214 /* Counting, and didn't match on count; go on to the
1220 something_changed
= true;
1224 *current_ptr_ptr
= ((*current_ptr_ptr
)->next
);
1229 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1232 if (verbose
&& found
== false)
1234 /* xgettext:c-format */
1235 printf (_("No member named `%s'\n"), *files_to_delete
);
1241 if (something_changed
== true)
1242 write_archive (arch
);
1244 output_filename
= NULL
;
1248 /* Reposition existing members within an archive */
1251 move_members (arch
, files_to_move
)
1253 char **files_to_move
;
1255 bfd
**after_bfd
; /* New entries go after this one */
1256 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1258 for (; *files_to_move
; ++files_to_move
)
1260 current_ptr_ptr
= &(arch
->next
);
1261 while (*current_ptr_ptr
)
1263 bfd
*current_ptr
= *current_ptr_ptr
;
1264 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1265 current_ptr
->filename
) == 0)
1267 /* Move this file to the end of the list - first cut from
1270 *current_ptr_ptr
= current_ptr
->next
;
1272 /* Now glue to end */
1273 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1275 *after_bfd
= current_ptr
;
1276 current_ptr
->next
= link
;
1279 printf ("m - %s\n", *files_to_move
);
1284 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1286 /* xgettext:c-format */
1287 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1292 write_archive (arch
);
1295 /* Ought to default to replacing in place, but this is existing practice! */
1298 replace_members (arch
, files_to_move
, quick
)
1300 char **files_to_move
;
1303 boolean changed
= false;
1304 bfd
**after_bfd
; /* New entries go after this one */
1309 while (files_to_move
&& *files_to_move
)
1313 current_ptr
= &arch
->next
;
1314 while (*current_ptr
)
1316 current
= *current_ptr
;
1318 /* For compatibility with existing ar programs, we
1319 permit the same file to be added multiple times. */
1320 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1321 normalize (current
->filename
, arch
)) == 0
1322 && current
->arelt_data
!= NULL
)
1326 struct stat fsbuf
, asbuf
;
1328 if (stat (*files_to_move
, &fsbuf
) != 0)
1330 if (errno
!= ENOENT
)
1331 bfd_fatal (*files_to_move
);
1334 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1335 /* xgettext:c-format */
1336 fatal (_("internal stat error on %s"), current
->filename
);
1338 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1342 after_bfd
= get_pos_bfd (&arch
->next
, pos_after
,
1346 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1347 if (*after_bfd
== (bfd
*) NULL
)
1349 bfd_fatal (*files_to_move
);
1351 (*after_bfd
)->next
= temp
;
1353 /* snip out this entry from the chain */
1354 *current_ptr
= (*current_ptr
)->next
;
1358 printf ("r - %s\n", *files_to_move
);
1365 current_ptr
= &(current
->next
);
1369 /* Add to the end of the archive. */
1371 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1373 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1374 if (*after_bfd
== (bfd
*) NULL
)
1376 bfd_fatal (*files_to_move
);
1380 printf ("a - %s\n", *files_to_move
);
1383 (*after_bfd
)->next
= temp
;
1393 write_archive (arch
);
1395 output_filename
= NULL
;
1399 ranlib_only (archname
)
1400 const char *archname
;
1405 arch
= open_inarch (archname
, (char *) NULL
);
1408 write_archive (arch
);
1411 /* Update the timestamp of the symbol map of an archive. */
1414 ranlib_touch (archname
)
1415 const char *archname
;
1418 /* I don't think updating works on go32. */
1419 ranlib_only (archname
);
1425 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1428 bfd_set_error (bfd_error_system_call
);
1429 bfd_fatal (archname
);
1432 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1434 bfd_fatal (archname
);
1435 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1437 bfd_nonfatal (archname
);
1438 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1440 list_matching_formats (matching
);
1446 if (! bfd_has_map (arch
))
1447 /* xgettext:c-format */
1448 fatal (_("%s: no archive map to update"), archname
);
1450 bfd_update_armap_timestamp (arch
);
1452 if (! bfd_close (arch
))
1453 bfd_fatal (archname
);
1457 /* Things which are interesting to map over all or some of the files: */
1463 print_arelt_descr (stdout
, abfd
, verbose
);