1 /* arsup.c - Archive support for MRI compatibility
2 Copyright 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2007, 2008 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* Contributed by Steve Chamberlain
26 This file looks after requests from arparse.y, to provide the MRI
27 style librarian command syntax + 1 word LIST. */
31 #include "libiberty.h"
32 #include "filenames.h"
36 static void map_over_list
37 (bfd *, void (*function) (bfd *, bfd *), struct list *);
38 static void ar_directory_doer (bfd *, bfd *);
39 static void ar_addlib_doer (bfd *, bfd *);
44 static char *real_name;
48 map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
56 head = arch->archive_next;
59 next = head->archive_next;
60 function (head, (bfd *) NULL);
68 /* This may appear to be a baroque way of accomplishing what we
69 want. however we have to iterate over the filenames in order
70 to notice where a filename is requested but does not exist in
71 the archive. Ditto mapping over each file each time -- we
72 want to hack multiple references. */
73 for (ptr = list; ptr; ptr = ptr->next)
75 bfd_boolean found = FALSE;
78 for (head = arch->archive_next; head; head = head->archive_next)
80 if (head->filename != NULL
81 && FILENAME_CMP (ptr->name, head->filename) == 0)
84 function (head, prev);
89 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
97 ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
99 print_arelt_descr(outfile, abfd, verbose);
103 ar_directory (char *ar_name, struct list *list, char *output)
107 arch = open_inarch (ar_name, (char *) NULL);
110 outfile = fopen(output,"w");
114 fprintf (stderr,_("Can't open file %s\n"), output);
121 map_over_list (arch, ar_directory_doer, list);
132 extern int interactive;
150 ar_open (char *name, int t)
152 char *tname = (char *) xmalloc (strlen (name) + 10);
153 const char *bname = lbasename (name);
156 /* Prepend tmp- to the beginning, to avoid file-name clashes after
157 truncation on filesystems with limited namespaces (DOS). */
158 sprintf (tname, "%.*stmp-%s", (int) (bname - name), name, bname);
159 obfd = bfd_openw (tname, NULL);
164 _("%s: Can't open output archive %s\n"),
165 program_name, tname);
177 ibfd = bfd_openr (name, NULL);
181 fprintf (stderr,_("%s: Can't open input archive %s\n"),
187 if (!bfd_check_format(ibfd, bfd_archive))
190 _("%s: file %s is not an archive\n"),
196 ptr = &(obfd->archive_head);
197 element = bfd_openr_next_archived_file (ibfd, NULL);
202 ptr = &element->archive_next;
203 element = bfd_openr_next_archived_file (ibfd, element);
207 bfd_set_format (obfd, bfd_archive);
210 obfd->is_thin_archive = 0;
215 ar_addlib_doer (bfd *abfd, bfd *prev)
217 /* Add this module to the output bfd. */
219 prev->archive_next = abfd->archive_next;
221 abfd->archive_next = obfd->archive_head;
222 obfd->archive_head = abfd;
226 ar_addlib (char *name, struct list *list)
230 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
237 arch = open_inarch (name, (char *) NULL);
239 map_over_list (arch, ar_addlib_doer, list);
241 /* Don't close the bfd, since it will make the elements disappear. */
246 ar_addmod (struct list *list)
250 fprintf (stderr, _("%s: no open output archive\n"), program_name);
257 bfd *abfd = bfd_openr (list->name, NULL);
261 fprintf (stderr, _("%s: can't open file %s\n"),
262 program_name, list->name);
267 abfd->archive_next = obfd->archive_head;
268 obfd->archive_head = abfd;
280 obfd->archive_head = 0;
284 ar_delete (struct list *list)
288 fprintf (stderr, _("%s: no open output archive\n"), program_name);
295 /* Find this name in the archive. */
296 bfd *member = obfd->archive_head;
297 bfd **prev = &(obfd->archive_head);
302 if (FILENAME_CMP(member->filename, list->name) == 0)
304 *prev = member->archive_next;
308 prev = &(member->archive_next);
310 member = member->archive_next;
315 fprintf (stderr, _("%s: can't find module file %s\n"),
316 program_name, list->name);
330 fprintf (stderr, _("%s: no open output archive\n"), program_name);
335 char *ofilename = xstrdup (bfd_get_filename (obfd));
339 smart_rename (ofilename, real_name, 0);
346 ar_replace (struct list *list)
350 fprintf (stderr, _("%s: no open output archive\n"), program_name);
357 /* Find this name in the archive. */
358 bfd *member = obfd->archive_head;
359 bfd **prev = &(obfd->archive_head);
364 if (FILENAME_CMP (member->filename, list->name) == 0)
366 /* Found the one to replace. */
367 bfd *abfd = bfd_openr (list->name, 0);
371 fprintf (stderr, _("%s: can't open file %s\n"),
372 program_name, list->name);
378 abfd->archive_next = member->archive_next;
384 prev = &(member->archive_next);
386 member = member->archive_next;
391 bfd *abfd = bfd_openr (list->name, 0);
393 fprintf (stderr,_("%s: can't find module file %s\n"),
394 program_name, list->name);
397 fprintf (stderr, _("%s: can't open file %s\n"),
398 program_name, list->name);
410 /* And I added this one. */
416 fprintf (stderr, _("%s: no open output archive\n"), program_name);
425 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
427 for (abfd = obfd->archive_head;
429 abfd = abfd->archive_next)
430 ar_directory_doer (abfd, (bfd *) NULL);
439 bfd_cache_close (obfd);
440 unlink (bfd_get_filename (obfd));
445 ar_extract (struct list *list)
449 fprintf (stderr, _("%s: no open archive\n"), program_name);
456 /* Find this name in the archive. */
457 bfd *member = obfd->archive_head;
460 while (member && !found)
462 if (FILENAME_CMP (member->filename, list->name) == 0)
464 extract_file (member);
468 member = member->archive_next;
473 bfd_openr (list->name, 0);
474 fprintf (stderr, _("%s: can't find module file %s\n"),
475 program_name, list->name);