1 /* arsup.c - Archive support for MRI compatibility
2 Copyright (C) 1992, 93, 94, 95, 96, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Contributed by Steve Chamberlain
24 This file looks after requests from arparse.y, to provide the MRI
25 style librarian command syntax + 1 word LIST
31 #include "libiberty.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 && strcmp (ptr
->name
, head
->filename
) == 0)
81 function (head
, prev
);
86 fprintf (stderr
, _("No entry %s in archive.\n"), ptr
->name
);
96 ar_directory_doer (abfd
, ignore
)
100 print_arelt_descr(outfile
, abfd
, verbose
);
104 ar_directory (ar_name
, list
, output
)
111 arch
= open_inarch (ar_name
, (char *) NULL
);
114 outfile
= fopen(output
,"w");
118 fprintf (stderr
,_("Can't open file %s\n"), output
);
125 map_over_list (arch
, ar_directory_doer
, list
);
136 extern int interactive
;
155 DEFUN(ar_open
,(name
, t
),
160 char *tname
= (char *) xmalloc (strlen (name
) + 10);
162 sprintf(tname
, "%s-tmp", name
);
163 obfd
= bfd_openw(tname
, NULL
);
166 fprintf(stderr
,_("%s: Can't open output archive %s\n"), program_name
,
176 ibfd
= bfd_openr(name
, NULL
);
178 fprintf(stderr
,_("%s: Can't open input archive %s\n"),
183 if (bfd_check_format(ibfd
, bfd_archive
) != true) {
184 fprintf(stderr
,_("%s: file %s is not an archive\n"), program_name
,
189 ptr
= &(obfd
->archive_head
);
190 element
= bfd_openr_next_archived_file(ibfd
, NULL
);
194 ptr
= &element
->next
;
195 element
= bfd_openr_next_archived_file(ibfd
, element
);
199 bfd_set_format(obfd
, bfd_archive
);
207 ar_addlib_doer (abfd
, prev
)
211 /* Add this module to the output bfd */
213 prev
->next
= abfd
->next
;
214 abfd
->next
= obfd
->archive_head
;
215 obfd
->archive_head
= abfd
;
219 ar_addlib (name
, list
)
225 fprintf (stderr
, _("%s: no output archive specified yet\n"), program_name
);
232 arch
= open_inarch (name
, (char *) NULL
);
234 map_over_list (arch
, ar_addlib_doer
, list
);
236 /* Don't close the bfd, since it will make the elements disasppear */
241 DEFUN(ar_addmod
, (list
),
245 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
251 bfd
*abfd
= bfd_openr(list
->name
, NULL
);
253 fprintf(stderr
,_("%s: can't open file %s\n"), program_name
,
258 abfd
->next
= obfd
->archive_head
;
259 obfd
->archive_head
= abfd
;
272 obfd
->archive_head
= 0;
276 DEFUN(ar_delete
, (list
),
280 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
286 /* Find this name in the archive */
287 bfd
*member
= obfd
->archive_head
;
288 bfd
**prev
= &(obfd
->archive_head
);
291 if (strcmp(member
->filename
, list
->name
) == 0) {
292 *prev
= member
->next
;
296 prev
= &(member
->next
);
298 member
= member
->next
;
301 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,
316 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
320 char *ofilename
= xstrdup (bfd_get_filename (obfd
));
323 rename (ofilename
, real_name
);
332 DEFUN(ar_replace
, (list
),
336 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
342 /* Find this name in the archive */
343 bfd
*member
= obfd
->archive_head
;
344 bfd
**prev
= &(obfd
->archive_head
);
348 if (strcmp(member
->filename
, list
->name
) == 0)
350 /* Found the one to replace */
351 bfd
*abfd
= bfd_openr(list
->name
, 0);
354 fprintf(stderr
, _("%s: can't open file %s\n"), program_name
, list
->name
);
359 abfd
->next
= member
->next
;
364 prev
= &(member
->next
);
366 member
= member
->next
;
369 bfd
*abfd
= bfd_openr(list
->name
, 0);
370 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,
374 fprintf(stderr
, _("%s: can't open file %s\n"), program_name
, list
->name
);
388 /* And I added this one */
394 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
401 printf(_("Current open archive is %s\n"), bfd_get_filename (obfd
));
402 for (abfd
= obfd
->archive_head
;
406 ar_directory_doer (abfd
, (bfd
*) NULL
);
417 fclose((FILE *)(obfd
->iostream
));
418 unlink(bfd_get_filename (obfd
));
422 DEFUN(ar_extract
,(list
),
428 fprintf(stderr
, _("%s: no open archive\n"), program_name
);
434 /* Find this name in the archive */
435 bfd
*member
= obfd
->archive_head
;
437 while (member
&& !found
)
439 if (strcmp(member
->filename
, list
->name
) == 0)
441 extract_file(member
);
445 member
= member
->next
;
448 bfd_openr(list
->name
, 0);
449 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,