1 /* arsup.c - Archive support for MRI compatibility
2 Copyright 1992, 1994, 1995, 1996, 1997, 2000, 2002, 2003
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 /* Contributed by Steve Chamberlain
25 This file looks after requests from arparse.y, to provide the MRI
26 style librarian command syntax + 1 word LIST. */
30 #include "libiberty.h"
32 #include "filenames.h"
34 static void map_over_list
35 (bfd *, void (*function) (bfd *, bfd *), struct list *);
36 static void ar_directory_doer (bfd *, bfd *);
37 static void ar_addlib_doer (bfd *, bfd *);
42 map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
54 function (head, (bfd *) NULL);
62 /* This may appear to be a baroque way of accomplishing what we
63 want. however we have to iterate over the filenames in order
64 to notice where a filename is requested but does not exist in
65 the archive. Ditto mapping over each file each time -- we
66 want to hack multiple references. */
67 for (ptr = list; ptr; ptr = ptr->next)
69 bfd_boolean found = FALSE;
72 for (head = arch->next; head; head = head->next)
74 if (head->filename != NULL
75 && FILENAME_CMP (ptr->name, head->filename) == 0)
78 function (head, prev);
83 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
92 ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
94 print_arelt_descr(outfile, abfd, verbose);
98 ar_directory (char *ar_name, struct list *list, char *output)
102 arch = open_inarch (ar_name, (char *) NULL);
105 outfile = fopen(output,"w");
109 fprintf (stderr,_("Can't open file %s\n"), output);
116 map_over_list (arch, ar_directory_doer, list);
127 extern int interactive;
148 ar_open (char *name, int t)
150 char *tname = (char *) xmalloc (strlen (name) + 10);
151 const char *bname = lbasename (name);
154 /* Prepend tmp- to the beginning, to avoid file-name clashes after
155 truncation on filesystems with limited namespaces (DOS). */
156 sprintf (tname, "%.*stmp-%s", (int) (bname - name), name, bname);
157 obfd = bfd_openw (tname, NULL);
162 _("%s: Can't open output archive %s\n"),
163 program_name, tname);
175 ibfd = bfd_openr (name, NULL);
179 fprintf (stderr,_("%s: Can't open input archive %s\n"),
185 if (!bfd_check_format(ibfd, bfd_archive))
188 _("%s: file %s is not an archive\n"),
194 ptr = &(obfd->archive_head);
195 element = bfd_openr_next_archived_file (ibfd, NULL);
200 ptr = &element->next;
201 element = bfd_openr_next_archived_file (ibfd, element);
205 bfd_set_format (obfd, bfd_archive);
212 ar_addlib_doer (bfd *abfd, bfd *prev)
214 /* Add this module to the output bfd. */
216 prev->next = abfd->next;
218 abfd->next = obfd->archive_head;
219 obfd->archive_head = abfd;
223 ar_addlib (char *name, struct list *list)
227 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
234 arch = open_inarch (name, (char *) NULL);
236 map_over_list (arch, ar_addlib_doer, list);
238 /* Don't close the bfd, since it will make the elements disasppear. */
243 ar_addmod (struct list *list)
247 fprintf (stderr, _("%s: no open output archive\n"), program_name);
254 bfd *abfd = bfd_openr (list->name, NULL);
258 fprintf (stderr, _("%s: can't open file %s\n"),
259 program_name, list->name);
264 abfd->next = obfd->archive_head;
265 obfd->archive_head = abfd;
277 obfd->archive_head = 0;
281 ar_delete (struct list *list)
285 fprintf (stderr, _("%s: no open output archive\n"), program_name);
292 /* Find this name in the archive. */
293 bfd *member = obfd->archive_head;
294 bfd **prev = &(obfd->archive_head);
299 if (FILENAME_CMP(member->filename, list->name) == 0)
301 *prev = member->next;
305 prev = &(member->next);
307 member = member->next;
312 fprintf (stderr, _("%s: can't find module file %s\n"),
313 program_name, list->name);
327 fprintf (stderr, _("%s: no open output archive\n"), program_name);
332 char *ofilename = xstrdup (bfd_get_filename (obfd));
336 rename (ofilename, real_name);
343 ar_replace (struct list *list)
347 fprintf (stderr, _("%s: no open output archive\n"), program_name);
354 /* Find this name in the archive. */
355 bfd *member = obfd->archive_head;
356 bfd **prev = &(obfd->archive_head);
361 if (FILENAME_CMP (member->filename, list->name) == 0)
363 /* Found the one to replace. */
364 bfd *abfd = bfd_openr (list->name, 0);
368 fprintf (stderr, _("%s: can't open file %s\n"),
369 program_name, list->name);
375 abfd->next = member->next;
381 prev = &(member->next);
383 member = member->next;
388 bfd *abfd = bfd_openr (list->name, 0);
390 fprintf (stderr,_("%s: can't find module file %s\n"),
391 program_name, list->name);
394 fprintf (stderr, _("%s: can't open file %s\n"),
395 program_name, list->name);
407 /* And I added this one. */
413 fprintf (stderr, _("%s: no open output archive\n"), program_name);
422 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
424 for (abfd = obfd->archive_head;
427 ar_directory_doer (abfd, (bfd *) NULL);
436 fclose ((FILE *)(obfd->iostream));
437 unlink (bfd_get_filename (obfd));
442 ar_extract (struct list *list)
446 fprintf (stderr, _("%s: no open archive\n"), program_name);
453 /* Find this name in the archive. */
454 bfd *member = obfd->archive_head;
457 while (member && !found)
459 if (FILENAME_CMP (member->filename, list->name) == 0)
461 extract_file (member);
465 member = member->next;
470 bfd_openr (list->name, 0);
471 fprintf (stderr, _("%s: can't find module file %s\n"),
472 program_name, list->name);