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 /* Nonzero means it's the name of an existing member; position new or moved
103 files with respect to this one. */
104 char *posname
= NULL
;
106 /* Sez how to use `posname': pos_before means position before that member.
107 pos_after means position after that member. pos_end means always at end.
108 pos_default means default appropriately. For the latter two, `posname'
109 should also be zero. */
112 pos_default
, pos_before
, pos_after
, pos_end
113 } postype
= pos_default
;
116 get_pos_bfd (bfd
**, enum pos
, const char *);
118 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
119 extract the COUNTED_NAME_COUNTER instance of that name. */
120 static bfd_boolean counted_name_mode
= 0;
121 static int counted_name_counter
= 0;
123 /* Whether to truncate names of files stored in the archive. */
124 static bfd_boolean ar_truncate
= FALSE
;
126 /* Whether to use a full file name match when searching an archive.
127 This is convenient for archives created by the Microsoft lib
129 static bfd_boolean full_pathname
= FALSE
;
131 /* Whether to create a "thin" archive (symbol index only -- no files). */
132 static bfd_boolean make_thin_archive
= FALSE
;
139 interactive
= isatty (fileno (stdin
));
143 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
144 COUNT is the length of the FILES chain; FUNCTION is called on each entry
145 whose name matches one in FILES. */
148 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
155 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
163 /* This may appear to be a baroque way of accomplishing what we want.
164 However we have to iterate over the filenames in order to notice where
165 a filename is requested but does not exist in the archive. Ditto
166 mapping over each file each time -- we want to hack multiple
169 for (; count
> 0; files
++, count
--)
171 bfd_boolean found
= FALSE
;
174 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
176 const char * filename
;
179 filename
= head
->filename
;
180 if (filename
== NULL
)
182 /* Some archive formats don't get the filenames filled in
183 until the elements are opened. */
185 bfd_stat_arch_elt (head
, &buf
);
187 else if (bfd_is_thin_archive (arch
))
189 /* Thin archives store full pathnames. Need to normalize. */
190 filename
= normalize (filename
, arch
);
193 if ((filename
!= NULL
) &&
194 (!FILENAME_CMP (normalize (*files
, arch
), filename
)))
197 if (counted_name_mode
198 && match_count
!= counted_name_counter
)
200 /* Counting, and didn't match on count; go on to the
211 /* xgettext:c-format */
212 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
216 bfd_boolean operation_alters_arch
= FALSE
;
223 s
= help
? stdout
: stderr
;
227 /* xgettext:c-format */
228 fprintf (s
, _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
230 /* xgettext:c-format */
231 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
232 fprintf (s
, _(" commands:\n"));
233 fprintf (s
, _(" d - delete file(s) from the archive\n"));
234 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
235 fprintf (s
, _(" p - print file(s) found in the archive\n"));
236 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
237 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
238 fprintf (s
, _(" t - display contents of archive\n"));
239 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
240 fprintf (s
, _(" command specific modifiers:\n"));
241 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
242 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
243 fprintf (s
, _(" [N] - use instance [count] of name\n"));
244 fprintf (s
, _(" [f] - truncate inserted file names\n"));
245 fprintf (s
, _(" [P] - use full path names when matching\n"));
246 fprintf (s
, _(" [o] - preserve original dates\n"));
247 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
248 fprintf (s
, _(" generic modifiers:\n"));
249 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
250 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
251 fprintf (s
, _(" [S] - do not build a symbol table\n"));
252 fprintf (s
, _(" [T] - make a thin archive\n"));
253 fprintf (s
, _(" [v] - be verbose\n"));
254 fprintf (s
, _(" [V] - display the version number\n"));
255 fprintf (s
, _(" @<file> - read options from <file>\n"));
261 /* xgettext:c-format */
262 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
263 fprintf (s
, _(" Generate an index to speed access to archives\n"));
264 fprintf (s
, _(" The options are:\n\
265 @<file> Read options from <file>\n\
266 -t Update the archive's symbol map timestamp\n\
267 -h --help Print this help message\n\
268 -v --version Print version information\n"));
271 list_supported_targets (program_name
, s
);
273 if (REPORT_BUGS_TO
[0] && help
)
274 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
276 xexit (help
? 0 : 1);
279 /* Normalize a file name specified on the command line into a file
280 name which we will use in an archive. */
283 normalize (const char *file
, bfd
*abfd
)
285 const char *filename
;
290 filename
= strrchr (file
, '/');
291 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
293 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
294 char *bslash
= strrchr (file
, '\\');
296 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
298 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
302 if (filename
!= (char *) NULL
)
309 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
314 s
= xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
315 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
316 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
323 /* Remove any output file. This is only called via xatexit. */
325 static const char *output_filename
= NULL
;
326 static FILE *output_file
= NULL
;
327 static bfd
*output_bfd
= NULL
;
332 if (output_filename
!= NULL
)
334 if (output_bfd
!= NULL
)
335 bfd_cache_close (output_bfd
);
336 if (output_file
!= NULL
)
337 fclose (output_file
);
338 unlink_if_ordinary (output_filename
);
342 /* The option parsing should be in its own function.
343 It will be when I have getopt working. */
345 int main (int, char **);
348 main (int argc
, char **argv
)
354 none
= 0, delete, replace
, print_table
,
355 print_files
, extract
, move
, quick_append
360 char *inarch_filename
;
365 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
366 setlocale (LC_MESSAGES
, "");
368 #if defined (HAVE_SETLOCALE)
369 setlocale (LC_CTYPE
, "");
371 bindtextdomain (PACKAGE
, LOCALEDIR
);
372 textdomain (PACKAGE
);
374 program_name
= argv
[0];
375 xmalloc_set_program_name (program_name
);
377 expandargv (&argc
, &argv
);
383 temp
= strrchr (program_name
, '/');
384 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
386 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
387 char *bslash
= strrchr (program_name
, '\\');
389 if (temp
== NULL
|| (bslash
!= NULL
&& bslash
> temp
))
391 if (temp
== NULL
&& program_name
[0] != '\0' && program_name
[1] == ':')
392 temp
= program_name
+ 1;
399 if (strlen (temp
) >= 6
400 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
406 if (argc
> 1 && argv
[1][0] == '-')
408 if (strcmp (argv
[1], "--help") == 0)
410 else if (strcmp (argv
[1], "--version") == 0)
413 print_version ("ranlib");
415 print_version ("ar");
419 START_PROGRESS (program_name
, 0);
422 set_default_bfd_target ();
426 xatexit (remove_output
);
428 for (i
= 1; i
< argc
; i
++)
429 if (! ar_emul_parse_arg (argv
[i
]))
437 bfd_boolean touch
= FALSE
;
440 || strcmp (argv
[1], "--help") == 0
441 || strcmp (argv
[1], "-h") == 0
442 || strcmp (argv
[1], "-H") == 0)
444 if (strcmp (argv
[1], "-V") == 0
445 || strcmp (argv
[1], "-v") == 0
446 || CONST_STRNEQ (argv
[1], "--v"))
447 print_version ("ranlib");
449 if (strcmp (argv
[1], "-t") == 0)
454 while (arg_index
< argc
)
457 status
|= ranlib_only (argv
[arg_index
]);
459 status
|= ranlib_touch (argv
[arg_index
]);
465 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
475 arg_ptr
= argv
[arg_index
];
479 /* When the first option starts with '-' we support POSIX-compatible
482 ++arg_ptr
; /* compatibility */
487 while ((c
= *arg_ptr
++) != '\0')
498 if (operation
!= none
)
499 fatal (_("two different operation options specified"));
504 operation_alters_arch
= TRUE
;
508 operation_alters_arch
= TRUE
;
511 operation
= print_files
;
514 operation
= quick_append
;
515 operation_alters_arch
= TRUE
;
519 operation_alters_arch
= TRUE
;
522 operation
= print_table
;
555 postype
= pos_before
;
558 postype
= pos_before
;
564 counted_name_mode
= TRUE
;
570 full_pathname
= TRUE
;
573 make_thin_archive
= TRUE
;
576 /* xgettext:c-format */
577 non_fatal (_("illegal option -- %c"), c
);
582 /* With POSIX-compatible option parsing continue with the next
583 argument if it starts with '-'. */
584 if (do_posix
&& arg_index
+ 1 < argc
&& argv
[arg_index
+ 1][0] == '-')
585 arg_ptr
= argv
[++arg_index
] + 1;
592 print_version ("ar");
595 if (arg_index
>= argc
)
606 /* We don't use do_quick_append any more. Too many systems
607 expect ar to always rebuild the symbol table even when q is
610 /* We can't write an armap when using ar q, so just do ar r
612 if (operation
== quick_append
&& write_armap
)
615 if ((operation
== none
|| operation
== print_table
)
617 xexit (ranlib_only (argv
[arg_index
]));
619 if (operation
== none
)
620 fatal (_("no operation specified"));
622 if (newer_only
&& operation
!= replace
)
623 fatal (_("`u' is only meaningful with the `r' option."));
625 if (postype
!= pos_default
)
626 posname
= argv
[arg_index
++];
628 if (counted_name_mode
)
630 if (operation
!= extract
&& operation
!= delete)
631 fatal (_("`N' is only meaningful with the `x' and `d' options."));
632 counted_name_counter
= atoi (argv
[arg_index
++]);
633 if (counted_name_counter
<= 0)
634 fatal (_("Value for `N' must be positive."));
637 inarch_filename
= argv
[arg_index
++];
639 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
640 file_count
= argc
- arg_index
;
642 arch
= open_inarch (inarch_filename
,
643 files
== NULL
? (char *) NULL
: files
[0]);
645 if (operation
== extract
&& bfd_is_thin_archive (arch
))
646 fatal (_("`x' cannot be used on thin archives."));
651 map_over_members (arch
, print_descr
, files
, file_count
);
655 map_over_members (arch
, print_contents
, files
, file_count
);
659 map_over_members (arch
, extract_file
, files
, file_count
);
664 delete_members (arch
, files
);
666 output_filename
= NULL
;
671 move_members (arch
, files
);
673 output_filename
= NULL
;
678 if (files
!= NULL
|| write_armap
> 0)
679 replace_members (arch
, files
, operation
== quick_append
);
681 output_filename
= NULL
;
684 /* Shouldn't happen! */
686 /* xgettext:c-format */
687 fatal (_("internal error -- this option not implemented"));
691 END_PROGRESS (program_name
);
698 open_inarch (const char *archive_filename
, const char *file
)
707 bfd_set_error (bfd_error_no_error
);
711 if (stat (archive_filename
, &sbuf
) != 0)
713 #if !defined(__GO32__) || defined(__DJGPP__)
715 /* FIXME: I don't understand why this fragment was ifndef'ed
716 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
717 stat() works just fine in v2.x, so I think this should be
718 removed. For now, I enable it for DJGPP v2. -- EZ. */
720 /* KLUDGE ALERT! Temporary fix until I figger why
721 stat() is wrong ... think it's buried in GO32's IDT - Jax */
723 bfd_fatal (archive_filename
);
726 if (!operation_alters_arch
)
728 fprintf (stderr
, "%s: ", program_name
);
729 perror (archive_filename
);
734 /* Try to figure out the target to use for the archive from the
735 first object on the list. */
740 obj
= bfd_openr (file
, NULL
);
743 if (bfd_check_format (obj
, bfd_object
))
744 target
= bfd_get_target (obj
);
745 (void) bfd_close (obj
);
749 /* Create an empty archive. */
750 arch
= bfd_openw (archive_filename
, target
);
752 || ! bfd_set_format (arch
, bfd_archive
)
753 || ! bfd_close (arch
))
754 bfd_fatal (archive_filename
);
755 else if (!silent_create
)
756 non_fatal (_("creating %s"), archive_filename
);
758 /* If we die creating a new archive, don't leave it around. */
759 output_filename
= archive_filename
;
762 arch
= bfd_openr (archive_filename
, target
);
766 bfd_fatal (archive_filename
);
769 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
771 bfd_nonfatal (archive_filename
);
772 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
774 list_matching_formats (matching
);
780 last_one
= &(arch
->archive_next
);
781 /* Read all the contents right away, regardless. */
782 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
784 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
787 *last_one
= next_one
;
788 last_one
= &next_one
->archive_next
;
790 *last_one
= (bfd
*) NULL
;
791 if (bfd_get_error () != bfd_error_no_more_archived_files
)
797 print_contents (bfd
*abfd
)
800 char *cbuf
= xmalloc (BUFSIZE
);
803 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
804 /* xgettext:c-format */
805 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
808 /* xgettext:c-format */
809 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd
));
811 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
814 while (ncopied
< size
)
818 size_t tocopy
= size
- ncopied
;
819 if (tocopy
> BUFSIZE
)
822 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
824 /* xgettext:c-format */
825 fatal (_("%s is not a valid archive"),
826 bfd_get_filename (bfd_my_archive (abfd
)));
828 /* fwrite in mingw32 may return int instead of size_t. Cast the
829 return value to size_t to avoid comparison between signed and
831 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
832 fatal ("stdout: %s", strerror (errno
));
838 /* Extract a member of the archive into its own file.
840 We defer opening the new file until after we have read a BUFSIZ chunk of the
841 old one, since we know we have just read the archive header for the old
842 one. Since most members are shorter than BUFSIZ, this means we will read
843 the old header, read the old data, write a new inode for the new file, and
844 write the new data, and be done. This 'optimization' is what comes from
845 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
849 extract_file (bfd
*abfd
)
852 char *cbuf
= xmalloc (BUFSIZE
);
853 size_t nread
, tocopy
;
858 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
859 /* xgettext:c-format */
860 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
864 printf ("x - %s\n", bfd_get_filename (abfd
));
866 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
871 /* Seems like an abstraction violation, eh? Well it's OK! */
872 output_filename
= bfd_get_filename (abfd
);
874 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
877 perror (bfd_get_filename (abfd
));
881 output_file
= ostream
;
884 while (ncopied
< size
)
886 tocopy
= size
- ncopied
;
887 if (tocopy
> BUFSIZE
)
890 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
892 /* xgettext:c-format */
893 fatal (_("%s is not a valid archive"),
894 bfd_get_filename (bfd_my_archive (abfd
)));
896 /* See comment above; this saves disk arm motion */
899 /* Seems like an abstraction violation, eh? Well it's OK! */
900 output_filename
= bfd_get_filename (abfd
);
902 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
905 perror (bfd_get_filename (abfd
));
909 output_file
= ostream
;
912 /* fwrite in mingw32 may return int instead of size_t. Cast
913 the return value to size_t to avoid comparison between
914 signed and unsigned values. */
915 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
916 fatal ("%s: %s", output_filename
, strerror (errno
));
924 output_filename
= NULL
;
926 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
930 /* Set access time to modification time. Only st_mtime is
931 initialized by bfd_stat_arch_elt. */
932 buf
.st_atime
= buf
.st_mtime
;
933 set_times (bfd_get_filename (abfd
), &buf
);
940 write_archive (bfd
*iarch
)
943 char *old_name
, *new_name
;
944 bfd
*contents_head
= iarch
->archive_next
;
946 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
947 strcpy (old_name
, bfd_get_filename (iarch
));
948 new_name
= make_tempname (old_name
);
950 if (new_name
== NULL
)
951 bfd_fatal ("could not create temporary file whilst writing archive");
953 output_filename
= new_name
;
955 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
958 bfd_fatal (old_name
);
962 bfd_set_format (obfd
, bfd_archive
);
964 /* Request writing the archive symbol table unless we've
965 been explicitly requested not to. */
966 obfd
->has_armap
= write_armap
>= 0;
970 /* This should really use bfd_set_file_flags, but that rejects
972 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
975 if (make_thin_archive
|| bfd_is_thin_archive (iarch
))
976 bfd_is_thin_archive (obfd
) = 1;
978 if (!bfd_set_archive_head (obfd
, contents_head
))
979 bfd_fatal (old_name
);
981 if (!bfd_close (obfd
))
982 bfd_fatal (old_name
);
985 output_filename
= NULL
;
987 /* We don't care if this fails; we might be creating the archive. */
990 if (smart_rename (new_name
, old_name
, 0) != 0)
994 /* Return a pointer to the pointer to the entry which should be rplacd'd
995 into when altering. DEFAULT_POS should be how to interpret pos_default,
996 and should be a pos value. */
999 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
1001 bfd
**after_bfd
= contents
;
1003 const char *realposname
;
1005 if (postype
== pos_default
)
1007 realpos
= default_pos
;
1008 realposname
= default_posname
;
1013 realposname
= posname
;
1016 if (realpos
== pos_end
)
1019 after_bfd
= &((*after_bfd
)->archive_next
);
1023 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->archive_next
)
1024 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1026 if (realpos
== pos_after
)
1027 after_bfd
= &(*after_bfd
)->archive_next
;
1035 delete_members (bfd
*arch
, char **files_to_delete
)
1037 bfd
**current_ptr_ptr
;
1039 bfd_boolean something_changed
= FALSE
;
1042 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1044 /* In a.out systems, the armap is optional. It's also called
1045 __.SYMDEF. So if the user asked to delete it, we should remember
1046 that fact. This isn't quite right for COFF systems (where
1047 __.SYMDEF might be regular member), but it's very unlikely
1048 to be a problem. FIXME */
1050 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1052 arch
->has_armap
= FALSE
;
1059 current_ptr_ptr
= &(arch
->archive_next
);
1060 while (*current_ptr_ptr
)
1062 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1063 (*current_ptr_ptr
)->filename
) == 0)
1066 if (counted_name_mode
1067 && match_count
!= counted_name_counter
)
1069 /* Counting, and didn't match on count; go on to the
1075 something_changed
= TRUE
;
1079 *current_ptr_ptr
= ((*current_ptr_ptr
)->archive_next
);
1084 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1087 if (verbose
&& !found
)
1089 /* xgettext:c-format */
1090 printf (_("No member named `%s'\n"), *files_to_delete
);
1096 if (something_changed
)
1097 write_archive (arch
);
1099 output_filename
= NULL
;
1103 /* Reposition existing members within an archive */
1106 move_members (bfd
*arch
, char **files_to_move
)
1108 bfd
**after_bfd
; /* New entries go after this one */
1109 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1111 for (; *files_to_move
; ++files_to_move
)
1113 current_ptr_ptr
= &(arch
->archive_next
);
1114 while (*current_ptr_ptr
)
1116 bfd
*current_ptr
= *current_ptr_ptr
;
1117 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1118 current_ptr
->filename
) == 0)
1120 /* Move this file to the end of the list - first cut from
1123 *current_ptr_ptr
= current_ptr
->archive_next
;
1125 /* Now glue to end */
1126 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1128 *after_bfd
= current_ptr
;
1129 current_ptr
->archive_next
= link
;
1132 printf ("m - %s\n", *files_to_move
);
1137 current_ptr_ptr
= &((*current_ptr_ptr
)->archive_next
);
1139 /* xgettext:c-format */
1140 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1145 write_archive (arch
);
1148 /* Ought to default to replacing in place, but this is existing practice! */
1151 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1153 bfd_boolean changed
= FALSE
;
1154 bfd
**after_bfd
; /* New entries go after this one. */
1158 while (files_to_move
&& *files_to_move
)
1162 current_ptr
= &arch
->archive_next
;
1163 while (*current_ptr
)
1165 current
= *current_ptr
;
1167 /* For compatibility with existing ar programs, we
1168 permit the same file to be added multiple times. */
1169 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1170 normalize (current
->filename
, arch
)) == 0
1171 && current
->arelt_data
!= NULL
)
1175 struct stat fsbuf
, asbuf
;
1177 if (stat (*files_to_move
, &fsbuf
) != 0)
1179 if (errno
!= ENOENT
)
1180 bfd_fatal (*files_to_move
);
1183 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1184 /* xgettext:c-format */
1185 fatal (_("internal stat error on %s"),
1188 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1192 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_after
,
1194 if (ar_emul_replace (after_bfd
, *files_to_move
,
1197 /* Snip out this entry from the chain. */
1198 *current_ptr
= (*current_ptr
)->archive_next
;
1204 current_ptr
= &(current
->archive_next
);
1208 /* Add to the end of the archive. */
1209 after_bfd
= get_pos_bfd (&arch
->archive_next
, pos_end
, NULL
);
1211 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
,
1221 write_archive (arch
);
1223 output_filename
= NULL
;
1227 ranlib_only (const char *archname
)
1231 if (get_file_size (archname
) < 1)
1234 arch
= open_inarch (archname
, (char *) NULL
);
1237 write_archive (arch
);
1241 /* Update the timestamp of the symbol map of an archive. */
1244 ranlib_touch (const char *archname
)
1247 /* I don't think updating works on go32. */
1248 ranlib_only (archname
);
1254 if (get_file_size (archname
) < 1)
1256 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1259 bfd_set_error (bfd_error_system_call
);
1260 bfd_fatal (archname
);
1263 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1265 bfd_fatal (archname
);
1266 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1268 bfd_nonfatal (archname
);
1269 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1271 list_matching_formats (matching
);
1277 if (! bfd_has_map (arch
))
1278 /* xgettext:c-format */
1279 fatal (_("%s: no archive map to update"), archname
);
1281 bfd_update_armap_timestamp (arch
);
1283 if (! bfd_close (arch
))
1284 bfd_fatal (archname
);
1289 /* Things which are interesting to map over all or some of the files: */
1292 print_descr (bfd
*abfd
)
1294 print_arelt_descr (stdout
, abfd
, verbose
);