1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 Free Software Foundation, Inc.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 Bugs: should use getopt the way tar does (complete w/optional -) and
24 should have long options too. GNU ar used to check file against filesystem
25 in quick_update and replace operations (would check mtime). Doesn't warn
26 when name truncated. No way to specify pos_end. Error messages should be
30 #include "libiberty.h"
36 #include "filenames.h"
40 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
42 #define EXT_NAME_LEN 6 /* ditto for *NIX */
45 /* We need to open files in binary modes on system where that makes a
53 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
56 bfd_special_undocumented_glue PARAMS ((bfd * abfd, const char *filename));
58 /* Static declarations */
61 mri_emul PARAMS ((void));
64 normalize PARAMS ((const char *, bfd *));
67 remove_output PARAMS ((void));
70 map_over_members PARAMS ((bfd *, void (*)(bfd *), char **, int));
73 print_contents PARAMS ((bfd * member));
76 delete_members PARAMS ((bfd *, char **files_to_delete));
80 do_quick_append PARAMS ((const char *archive_filename,
81 char **files_to_append));
85 move_members PARAMS ((bfd *, char **files_to_move));
88 replace_members PARAMS ((bfd *, char **files_to_replace, boolean quick));
91 print_descr PARAMS ((bfd * abfd));
94 write_archive PARAMS ((bfd *));
97 ranlib_only PARAMS ((const char *archname));
100 ranlib_touch PARAMS ((const char *archname));
103 usage PARAMS ((int));
105 /** Globals and flags */
109 /* This flag distinguishes between ar and ranlib:
110 1 means this is 'ranlib'; 0 means this is 'ar'.
111 -1 means if we should use argv[0] to decide. */
112 extern int is_ranlib;
114 /* Nonzero means don't warn about creating the archive file if necessary. */
115 int silent_create = 0;
117 /* Nonzero means describe each action performed. */
120 /* Nonzero means preserve dates of members when extracting them. */
121 int preserve_dates = 0;
123 /* Nonzero means don't replace existing members whose dates are more recent
124 than the corresponding files. */
127 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
128 member). -1 means we've been explicitly asked to not write a symbol table;
129 +1 means we've been explictly asked to write it;
131 Traditionally, the default in BSD has been to not write the table.
132 However, for POSIX.2 compliance the default is now to write a symbol table
133 if any of the members are object files. */
136 /* Nonzero means it's the name of an existing member; position new or moved
137 files with respect to this one. */
138 char *posname = NULL;
140 /* Sez how to use `posname': pos_before means position before that member.
141 pos_after means position after that member. pos_end means always at end.
142 pos_default means default appropriately. For the latter two, `posname'
143 should also be zero. */
146 pos_default, pos_before, pos_after, pos_end
147 } postype = pos_default;
150 get_pos_bfd PARAMS ((bfd **, enum pos, const char *));
152 /* For extract/delete only. If COUNTED_NAME_MODE is true, we only
153 extract the COUNTED_NAME_COUNTER instance of that name. */
154 static boolean counted_name_mode = 0;
155 static int counted_name_counter = 0;
157 /* Whether to truncate names of files stored in the archive. */
158 static boolean ar_truncate = false;
160 /* Whether to use a full file name match when searching an archive.
161 This is convenient for archives created by the Microsoft lib
163 static boolean full_pathname = false;
170 interactive = isatty (fileno (stdin));
174 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
175 COUNT is the length of the FILES chain; FUNCTION is called on each entry
176 whose name matches one in FILES. */
179 map_over_members (arch, function, files, count)
181 void (*function) PARAMS ((bfd *));
190 for (head = arch->next; head; head = head->next)
198 /* This may appear to be a baroque way of accomplishing what we want.
199 However we have to iterate over the filenames in order to notice where
200 a filename is requested but does not exist in the archive. Ditto
201 mapping over each file each time -- we want to hack multiple
204 for (; count > 0; files++, count--)
206 boolean found = false;
209 for (head = arch->next; head; head = head->next)
212 if (head->filename == NULL)
214 /* Some archive formats don't get the filenames filled in
215 until the elements are opened. */
217 bfd_stat_arch_elt (head, &buf);
219 if ((head->filename != NULL) &&
220 (!FILENAME_CMP (normalize (*files, arch), head->filename)))
223 if (counted_name_mode
224 && match_count != counted_name_counter)
226 /* Counting, and didn't match on count; go on to the
236 /* xgettext:c-format */
237 fprintf (stderr, _("no entry %s in archive\n"), *files);
241 boolean operation_alters_arch = false;
249 s = help ? stdout : stderr;
253 /* xgettext:c-format */
254 fprintf (s, _("Usage: %s [-X32_64] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n"),
256 /* xgettext:c-format */
257 fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
258 fprintf (s, _(" commands:\n"));
259 fprintf (s, _(" d - delete file(s) from the archive\n"));
260 fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
261 fprintf (s, _(" p - print file(s) found in the archive\n"));
262 fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
263 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
264 fprintf (s, _(" t - display contents of archive\n"));
265 fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
266 fprintf (s, _(" command specific modifiers:\n"));
267 fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
268 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
269 fprintf (s, _(" [N] - use instance [count] of name\n"));
270 fprintf (s, _(" [f] - truncate inserted file names\n"));
271 fprintf (s, _(" [P] - use full path names when matching\n"));
272 fprintf (s, _(" [o] - preserve original dates\n"));
273 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
274 fprintf (s, _(" generic modifiers:\n"));
275 fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
276 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
277 fprintf (s, _(" [S] - do not build a symbol table\n"));
278 fprintf (s, _(" [v] - be verbose\n"));
279 fprintf (s, _(" [V] - display the version number\n"));
280 fprintf (s, _(" [-X32_64] - (ignored)\n"));
283 /* xgettext:c-format */
284 fprintf (s, _("Usage: %s [-vV] archive\n"), program_name);
286 list_supported_targets (program_name, stderr);
289 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
291 xexit (help ? 0 : 1);
294 /* Normalize a file name specified on the command line into a file
295 name which we will use in an archive. */
298 normalize (file, abfd)
302 const char *filename;
307 filename = strrchr (file, '/');
308 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
310 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
311 char *bslash = strrchr (file, '\\');
312 if (filename == NULL || (bslash != NULL && bslash > filename))
314 if (filename == NULL && file[0] != '\0' && file[1] == ':')
318 if (filename != (char *) NULL)
325 && strlen (filename) > abfd->xvec->ar_max_namelen)
330 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
331 memcpy (s, filename, abfd->xvec->ar_max_namelen);
332 s[abfd->xvec->ar_max_namelen] = '\0';
339 /* Remove any output file. This is only called via xatexit. */
341 static const char *output_filename = NULL;
342 static FILE *output_file = NULL;
343 static bfd *output_bfd = NULL;
348 if (output_filename != NULL)
350 if (output_bfd != NULL && output_bfd->iostream != NULL)
351 fclose ((FILE *) (output_bfd->iostream));
352 if (output_file != NULL)
353 fclose (output_file);
354 unlink (output_filename);
358 /* The option parsing should be in its own function.
359 It will be when I have getopt working. */
370 none = 0, delete, replace, print_table,
371 print_files, extract, move, quick_append
376 char *inarch_filename;
379 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
380 setlocale (LC_MESSAGES, "");
382 bindtextdomain (PACKAGE, LOCALEDIR);
383 textdomain (PACKAGE);
385 program_name = argv[0];
386 xmalloc_set_program_name (program_name);
392 temp = strrchr (program_name, '/');
393 #ifdef HAVE_DOS_BASED_FILE_SYSTEM
395 /* We could have foo/bar\\baz, or foo\\bar, or d:bar. */
396 char *bslash = strrchr (program_name, '\\');
397 if (temp == NULL || (bslash != NULL && bslash > temp))
399 if (temp == NULL && program_name[0] != '\0' && program_name[1] == ':')
400 temp = program_name + 1;
407 if (strlen (temp) >= 6
408 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
414 if (argc > 1 && argv[1][0] == '-')
416 if (strcmp (argv[1], "--help") == 0)
418 else if (strcmp (argv[1], "--version") == 0)
421 print_version ("ranlib");
423 print_version ("ar");
427 START_PROGRESS (program_name, 0);
430 set_default_bfd_target ();
434 xatexit (remove_output);
436 /* Ignored for (partial) AIX compatibility. On AIX,
437 the -X option can be used to ignore certain kinds
438 of object files in the archive (the 64-bit objects
439 or the 32-bit objects). GNU ar always looks at all
440 kinds of objects in an archive. */
441 while (argc > 1 && strcmp (argv[1], "-X32_64") == 0)
449 boolean touch = false;
451 if (argc < 2 || strcmp (argv[1], "--help") == 0)
453 if (strcmp (argv[1], "-V") == 0
454 || strcmp (argv[1], "-v") == 0
455 || strncmp (argv[1], "--v", 3) == 0)
456 print_version ("ranlib");
458 if (strcmp (argv[1], "-t") == 0)
463 while (arg_index < argc)
466 ranlib_only (argv[arg_index]);
468 ranlib_touch (argv[arg_index]);
474 if (argc == 2 && strcmp (argv[1], "-M") == 0)
486 ++arg_ptr; /* compatibility */
488 while ((c = *arg_ptr++) != '\0')
499 if (operation != none)
500 fatal (_("two different operation options specified"));
505 operation_alters_arch = true;
509 operation_alters_arch = true;
512 operation = print_files;
515 operation = quick_append;
516 operation_alters_arch = true;
520 operation_alters_arch = true;
523 operation = print_table;
556 postype = pos_before;
559 postype = pos_before;
565 counted_name_mode = true;
571 full_pathname = true;
574 /* xgettext:c-format */
575 non_fatal (_("illegal option -- %c"), c);
581 print_version ("ar");
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)
602 ranlib_only (argv[2]);
606 if (operation == none)
607 fatal (_("no operation specified"));
609 if (newer_only && operation != replace)
610 fatal (_("`u' is only meaningful with the `r' option."));
614 if (postype != pos_default)
615 posname = argv[arg_index++];
617 if (counted_name_mode)
619 if (operation != extract && operation != delete)
620 fatal (_("`N' is only meaningful with the `x' and `d' options."));
621 counted_name_counter = atoi (argv[arg_index++]);
622 if (counted_name_counter <= 0)
623 fatal (_("Value for `N' must be positive."));
626 inarch_filename = argv[arg_index++];
628 files = arg_index < argc ? argv + arg_index : NULL;
629 file_count = argc - arg_index;
632 /* We don't use do_quick_append any more. Too many systems
633 expect ar to always rebuild the symbol table even when q is
636 /* We can't do a quick append if we need to construct an
637 extended name table, because do_quick_append won't be able to
638 rebuild the name table. Unfortunately, at this point we
639 don't actually know the maximum name length permitted by this
640 object file format. So, we guess. FIXME. */
641 if (operation == quick_append && ! ar_truncate)
645 for (chk = files; chk != NULL && *chk != '\0'; chk++)
647 if (strlen (normalize (*chk, (bfd *) NULL)) > 14)
655 if (operation == quick_append)
657 /* Note that quick appending to a non-existent archive creates it,
658 even if there are no files to append. */
659 do_quick_append (inarch_filename, files);
664 arch = open_inarch (inarch_filename,
665 files == NULL ? (char *) NULL : files[0]);
670 map_over_members (arch, print_descr, files, file_count);
674 map_over_members (arch, print_contents, files, file_count);
678 map_over_members (arch, extract_file, files, file_count);
683 delete_members (arch, files);
685 output_filename = NULL;
690 move_members (arch, files);
692 output_filename = NULL;
697 if (files != NULL || write_armap > 0)
698 replace_members (arch, files, operation == quick_append);
700 output_filename = NULL;
703 /* Shouldn't happen! */
705 /* xgettext:c-format */
706 fatal (_("internal error -- this option not implemented"));
710 END_PROGRESS (program_name);
717 open_inarch (archive_filename, file)
718 const char *archive_filename;
728 bfd_set_error (bfd_error_no_error);
732 if (stat (archive_filename, &sbuf) != 0)
734 #if !defined(__GO32__) || defined(__DJGPP__)
736 /* FIXME: I don't understand why this fragment was ifndef'ed
737 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
738 stat() works just fine in v2.x, so I think this should be
739 removed. For now, I enable it for DJGPP v2. -- EZ. */
741 /* KLUDGE ALERT! Temporary fix until I figger why
742 stat() is wrong ... think it's buried in GO32's IDT - Jax */
744 bfd_fatal (archive_filename);
747 if (!operation_alters_arch)
749 fprintf (stderr, "%s: ", program_name);
750 perror (archive_filename);
755 /* Try to figure out the target to use for the archive from the
756 first object on the list. */
761 obj = bfd_openr (file, NULL);
764 if (bfd_check_format (obj, bfd_object))
765 target = bfd_get_target (obj);
766 (void) bfd_close (obj);
770 /* Create an empty archive. */
771 arch = bfd_openw (archive_filename, target);
773 || ! bfd_set_format (arch, bfd_archive)
774 || ! bfd_close (arch))
775 bfd_fatal (archive_filename);
777 /* If we die creating a new archive, don't leave it around. */
778 output_filename = archive_filename;
781 arch = bfd_openr (archive_filename, target);
785 bfd_fatal (archive_filename);
788 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
790 bfd_nonfatal (archive_filename);
791 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
793 list_matching_formats (matching);
799 last_one = &(arch->next);
800 /* Read all the contents right away, regardless. */
801 for (next_one = bfd_openr_next_archived_file (arch, NULL);
803 next_one = bfd_openr_next_archived_file (arch, next_one))
806 *last_one = next_one;
807 last_one = &next_one->next;
809 *last_one = (bfd *) NULL;
810 if (bfd_get_error () != bfd_error_no_more_archived_files)
816 print_contents (abfd)
820 char *cbuf = xmalloc (BUFSIZE);
823 if (bfd_stat_arch_elt (abfd, &buf) != 0)
824 /* xgettext:c-format */
825 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
828 /* xgettext:c-format */
829 printf (_("\n<member %s>\n\n"), bfd_get_filename (abfd));
831 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
834 while (ncopied < size)
838 int tocopy = size - ncopied;
839 if (tocopy > BUFSIZE)
842 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
844 /* xgettext:c-format */
845 fatal (_("%s is not a valid archive"),
846 bfd_get_filename (bfd_my_archive (abfd)));
847 fwrite (cbuf, 1, nread, stdout);
853 /* Extract a member of the archive into its own file.
855 We defer opening the new file until after we have read a BUFSIZ chunk of the
856 old one, since we know we have just read the archive header for the old
857 one. Since most members are shorter than BUFSIZ, this means we will read
858 the old header, read the old data, write a new inode for the new file, and
859 write the new data, and be done. This 'optimization' is what comes from
860 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
868 char *cbuf = xmalloc (BUFSIZE);
874 if (bfd_stat_arch_elt (abfd, &buf) != 0)
875 /* xgettext:c-format */
876 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
880 /* xgettext:c-format */
881 fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd));
884 printf ("x - %s\n", bfd_get_filename (abfd));
886 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
891 /* Seems like an abstraction violation, eh? Well it's OK! */
892 output_filename = bfd_get_filename (abfd);
894 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
897 perror (bfd_get_filename (abfd));
901 output_file = ostream;
904 while (ncopied < size)
906 tocopy = size - ncopied;
907 if (tocopy > BUFSIZE)
910 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
912 /* xgettext:c-format */
913 fatal (_("%s is not a valid archive"),
914 bfd_get_filename (bfd_my_archive (abfd)));
916 /* See comment above; this saves disk arm motion */
919 /* Seems like an abstraction violation, eh? Well it's OK! */
920 output_filename = bfd_get_filename (abfd);
922 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
925 perror (bfd_get_filename (abfd));
929 output_file = ostream;
931 fwrite (cbuf, 1, nread, ostream);
939 output_filename = NULL;
941 chmod (bfd_get_filename (abfd), buf.st_mode);
944 set_times (bfd_get_filename (abfd), &buf);
951 /* We don't use this anymore. Too many systems expect ar to rebuild
952 the symbol table even when q is used. */
954 /* Just do it quickly; don't worry about dups, armap, or anything like that */
957 do_quick_append (archive_filename, files_to_append)
958 const char *archive_filename;
959 char **files_to_append;
962 char *buf = xmalloc (BUFSIZE);
963 long tocopy, thistime;
966 boolean newfile = false;
967 bfd_set_error (bfd_error_no_error);
969 if (stat (archive_filename, &sbuf) != 0)
972 #if !defined(__GO32__) || defined(__DJGPP__)
974 /* FIXME: I don't understand why this fragment was ifndef'ed
975 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
976 stat() works just fine in v2.x, so I think this should be
977 removed. For now, I enable it for DJGPP v2.
979 (And yes, I know this is all unused, but somebody, someday,
980 might wish to resurrect this again... -- EZ. */
982 /* KLUDGE ALERT! Temporary fix until I figger why
983 stat() is wrong ... think it's buried in GO32's IDT - Jax */
986 bfd_fatal (archive_filename);
992 ofile = fopen (archive_filename, FOPEN_AUB);
995 perror (program_name);
999 temp = bfd_openr (archive_filename, NULL);
1002 bfd_fatal (archive_filename);
1004 if (newfile == false)
1006 if (bfd_check_format (temp, bfd_archive) != true)
1007 /* xgettext:c-format */
1008 fatal (_("%s is not an archive"), archive_filename);
1012 fwrite (ARMAG, 1, SARMAG, ofile);
1014 /* xgettext:c-format */
1015 non_fatal (_("creating %s"), archive_filename);
1019 temp->flags |= BFD_TRADITIONAL_FORMAT;
1021 /* assume it's an achive, go straight to the end, sans $200 */
1022 fseek (ofile, 0, 2);
1024 for (; files_to_append && *files_to_append; ++files_to_append)
1026 struct ar_hdr *hdr = bfd_special_undocumented_glue (temp, *files_to_append);
1029 bfd_fatal (*files_to_append);
1032 BFD_SEND (temp, _bfd_truncate_arname, (temp, *files_to_append, (char *) hdr));
1034 ifile = fopen (*files_to_append, FOPEN_RB);
1037 bfd_nonfatal (*files_to_append);
1040 if (stat (*files_to_append, &sbuf) != 0)
1042 bfd_nonfatal (*files_to_append);
1045 tocopy = sbuf.st_size;
1047 /* XXX should do error-checking! */
1048 fwrite (hdr, 1, sizeof (struct ar_hdr), ofile);
1053 if (thistime > BUFSIZE)
1055 fread (buf, 1, thistime, ifile);
1056 fwrite (buf, 1, thistime, ofile);
1060 if ((sbuf.st_size % 2) == 1)
1061 putc ('\012', ofile);
1071 write_archive (iarch)
1075 char *old_name, *new_name;
1076 bfd *contents_head = iarch->next;
1078 old_name = xmalloc (strlen (bfd_get_filename (iarch)) + 1);
1079 strcpy (old_name, bfd_get_filename (iarch));
1080 new_name = make_tempname (old_name);
1082 output_filename = new_name;
1084 obfd = bfd_openw (new_name, bfd_get_target (iarch));
1087 bfd_fatal (old_name);
1091 bfd_set_format (obfd, bfd_archive);
1093 /* Request writing the archive symbol table unless we've
1094 been explicitly requested not to. */
1095 obfd->has_armap = write_armap >= 0;
1099 /* This should really use bfd_set_file_flags, but that rejects
1101 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1104 if (bfd_set_archive_head (obfd, contents_head) != true)
1105 bfd_fatal (old_name);
1107 if (!bfd_close (obfd))
1108 bfd_fatal (old_name);
1111 output_filename = NULL;
1113 /* We don't care if this fails; we might be creating the archive. */
1116 if (smart_rename (new_name, old_name, 0) != 0)
1120 /* Return a pointer to the pointer to the entry which should be rplacd'd
1121 into when altering. DEFAULT_POS should be how to interpret pos_default,
1122 and should be a pos value. */
1125 get_pos_bfd (contents, default_pos, default_posname)
1127 enum pos default_pos;
1128 const char *default_posname;
1130 bfd **after_bfd = contents;
1132 const char *realposname;
1134 if (postype == pos_default)
1136 realpos = default_pos;
1137 realposname = default_posname;
1142 realposname = posname;
1145 if (realpos == pos_end)
1148 after_bfd = &((*after_bfd)->next);
1152 for (; *after_bfd; after_bfd = &(*after_bfd)->next)
1153 if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1155 if (realpos == pos_after)
1156 after_bfd = &(*after_bfd)->next;
1164 delete_members (arch, files_to_delete)
1166 char **files_to_delete;
1168 bfd **current_ptr_ptr;
1170 boolean something_changed = false;
1173 for (; *files_to_delete != NULL; ++files_to_delete)
1175 /* In a.out systems, the armap is optional. It's also called
1176 __.SYMDEF. So if the user asked to delete it, we should remember
1177 that fact. This isn't quite right for COFF systems (where
1178 __.SYMDEF might be regular member), but it's very unlikely
1179 to be a problem. FIXME */
1181 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1183 arch->has_armap = false;
1190 current_ptr_ptr = &(arch->next);
1191 while (*current_ptr_ptr)
1193 if (FILENAME_CMP (normalize (*files_to_delete, arch),
1194 (*current_ptr_ptr)->filename) == 0)
1197 if (counted_name_mode
1198 && match_count != counted_name_counter)
1200 /* Counting, and didn't match on count; go on to the
1206 something_changed = true;
1210 *current_ptr_ptr = ((*current_ptr_ptr)->next);
1215 current_ptr_ptr = &((*current_ptr_ptr)->next);
1218 if (verbose && found == false)
1220 /* xgettext:c-format */
1221 printf (_("No member named `%s'\n"), *files_to_delete);
1227 if (something_changed == true)
1228 write_archive (arch);
1230 output_filename = NULL;
1234 /* Reposition existing members within an archive */
1237 move_members (arch, files_to_move)
1239 char **files_to_move;
1241 bfd **after_bfd; /* New entries go after this one */
1242 bfd **current_ptr_ptr; /* cdr pointer into contents */
1244 for (; *files_to_move; ++files_to_move)
1246 current_ptr_ptr = &(arch->next);
1247 while (*current_ptr_ptr)
1249 bfd *current_ptr = *current_ptr_ptr;
1250 if (FILENAME_CMP (normalize (*files_to_move, arch),
1251 current_ptr->filename) == 0)
1253 /* Move this file to the end of the list - first cut from
1256 *current_ptr_ptr = current_ptr->next;
1258 /* Now glue to end */
1259 after_bfd = get_pos_bfd (&arch->next, pos_end, NULL);
1261 *after_bfd = current_ptr;
1262 current_ptr->next = link;
1265 printf ("m - %s\n", *files_to_move);
1270 current_ptr_ptr = &((*current_ptr_ptr)->next);
1272 /* xgettext:c-format */
1273 fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1278 write_archive (arch);
1281 /* Ought to default to replacing in place, but this is existing practice! */
1284 replace_members (arch, files_to_move, quick)
1286 char **files_to_move;
1289 boolean changed = false;
1290 bfd **after_bfd; /* New entries go after this one */
1295 while (files_to_move && *files_to_move)
1299 current_ptr = &arch->next;
1300 while (*current_ptr)
1302 current = *current_ptr;
1304 /* For compatibility with existing ar programs, we
1305 permit the same file to be added multiple times. */
1306 if (FILENAME_CMP (normalize (*files_to_move, arch),
1307 normalize (current->filename, arch)) == 0
1308 && current->arelt_data != NULL)
1312 struct stat fsbuf, asbuf;
1314 if (stat (*files_to_move, &fsbuf) != 0)
1316 if (errno != ENOENT)
1317 bfd_fatal (*files_to_move);
1320 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1321 /* xgettext:c-format */
1322 fatal (_("internal stat error on %s"), current->filename);
1324 if (fsbuf.st_mtime <= asbuf.st_mtime)
1328 after_bfd = get_pos_bfd (&arch->next, pos_after,
1332 *after_bfd = bfd_openr (*files_to_move, NULL);
1333 if (*after_bfd == (bfd *) NULL)
1335 bfd_fatal (*files_to_move);
1337 (*after_bfd)->next = temp;
1339 /* snip out this entry from the chain */
1340 *current_ptr = (*current_ptr)->next;
1344 printf ("r - %s\n", *files_to_move);
1351 current_ptr = &(current->next);
1355 /* Add to the end of the archive. */
1357 after_bfd = get_pos_bfd (&arch->next, pos_end, NULL);
1359 *after_bfd = bfd_openr (*files_to_move, NULL);
1360 if (*after_bfd == (bfd *) NULL)
1362 bfd_fatal (*files_to_move);
1366 printf ("a - %s\n", *files_to_move);
1369 (*after_bfd)->next = temp;
1379 write_archive (arch);
1381 output_filename = NULL;
1385 ranlib_only (archname)
1386 const char *archname;
1391 arch = open_inarch (archname, (char *) NULL);
1394 write_archive (arch);
1397 /* Update the timestamp of the symbol map of an archive. */
1400 ranlib_touch (archname)
1401 const char *archname;
1404 /* I don't think updating works on go32. */
1405 ranlib_only (archname);
1411 f = open (archname, O_RDWR | O_BINARY, 0);
1414 bfd_set_error (bfd_error_system_call);
1415 bfd_fatal (archname);
1418 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1420 bfd_fatal (archname);
1421 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1423 bfd_nonfatal (archname);
1424 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1426 list_matching_formats (matching);
1432 if (! bfd_has_map (arch))
1433 /* xgettext:c-format */
1434 fatal (_("%s: no archive map to update"), archname);
1436 bfd_update_armap_timestamp (arch);
1438 if (! bfd_close (arch))
1439 bfd_fatal (archname);
1443 /* Things which are interesting to map over all or some of the files: */
1449 print_arelt_descr (stdout, abfd, verbose);