1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 Bugs: should use getopt the way tar does (complete w/optional -) and
25 should have long options too. GNU ar used to check file against filesystem
26 in quick_update and replace operations (would check mtime). Doesn't warn
27 when name truncated. No way to specify pos_end. Error messages should be
32 #include "libiberty.h"
38 #include "filenames.h"
43 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
45 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
48 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
51 bfd_special_undocumented_glue (bfd
* abfd
, const char *filename
);
53 /* Static declarations. */
55 static void mri_emul (void);
56 static const char *normalize (const char *, bfd
*);
57 static void remove_output (void);
58 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
59 static void print_contents (bfd
* member
);
60 static void delete_members (bfd
*, char **files_to_delete
);
62 static void move_members (bfd
*, char **files_to_move
);
63 static void replace_members
64 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
65 static void print_descr (bfd
* abfd
);
66 static void write_archive (bfd
*);
67 static int ranlib_only (const char *archname
);
68 static int ranlib_touch (const char *archname
);
69 static void usage (int);
71 /** Globals and flags. */
75 /* This flag distinguishes between ar and ranlib:
76 1 means this is 'ranlib'; 0 means this is 'ar'.
77 -1 means if we should use argv[0] to decide. */
80 /* Nonzero means don't warn about creating the archive file if necessary. */
81 int silent_create
= 0;
83 /* Nonzero means describe each action performed. */
86 /* Nonzero means preserve dates of members when extracting them. */
87 int preserve_dates
= 0;
89 /* Nonzero means don't replace existing members whose dates are more recent
90 than the corresponding files. */
93 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
94 member). -1 means we've been explicitly asked to not write a symbol table;
95 +1 means we've been explicitly asked to write it;
97 Traditionally, the default in BSD has been to not write the table.
98 However, for POSIX.2 compliance the default is now to write a symbol table
99 if any of the members are object files. */
102 /* Operate in deterministic mode: write zero for timestamps, uids,
103 and gids for archive members and the archive symbol table, and write
104 consistent file modes. */
105 int deterministic
= 0;
107 /* Nonzero means it's the name of an existing member; position new or moved
108 files with respect to this one. */
109 char *posname
= NULL
;
111 /* Sez how to use `posname': pos_before means position before that member.
112 pos_after means position after that member. pos_end means always at end.
113 pos_default means default appropriately. For the latter two, `posname'
114 should also be zero. */
117 pos_default
, pos_before
, pos_after
, pos_end
118 } postype
= pos_default
;
121 get_pos_bfd (bfd
**, enum pos
, const char *);
123 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
124 extract the COUNTED_NAME_COUNTER instance of that name. */
125 static bfd_boolean counted_name_mode
= 0;
126 static int counted_name_counter
= 0;
128 /* Whether to truncate names of files stored in the archive. */
129 static bfd_boolean ar_truncate
= FALSE
;
131 /* Whether to use a full file name match when searching an archive.
132 This is convenient for archives created by the Microsoft lib
134 static bfd_boolean full_pathname
= FALSE
;
136 /* Whether to create a "thin" archive (symbol index only -- no files). */
137 static bfd_boolean make_thin_archive
= FALSE
;
144 interactive
= isatty (fileno (stdin
));
148 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
149 COUNT is the length of the FILES chain; FUNCTION is called on each entry
150 whose name matches one in FILES. */
153 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
160 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
168 /* This may appear to be a baroque way of accomplishing what we want.
169 However we have to iterate over the filenames in order to notice where
170 a filename is requested but does not exist in the archive. Ditto
171 mapping over each file each time -- we want to hack multiple
174 for (; count
> 0; files
++, count
--)
176 bfd_boolean found
= FALSE
;
179 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
181 const char * filename
;
184 filename
= head
->filename
;
185 if (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 else if (bfd_is_thin_archive (arch
))
194 /* Thin archives store full pathnames. Need to normalize. */
195 filename
= normalize (filename
, arch
);
198 if ((filename
!= NULL
) &&
199 (!FILENAME_CMP (normalize (*files
, arch
), filename
)))
202 if (counted_name_mode
203 && match_count
!= counted_name_counter
)
205 /* Counting, and didn't match on count; go on to the
216 /* xgettext:c-format */
217 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
221 bfd_boolean operation_alters_arch
= FALSE
;
228 s
= help
? stdout
: stderr
;
232 /* xgettext:c-format */
233 fprintf (s
, _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
235 /* xgettext:c-format */
236 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
237 fprintf (s
, _(" commands:\n"));
238 fprintf (s
, _(" d - delete file(s) from the archive\n"));
239 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
240 fprintf (s
, _(" p - print file(s) found in the archive\n"));
241 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
242 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
243 fprintf (s
, _(" t - display contents of archive\n"));
244 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
245 fprintf (s
, _(" command specific modifiers:\n"));
246 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
247 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
248 fprintf (s
, _(" [D] - use zero for timestamps and uids/gids\n"));
249 fprintf (s
, _(" [N] - use instance [count] of name\n"));
250 fprintf (s
, _(" [f] - truncate inserted file names\n"));
251 fprintf (s
, _(" [P] - use full path names when matching\n"));
252 fprintf (s
, _(" [o] - preserve original dates\n"));
253 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
254 fprintf (s
, _(" generic modifiers:\n"));
255 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
256 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
257 fprintf (s
, _(" [S] - do not build a symbol table\n"));
258 fprintf (s
, _(" [T] - make a thin archive\n"));
259 fprintf (s
, _(" [v] - be verbose\n"));
260 fprintf (s
, _(" [V] - display the version number\n"));
261 fprintf (s
, _(" @<file> - read options from <file>\n"));
267 /* xgettext:c-format */
268 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
269 fprintf (s
, _(" Generate an index to speed access to archives\n"));
270 fprintf (s
, _(" The options are:\n\
271 @<file> Read options from <file>\n\
272 -t Update the archive's symbol map timestamp\n\
273 -h --help Print this help message\n\
274 -v --version Print version information\n"));
277 list_supported_targets (program_name
, s
);
279 if (REPORT_BUGS_TO
[0] && help
)
280 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
282 xexit (help
? 0 : 1);
285 /* Normalize a file name specified on the command line into a file
286 name which we will use in an archive. */
289 normalize (const char *file
, bfd
*abfd
)
291 const char *filename
;
296 filename
= strrchr (file
, '/');
297 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
299 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
300 char *bslash
= strrchr (file
, '\\');
302 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
304 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
308 if (filename
!= (char *) NULL
)
315 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
320 s
= xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
321 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
322 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
329 /* Remove any output file. This is only called via xatexit. */
331 static const char *output_filename
= NULL
;
332 static FILE *output_file
= NULL
;
333 static bfd
*output_bfd
= NULL
;
338 if (output_filename
!= NULL
)
340 if (output_bfd
!= NULL
)
341 bfd_cache_close (output_bfd
);
342 if (output_file
!= NULL
)
343 fclose (output_file
);
344 unlink_if_ordinary (output_filename
);
348 /* The option parsing should be in its own function.
349 It will be when I have getopt working. */
351 int main (int, char **);
354 main (int argc
, char **argv
)
360 none
= 0, delete, replace
, print_table
,
361 print_files
, extract
, move
, quick_append
366 char *inarch_filename
;
371 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
372 setlocale (LC_MESSAGES
, "");
374 #if defined (HAVE_SETLOCALE)
375 setlocale (LC_CTYPE
, "");
377 bindtextdomain (PACKAGE
, LOCALEDIR
);
378 textdomain (PACKAGE
);
380 program_name
= argv
[0];
381 xmalloc_set_program_name (program_name
);
383 expandargv (&argc
, &argv
);
389 temp
= strrchr (program_name
, '/');
390 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
392 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
393 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
);
434 for (i
= 1; i
< argc
; i
++)
435 if (! ar_emul_parse_arg (argv
[i
]))
443 bfd_boolean touch
= FALSE
;
446 || strcmp (argv
[1], "--help") == 0
447 || strcmp (argv
[1], "-h") == 0
448 || strcmp (argv
[1], "-H") == 0)
450 if (strcmp (argv
[1], "-V") == 0
451 || strcmp (argv
[1], "-v") == 0
452 || CONST_STRNEQ (argv
[1], "--v"))
453 print_version ("ranlib");
455 if (strcmp (argv
[1], "-t") == 0)
460 while (arg_index
< argc
)
463 status
|= ranlib_only (argv
[arg_index
]);
465 status
|= ranlib_touch (argv
[arg_index
]);
471 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
481 arg_ptr
= argv
[arg_index
];
485 /* When the first option starts with '-' we support POSIX-compatible
488 ++arg_ptr
; /* compatibility */
493 while ((c
= *arg_ptr
++) != '\0')
504 if (operation
!= none
)
505 fatal (_("two different operation options specified"));
510 operation_alters_arch
= TRUE
;
514 operation_alters_arch
= TRUE
;
517 operation
= print_files
;
520 operation
= quick_append
;
521 operation_alters_arch
= TRUE
;
525 operation_alters_arch
= TRUE
;
528 operation
= print_table
;
561 postype
= pos_before
;
564 postype
= pos_before
;
570 counted_name_mode
= TRUE
;
576 full_pathname
= TRUE
;
579 make_thin_archive
= TRUE
;
582 deterministic
= TRUE
;
585 /* xgettext:c-format */
586 non_fatal (_("illegal option -- %c"), c
);
591 /* With POSIX-compatible option parsing continue with the next
592 argument if it starts with '-'. */
593 if (do_posix
&& arg_index
+ 1 < argc
&& argv
[arg_index
+ 1][0] == '-')
594 arg_ptr
= argv
[++arg_index
] + 1;
601 print_version ("ar");
604 if (arg_index
>= argc
)
615 /* We don't use do_quick_append any more. Too many systems
616 expect ar to always rebuild the symbol table even when q is
619 /* We can't write an armap when using ar q, so just do ar r
621 if (operation
== quick_append
&& write_armap
)
624 if ((operation
== none
|| operation
== print_table
)
626 xexit (ranlib_only (argv
[arg_index
]));
628 if (operation
== none
)
629 fatal (_("no operation specified"));
631 if (newer_only
&& operation
!= replace
)
632 fatal (_("`u' is only meaningful with the `r' option."));
634 if (newer_only
&& deterministic
)
635 fatal (_("`u' is not meaningful with the `D' option."));
637 if (postype
!= pos_default
)
638 posname
= argv
[arg_index
++];
640 if (counted_name_mode
)
642 if (operation
!= extract
&& operation
!= delete)
643 fatal (_("`N' is only meaningful with the `x' and `d' options."));
644 counted_name_counter
= atoi (argv
[arg_index
++]);
645 if (counted_name_counter
<= 0)
646 fatal (_("Value for `N' must be positive."));
649 inarch_filename
= argv
[arg_index
++];
651 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
652 file_count
= argc
- arg_index
;
654 arch
= open_inarch (inarch_filename
,
655 files
== NULL
? (char *) NULL
: files
[0]);
657 if (operation
== extract
&& bfd_is_thin_archive (arch
))
658 fatal (_("`x' cannot be used on thin archives."));
663 map_over_members (arch
, print_descr
, files
, file_count
);
667 map_over_members (arch
, print_contents
, files
, file_count
);
671 map_over_members (arch
, extract_file
, files
, file_count
);
676 delete_members (arch
, files
);
678 output_filename
= NULL
;
683 move_members (arch
, files
);
685 output_filename
= NULL
;
690 if (files
!= NULL
|| write_armap
> 0)
691 replace_members (arch
, files
, operation
== quick_append
);
693 output_filename
= NULL
;
696 /* Shouldn't happen! */
698 /* xgettext:c-format */
699 fatal (_("internal error -- this option not implemented"));
703 END_PROGRESS (program_name
);
710 open_inarch (const char *archive_filename
, const char *file
)
719 bfd_set_error (bfd_error_no_error
);
723 if (stat (archive_filename
, &sbuf
) != 0)
725 #if !defined(__GO32__) || defined(__DJGPP__)
727 /* FIXME: I don't understand why this fragment was ifndef'ed
728 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
729 stat() works just fine in v2.x, so I think this should be
730 removed. For now, I enable it for DJGPP v2. -- EZ. */
732 /* KLUDGE ALERT! Temporary fix until I figger why
733 stat() is wrong ... think it's buried in GO32's IDT - Jax */
735 bfd_fatal (archive_filename
);
738 if (!operation_alters_arch
)
740 fprintf (stderr
, "%s: ", program_name
);
741 perror (archive_filename
);
746 /* Try to figure out the target to use for the archive from the
747 first object on the list. */
752 obj
= bfd_openr (file
, NULL
);
755 if (bfd_check_format (obj
, bfd_object
))
756 target
= bfd_get_target (obj
);
757 (void) bfd_close (obj
);
761 /* Create an empty archive. */
762 arch
= bfd_openw (archive_filename
, target
);
764 || ! bfd_set_format (arch
, bfd_archive
)
765 || ! bfd_close (arch
))
766 bfd_fatal (archive_filename
);
767 else if (!silent_create
)
768 non_fatal (_("creating %s"), archive_filename
);
770 /* If we die creating a new archive, don't leave it around. */
771 output_filename
= archive_filename
;
774 arch
= bfd_openr (archive_filename
, target
);
778 bfd_fatal (archive_filename
);
781 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
783 bfd_nonfatal (archive_filename
);
784 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
786 list_matching_formats (matching
);
792 last_one
= &(arch
->archive_next
);
793 /* Read all the contents right away, regardless. */
794 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
796 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
799 *last_one
= next_one
;
800 last_one
= &next_one
->archive_next
;
802 *last_one
= (bfd
*) NULL
;
803 if (bfd_get_error () != bfd_error_no_more_archived_files
)
809 print_contents (bfd
*abfd
)
812 char *cbuf
= xmalloc (BUFSIZE
);
815 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
816 /* xgettext:c-format */
817 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
820 /* xgettext:c-format */
821 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd
));
823 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
826 while (ncopied
< size
)
830 size_t tocopy
= size
- ncopied
;
831 if (tocopy
> BUFSIZE
)
834 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
836 /* xgettext:c-format */
837 fatal (_("%s is not a valid archive"),
838 bfd_get_filename (bfd_my_archive (abfd
)));
840 /* fwrite in mingw32 may return int instead of size_t. Cast the
841 return value to size_t to avoid comparison between signed and
843 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
844 fatal ("stdout: %s", strerror (errno
));
850 /* Extract a member of the archive into its own file.
852 We defer opening the new file until after we have read a BUFSIZ chunk of the
853 old one, since we know we have just read the archive header for the old
854 one. Since most members are shorter than BUFSIZ, this means we will read
855 the old header, read the old data, write a new inode for the new file, and
856 write the new data, and be done. This 'optimization' is what comes from
857 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
861 extract_file (bfd
*abfd
)
864 char *cbuf
= xmalloc (BUFSIZE
);
865 size_t nread
, tocopy
;
870 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
871 /* xgettext:c-format */
872 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
876 printf ("x - %s\n", bfd_get_filename (abfd
));
878 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
883 /* Seems like an abstraction violation, eh? Well it's OK! */
884 output_filename
= bfd_get_filename (abfd
);
886 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
889 perror (bfd_get_filename (abfd
));
893 output_file
= ostream
;
896 while (ncopied
< size
)
898 tocopy
= size
- ncopied
;
899 if (tocopy
> BUFSIZE
)
902 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
904 /* xgettext:c-format */
905 fatal (_("%s is not a valid archive"),
906 bfd_get_filename (bfd_my_archive (abfd
)));
908 /* See comment above; this saves disk arm motion */
911 /* Seems like an abstraction violation, eh? Well it's OK! */
912 output_filename
= bfd_get_filename (abfd
);
914 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
917 perror (bfd_get_filename (abfd
));
921 output_file
= ostream
;
924 /* fwrite in mingw32 may return int instead of size_t. Cast
925 the return value to size_t to avoid comparison between
926 signed and unsigned values. */
927 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
928 fatal ("%s: %s", output_filename
, strerror (errno
));
936 output_filename
= NULL
;
938 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
942 /* Set access time to modification time. Only st_mtime is
943 initialized by bfd_stat_arch_elt. */
944 buf
.st_atime
= buf
.st_mtime
;
945 set_times (bfd_get_filename (abfd
), &buf
);
952 write_archive (bfd
*iarch
)
955 char *old_name
, *new_name
;
956 bfd
*contents_head
= iarch
->archive_next
;
958 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
959 strcpy (old_name
, bfd_get_filename (iarch
));
960 new_name
= make_tempname (old_name
);
962 if (new_name
== NULL
)
963 bfd_fatal ("could not create temporary file whilst writing archive");
965 output_filename
= new_name
;
967 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
970 bfd_fatal (old_name
);
974 bfd_set_format (obfd
, bfd_archive
);
976 /* Request writing the archive symbol table unless we've
977 been explicitly requested not to. */
978 obfd
->has_armap
= write_armap
>= 0;
982 /* This should really use bfd_set_file_flags, but that rejects
984 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
988 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
990 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
991 bfd_is_thin_archive (obfd
) = 1;
993 if (!bfd_set_archive_head (obfd
, contents_head
))
994 bfd_fatal (old_name
);
996 if (!bfd_close (obfd
))
997 bfd_fatal (old_name
);
1000 output_filename
= NULL
;
1002 /* We don't care if this fails; we might be creating the archive. */
1005 if (smart_rename (new_name
, old_name
, 0) != 0)
1009 /* Return a pointer to the pointer to the entry which should be rplacd'd
1010 into when altering. DEFAULT_POS should be how to interpret pos_default,
1011 and should be a pos value. */
1014 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1016 bfd
**after_bfd
= contents
;
1018 const char *realposname
;
1020 if (postype
== pos_default
)
1022 realpos
= default_pos
;
1023 realposname
= default_posname
;
1028 realposname
= posname
;
1031 if (realpos
== pos_end
)
1034 after_bfd
= &((*after_bfd
)->archive_next
);
1038 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1039 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1041 if (realpos
== pos_after
)
1042 after_bfd
= &(*after_bfd
)->archive_next
;
1050 delete_members (bfd
*arch
, char **files_to_delete
)
1052 bfd
**current_ptr_ptr
;
1054 bfd_boolean something_changed
= FALSE
;
1057 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1059 /* In a.out systems, the armap is optional. It's also called
1060 __.SYMDEF. So if the user asked to delete it, we should remember
1061 that fact. This isn't quite right for COFF systems (where
1062 __.SYMDEF might be regular member), but it's very unlikely
1063 to be a problem. FIXME */
1065 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1067 arch
->has_armap
= FALSE
;
1074 current_ptr_ptr
= &(arch
->archive_next
);
1075 while (*current_ptr_ptr
)
1077 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1078 (*current_ptr_ptr
)->filename
) == 0)
1081 if (counted_name_mode
1082 && match_count
!= counted_name_counter
)
1084 /* Counting, and didn't match on count; go on to the
1090 something_changed
= TRUE
;
1094 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1099 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1102 if (verbose
&& !found
)
1104 /* xgettext:c-format */
1105 printf (_("No member named `%s'\n"), *files_to_delete
);
1111 if (something_changed
)
1112 write_archive (arch
);
1114 output_filename
= NULL
;
1118 /* Reposition existing members within an archive */
1121 move_members (bfd
*arch
, char **files_to_move
)
1123 bfd
**after_bfd
; /* New entries go after this one */
1124 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1126 for (; *files_to_move
; ++files_to_move
)
1128 current_ptr_ptr
= &(arch
->archive_next
);
1129 while (*current_ptr_ptr
)
1131 bfd
*current_ptr
= *current_ptr_ptr
;
1132 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1133 current_ptr
->filename
) == 0)
1135 /* Move this file to the end of the list - first cut from
1138 *current_ptr_ptr
= current_ptr
->archive_next
;
1140 /* Now glue to end */
1141 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1143 *after_bfd
= current_ptr
;
1144 current_ptr
->archive_next
= link
;
1147 printf ("m - %s\n", *files_to_move
);
1152 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1154 /* xgettext:c-format */
1155 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1160 write_archive (arch
);
1163 /* Ought to default to replacing in place, but this is existing practice! */
1166 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1168 bfd_boolean changed
= FALSE
;
1169 bfd
**after_bfd
; /* New entries go after this one. */
1173 while (files_to_move
&& *files_to_move
)
1177 current_ptr
= &arch
->archive_next
;
1178 while (*current_ptr
)
1180 current
= *current_ptr
;
1182 /* For compatibility with existing ar programs, we
1183 permit the same file to be added multiple times. */
1184 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1185 normalize (current
->filename
, arch
)) == 0
1186 && current
->arelt_data
!= NULL
)
1190 struct stat fsbuf
, asbuf
;
1192 if (stat (*files_to_move
, &fsbuf
) != 0)
1194 if (errno
!= ENOENT
)
1195 bfd_fatal (*files_to_move
);
1198 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1199 /* xgettext:c-format */
1200 fatal (_("internal stat error on %s"),
1203 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1207 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1209 if (ar_emul_replace (after_bfd
, *files_to_move
,
1212 /* Snip out this entry from the chain. */
1213 *current_ptr
= (*current_ptr
)->archive_next
;
1219 current_ptr
= &(current
->archive_next
);
1223 /* Add to the end of the archive. */
1224 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1226 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
,
1236 write_archive (arch
);
1238 output_filename
= NULL
;
1242 ranlib_only (const char *archname
)
1246 if (get_file_size (archname
) < 1)
1249 arch
= open_inarch (archname
, (char *) NULL
);
1252 write_archive (arch
);
1256 /* Update the timestamp of the symbol map of an archive. */
1259 ranlib_touch (const char *archname
)
1262 /* I don't think updating works on go32. */
1263 ranlib_only (archname
);
1269 if (get_file_size (archname
) < 1)
1271 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1274 bfd_set_error (bfd_error_system_call
);
1275 bfd_fatal (archname
);
1278 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1280 bfd_fatal (archname
);
1281 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1283 bfd_nonfatal (archname
);
1284 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1286 list_matching_formats (matching
);
1292 if (! bfd_has_map (arch
))
1293 /* xgettext:c-format */
1294 fatal (_("%s: no archive map to update"), archname
);
1296 bfd_update_armap_timestamp (arch
);
1298 if (! bfd_close (arch
))
1299 bfd_fatal (archname
);
1304 /* Things which are interesting to map over all or some of the files: */
1307 print_descr (bfd
*abfd
)
1309 print_arelt_descr (stdout
, abfd
, verbose
);