1 /* arsup.c - Archive support for MRI compatibility
2 Copyright 1992, 1994, 1995, 1996, 1997, 1999, 2000, 2001, 2002, 2003,
3 2004, 2007 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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, 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"
31 #include "filenames.h"
35 static void map_over_list
36 (bfd *, void (*function) (bfd *, bfd *), struct list *);
37 static void ar_directory_doer (bfd *, bfd *);
38 static void ar_addlib_doer (bfd *, bfd *);
43 static char *real_name;
47 map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
55 head = arch->archive_next;
58 next = head->archive_next;
59 function (head, (bfd *) NULL);
67 /* This may appear to be a baroque way of accomplishing what we
68 want. however we have to iterate over the filenames in order
69 to notice where a filename is requested but does not exist in
70 the archive. Ditto mapping over each file each time -- we
71 want to hack multiple references. */
72 for (ptr = list; ptr; ptr = ptr->next)
74 bfd_boolean found = FALSE;
77 for (head = arch->archive_next; head; head = head->archive_next)
79 if (head->filename != NULL
80 && FILENAME_CMP (ptr->name, head->filename) == 0)
83 function (head, prev);
88 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
96 ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
98 print_arelt_descr(outfile, abfd, verbose);
102 ar_directory (char *ar_name, struct list *list, char *output)
106 arch = open_inarch (ar_name, (char *) NULL);
109 outfile = fopen(output,"w");
113 fprintf (stderr,_("Can't open file %s\n"), output);
120 map_over_list (arch, ar_directory_doer, list);
131 extern int interactive;
149 ar_open (char *name, int t)
151 char *tname = (char *) xmalloc (strlen (name) + 10);
152 const char *bname = lbasename (name);
155 /* Prepend tmp- to the beginning, to avoid file-name clashes after
156 truncation on filesystems with limited namespaces (DOS). */
157 sprintf (tname, "%.*stmp-%s", (int) (bname - name), name, bname);
158 obfd = bfd_openw (tname, NULL);
163 _("%s: Can't open output archive %s\n"),
164 program_name, tname);
176 ibfd = bfd_openr (name, NULL);
180 fprintf (stderr,_("%s: Can't open input archive %s\n"),
186 if (!bfd_check_format(ibfd, bfd_archive))
189 _("%s: file %s is not an archive\n"),
195 ptr = &(obfd->archive_head);
196 element = bfd_openr_next_archived_file (ibfd, NULL);
201 ptr = &element->archive_next;
202 element = bfd_openr_next_archived_file (ibfd, element);
206 bfd_set_format (obfd, bfd_archive);
213 ar_addlib_doer (bfd *abfd, bfd *prev)
215 /* Add this module to the output bfd. */
217 prev->archive_next = abfd->archive_next;
219 abfd->archive_next = obfd->archive_head;
220 obfd->archive_head = abfd;
224 ar_addlib (char *name, struct list *list)
228 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
235 arch = open_inarch (name, (char *) NULL);
237 map_over_list (arch, ar_addlib_doer, list);
239 /* Don't close the bfd, since it will make the elements disappear. */
244 ar_addmod (struct list *list)
248 fprintf (stderr, _("%s: no open output archive\n"), program_name);
255 bfd *abfd = bfd_openr (list->name, NULL);
259 fprintf (stderr, _("%s: can't open file %s\n"),
260 program_name, list->name);
265 abfd->archive_next = obfd->archive_head;
266 obfd->archive_head = abfd;
278 obfd->archive_head = 0;
282 ar_delete (struct list *list)
286 fprintf (stderr, _("%s: no open output archive\n"), program_name);
293 /* Find this name in the archive. */
294 bfd *member = obfd->archive_head;
295 bfd **prev = &(obfd->archive_head);
300 if (FILENAME_CMP(member->filename, list->name) == 0)
302 *prev = member->archive_next;
306 prev = &(member->archive_next);
308 member = member->archive_next;
313 fprintf (stderr, _("%s: can't find module file %s\n"),
314 program_name, list->name);
328 fprintf (stderr, _("%s: no open output archive\n"), program_name);
333 char *ofilename = xstrdup (bfd_get_filename (obfd));
337 smart_rename (ofilename, real_name, 0);
344 ar_replace (struct list *list)
348 fprintf (stderr, _("%s: no open output archive\n"), program_name);
355 /* Find this name in the archive. */
356 bfd *member = obfd->archive_head;
357 bfd **prev = &(obfd->archive_head);
362 if (FILENAME_CMP (member->filename, list->name) == 0)
364 /* Found the one to replace. */
365 bfd *abfd = bfd_openr (list->name, 0);
369 fprintf (stderr, _("%s: can't open file %s\n"),
370 program_name, list->name);
376 abfd->archive_next = member->archive_next;
382 prev = &(member->archive_next);
384 member = member->archive_next;
389 bfd *abfd = bfd_openr (list->name, 0);
391 fprintf (stderr,_("%s: can't find module file %s\n"),
392 program_name, list->name);
395 fprintf (stderr, _("%s: can't open file %s\n"),
396 program_name, list->name);
408 /* And I added this one. */
414 fprintf (stderr, _("%s: no open output archive\n"), program_name);
423 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
425 for (abfd = obfd->archive_head;
427 abfd = abfd->archive_next)
428 ar_directory_doer (abfd, (bfd *) NULL);
437 bfd_cache_close (obfd);
438 unlink (bfd_get_filename (obfd));
443 ar_extract (struct list *list)
447 fprintf (stderr, _("%s: no open archive\n"), program_name);
454 /* Find this name in the archive. */
455 bfd *member = obfd->archive_head;
458 while (member && !found)
460 if (FILENAME_CMP (member->filename, list->name) == 0)
462 extract_file (member);
466 member = member->archive_next;
471 bfd_openr (list->name, 0);
472 fprintf (stderr, _("%s: can't find module file %s\n"),
473 program_name, list->name);