1 /* arsup.c - Archive support for MRI compatibility
2 Copyright 1992, 1994, 1995, 1996, 1997, 2000
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"
34 #include "filenames.h"
36 static void map_over_list
37 PARAMS ((bfd
*, void (*function
) (bfd
*, bfd
*), struct list
*));
38 static void ar_directory_doer
PARAMS ((bfd
*, bfd
*));
39 static void ar_addlib_doer
PARAMS ((bfd
*, bfd
*));
44 map_over_list (arch
, function
, list
)
46 void (*function
) PARAMS ((bfd
*, bfd
*));
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 boolean found
= false;
77 for (head
= arch
->next
; head
; head
= head
->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
);
98 ar_directory_doer (abfd
, ignore
)
100 bfd
*ignore ATTRIBUTE_UNUSED
;
102 print_arelt_descr(outfile
, abfd
, verbose
);
106 ar_directory (ar_name
, list
, output
)
113 arch
= open_inarch (ar_name
, (char *) NULL
);
116 outfile
= fopen(output
,"w");
120 fprintf (stderr
,_("Can't open file %s\n"), output
);
127 map_over_list (arch
, ar_directory_doer
, list
);
138 extern int interactive
;
157 DEFUN(ar_open
,(name
, t
),
162 char *tname
= (char *) xmalloc (strlen (name
) + 10);
163 const char *bname
= lbasename (name
);
165 /* Prepend tmp- to the beginning, to avoid file-name clashes after
166 truncation on filesystems with limited namespaces (DOS). */
167 sprintf(tname
, "%.*stmp-%s", (int) (bname
- name
), name
, bname
);
168 obfd
= bfd_openw(tname
, NULL
);
171 fprintf(stderr
,_("%s: Can't open output archive %s\n"), program_name
,
181 ibfd
= bfd_openr(name
, NULL
);
183 fprintf(stderr
,_("%s: Can't open input archive %s\n"),
188 if (bfd_check_format(ibfd
, bfd_archive
) != true) {
189 fprintf(stderr
,_("%s: file %s is not an archive\n"), program_name
,
194 ptr
= &(obfd
->archive_head
);
195 element
= bfd_openr_next_archived_file(ibfd
, NULL
);
199 ptr
= &element
->next
;
200 element
= bfd_openr_next_archived_file(ibfd
, element
);
204 bfd_set_format(obfd
, bfd_archive
);
212 ar_addlib_doer (abfd
, prev
)
216 /* Add this module to the output bfd */
218 prev
->next
= abfd
->next
;
219 abfd
->next
= obfd
->archive_head
;
220 obfd
->archive_head
= abfd
;
224 ar_addlib (name
, list
)
230 fprintf (stderr
, _("%s: no output archive specified yet\n"), program_name
);
237 arch
= open_inarch (name
, (char *) NULL
);
239 map_over_list (arch
, ar_addlib_doer
, list
);
241 /* Don't close the bfd, since it will make the elements disasppear */
246 DEFUN(ar_addmod
, (list
),
250 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
256 bfd
*abfd
= bfd_openr(list
->name
, NULL
);
258 fprintf(stderr
,_("%s: can't open file %s\n"), program_name
,
263 abfd
->next
= obfd
->archive_head
;
264 obfd
->archive_head
= abfd
;
277 obfd
->archive_head
= 0;
281 DEFUN(ar_delete
, (list
),
285 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
291 /* Find this name in the archive */
292 bfd
*member
= obfd
->archive_head
;
293 bfd
**prev
= &(obfd
->archive_head
);
296 if (FILENAME_CMP(member
->filename
, list
->name
) == 0) {
297 *prev
= member
->next
;
301 prev
= &(member
->next
);
303 member
= member
->next
;
306 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,
321 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
325 char *ofilename
= xstrdup (bfd_get_filename (obfd
));
328 rename (ofilename
, real_name
);
337 DEFUN(ar_replace
, (list
),
341 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
347 /* Find this name in the archive */
348 bfd
*member
= obfd
->archive_head
;
349 bfd
**prev
= &(obfd
->archive_head
);
353 if (FILENAME_CMP(member
->filename
, list
->name
) == 0)
355 /* Found the one to replace */
356 bfd
*abfd
= bfd_openr(list
->name
, 0);
359 fprintf(stderr
, _("%s: can't open file %s\n"), program_name
, list
->name
);
364 abfd
->next
= member
->next
;
369 prev
= &(member
->next
);
371 member
= member
->next
;
374 bfd
*abfd
= bfd_openr(list
->name
, 0);
375 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,
379 fprintf(stderr
, _("%s: can't open file %s\n"), program_name
, list
->name
);
393 /* And I added this one */
399 fprintf(stderr
, _("%s: no open output archive\n"), program_name
);
406 printf(_("Current open archive is %s\n"), bfd_get_filename (obfd
));
407 for (abfd
= obfd
->archive_head
;
411 ar_directory_doer (abfd
, (bfd
*) NULL
);
422 fclose((FILE *)(obfd
->iostream
));
423 unlink(bfd_get_filename (obfd
));
427 DEFUN(ar_extract
,(list
),
433 fprintf(stderr
, _("%s: no open archive\n"), program_name
);
439 /* Find this name in the archive */
440 bfd
*member
= obfd
->archive_head
;
442 while (member
&& !found
)
444 if (FILENAME_CMP(member
->filename
, list
->name
) == 0)
446 extract_file(member
);
450 member
= member
->next
;
453 bfd_openr(list
->name
, 0);
454 fprintf(stderr
,_("%s: can't find module file %s\n"), program_name
,