1 /* arsup.c - Archive support for MRI compatibility
2 Copyright (C) 1992, 93, 94, 95, 96, 97, 98, 1999
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
32 #include "libiberty.h"
35 static void map_over_list
36 PARAMS ((bfd
*, void (*function
) (bfd
*, bfd
*), struct list
*));
37 static void ar_directory_doer
PARAMS ((bfd
*, bfd
*));
38 static void ar_addlib_doer
PARAMS ((bfd
*, bfd
*));
43 map_over_list (arch
, function
, list
)
45 void (*function
) PARAMS ((bfd
*, bfd
*));
58 function (head
, (bfd
*) NULL
);
66 /* This may appear to be a baroque way of accomplishing what we
67 want. however we have to iterate over the filenames in order
68 to notice where a filename is requested but does not exist in
69 the archive. Ditto mapping over each file each time -- we
70 want to hack multiple references. */
71 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
73 boolean found
= false;
76 for (head
= arch
->next
; head
; head
= head
->next
)
78 if (head
->filename
!= NULL
79 && strcmp (ptr
->name
, head
->filename
) == 0)
82 function (head
, prev
);
87 fprintf (stderr
, _("No entry %s in archive.\n"), ptr
->name
);
97 ar_directory_doer (abfd
, ignore
)
99 bfd
*ignore ATTRIBUTE_UNUSED
;
101 print_arelt_descr(outfile
, abfd
, verbose
);
105 ar_directory (ar_name
, list
, output
)
112 arch
= open_inarch (ar_name
, (char *) NULL
);
115 outfile
= fopen(output
,"w");
119 fprintf (stderr
,_("Can't open file %s\n"), output
);
126 map_over_list (arch
, ar_directory_doer
, list
);
137 extern int interactive
;
156 DEFUN(ar_open
,(name
, t
),
161 char *tname
= (char *) xmalloc (strlen (name
) + 10);
163 sprintf(tname
, "%s-tmp", name
);
164 obfd
= bfd_openw(tname
, NULL
);
167 fprintf(stderr
,_("%s: Can't open output archive %s\n"), program_name
,
177 ibfd
= bfd_openr(name
, NULL
);
179 fprintf(stderr
,_("%s: Can't open input archive %s\n"),
184 if (bfd_check_format(ibfd
, bfd_archive
) != true) {
185 fprintf(stderr
,_("%s: file %s is not an archive\n"), program_name
,
190 ptr
= &(obfd
->archive_head
);
191 element
= bfd_openr_next_archived_file(ibfd
, NULL
);
195 ptr
= &element
->next
;
196 element
= bfd_openr_next_archived_file(ibfd
, element
);
200 bfd_set_format(obfd
, bfd_archive
);
208 ar_addlib_doer (abfd
, prev
)
212 /* Add this module to the output bfd */
214 prev
->next
= abfd
->next
;
215 abfd
->next
= obfd
->archive_head
;
216 obfd
->archive_head
= abfd
;
220 ar_addlib (name
, list
)
226 fprintf (stderr
, _("%s: no output archive specified yet\n"), program_name
);
233 arch
= open_inarch (name
, (char *) NULL
);
235 map_over_list (arch
, ar_addlib_doer
, list
);
237 /* Don't close the bfd, since it will make the elements disasppear */
242 DEFUN(ar_addmod
, (list
),
246 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
252 bfd
*abfd
= bfd_openr(list
->name
, NULL
);
254 fprintf(stderr
,_("%s: can't open file %s\n"), program_name
,
259 abfd
->next
= obfd
->archive_head
;
260 obfd
->archive_head
= abfd
;
273 obfd
->archive_head
= 0;
277 DEFUN(ar_delete
, (list
),
281 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
287 /* Find this name in the archive */
288 bfd
*member
= obfd
->archive_head
;
289 bfd
**prev
= &(obfd
->archive_head
);
292 if (strcmp(member
->filename
, list
->name
) == 0) {
293 *prev
= member
->next
;
297 prev
= &(member
->next
);
299 member
= member
->next
;
302 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,
317 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
321 char *ofilename
= xstrdup (bfd_get_filename (obfd
));
324 rename (ofilename
, real_name
);
333 DEFUN(ar_replace
, (list
),
337 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
343 /* Find this name in the archive */
344 bfd
*member
= obfd
->archive_head
;
345 bfd
**prev
= &(obfd
->archive_head
);
349 if (strcmp(member
->filename
, list
->name
) == 0)
351 /* Found the one to replace */
352 bfd
*abfd
= bfd_openr(list
->name
, 0);
355 fprintf(stderr
, _("%s: can't open file %s\n"), program_name
, list
->name
);
360 abfd
->next
= member
->next
;
365 prev
= &(member
->next
);
367 member
= member
->next
;
370 bfd
*abfd
= bfd_openr(list
->name
, 0);
371 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,
375 fprintf(stderr
, _("%s: can't open file %s\n"), program_name
, list
->name
);
389 /* And I added this one */
395 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
402 printf(_("Current open archive is %s\n"), bfd_get_filename (obfd
));
403 for (abfd
= obfd
->archive_head
;
407 ar_directory_doer (abfd
, (bfd
*) NULL
);
418 fclose((FILE *)(obfd
->iostream
));
419 unlink(bfd_get_filename (obfd
));
423 DEFUN(ar_extract
,(list
),
429 fprintf(stderr
, _("%s: no open archive\n"), program_name
);
435 /* Find this name in the archive */
436 bfd
*member
= obfd
->archive_head
;
438 while (member
&& !found
)
440 if (strcmp(member
->filename
, list
->name
) == 0)
442 extract_file(member
);
446 member
= member
->next
;
449 bfd_openr(list
->name
, 0);
450 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,