1 /* arsup.c - Archive support for MRI compatibility
2 Copyright 1992, 1994, 1995, 1996, 1997, 2000, 2002
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 PARAMS ((bfd
*, void (*function
) (bfd
*, bfd
*), struct list
*));
36 static void ar_directory_doer
PARAMS ((bfd
*, bfd
*));
37 static void ar_addlib_doer
PARAMS ((bfd
*, bfd
*));
42 map_over_list (arch
, function
, list
)
44 void (*function
) PARAMS ((bfd
*, bfd
*));
57 function (head
, (bfd
*) NULL
);
65 /* This may appear to be a baroque way of accomplishing what we
66 want. however we have to iterate over the filenames in order
67 to notice where a filename is requested but does not exist in
68 the archive. Ditto mapping over each file each time -- we
69 want to hack multiple references. */
70 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
72 boolean found
= false;
75 for (head
= arch
->next
; head
; head
= head
->next
)
77 if (head
->filename
!= NULL
78 && FILENAME_CMP (ptr
->name
, head
->filename
) == 0)
81 function (head
, prev
);
86 fprintf (stderr
, _("No entry %s in archive.\n"), ptr
->name
);
95 ar_directory_doer (abfd
, ignore
)
97 bfd
*ignore ATTRIBUTE_UNUSED
;
99 print_arelt_descr(outfile
, abfd
, verbose
);
103 ar_directory (ar_name
, list
, output
)
110 arch
= open_inarch (ar_name
, (char *) NULL
);
113 outfile
= fopen(output
,"w");
117 fprintf (stderr
,_("Can't open file %s\n"), output
);
124 map_over_list (arch
, ar_directory_doer
, list
);
135 extern int interactive
;
160 char *tname
= (char *) xmalloc (strlen (name
) + 10);
161 const char *bname
= lbasename (name
);
164 /* Prepend tmp- to the beginning, to avoid file-name clashes after
165 truncation on filesystems with limited namespaces (DOS). */
166 sprintf (tname
, "%.*stmp-%s", (int) (bname
- name
), name
, bname
);
167 obfd
= bfd_openw (tname
, NULL
);
172 _("%s: Can't open output archive %s\n"),
173 program_name
, tname
);
185 ibfd
= bfd_openr (name
, NULL
);
189 fprintf (stderr
,_("%s: Can't open input archive %s\n"),
195 if (bfd_check_format(ibfd
, bfd_archive
) != true)
198 _("%s: file %s is not an archive\n"),
204 ptr
= &(obfd
->archive_head
);
205 element
= bfd_openr_next_archived_file (ibfd
, NULL
);
210 ptr
= &element
->next
;
211 element
= bfd_openr_next_archived_file (ibfd
, element
);
215 bfd_set_format (obfd
, bfd_archive
);
222 ar_addlib_doer (abfd
, prev
)
226 /* Add this module to the output bfd. */
228 prev
->next
= abfd
->next
;
230 abfd
->next
= obfd
->archive_head
;
231 obfd
->archive_head
= abfd
;
235 ar_addlib (name
, list
)
241 fprintf (stderr
, _("%s: no output archive specified yet\n"), program_name
);
248 arch
= open_inarch (name
, (char *) NULL
);
250 map_over_list (arch
, ar_addlib_doer
, list
);
252 /* Don't close the bfd, since it will make the elements disasppear. */
262 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
269 bfd
*abfd
= bfd_openr (list
->name
, NULL
);
273 fprintf (stderr
, _("%s: can't open file %s\n"),
274 program_name
, list
->name
);
279 abfd
->next
= obfd
->archive_head
;
280 obfd
->archive_head
= abfd
;
292 obfd
->archive_head
= 0;
301 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
308 /* Find this name in the archive. */
309 bfd
*member
= obfd
->archive_head
;
310 bfd
**prev
= &(obfd
->archive_head
);
315 if (FILENAME_CMP(member
->filename
, list
->name
) == 0)
317 *prev
= member
->next
;
321 prev
= &(member
->next
);
323 member
= member
->next
;
328 fprintf (stderr
, _("%s: can't find module file %s\n"),
329 program_name
, list
->name
);
343 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
348 char *ofilename
= xstrdup (bfd_get_filename (obfd
));
352 rename (ofilename
, real_name
);
364 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
371 /* Find this name in the archive. */
372 bfd
*member
= obfd
->archive_head
;
373 bfd
**prev
= &(obfd
->archive_head
);
378 if (FILENAME_CMP (member
->filename
, list
->name
) == 0)
380 /* Found the one to replace. */
381 bfd
*abfd
= bfd_openr (list
->name
, 0);
385 fprintf (stderr
, _("%s: can't open file %s\n"),
386 program_name
, list
->name
);
392 abfd
->next
= member
->next
;
398 prev
= &(member
->next
);
400 member
= member
->next
;
405 bfd
*abfd
= bfd_openr (list
->name
, 0);
407 fprintf (stderr
,_("%s: can't find module file %s\n"),
408 program_name
, list
->name
);
411 fprintf (stderr
, _("%s: can't open file %s\n"),
412 program_name
, list
->name
);
424 /* And I added this one. */
430 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
439 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd
));
441 for (abfd
= obfd
->archive_head
;
444 ar_directory_doer (abfd
, (bfd
*) NULL
);
453 fclose ((FILE *)(obfd
->iostream
));
454 unlink (bfd_get_filename (obfd
));
464 fprintf (stderr
, _("%s: no open archive\n"), program_name
);
471 /* Find this name in the archive. */
472 bfd
*member
= obfd
->archive_head
;
475 while (member
&& !found
)
477 if (FILENAME_CMP (member
->filename
, list
->name
) == 0)
479 extract_file (member
);
483 member
= member
->next
;
488 bfd_openr (list
->name
, 0);
489 fprintf (stderr
, _("%s: can't find module file %s\n"),
490 program_name
, list
->name
);