1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 98, 99, 2000
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 Bugs: should use getopt the way tar does (complete w/optional -) and
23 should have long options too. GNU ar used to check file against filesystem
24 in quick_update and replace operations (would check mtime). Doesn't warn
25 when name truncated. No way to specify pos_end. Error messages should be
29 #include "libiberty.h"
38 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
40 #define EXT_NAME_LEN 6 /* ditto for *NIX */
43 /* We need to open files in binary modes on system where that makes a
51 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
54 bfd_special_undocumented_glue
PARAMS ((bfd
* abfd
, const char *filename
));
56 /* Static declarations */
59 mri_emul
PARAMS ((void));
62 normalize
PARAMS ((const char *, bfd
*));
65 remove_output
PARAMS ((void));
68 map_over_members
PARAMS ((bfd
*, void (*)(bfd
*), char **, int));
71 print_contents
PARAMS ((bfd
* member
));
74 delete_members
PARAMS ((bfd
*, char **files_to_delete
));
78 do_quick_append
PARAMS ((const char *archive_filename
,
79 char **files_to_append
));
83 move_members
PARAMS ((bfd
*, char **files_to_move
));
86 replace_members
PARAMS ((bfd
*, char **files_to_replace
, boolean quick
));
89 print_descr
PARAMS ((bfd
* abfd
));
92 write_archive
PARAMS ((bfd
*));
95 ranlib_only
PARAMS ((const char *archname
));
98 ranlib_touch
PARAMS ((const char *archname
));
101 usage
PARAMS ((int));
103 /** Globals and flags */
107 /* This flag distinguishes between ar and ranlib:
108 1 means this is 'ranlib'; 0 means this is 'ar'.
109 -1 means if we should use argv[0] to decide. */
110 extern int is_ranlib
;
112 /* Nonzero means don't warn about creating the archive file if necessary. */
113 int silent_create
= 0;
115 /* Nonzero means describe each action performed. */
118 /* Nonzero means preserve dates of members when extracting them. */
119 int preserve_dates
= 0;
121 /* Nonzero means don't replace existing members whose dates are more recent
122 than the corresponding files. */
125 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
126 member). -1 means we've been explicitly asked to not write a symbol table;
127 +1 means we've been explictly asked to write it;
129 Traditionally, the default in BSD has been to not write the table.
130 However, for POSIX.2 compliance the default is now to write a symbol table
131 if any of the members are object files. */
134 /* Nonzero means it's the name of an existing member; position new or moved
135 files with respect to this one. */
136 char *posname
= NULL
;
138 /* Sez how to use `posname': pos_before means position before that member.
139 pos_after means position after that member. pos_end means always at end.
140 pos_default means default appropriately. For the latter two, `posname'
141 should also be zero. */
144 pos_default
, pos_before
, pos_after
, pos_end
145 } postype
= pos_default
;
148 get_pos_bfd
PARAMS ((bfd
**, enum pos
, const char *));
150 /* For extract/delete only. If COUNTED_NAME_MODE is true, we only
151 extract the COUNTED_NAME_COUNTER instance of that name. */
152 static boolean counted_name_mode
= 0;
153 static int counted_name_counter
= 0;
155 /* Whether to truncate names of files stored in the archive. */
156 static boolean ar_truncate
= false;
158 /* Whether to use a full file name match when searching an archive.
159 This is convenient for archives created by the Microsoft lib
161 static boolean full_pathname
= false;
168 interactive
= isatty (fileno (stdin
));
172 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
173 COUNT is the length of the FILES chain; FUNCTION is called on each entry
174 whose name matches one in FILES. */
177 map_over_members (arch
, function
, files
, count
)
179 void (*function
) PARAMS ((bfd
*));
188 for (head
= arch
->next
; head
; head
= head
->next
)
196 /* This may appear to be a baroque way of accomplishing what we want.
197 However we have to iterate over the filenames in order to notice where
198 a filename is requested but does not exist in the archive. Ditto
199 mapping over each file each time -- we want to hack multiple
202 for (; count
> 0; files
++, count
--)
204 boolean found
= false;
207 for (head
= arch
->next
; head
; head
= head
->next
)
210 if (head
->filename
== NULL
)
212 /* Some archive formats don't get the filenames filled in
213 until the elements are opened. */
215 bfd_stat_arch_elt (head
, &buf
);
217 if ((head
->filename
!= NULL
) &&
218 (!strcmp (normalize (*files
, arch
), head
->filename
)))
221 if (counted_name_mode
222 && match_count
!= counted_name_counter
)
224 /* Counting, and didn't match on count; go on to the
234 /* xgettext:c-format */
235 fprintf (stderr
, _("no entry %s in archive\n"), *files
);
239 boolean operation_alters_arch
= false;
247 s
= help
? stdout
: stderr
;
251 /* xgettext:c-format */
252 fprintf (s
, _("Usage: %s [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
254 /* xgettext:c-format */
255 fprintf (s
, _(" %s -M [<mri-script]\n"), program_name
);
256 fprintf (s
, _(" commands:\n"));
257 fprintf (s
, _(" d - delete file(s) from the archive\n"));
258 fprintf (s
, _(" m[ab] - move file(s) in the archive\n"));
259 fprintf (s
, _(" p - print file(s) found in the archive\n"));
260 fprintf (s
, _(" q[f] - quick append file(s) to the archive\n"));
261 fprintf (s
, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
262 fprintf (s
, _(" t - display contents of archive\n"));
263 fprintf (s
, _(" x[o] - extract file(s) from the archive\n"));
264 fprintf (s
, _(" command specific modifiers:\n"));
265 fprintf (s
, _(" [a] - put file(s) after [member-name]\n"));
266 fprintf (s
, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
267 fprintf (s
, _(" [N] - use instance [count] of name\n"));
268 fprintf (s
, _(" [f] - truncate inserted file names\n"));
269 fprintf (s
, _(" [P] - use full path names when matching\n"));
270 fprintf (s
, _(" [o] - preserve original dates\n"));
271 fprintf (s
, _(" [u] - only replace files that are newer than current archive contents\n"));
272 fprintf (s
, _(" generic modifiers:\n"));
273 fprintf (s
, _(" [c] - do not warn if the library had to be created\n"));
274 fprintf (s
, _(" [s] - create an archive index (cf. ranlib)\n"));
275 fprintf (s
, _(" [S] - do not build a symbol table\n"));
276 fprintf (s
, _(" [v] - be verbose\n"));
277 fprintf (s
, _(" [V] - display the version number\n"));
280 /* xgettext:c-format */
281 fprintf (s
, _("Usage: %s [-vV] archive\n"), program_name
);
283 list_supported_targets (program_name
, stderr
);
286 fprintf (s
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
288 xexit (help
? 0 : 1);
291 /* Normalize a file name specified on the command line into a file
292 name which we will use in an archive. */
295 normalize (file
, abfd
)
299 const char *filename
;
304 filename
= strrchr (file
, '/');
305 if (filename
!= (char *) NULL
)
312 && strlen (filename
) > abfd
->xvec
->ar_max_namelen
)
317 s
= (char *) xmalloc (abfd
->xvec
->ar_max_namelen
+ 1);
318 memcpy (s
, filename
, abfd
->xvec
->ar_max_namelen
);
319 s
[abfd
->xvec
->ar_max_namelen
] = '\0';
326 /* Remove any output file. This is only called via xatexit. */
328 static const char *output_filename
= NULL
;
329 static FILE *output_file
= NULL
;
330 static bfd
*output_bfd
= NULL
;
335 if (output_filename
!= NULL
)
337 if (output_bfd
!= NULL
&& output_bfd
->iostream
!= NULL
)
338 fclose ((FILE *) (output_bfd
->iostream
));
339 if (output_file
!= NULL
)
340 fclose (output_file
);
341 unlink (output_filename
);
345 /* The option parsing should be in its own function.
346 It will be when I have getopt working. */
357 none
= 0, delete, replace
, print_table
,
358 print_files
, extract
, move
, quick_append
363 char *inarch_filename
;
366 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
367 setlocale (LC_MESSAGES
, "");
369 bindtextdomain (PACKAGE
, LOCALEDIR
);
370 textdomain (PACKAGE
);
372 program_name
= argv
[0];
373 xmalloc_set_program_name (program_name
);
379 temp
= strrchr (program_name
, '/');
384 if (strlen (temp
) >= 6
385 && strcmp (temp
+ strlen (temp
) - 6, "ranlib") == 0)
391 if (argc
> 1 && argv
[1][0] == '-')
393 if (strcmp (argv
[1], "--help") == 0)
395 else if (strcmp (argv
[1], "--version") == 0)
398 print_version ("ranlib");
400 print_version ("ar");
404 START_PROGRESS (program_name
, 0);
407 set_default_bfd_target ();
411 xatexit (remove_output
);
415 boolean touch
= false;
417 if (argc
< 2 || strcmp (argv
[1], "--help") == 0)
419 if (strcmp (argv
[1], "-V") == 0
420 || strcmp (argv
[1], "-v") == 0
421 || strncmp (argv
[1], "--v", 3) == 0)
422 print_version ("ranlib");
424 if (strcmp (argv
[1], "-t") == 0)
429 while (arg_index
< argc
)
432 ranlib_only (argv
[arg_index
]);
434 ranlib_touch (argv
[arg_index
]);
440 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
452 ++arg_ptr
; /* compatibility */
454 while ((c
= *arg_ptr
++) != '\0')
465 if (operation
!= none
)
466 fatal (_("two different operation options specified"));
471 operation_alters_arch
= true;
475 operation_alters_arch
= true;
478 operation
= print_files
;
481 operation
= quick_append
;
482 operation_alters_arch
= true;
486 operation_alters_arch
= true;
489 operation
= print_table
;
522 postype
= pos_before
;
525 postype
= pos_before
;
531 counted_name_mode
= true;
537 full_pathname
= true;
540 /* xgettext:c-format */
541 non_fatal (_("illegal option -- %c"), c
);
547 print_version ("ar");
560 /* We can't write an armap when using ar q, so just do ar r
562 if (operation
== quick_append
&& write_armap
)
565 if ((operation
== none
|| operation
== print_table
)
568 ranlib_only (argv
[2]);
572 if (operation
== none
)
573 fatal (_("no operation specified"));
575 if (newer_only
&& operation
!= replace
)
576 fatal (_("`u' is only meaningful with the `r' option."));
580 if (postype
!= pos_default
)
581 posname
= argv
[arg_index
++];
583 if (counted_name_mode
)
585 if (operation
!= extract
&& operation
!= delete)
586 fatal (_("`N' is only meaningful with the `x' and `d' options."));
587 counted_name_counter
= atoi (argv
[arg_index
++]);
588 if (counted_name_counter
<= 0)
589 fatal (_("Value for `N' must be positive."));
592 inarch_filename
= argv
[arg_index
++];
594 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
595 file_count
= argc
- arg_index
;
598 /* We don't use do_quick_append any more. Too many systems
599 expect ar to always rebuild the symbol table even when q is
602 /* We can't do a quick append if we need to construct an
603 extended name table, because do_quick_append won't be able to
604 rebuild the name table. Unfortunately, at this point we
605 don't actually know the maximum name length permitted by this
606 object file format. So, we guess. FIXME. */
607 if (operation
== quick_append
&& ! ar_truncate
)
611 for (chk
= files
; chk
!= NULL
&& *chk
!= '\0'; chk
++)
613 if (strlen (normalize (*chk
, (bfd
*) NULL
)) > 14)
621 if (operation
== quick_append
)
623 /* Note that quick appending to a non-existent archive creates it,
624 even if there are no files to append. */
625 do_quick_append (inarch_filename
, files
);
630 arch
= open_inarch (inarch_filename
,
631 files
== NULL
? (char *) NULL
: files
[0]);
636 map_over_members (arch
, print_descr
, files
, file_count
);
640 map_over_members (arch
, print_contents
, files
, file_count
);
644 map_over_members (arch
, extract_file
, files
, file_count
);
649 delete_members (arch
, files
);
651 output_filename
= NULL
;
656 move_members (arch
, files
);
658 output_filename
= NULL
;
663 if (files
!= NULL
|| write_armap
> 0)
664 replace_members (arch
, files
, operation
== quick_append
);
666 output_filename
= NULL
;
669 /* Shouldn't happen! */
671 /* xgettext:c-format */
672 fatal (_("internal error -- this option not implemented"));
676 END_PROGRESS (program_name
);
683 open_inarch (archive_filename
, file
)
684 const char *archive_filename
;
694 bfd_set_error (bfd_error_no_error
);
698 if (stat (archive_filename
, &sbuf
) != 0)
702 /* KLUDGE ALERT! Temporary fix until I figger why
703 * stat() is wrong ... think it's buried in GO32's IDT
707 bfd_fatal (archive_filename
);
710 if (!operation_alters_arch
)
712 fprintf (stderr
, "%s: ", program_name
);
713 perror (archive_filename
);
718 /* Try to figure out the target to use for the archive from the
719 first object on the list. */
724 obj
= bfd_openr (file
, NULL
);
727 if (bfd_check_format (obj
, bfd_object
))
728 target
= bfd_get_target (obj
);
729 (void) bfd_close (obj
);
733 /* Create an empty archive. */
734 arch
= bfd_openw (archive_filename
, target
);
736 || ! bfd_set_format (arch
, bfd_archive
)
737 || ! bfd_close (arch
))
738 bfd_fatal (archive_filename
);
740 /* If we die creating a new archive, don't leave it around. */
741 output_filename
= archive_filename
;
744 arch
= bfd_openr (archive_filename
, target
);
748 bfd_fatal (archive_filename
);
751 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
753 bfd_nonfatal (archive_filename
);
754 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
756 list_matching_formats (matching
);
762 last_one
= &(arch
->next
);
763 /* Read all the contents right away, regardless. */
764 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
766 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
769 *last_one
= next_one
;
770 last_one
= &next_one
->next
;
772 *last_one
= (bfd
*) NULL
;
773 if (bfd_get_error () != bfd_error_no_more_archived_files
)
779 print_contents (abfd
)
783 char *cbuf
= xmalloc (BUFSIZE
);
786 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
787 /* xgettext:c-format */
788 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
791 printf ("\n<%s>\n\n", bfd_get_filename (abfd
));
793 bfd_seek (abfd
, 0, SEEK_SET
);
796 while (ncopied
< size
)
800 int tocopy
= size
- ncopied
;
801 if (tocopy
> BUFSIZE
)
804 nread
= bfd_read (cbuf
, 1, tocopy
, abfd
); /* oops -- broke
807 /* xgettext:c-format */
808 fatal (_("%s is not a valid archive"),
809 bfd_get_filename (bfd_my_archive (abfd
)));
810 fwrite (cbuf
, 1, nread
, stdout
);
816 /* Extract a member of the archive into its own file.
818 We defer opening the new file until after we have read a BUFSIZ chunk of the
819 old one, since we know we have just read the archive header for the old
820 one. Since most members are shorter than BUFSIZ, this means we will read
821 the old header, read the old data, write a new inode for the new file, and
822 write the new data, and be done. This 'optimization' is what comes from
823 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
831 char *cbuf
= xmalloc (BUFSIZE
);
837 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
838 /* xgettext:c-format */
839 fatal (_("internal stat error on %s"), bfd_get_filename (abfd
));
843 /* xgettext:c-format */
844 fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd
));
847 printf ("x - %s\n", bfd_get_filename (abfd
));
849 bfd_seek (abfd
, 0, SEEK_SET
);
854 /* Seems like an abstraction violation, eh? Well it's OK! */
855 output_filename
= bfd_get_filename (abfd
);
857 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
860 perror (bfd_get_filename (abfd
));
864 output_file
= ostream
;
867 while (ncopied
< size
)
869 tocopy
= size
- ncopied
;
870 if (tocopy
> BUFSIZE
)
873 nread
= bfd_read (cbuf
, 1, tocopy
, abfd
);
875 /* xgettext:c-format */
876 fatal (_("%s is not a valid archive"),
877 bfd_get_filename (bfd_my_archive (abfd
)));
879 /* See comment above; this saves disk arm motion */
882 /* Seems like an abstraction violation, eh? Well it's OK! */
883 output_filename
= bfd_get_filename (abfd
);
885 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
888 perror (bfd_get_filename (abfd
));
892 output_file
= ostream
;
894 fwrite (cbuf
, 1, nread
, ostream
);
902 output_filename
= NULL
;
904 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
907 set_times (bfd_get_filename (abfd
), &buf
);
914 /* We don't use this anymore. Too many systems expect ar to rebuild
915 the symbol table even when q is used. */
917 /* Just do it quickly; don't worry about dups, armap, or anything like that */
920 do_quick_append (archive_filename
, files_to_append
)
921 const char *archive_filename
;
922 char **files_to_append
;
925 char *buf
= xmalloc (BUFSIZE
);
926 long tocopy
, thistime
;
929 boolean newfile
= false;
930 bfd_set_error (bfd_error_no_error
);
932 if (stat (archive_filename
, &sbuf
) != 0)
937 /* KLUDGE ALERT! Temporary fix until I figger why
938 * stat() is wrong ... think it's buried in GO32's IDT
943 bfd_fatal (archive_filename
);
949 ofile
= fopen (archive_filename
, FOPEN_AUB
);
952 perror (program_name
);
956 temp
= bfd_openr (archive_filename
, NULL
);
959 bfd_fatal (archive_filename
);
961 if (newfile
== false)
963 if (bfd_check_format (temp
, bfd_archive
) != true)
964 /* xgettext:c-format */
965 fatal (_("%s is not an archive"), archive_filename
);
969 fwrite (ARMAG
, 1, SARMAG
, ofile
);
971 /* xgettext:c-format */
972 non_fatal (_("creating %s"), archive_filename
);
976 temp
->flags
|= BFD_TRADITIONAL_FORMAT
;
978 /* assume it's an achive, go straight to the end, sans $200 */
981 for (; files_to_append
&& *files_to_append
; ++files_to_append
)
983 struct ar_hdr
*hdr
= bfd_special_undocumented_glue (temp
, *files_to_append
);
986 bfd_fatal (*files_to_append
);
989 BFD_SEND (temp
, _bfd_truncate_arname
, (temp
, *files_to_append
, (char *) hdr
));
991 ifile
= fopen (*files_to_append
, FOPEN_RB
);
994 bfd_nonfatal (*files_to_append
);
997 if (stat (*files_to_append
, &sbuf
) != 0)
999 bfd_nonfatal (*files_to_append
);
1002 tocopy
= sbuf
.st_size
;
1004 /* XXX should do error-checking! */
1005 fwrite (hdr
, 1, sizeof (struct ar_hdr
), ofile
);
1010 if (thistime
> BUFSIZE
)
1012 fread (buf
, 1, thistime
, ifile
);
1013 fwrite (buf
, 1, thistime
, ofile
);
1017 if ((sbuf
.st_size
% 2) == 1)
1018 putc ('\012', ofile
);
1028 write_archive (iarch
)
1032 char *old_name
, *new_name
;
1033 bfd
*contents_head
= iarch
->next
;
1035 old_name
= xmalloc (strlen (bfd_get_filename (iarch
)) + 1);
1036 strcpy (old_name
, bfd_get_filename (iarch
));
1037 new_name
= make_tempname (old_name
);
1039 output_filename
= new_name
;
1041 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
1044 bfd_fatal (old_name
);
1048 bfd_set_format (obfd
, bfd_archive
);
1050 /* Request writing the archive symbol table unless we've
1051 been explicitly requested not to. */
1052 obfd
->has_armap
= write_armap
>= 0;
1056 /* This should really use bfd_set_file_flags, but that rejects
1058 obfd
->flags
|= BFD_TRADITIONAL_FORMAT
;
1061 if (bfd_set_archive_head (obfd
, contents_head
) != true)
1062 bfd_fatal (old_name
);
1064 if (!bfd_close (obfd
))
1065 bfd_fatal (old_name
);
1068 output_filename
= NULL
;
1070 /* We don't care if this fails; we might be creating the archive. */
1073 if (smart_rename (new_name
, old_name
, 0) != 0)
1077 /* Return a pointer to the pointer to the entry which should be rplacd'd
1078 into when altering. DEFAULT_POS should be how to interpret pos_default,
1079 and should be a pos value. */
1082 get_pos_bfd (contents
, default_pos
, default_posname
)
1084 enum pos default_pos
;
1085 const char *default_posname
;
1087 bfd
**after_bfd
= contents
;
1089 const char *realposname
;
1091 if (postype
== pos_default
)
1093 realpos
= default_pos
;
1094 realposname
= default_posname
;
1099 realposname
= posname
;
1102 if (realpos
== pos_end
)
1105 after_bfd
= &((*after_bfd
)->next
);
1109 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->next
)
1110 if (strcmp ((*after_bfd
)->filename
, realposname
) == 0)
1112 if (realpos
== pos_after
)
1113 after_bfd
= &(*after_bfd
)->next
;
1121 delete_members (arch
, files_to_delete
)
1123 char **files_to_delete
;
1125 bfd
**current_ptr_ptr
;
1127 boolean something_changed
= false;
1130 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
1132 /* In a.out systems, the armap is optional. It's also called
1133 __.SYMDEF. So if the user asked to delete it, we should remember
1134 that fact. This isn't quite right for COFF systems (where
1135 __.SYMDEF might be regular member), but it's very unlikely
1136 to be a problem. FIXME */
1138 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
1140 arch
->has_armap
= false;
1147 current_ptr_ptr
= &(arch
->next
);
1148 while (*current_ptr_ptr
)
1150 if (strcmp (normalize (*files_to_delete
, arch
),
1151 (*current_ptr_ptr
)->filename
) == 0)
1154 if (counted_name_mode
1155 && match_count
!= counted_name_counter
)
1157 /* Counting, and didn't match on count; go on to the
1163 something_changed
= true;
1167 *current_ptr_ptr
= ((*current_ptr_ptr
)->next
);
1172 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1175 if (verbose
&& found
== false)
1177 /* xgettext:c-format */
1178 printf (_("No member named `%s'\n"), *files_to_delete
);
1184 if (something_changed
== true)
1185 write_archive (arch
);
1187 output_filename
= NULL
;
1191 /* Reposition existing members within an archive */
1194 move_members (arch
, files_to_move
)
1196 char **files_to_move
;
1198 bfd
**after_bfd
; /* New entries go after this one */
1199 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
1201 for (; *files_to_move
; ++files_to_move
)
1203 current_ptr_ptr
= &(arch
->next
);
1204 while (*current_ptr_ptr
)
1206 bfd
*current_ptr
= *current_ptr_ptr
;
1207 if (strcmp (normalize (*files_to_move
, arch
),
1208 current_ptr
->filename
) == 0)
1210 /* Move this file to the end of the list - first cut from
1213 *current_ptr_ptr
= current_ptr
->next
;
1215 /* Now glue to end */
1216 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1218 *after_bfd
= current_ptr
;
1219 current_ptr
->next
= link
;
1222 printf ("m - %s\n", *files_to_move
);
1227 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1229 /* xgettext:c-format */
1230 fatal (_("no entry %s in archive %s!"), *files_to_move
, arch
->filename
);
1235 write_archive (arch
);
1238 /* Ought to default to replacing in place, but this is existing practice! */
1241 replace_members (arch
, files_to_move
, quick
)
1243 char **files_to_move
;
1246 boolean changed
= false;
1247 bfd
**after_bfd
; /* New entries go after this one */
1252 while (files_to_move
&& *files_to_move
)
1256 current_ptr
= &arch
->next
;
1257 while (*current_ptr
)
1259 current
= *current_ptr
;
1261 /* For compatibility with existing ar programs, we
1262 permit the same file to be added multiple times. */
1263 if (strcmp (normalize (*files_to_move
, arch
),
1264 normalize (current
->filename
, arch
)) == 0
1265 && current
->arelt_data
!= NULL
)
1269 struct stat fsbuf
, asbuf
;
1271 if (stat (*files_to_move
, &fsbuf
) != 0)
1273 if (errno
!= ENOENT
)
1274 bfd_fatal (*files_to_move
);
1277 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1278 /* xgettext:c-format */
1279 fatal (_("internal stat error on %s"), current
->filename
);
1281 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1285 after_bfd
= get_pos_bfd (&arch
->next
, pos_after
,
1289 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1290 if (*after_bfd
== (bfd
*) NULL
)
1292 bfd_fatal (*files_to_move
);
1294 (*after_bfd
)->next
= temp
;
1296 /* snip out this entry from the chain */
1297 *current_ptr
= (*current_ptr
)->next
;
1301 printf ("r - %s\n", *files_to_move
);
1308 current_ptr
= &(current
->next
);
1312 /* Add to the end of the archive. */
1314 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
, NULL
);
1316 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1317 if (*after_bfd
== (bfd
*) NULL
)
1319 bfd_fatal (*files_to_move
);
1323 printf ("a - %s\n", *files_to_move
);
1326 (*after_bfd
)->next
= temp
;
1336 write_archive (arch
);
1338 output_filename
= NULL
;
1342 ranlib_only (archname
)
1343 const char *archname
;
1348 arch
= open_inarch (archname
, (char *) NULL
);
1351 write_archive (arch
);
1354 /* Update the timestamp of the symbol map of an archive. */
1357 ranlib_touch (archname
)
1358 const char *archname
;
1361 /* I don't think updating works on go32. */
1362 ranlib_only (archname
);
1368 f
= open (archname
, O_RDWR
| O_BINARY
, 0);
1371 bfd_set_error (bfd_error_system_call
);
1372 bfd_fatal (archname
);
1375 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1377 bfd_fatal (archname
);
1378 if (! bfd_check_format_matches (arch
, bfd_archive
, &matching
))
1380 bfd_nonfatal (archname
);
1381 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1383 list_matching_formats (matching
);
1389 if (! bfd_has_map (arch
))
1390 /* xgettext:c-format */
1391 fatal (_("%s: no archive map to update"), archname
);
1393 bfd_update_armap_timestamp (arch
);
1395 if (! bfd_close (arch
))
1396 bfd_fatal (archname
);
1400 /* Things which are interesting to map over all or some of the files: */
1406 print_arelt_descr (stdout
, abfd
, verbose
);