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 (bfd
* abfd
, const char *filename
);
59 /* Static declarations */
61 static void mri_emul (void);
62 static const char *normalize (const char *, bfd
*);
63 static void remove_output (void);
64 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
65 static void print_contents (bfd
* member
);
66 static void delete_members (bfd
*, char **files_to_delete
);
69 static void do_quick_append
70 (const char *archive_filename
, char **files_to_append
);
73 static void move_members (bfd
*, char **files_to_move
);
74 static void replace_members
75 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
76 static void print_descr (bfd
* abfd
);
77 static void write_archive (bfd
*);
78 static void ranlib_only (const char *archname
);
79 static void ranlib_touch (const char *archname
);
80 static void usage (int);
82 /** Globals and flags */
86 /* This flag distinguishes between ar and ranlib:
87 1 means this is 'ranlib'; 0 means this is 'ar'.
88 -1 means if we should use argv[0] to decide. */
91 /* Nonzero means don't warn about creating the archive file if necessary. */
92 int silent_create
= 0;
94 /* Nonzero means describe each action performed. */
97 /* Nonzero means preserve dates of members when extracting them. */
98 int preserve_dates
= 0;
100 /* Nonzero means don't replace existing members whose dates are more recent
101 than the corresponding files. */
104 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
105 member). -1 means we've been explicitly asked to not write a symbol table;
106 +1 means we've been explicitly asked to write it;
108 Traditionally, the default in BSD has been to not write the table.
109 However, for POSIX.2 compliance the default is now to write a symbol table
110 if any of the members are object files. */
113 /* Nonzero means it's the name of an existing member; position new or moved
114 files with respect to this one. */
115 char *posname
= NULL
;
117 /* Sez how to use `posname': pos_before means position before that member.
118 pos_after means position after that member. pos_end means always at end.
119 pos_default means default appropriately. For the latter two, `posname'
120 should also be zero. */
123 pos_default
, pos_before
, pos_after
, pos_end
124 } postype
= pos_default
;
127 get_pos_bfd (bfd
**, enum pos
, const char *);
129 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
130 extract the COUNTED_NAME_COUNTER instance of that name. */
131 static bfd_boolean counted_name_mode
= 0;
132 static int counted_name_counter
= 0;
134 /* Whether to truncate names of files stored in the archive. */
135 static bfd_boolean ar_truncate
= FALSE
;
137 /* Whether to use a full file name match when searching an archive.
138 This is convenient for archives created by the Microsoft lib
140 static bfd_boolean full_pathname
= FALSE
;
147 interactive
= isatty (fileno (stdin
));
151 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
152 COUNT is the length of the FILES chain; FUNCTION is called on each entry
153 whose name matches one in FILES. */
156 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
163 for (head
= arch
->next
; head
; head
= head
->next
)
171 /* This may appear to be a baroque way of accomplishing what we want.
172 However we have to iterate over the filenames in order to notice where
173 a filename is requested but does not exist in the archive. Ditto
174 mapping over each file each time -- we want to hack multiple
177 for (; count
> 0; files
++, count
--)
179 bfd_boolean found
= FALSE
;
182 for (head
= arch
->next
; head
; head
= head
->next
)
185 if (head
->filename
== NULL
)
187 /* Some archive formats don't get the filenames filled in
188 until the elements are opened. */
190 bfd_stat_arch_elt (head
, &buf
);
192 if ((head
->filename
!= NULL
) &&
193 (!FILENAME_CMP (normalize (*files
, arch
), head
->filename
)))
196 if (counted_name_mode
197 && match_count
!= counted_name_counter
)
199 /* Counting, and didn't match on count; go on to the
209 /* xgettext:c-format */
210 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
214 bfd_boolean operation_alters_arch
= FALSE
;
221 s
= help
? stdout
: stderr
;
225 /* xgettext:c-format */
226 fprintf (s
, _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
228 /* xgettext:c-format */
229 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
230 fprintf (s
, _(" commands:\n"));
231 fprintf (s
, _(" d - delete file(s) from the archive\n"));
232 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
233 fprintf (s
, _(" p - print file(s) found in the archive\n"));
234 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
235 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
236 fprintf (s
, _(" t - display contents of archive\n"));
237 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
238 fprintf (s
, _(" command specific modifiers:\n"));
239 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
240 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
241 fprintf (s
, _(" [N] - use instance [count] of name\n"));
242 fprintf (s
, _(" [f] - truncate inserted file names\n"));
243 fprintf (s
, _(" [P] - use full path names when matching\n"));
244 fprintf (s
, _(" [o] - preserve original dates\n"));
245 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
246 fprintf (s
, _(" generic modifiers:\n"));
247 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
248 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
249 fprintf (s
, _(" [S] - do not build a symbol table\n"));
250 fprintf (s
, _(" [v] - be verbose\n"));
251 fprintf (s
, _(" [V] - display the version number\n"));
257 /* xgettext:c-format */
258 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
259 fprintf (s
, _(" Generate an index to speed access to archives\n"));
260 fprintf (s
, _(" The options are:\n\
261 -h --help Print this help message\n\
262 -V --version Print version information\n"));
265 list_supported_targets (program_name
, stderr
);
268 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
270 xexit (help
? 0 : 1);
273 /* Normalize a file name specified on the command line into a file
274 name which we will use in an archive. */
277 normalize (const char *file
, bfd
*abfd
)
279 const char *filename
;
284 filename
= strrchr (file
, '/');
285 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
287 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
288 char *bslash
= strrchr (file
, '\\');
289 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
291 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
295 if (filename
!= (char *) NULL
)
302 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
307 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
308 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
309 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
316 /* Remove any output file. This is only called via xatexit. */
318 static const char *output_filename
= NULL
;
319 static FILE *output_file
= NULL
;
320 static bfd
*output_bfd
= NULL
;
325 if (output_filename
!= NULL
)
327 if (output_bfd
!= NULL
)
328 bfd_cache_close (output_bfd
);
329 if (output_file
!= NULL
)
330 fclose (output_file
);
331 unlink (output_filename
);
335 /* The option parsing should be in its own function.
336 It will be when I have getopt working. */
338 int main (int, char **);
341 main (int argc
, char **argv
)
347 none
= 0, delete, replace
, print_table
,
348 print_files
, extract
, move
, quick_append
353 char *inarch_filename
;
358 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
359 setlocale (LC_MESSAGES
, "");
361 #if defined (HAVE_SETLOCALE)
362 setlocale (LC_CTYPE
, "");
364 bindtextdomain (PACKAGE
, LOCALEDIR
);
365 textdomain (PACKAGE
);
367 program_name
= argv
[0];
368 xmalloc_set_program_name (program_name
);
374 temp
= strrchr (program_name
, '/');
375 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
377 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
378 char *bslash
= strrchr (program_name
, '\\');
379 if (temp
== NULL
|| (bslash
!= NULL
&& bslash
> temp
))
381 if (temp
== NULL
&& program_name
[0] != '\0' && program_name
[1] == ':')
382 temp
= program_name
+ 1;
389 if (strlen (temp
) >= 6
390 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
396 if (argc
> 1 && argv
[1][0] == '-')
398 if (strcmp (argv
[1], "--help") == 0)
400 else if (strcmp (argv
[1], "--version") == 0)
403 print_version ("ranlib");
405 print_version ("ar");
409 START_PROGRESS (program_name
, 0);
412 set_default_bfd_target ();
416 xatexit (remove_output
);
418 for (i
= 1; i
< argc
; i
++)
419 if (! ar_emul_parse_arg (argv
[i
]))
426 bfd_boolean touch
= FALSE
;
429 || strcmp (argv
[1], "--help") == 0
430 || strcmp (argv
[1], "-h") == 0
431 || strcmp (argv
[1], "-H") == 0)
433 if (strcmp (argv
[1], "-V") == 0
434 || strcmp (argv
[1], "-v") == 0
435 || strncmp (argv
[1], "--v", 3) == 0)
436 print_version ("ranlib");
438 if (strcmp (argv
[1], "-t") == 0)
443 while (arg_index
< argc
)
446 ranlib_only (argv
[arg_index
]);
448 ranlib_touch (argv
[arg_index
]);
454 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
464 arg_ptr
= argv
[arg_index
];
468 /* When the first option starts with '-' we support POSIX-compatible
471 ++arg_ptr
; /* compatibility */
476 while ((c
= *arg_ptr
++) != '\0')
487 if (operation
!= none
)
488 fatal (_("two different operation options specified"));
493 operation_alters_arch
= TRUE
;
497 operation_alters_arch
= TRUE
;
500 operation
= print_files
;
503 operation
= quick_append
;
504 operation_alters_arch
= TRUE
;
508 operation_alters_arch
= TRUE
;
511 operation
= print_table
;
544 postype
= pos_before
;
547 postype
= pos_before
;
553 counted_name_mode
= TRUE
;
559 full_pathname
= TRUE
;
562 /* xgettext:c-format */
563 non_fatal (_("illegal option -- %c"), c
);
568 /* With POSIX-compatible option parsing continue with the next
569 argument if it starts with '-'. */
570 if (do_posix
&& arg_index
+ 1 < argc
&& argv
[arg_index
+ 1][0] == '-')
571 arg_ptr
= argv
[++arg_index
] + 1;
578 print_version ("ar");
581 if (arg_index
>= argc
)
592 /* We can't write an armap when using ar q, so just do ar r
594 if (operation
== quick_append
&& write_armap
)
597 if ((operation
== none
|| operation
== print_table
)
600 ranlib_only (argv
[arg_index
]);
604 if (operation
== none
)
605 fatal (_("no operation specified"));
607 if (newer_only
&& operation
!= replace
)
608 fatal (_("`u' is only meaningful with the `r' option."));
610 if (postype
!= pos_default
)
611 posname
= argv
[arg_index
++];
613 if (counted_name_mode
)
615 if (operation
!= extract
&& operation
!= delete)
616 fatal (_("`N' is only meaningful with the `x' and `d' options."));
617 counted_name_counter
= atoi (argv
[arg_index
++]);
618 if (counted_name_counter
<= 0)
619 fatal (_("Value for `N' must be positive."));
622 inarch_filename
= argv
[arg_index
++];
624 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
625 file_count
= argc
- arg_index
;
628 /* We don't use do_quick_append any more. Too many systems
629 expect ar to always rebuild the symbol table even when q is
632 /* We can't do a quick append if we need to construct an
633 extended name table, because do_quick_append won't be able to
634 rebuild the name table. Unfortunately, at this point we
635 don't actually know the maximum name length permitted by this
636 object file format. So, we guess. FIXME. */
637 if (operation
== quick_append
&& ! ar_truncate
)
641 for (chk
= files
; chk
!= NULL
&& *chk
!= '\0'; chk
++)
643 if (strlen (normalize (*chk
, (bfd
*) NULL
)) > 14)
651 if (operation
== quick_append
)
653 /* Note that quick appending to a non-existent archive creates it,
654 even if there are no files to append. */
655 do_quick_append (inarch_filename
, files
);
660 arch
= open_inarch (inarch_filename
,
661 files
== NULL
? (char *) NULL
: files
[0]);
666 map_over_members (arch
, print_descr
, files
, file_count
);
670 map_over_members (arch
, print_contents
, files
, file_count
);
674 map_over_members (arch
, extract_file
, files
, file_count
);
679 delete_members (arch
, files
);
681 output_filename
= NULL
;
686 move_members (arch
, files
);
688 output_filename
= NULL
;
693 if (files
!= NULL
|| write_armap
> 0)
694 replace_members (arch
, files
, operation
== quick_append
);
696 output_filename
= NULL
;
699 /* Shouldn't happen! */
701 /* xgettext:c-format */
702 fatal (_("internal error -- this option not implemented"));
706 END_PROGRESS (program_name
);
713 open_inarch (const char *archive_filename
, const char *file
)
722 bfd_set_error (bfd_error_no_error
);
726 if (stat (archive_filename
, &sbuf
) != 0)
728 #if !defined(__GO32__) || defined(__DJGPP__)
730 /* FIXME: I don't understand why this fragment was ifndef'ed
731 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
732 stat() works just fine in v2.x, so I think this should be
733 removed. For now, I enable it for DJGPP v2. -- EZ. */
735 /* KLUDGE ALERT! Temporary fix until I figger why
736 stat() is wrong ... think it's buried in GO32's IDT - Jax */
738 bfd_fatal (archive_filename
);
741 if (!operation_alters_arch
)
743 fprintf (stderr
, "%s: ", program_name
);
744 perror (archive_filename
);
749 /* Try to figure out the target to use for the archive from the
750 first object on the list. */
755 obj
= bfd_openr (file
, NULL
);
758 if (bfd_check_format (obj
, bfd_object
))
759 target
= bfd_get_target (obj
);
760 (void) bfd_close (obj
);
764 /* Create an empty archive. */
765 arch
= bfd_openw (archive_filename
, target
);
767 || ! bfd_set_format (arch
, bfd_archive
)
768 || ! bfd_close (arch
))
769 bfd_fatal (archive_filename
);
770 else if (!silent_create
)
771 non_fatal (_("creating %s"), archive_filename
);
773 /* If we die creating a new archive, don't leave it around. */
774 output_filename
= archive_filename
;
777 arch
= bfd_openr (archive_filename
, target
);
781 bfd_fatal (archive_filename
);
784 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
786 bfd_nonfatal (archive_filename
);
787 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
789 list_matching_formats (matching
);
795 last_one
= &(arch
->next
);
796 /* Read all the contents right away, regardless. */
797 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
799 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
802 *last_one
= next_one
;
803 last_one
= &next_one
->next
;
805 *last_one
= (bfd
*) NULL
;
806 if (bfd_get_error () != bfd_error_no_more_archived_files
)
812 print_contents (bfd
*abfd
)
815 char *cbuf
= xmalloc (BUFSIZE
);
818 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
819 /* xgettext:c-format */
820 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
823 /* xgettext:c-format */
824 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd
));
826 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
829 while (ncopied
< size
)
833 int tocopy
= size
- ncopied
;
834 if (tocopy
> BUFSIZE
)
837 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
839 /* xgettext:c-format */
840 fatal (_("%s is not a valid archive"),
841 bfd_get_filename (bfd_my_archive (abfd
)));
842 fwrite (cbuf
, 1, nread
, stdout
);
848 /* Extract a member of the archive into its own file.
850 We defer opening the new file until after we have read a BUFSIZ chunk of the
851 old one, since we know we have just read the archive header for the old
852 one. Since most members are shorter than BUFSIZ, this means we will read
853 the old header, read the old data, write a new inode for the new file, and
854 write the new data, and be done. This 'optimization' is what comes from
855 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
859 extract_file (bfd
*abfd
)
862 char *cbuf
= xmalloc (BUFSIZE
);
868 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
869 /* xgettext:c-format */
870 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
874 /* xgettext:c-format */
875 fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd
));
878 printf ("x - %s\n", bfd_get_filename (abfd
));
880 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
885 /* Seems like an abstraction violation, eh? Well it's OK! */
886 output_filename
= bfd_get_filename (abfd
);
888 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
891 perror (bfd_get_filename (abfd
));
895 output_file
= ostream
;
898 while (ncopied
< size
)
900 tocopy
= size
- ncopied
;
901 if (tocopy
> BUFSIZE
)
904 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
906 /* xgettext:c-format */
907 fatal (_("%s is not a valid archive"),
908 bfd_get_filename (bfd_my_archive (abfd
)));
910 /* See comment above; this saves disk arm motion */
913 /* Seems like an abstraction violation, eh? Well it's OK! */
914 output_filename
= bfd_get_filename (abfd
);
916 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
919 perror (bfd_get_filename (abfd
));
923 output_file
= ostream
;
925 fwrite (cbuf
, 1, nread
, ostream
);
933 output_filename
= NULL
;
935 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
939 /* Set access time to modification time. Only st_mtime is
940 initialized by bfd_stat_arch_elt. */
941 buf
.st_atime
= buf
.st_mtime
;
942 set_times (bfd_get_filename (abfd
), &buf
);
950 /* We don't use this anymore. Too many systems expect ar to rebuild
951 the symbol table even when q is used. */
953 /* Just do it quickly; don't worry about dups, armap, or anything like that */
956 do_quick_append (const char *archive_filename
, char **files_to_append
)
959 char *buf
= xmalloc (BUFSIZE
);
960 long tocopy
, thistime
;
963 bfd_boolean newfile
= FALSE
;
964 bfd_set_error (bfd_error_no_error
);
966 if (stat (archive_filename
, &sbuf
) != 0)
969 #if !defined(__GO32__) || defined(__DJGPP__)
971 /* FIXME: I don't understand why this fragment was ifndef'ed
972 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
973 stat() works just fine in v2.x, so I think this should be
974 removed. For now, I enable it for DJGPP v2.
976 (And yes, I know this is all unused, but somebody, someday,
977 might wish to resurrect this again... -- EZ. */
979 /* KLUDGE ALERT! Temporary fix until I figger why
980 stat() is wrong ... think it's buried in GO32's IDT - Jax */
983 bfd_fatal (archive_filename
);
989 ofile
= fopen (archive_filename
, FOPEN_AUB
);
992 perror (program_name
);
996 temp
= bfd_openr (archive_filename
, NULL
);
999 bfd_fatal (archive_filename
);
1003 if (!bfd_check_format (temp
, bfd_archive
))
1004 /* xgettext:c-format */
1005 fatal (_("%s is not an archive"), archive_filename
);
1009 fwrite (ARMAG
, 1, SARMAG
, ofile
);
1011 /* xgettext:c-format */
1012 non_fatal (_("creating %s"), archive_filename
);
1016 temp
->flags
|= BFD_TRADITIONAL_FORMAT
;
1018 /* assume it's an archive, go straight to the end, sans $200 */
1019 fseek (ofile
, 0, 2);
1021 for (; files_to_append
&& *files_to_append
; ++files_to_append
)
1023 struct ar_hdr
*hdr
= bfd_special_undocumented_glue (temp
, *files_to_append
);
1026 bfd_fatal (*files_to_append
);
1029 BFD_SEND (temp
, _bfd_truncate_arname
, (temp
, *files_to_append
, (char *) hdr
));
1031 ifile
= fopen (*files_to_append
, FOPEN_RB
);
1034 bfd_nonfatal (*files_to_append
);
1037 if (stat (*files_to_append
, &sbuf
) != 0)
1039 bfd_nonfatal (*files_to_append
);
1042 tocopy
= sbuf
.st_size
;
1044 /* XXX should do error-checking! */
1045 fwrite (hdr
, 1, sizeof (struct ar_hdr
), ofile
);
1050 if (thistime
> BUFSIZE
)
1052 fread (buf
, 1, thistime
, ifile
);
1053 fwrite (buf
, 1, thistime
, ofile
);
1057 if ((sbuf
.st_size
% 2) == 1)
1058 putc ('\012', ofile
);
1068 write_archive (bfd
*iarch
)
1071 char *old_name
, *new_name
;
1072 bfd
*contents_head
= iarch
->next
;
1074 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1075 strcpy (old_name
, bfd_get_filename (iarch
));
1076 new_name
= make_tempname (old_name
);
1078 output_filename
= new_name
;
1080 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1083 bfd_fatal (old_name
);
1087 bfd_set_format (obfd
, bfd_archive
);
1089 /* Request writing the archive symbol table unless we've
1090 been explicitly requested not to. */
1091 obfd
->has_armap
= write_armap
>= 0;
1095 /* This should really use bfd_set_file_flags, but that rejects
1097 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1100 if (!bfd_set_archive_head (obfd
, contents_head
))
1101 bfd_fatal (old_name
);
1103 if (!bfd_close (obfd
))
1104 bfd_fatal (old_name
);
1107 output_filename
= NULL
;
1109 /* We don't care if this fails; we might be creating the archive. */
1112 if (smart_rename (new_name
, old_name
, 0) != 0)
1116 /* Return a pointer to the pointer to the entry which should be rplacd'd
1117 into when altering. DEFAULT_POS should be how to interpret pos_default,
1118 and should be a pos value. */
1121 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1123 bfd
**after_bfd
= contents
;
1125 const char *realposname
;
1127 if (postype
== pos_default
)
1129 realpos
= default_pos
;
1130 realposname
= default_posname
;
1135 realposname
= posname
;
1138 if (realpos
== pos_end
)
1141 after_bfd
= &((*after_bfd
)->next
);
1145 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->next
)
1146 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1148 if (realpos
== pos_after
)
1149 after_bfd
= &(*after_bfd
)->next
;
1157 delete_members (bfd
*arch
, char **files_to_delete
)
1159 bfd
**current_ptr_ptr
;
1161 bfd_boolean something_changed
= FALSE
;
1164 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1166 /* In a.out systems, the armap is optional. It's also called
1167 __.SYMDEF. So if the user asked to delete it, we should remember
1168 that fact. This isn't quite right for COFF systems (where
1169 __.SYMDEF might be regular member), but it's very unlikely
1170 to be a problem. FIXME */
1172 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1174 arch
->has_armap
= FALSE
;
1181 current_ptr_ptr
= &(arch
->next
);
1182 while (*current_ptr_ptr
)
1184 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1185 (*current_ptr_ptr
)->filename
) == 0)
1188 if (counted_name_mode
1189 && match_count
!= counted_name_counter
)
1191 /* Counting, and didn't match on count; go on to the
1197 something_changed
= TRUE
;
1201 *current_ptr_ptr
= ((*current_ptr_ptr
)->next
);
1206 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1209 if (verbose
&& !found
)
1211 /* xgettext:c-format */
1212 printf (_("No member named `%s'\n"), *files_to_delete
);
1218 if (something_changed
)
1219 write_archive (arch
);
1221 output_filename
= NULL
;
1225 /* Reposition existing members within an archive */
1228 move_members (bfd
*arch
, char **files_to_move
)
1230 bfd
**after_bfd
; /* New entries go after this one */
1231 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1233 for (; *files_to_move
; ++files_to_move
)
1235 current_ptr_ptr
= &(arch
->next
);
1236 while (*current_ptr_ptr
)
1238 bfd
*current_ptr
= *current_ptr_ptr
;
1239 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1240 current_ptr
->filename
) == 0)
1242 /* Move this file to the end of the list - first cut from
1245 *current_ptr_ptr
= current_ptr
->next
;
1247 /* Now glue to end */
1248 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1250 *after_bfd
= current_ptr
;
1251 current_ptr
->next
= link
;
1254 printf ("m - %s\n", *files_to_move
);
1259 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1261 /* xgettext:c-format */
1262 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1267 write_archive (arch
);
1270 /* Ought to default to replacing in place, but this is existing practice! */
1273 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1275 bfd_boolean changed
= FALSE
;
1276 bfd
**after_bfd
; /* New entries go after this one. */
1280 while (files_to_move
&& *files_to_move
)
1284 current_ptr
= &arch
->next
;
1285 while (*current_ptr
)
1287 current
= *current_ptr
;
1289 /* For compatibility with existing ar programs, we
1290 permit the same file to be added multiple times. */
1291 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1292 normalize (current
->filename
, arch
)) == 0
1293 && current
->arelt_data
!= NULL
)
1297 struct stat fsbuf
, asbuf
;
1299 if (stat (*files_to_move
, &fsbuf
) != 0)
1301 if (errno
!= ENOENT
)
1302 bfd_fatal (*files_to_move
);
1305 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1306 /* xgettext:c-format */
1307 fatal (_("internal stat error on %s"),
1310 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1314 after_bfd
= get_pos_bfd (&arch
->next
, pos_after
,
1316 if (ar_emul_replace (after_bfd
, *files_to_move
,
1319 /* Snip out this entry from the chain. */
1320 *current_ptr
= (*current_ptr
)->next
;
1326 current_ptr
= &(current
->next
);
1330 /* Add to the end of the archive. */
1331 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1333 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
))
1342 write_archive (arch
);
1344 output_filename
= NULL
;
1348 ranlib_only (const char *archname
)
1352 if (get_file_size (archname
) < 1)
1355 arch
= open_inarch (archname
, (char *) NULL
);
1358 write_archive (arch
);
1361 /* Update the timestamp of the symbol map of an archive. */
1364 ranlib_touch (const char *archname
)
1367 /* I don't think updating works on go32. */
1368 ranlib_only (archname
);
1374 if (get_file_size (archname
) < 1)
1376 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1379 bfd_set_error (bfd_error_system_call
);
1380 bfd_fatal (archname
);
1383 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1385 bfd_fatal (archname
);
1386 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1388 bfd_nonfatal (archname
);
1389 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1391 list_matching_formats (matching
);
1397 if (! bfd_has_map (arch
))
1398 /* xgettext:c-format */
1399 fatal (_("%s: no archive map to update"), archname
);
1401 bfd_update_armap_timestamp (arch
);
1403 if (! bfd_close (arch
))
1404 bfd_fatal (archname
);
1408 /* Things which are interesting to map over all or some of the files: */
1411 print_descr (bfd
*abfd
)
1413 print_arelt_descr (stdout
, abfd
, verbose
);