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
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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
52 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
55 bfd_special_undocumented_glue (bfd
* abfd
, const char *filename
);
57 /* Static declarations */
59 static void mri_emul (void);
60 static const char *normalize (const char *, bfd
*);
61 static void remove_output (void);
62 static void map_over_members (bfd
*, void (*)(bfd
*), char **, int);
63 static void print_contents (bfd
* member
);
64 static void delete_members (bfd
*, char **files_to_delete
);
66 static void move_members (bfd
*, char **files_to_move
);
67 static void replace_members
68 (bfd
*, char **files_to_replace
, bfd_boolean quick
);
69 static void print_descr (bfd
* abfd
);
70 static void write_archive (bfd
*);
71 static int ranlib_only (const char *archname
);
72 static int ranlib_touch (const char *archname
);
73 static void usage (int);
75 /** Globals and flags */
79 /* This flag distinguishes between ar and ranlib:
80 1 means this is 'ranlib'; 0 means this is 'ar'.
81 -1 means if we should use argv[0] to decide. */
84 /* Nonzero means don't warn about creating the archive file if necessary. */
85 int silent_create
= 0;
87 /* Nonzero means describe each action performed. */
90 /* Nonzero means preserve dates of members when extracting them. */
91 int preserve_dates
= 0;
93 /* Nonzero means don't replace existing members whose dates are more recent
94 than the corresponding files. */
97 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
98 member). -1 means we've been explicitly asked to not write a symbol table;
99 +1 means we've been explicitly asked to write it;
101 Traditionally, the default in BSD has been to not write the table.
102 However, for POSIX.2 compliance the default is now to write a symbol table
103 if any of the members are object files. */
106 /* Nonzero means it's the name of an existing member; position new or moved
107 files with respect to this one. */
108 char *posname
= NULL
;
110 /* Sez how to use `posname': pos_before means position before that member.
111 pos_after means position after that member. pos_end means always at end.
112 pos_default means default appropriately. For the latter two, `posname'
113 should also be zero. */
116 pos_default
, pos_before
, pos_after
, pos_end
117 } postype
= pos_default
;
120 get_pos_bfd (bfd
**, enum pos
, const char *);
122 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
123 extract the COUNTED_NAME_COUNTER instance of that name. */
124 static bfd_boolean counted_name_mode
= 0;
125 static int counted_name_counter
= 0;
127 /* Whether to truncate names of files stored in the archive. */
128 static bfd_boolean ar_truncate
= FALSE
;
130 /* Whether to use a full file name match when searching an archive.
131 This is convenient for archives created by the Microsoft lib
133 static bfd_boolean full_pathname
= FALSE
;
140 interactive
= isatty (fileno (stdin
));
144 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
145 COUNT is the length of the FILES chain; FUNCTION is called on each entry
146 whose name matches one in FILES. */
149 map_over_members (bfd
*arch
, void (*function
)(bfd
*), char **files
, int count
)
156 for (head
= arch
->next
; head
; head
= head
->next
)
164 /* This may appear to be a baroque way of accomplishing what we want.
165 However we have to iterate over the filenames in order to notice where
166 a filename is requested but does not exist in the archive. Ditto
167 mapping over each file each time -- we want to hack multiple
170 for (; count
> 0; files
++, count
--)
172 bfd_boolean found
= FALSE
;
175 for (head
= arch
->next
; head
; head
= head
->next
)
178 if (head
->filename
== NULL
)
180 /* Some archive formats don't get the filenames filled in
181 until the elements are opened. */
183 bfd_stat_arch_elt (head
, &buf
);
185 if ((head
->filename
!= NULL
) &&
186 (!FILENAME_CMP (normalize (*files
, arch
), head
->filename
)))
189 if (counted_name_mode
190 && match_count
!= counted_name_counter
)
192 /* Counting, and didn't match on count; go on to the
202 /* xgettext:c-format */
203 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
207 bfd_boolean operation_alters_arch
= FALSE
;
214 s
= help
? stdout
: stderr
;
218 /* xgettext:c-format */
219 fprintf (s
, _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
221 /* xgettext:c-format */
222 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
223 fprintf (s
, _(" commands:\n"));
224 fprintf (s
, _(" d - delete file(s) from the archive\n"));
225 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
226 fprintf (s
, _(" p - print file(s) found in the archive\n"));
227 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
228 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
229 fprintf (s
, _(" t - display contents of archive\n"));
230 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
231 fprintf (s
, _(" command specific modifiers:\n"));
232 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
233 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
234 fprintf (s
, _(" [N] - use instance [count] of name\n"));
235 fprintf (s
, _(" [f] - truncate inserted file names\n"));
236 fprintf (s
, _(" [P] - use full path names when matching\n"));
237 fprintf (s
, _(" [o] - preserve original dates\n"));
238 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
239 fprintf (s
, _(" generic modifiers:\n"));
240 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
241 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
242 fprintf (s
, _(" [S] - do not build a symbol table\n"));
243 fprintf (s
, _(" [v] - be verbose\n"));
244 fprintf (s
, _(" [V] - display the version number\n"));
245 fprintf (s
, _(" @<file> - read options from <file>\n"));
251 /* xgettext:c-format */
252 fprintf (s
, _("Usage: %s [options] archive\n"), program_name
);
253 fprintf (s
, _(" Generate an index to speed access to archives\n"));
254 fprintf (s
, _(" The options are:\n\
255 @<file> Read options from <file>\n\
256 -h --help Print this help message\n\
257 -V --version Print version information\n"));
260 list_supported_targets (program_name
, s
);
262 if (REPORT_BUGS_TO
[0] && help
)
263 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
265 xexit (help
? 0 : 1);
268 /* Normalize a file name specified on the command line into a file
269 name which we will use in an archive. */
272 normalize (const char *file
, bfd
*abfd
)
274 const char *filename
;
279 filename
= strrchr (file
, '/');
280 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
282 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
283 char *bslash
= strrchr (file
, '\\');
284 if (filename
== NULL
|| (bslash
!= NULL
&& bslash
> filename
))
286 if (filename
== NULL
&& file
[0] != '\0' && file
[1] == ':')
290 if (filename
!= (char *) NULL
)
297 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
302 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
303 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
304 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
311 /* Remove any output file. This is only called via xatexit. */
313 static const char *output_filename
= NULL
;
314 static FILE *output_file
= NULL
;
315 static bfd
*output_bfd
= NULL
;
320 if (output_filename
!= NULL
)
322 if (output_bfd
!= NULL
)
323 bfd_cache_close (output_bfd
);
324 if (output_file
!= NULL
)
325 fclose (output_file
);
326 unlink_if_ordinary (output_filename
);
330 /* The option parsing should be in its own function.
331 It will be when I have getopt working. */
333 int main (int, char **);
336 main (int argc
, char **argv
)
342 none
= 0, delete, replace
, print_table
,
343 print_files
, extract
, move
, quick_append
348 char *inarch_filename
;
353 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
354 setlocale (LC_MESSAGES
, "");
356 #if defined (HAVE_SETLOCALE)
357 setlocale (LC_CTYPE
, "");
359 bindtextdomain (PACKAGE
, LOCALEDIR
);
360 textdomain (PACKAGE
);
362 program_name
= argv
[0];
363 xmalloc_set_program_name (program_name
);
365 expandargv (&argc
, &argv
);
371 temp
= strrchr (program_name
, '/');
372 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
374 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
375 char *bslash
= strrchr (program_name
, '\\');
376 if (temp
== NULL
|| (bslash
!= NULL
&& bslash
> temp
))
378 if (temp
== NULL
&& program_name
[0] != '\0' && program_name
[1] == ':')
379 temp
= program_name
+ 1;
386 if (strlen (temp
) >= 6
387 && FILENAME_CMP (temp
+ strlen (temp
) - 6, "ranlib") == 0)
393 if (argc
> 1 && argv
[1][0] == '-')
395 if (strcmp (argv
[1], "--help") == 0)
397 else if (strcmp (argv
[1], "--version") == 0)
400 print_version ("ranlib");
402 print_version ("ar");
406 START_PROGRESS (program_name
, 0);
409 set_default_bfd_target ();
413 xatexit (remove_output
);
415 for (i
= 1; i
< argc
; i
++)
416 if (! ar_emul_parse_arg (argv
[i
]))
424 bfd_boolean touch
= FALSE
;
427 || strcmp (argv
[1], "--help") == 0
428 || strcmp (argv
[1], "-h") == 0
429 || strcmp (argv
[1], "-H") == 0)
431 if (strcmp (argv
[1], "-V") == 0
432 || strcmp (argv
[1], "-v") == 0
433 || CONST_STRNEQ (argv
[1], "--v"))
434 print_version ("ranlib");
436 if (strcmp (argv
[1], "-t") == 0)
441 while (arg_index
< argc
)
444 status
|= ranlib_only (argv
[arg_index
]);
446 status
|= ranlib_touch (argv
[arg_index
]);
452 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
462 arg_ptr
= argv
[arg_index
];
466 /* When the first option starts with '-' we support POSIX-compatible
469 ++arg_ptr
; /* compatibility */
474 while ((c
= *arg_ptr
++) != '\0')
485 if (operation
!= none
)
486 fatal (_("two different operation options specified"));
491 operation_alters_arch
= TRUE
;
495 operation_alters_arch
= TRUE
;
498 operation
= print_files
;
501 operation
= quick_append
;
502 operation_alters_arch
= TRUE
;
506 operation_alters_arch
= TRUE
;
509 operation
= print_table
;
542 postype
= pos_before
;
545 postype
= pos_before
;
551 counted_name_mode
= TRUE
;
557 full_pathname
= TRUE
;
560 /* xgettext:c-format */
561 non_fatal (_("illegal option -- %c"), c
);
566 /* With POSIX-compatible option parsing continue with the next
567 argument if it starts with '-'. */
568 if (do_posix
&& arg_index
+ 1 < argc
&& argv
[arg_index
+ 1][0] == '-')
569 arg_ptr
= argv
[++arg_index
] + 1;
576 print_version ("ar");
579 if (arg_index
>= argc
)
590 /* We don't use do_quick_append any more. Too many systems
591 expect ar to always rebuild the symbol table even when q is
594 /* We can't write an armap when using ar q, so just do ar r
596 if (operation
== quick_append
&& write_armap
)
599 if ((operation
== none
|| operation
== print_table
)
601 xexit (ranlib_only (argv
[arg_index
]));
603 if (operation
== none
)
604 fatal (_("no operation specified"));
606 if (newer_only
&& operation
!= replace
)
607 fatal (_("`u' is only meaningful with the `r' option."));
609 if (postype
!= pos_default
)
610 posname
= argv
[arg_index
++];
612 if (counted_name_mode
)
614 if (operation
!= extract
&& operation
!= delete)
615 fatal (_("`N' is only meaningful with the `x' and `d' options."));
616 counted_name_counter
= atoi (argv
[arg_index
++]);
617 if (counted_name_counter
<= 0)
618 fatal (_("Value for `N' must be positive."));
621 inarch_filename
= argv
[arg_index
++];
623 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
624 file_count
= argc
- arg_index
;
626 arch
= open_inarch (inarch_filename
,
627 files
== NULL
? (char *) NULL
: files
[0]);
632 map_over_members (arch
, print_descr
, files
, file_count
);
636 map_over_members (arch
, print_contents
, files
, file_count
);
640 map_over_members (arch
, extract_file
, files
, file_count
);
645 delete_members (arch
, files
);
647 output_filename
= NULL
;
652 move_members (arch
, files
);
654 output_filename
= NULL
;
659 if (files
!= NULL
|| write_armap
> 0)
660 replace_members (arch
, files
, operation
== quick_append
);
662 output_filename
= NULL
;
665 /* Shouldn't happen! */
667 /* xgettext:c-format */
668 fatal (_("internal error -- this option not implemented"));
672 END_PROGRESS (program_name
);
679 open_inarch (const char *archive_filename
, const char *file
)
688 bfd_set_error (bfd_error_no_error
);
692 if (stat (archive_filename
, &sbuf
) != 0)
694 #if !defined(__GO32__) || defined(__DJGPP__)
696 /* FIXME: I don't understand why this fragment was ifndef'ed
697 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
698 stat() works just fine in v2.x, so I think this should be
699 removed. For now, I enable it for DJGPP v2. -- EZ. */
701 /* KLUDGE ALERT! Temporary fix until I figger why
702 stat() is wrong ... think it's buried in GO32's IDT - Jax */
704 bfd_fatal (archive_filename
);
707 if (!operation_alters_arch
)
709 fprintf (stderr
, "%s: ", program_name
);
710 perror (archive_filename
);
715 /* Try to figure out the target to use for the archive from the
716 first object on the list. */
721 obj
= bfd_openr (file
, NULL
);
724 if (bfd_check_format (obj
, bfd_object
))
725 target
= bfd_get_target (obj
);
726 (void) bfd_close (obj
);
730 /* Create an empty archive. */
731 arch
= bfd_openw (archive_filename
, target
);
733 || ! bfd_set_format (arch
, bfd_archive
)
734 || ! bfd_close (arch
))
735 bfd_fatal (archive_filename
);
736 else if (!silent_create
)
737 non_fatal (_("creating %s"), archive_filename
);
739 /* If we die creating a new archive, don't leave it around. */
740 output_filename
= archive_filename
;
743 arch
= bfd_openr (archive_filename
, target
);
747 bfd_fatal (archive_filename
);
750 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
752 bfd_nonfatal (archive_filename
);
753 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
755 list_matching_formats (matching
);
761 last_one
= &(arch
->next
);
762 /* Read all the contents right away, regardless. */
763 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
765 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
768 *last_one
= next_one
;
769 last_one
= &next_one
->next
;
771 *last_one
= (bfd
*) NULL
;
772 if (bfd_get_error () != bfd_error_no_more_archived_files
)
778 print_contents (bfd
*abfd
)
781 char *cbuf
= xmalloc (BUFSIZE
);
784 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
785 /* xgettext:c-format */
786 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
789 /* xgettext:c-format */
790 printf (_("\n<%s>\n\n"), bfd_get_filename (abfd
));
792 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
795 while (ncopied
< size
)
799 size_t tocopy
= size
- ncopied
;
800 if (tocopy
> BUFSIZE
)
803 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
805 /* xgettext:c-format */
806 fatal (_("%s is not a valid archive"),
807 bfd_get_filename (bfd_my_archive (abfd
)));
809 /* fwrite in mingw32 may return int instead of size_t. Cast the
810 return value to size_t to avoid comparison between signed and
812 if ((size_t) fwrite (cbuf
, 1, nread
, stdout
) != nread
)
813 fatal ("stdout: %s", strerror (errno
));
819 /* Extract a member of the archive into its own file.
821 We defer opening the new file until after we have read a BUFSIZ chunk of the
822 old one, since we know we have just read the archive header for the old
823 one. Since most members are shorter than BUFSIZ, this means we will read
824 the old header, read the old data, write a new inode for the new file, and
825 write the new data, and be done. This 'optimization' is what comes from
826 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
830 extract_file (bfd
*abfd
)
833 char *cbuf
= xmalloc (BUFSIZE
);
834 size_t nread
, tocopy
;
839 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
840 /* xgettext:c-format */
841 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
845 printf ("x - %s\n", bfd_get_filename (abfd
));
847 bfd_seek (abfd
, (file_ptr
) 0, SEEK_SET
);
852 /* Seems like an abstraction violation, eh? Well it's OK! */
853 output_filename
= bfd_get_filename (abfd
);
855 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
858 perror (bfd_get_filename (abfd
));
862 output_file
= ostream
;
865 while (ncopied
< size
)
867 tocopy
= size
- ncopied
;
868 if (tocopy
> BUFSIZE
)
871 nread
= bfd_bread (cbuf
, (bfd_size_type
) tocopy
, abfd
);
873 /* xgettext:c-format */
874 fatal (_("%s is not a valid archive"),
875 bfd_get_filename (bfd_my_archive (abfd
)));
877 /* See comment above; this saves disk arm motion */
880 /* Seems like an abstraction violation, eh? Well it's OK! */
881 output_filename
= bfd_get_filename (abfd
);
883 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
886 perror (bfd_get_filename (abfd
));
890 output_file
= ostream
;
893 /* fwrite in mingw32 may return int instead of size_t. Cast
894 the return value to size_t to avoid comparison between
895 signed and unsigned values. */
896 if ((size_t) fwrite (cbuf
, 1, nread
, ostream
) != nread
)
897 fatal ("%s: %s", output_filename
, strerror (errno
));
905 output_filename
= NULL
;
907 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
911 /* Set access time to modification time. Only st_mtime is
912 initialized by bfd_stat_arch_elt. */
913 buf
.st_atime
= buf
.st_mtime
;
914 set_times (bfd_get_filename (abfd
), &buf
);
921 write_archive (bfd
*iarch
)
924 char *old_name
, *new_name
;
925 bfd
*contents_head
= iarch
->next
;
927 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
928 strcpy (old_name
, bfd_get_filename (iarch
));
929 new_name
= make_tempname (old_name
);
931 if (new_name
== NULL
)
932 bfd_fatal ("could not create temporary file whilst writing archive");
934 output_filename
= new_name
;
936 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
939 bfd_fatal (old_name
);
943 bfd_set_format (obfd
, bfd_archive
);
945 /* Request writing the archive symbol table unless we've
946 been explicitly requested not to. */
947 obfd
->has_armap
= write_armap
>= 0;
951 /* This should really use bfd_set_file_flags, but that rejects
953 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
956 if (!bfd_set_archive_head (obfd
, contents_head
))
957 bfd_fatal (old_name
);
959 if (!bfd_close (obfd
))
960 bfd_fatal (old_name
);
963 output_filename
= NULL
;
965 /* We don't care if this fails; we might be creating the archive. */
968 if (smart_rename (new_name
, old_name
, 0) != 0)
972 /* Return a pointer to the pointer to the entry which should be rplacd'd
973 into when altering. DEFAULT_POS should be how to interpret pos_default,
974 and should be a pos value. */
977 get_pos_bfd (bfd
**contents
, enum pos default_pos
, const char *default_posname
)
979 bfd
**after_bfd
= contents
;
981 const char *realposname
;
983 if (postype
== pos_default
)
985 realpos
= default_pos
;
986 realposname
= default_posname
;
991 realposname
= posname
;
994 if (realpos
== pos_end
)
997 after_bfd
= &((*after_bfd
)->next
);
1001 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->next
)
1002 if (FILENAME_CMP ((*after_bfd
)->filename
, realposname
) == 0)
1004 if (realpos
== pos_after
)
1005 after_bfd
= &(*after_bfd
)->next
;
1013 delete_members (bfd
*arch
, char **files_to_delete
)
1015 bfd
**current_ptr_ptr
;
1017 bfd_boolean something_changed
= FALSE
;
1020 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1022 /* In a.out systems, the armap is optional. It's also called
1023 __.SYMDEF. So if the user asked to delete it, we should remember
1024 that fact. This isn't quite right for COFF systems (where
1025 __.SYMDEF might be regular member), but it's very unlikely
1026 to be a problem. FIXME */
1028 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1030 arch
->has_armap
= FALSE
;
1037 current_ptr_ptr
= &(arch
->next
);
1038 while (*current_ptr_ptr
)
1040 if (FILENAME_CMP (normalize (*files_to_delete
, arch
),
1041 (*current_ptr_ptr
)->filename
) == 0)
1044 if (counted_name_mode
1045 && match_count
!= counted_name_counter
)
1047 /* Counting, and didn't match on count; go on to the
1053 something_changed
= TRUE
;
1057 *current_ptr_ptr
= ((*current_ptr_ptr
)->next
);
1062 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1065 if (verbose
&& !found
)
1067 /* xgettext:c-format */
1068 printf (_("No member named `%s'\n"), *files_to_delete
);
1074 if (something_changed
)
1075 write_archive (arch
);
1077 output_filename
= NULL
;
1081 /* Reposition existing members within an archive */
1084 move_members (bfd
*arch
, char **files_to_move
)
1086 bfd
**after_bfd
; /* New entries go after this one */
1087 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1089 for (; *files_to_move
; ++files_to_move
)
1091 current_ptr_ptr
= &(arch
->next
);
1092 while (*current_ptr_ptr
)
1094 bfd
*current_ptr
= *current_ptr_ptr
;
1095 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1096 current_ptr
->filename
) == 0)
1098 /* Move this file to the end of the list - first cut from
1101 *current_ptr_ptr
= current_ptr
->next
;
1103 /* Now glue to end */
1104 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1106 *after_bfd
= current_ptr
;
1107 current_ptr
->next
= link
;
1110 printf ("m - %s\n", *files_to_move
);
1115 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1117 /* xgettext:c-format */
1118 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1123 write_archive (arch
);
1126 /* Ought to default to replacing in place, but this is existing practice! */
1129 replace_members (bfd
*arch
, char **files_to_move
, bfd_boolean quick
)
1131 bfd_boolean changed
= FALSE
;
1132 bfd
**after_bfd
; /* New entries go after this one. */
1136 while (files_to_move
&& *files_to_move
)
1140 current_ptr
= &arch
->next
;
1141 while (*current_ptr
)
1143 current
= *current_ptr
;
1145 /* For compatibility with existing ar programs, we
1146 permit the same file to be added multiple times. */
1147 if (FILENAME_CMP (normalize (*files_to_move
, arch
),
1148 normalize (current
->filename
, arch
)) == 0
1149 && current
->arelt_data
!= NULL
)
1153 struct stat fsbuf
, asbuf
;
1155 if (stat (*files_to_move
, &fsbuf
) != 0)
1157 if (errno
!= ENOENT
)
1158 bfd_fatal (*files_to_move
);
1161 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1162 /* xgettext:c-format */
1163 fatal (_("internal stat error on %s"),
1166 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1170 after_bfd
= get_pos_bfd (&arch
->next
, pos_after
,
1172 if (ar_emul_replace (after_bfd
, *files_to_move
,
1175 /* Snip out this entry from the chain. */
1176 *current_ptr
= (*current_ptr
)->next
;
1182 current_ptr
= &(current
->next
);
1186 /* Add to the end of the archive. */
1187 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1189 if (ar_emul_append (after_bfd
, *files_to_move
, verbose
))
1198 write_archive (arch
);
1200 output_filename
= NULL
;
1204 ranlib_only (const char *archname
)
1208 if (get_file_size (archname
) < 1)
1211 arch
= open_inarch (archname
, (char *) NULL
);
1214 write_archive (arch
);
1218 /* Update the timestamp of the symbol map of an archive. */
1221 ranlib_touch (const char *archname
)
1224 /* I don't think updating works on go32. */
1225 ranlib_only (archname
);
1231 if (get_file_size (archname
) < 1)
1233 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1236 bfd_set_error (bfd_error_system_call
);
1237 bfd_fatal (archname
);
1240 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1242 bfd_fatal (archname
);
1243 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1245 bfd_nonfatal (archname
);
1246 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1248 list_matching_formats (matching
);
1254 if (! bfd_has_map (arch
))
1255 /* xgettext:c-format */
1256 fatal (_("%s: no archive map to update"), archname
);
1258 bfd_update_armap_timestamp (arch
);
1260 if (! bfd_close (arch
))
1261 bfd_fatal (archname
);
1266 /* Things which are interesting to map over all or some of the files: */
1269 print_descr (bfd
*abfd
)
1271 print_arelt_descr (stdout
, abfd
, verbose
);