1 /* arsup.c - Archive support for MRI compatibility
2 Copyright (C) 1992-2021 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 3 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., 51 Franklin Street - Fifth Floor, Boston,
19 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
*);
41 extern int deterministic
;
44 static char *real_name
;
48 map_over_list (bfd
*arch
, void (*function
) (bfd
*, bfd
*), struct list
*list
)
56 head
= arch
->archive_next
;
59 next
= head
->archive_next
;
60 function (head
, (bfd
*) NULL
);
68 /* This may appear to be a baroque way of accomplishing what we
69 want. however we have to iterate over the filenames in order
70 to notice where a filename is requested but does not exist in
71 the archive. Ditto mapping over each file each time -- we
72 want to hack multiple references. */
73 for (ptr
= list
; ptr
; ptr
= ptr
->next
)
75 bfd_boolean found
= FALSE
;
78 for (head
= arch
->archive_next
; head
; head
= head
->archive_next
)
80 if (bfd_get_filename (head
) != NULL
81 && FILENAME_CMP (ptr
->name
, bfd_get_filename (head
)) == 0)
84 function (head
, prev
);
89 fprintf (stderr
, _("No entry %s in archive.\n"), ptr
->name
);
97 ar_directory_doer (bfd
*abfd
, bfd
*ignore ATTRIBUTE_UNUSED
)
99 print_arelt_descr(outfile
, abfd
, verbose
, FALSE
);
103 ar_directory (char *ar_name
, struct list
*list
, char *output
)
107 arch
= open_inarch (ar_name
, (char *) NULL
);
110 outfile
= fopen(output
,"w");
114 fprintf (stderr
,_("Can't open file %s\n"), output
);
121 map_over_list (arch
, ar_directory_doer
, list
);
132 extern int interactive
;
150 ar_open (char *name
, int t
)
153 const char *bname
= lbasename (name
);
156 /* Prepend tmp- to the beginning, to avoid file-name clashes after
157 truncation on filesystems with limited namespaces (DOS). */
158 if (asprintf (&tname
, "%.*stmp-%s", (int) (bname
- name
), name
, bname
) == -1)
160 fprintf (stderr
, _("%s: Can't allocate memory for temp name (%s)\n"),
161 program_name
, strerror(errno
));
166 obfd
= bfd_openw (tname
, NULL
);
171 _("%s: Can't open output archive %s\n"),
172 program_name
, tname
);
184 ibfd
= bfd_openr (name
, NULL
);
188 fprintf (stderr
,_("%s: Can't open input archive %s\n"),
194 if (!bfd_check_format(ibfd
, bfd_archive
))
197 _("%s: file %s is not an archive\n"),
203 ptr
= &(obfd
->archive_head
);
204 element
= bfd_openr_next_archived_file (ibfd
, NULL
);
209 ptr
= &element
->archive_next
;
210 element
= bfd_openr_next_archived_file (ibfd
, element
);
214 bfd_set_format (obfd
, bfd_archive
);
217 obfd
->is_thin_archive
= 0;
222 ar_addlib_doer (bfd
*abfd
, bfd
*prev
)
224 /* Add this module to the output bfd. */
226 prev
->archive_next
= abfd
->archive_next
;
228 abfd
->archive_next
= obfd
->archive_head
;
229 obfd
->archive_head
= abfd
;
233 ar_addlib (char *name
, struct list
*list
)
237 fprintf (stderr
, _("%s: no output archive specified yet\n"), program_name
);
244 arch
= open_inarch (name
, (char *) NULL
);
246 map_over_list (arch
, ar_addlib_doer
, list
);
248 /* Don't close the bfd, since it will make the elements disappear. */
253 ar_addmod (struct list
*list
)
257 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
266 #if BFD_SUPPORTS_PLUGINS
267 abfd
= bfd_openr (list
->name
, "plugin");
269 abfd
= bfd_openr (list
->name
, NULL
);
273 fprintf (stderr
, _("%s: can't open file %s\n"),
274 program_name
, list
->name
);
279 abfd
->archive_next
= obfd
->archive_head
;
280 obfd
->archive_head
= abfd
;
292 obfd
->archive_head
= 0;
296 ar_delete (struct list
*list
)
300 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
307 /* Find this name in the archive. */
308 bfd
*member
= obfd
->archive_head
;
309 bfd
**prev
= &(obfd
->archive_head
);
314 if (FILENAME_CMP (bfd_get_filename (member
), list
->name
) == 0)
316 *prev
= member
->archive_next
;
320 prev
= &(member
->archive_next
);
322 member
= member
->archive_next
;
327 fprintf (stderr
, _("%s: can't find module file %s\n"),
328 program_name
, list
->name
);
342 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
347 char *ofilename
= xstrdup (bfd_get_filename (obfd
));
348 bfd_boolean skip_stat
= FALSE
;
349 struct stat target_stat
;
352 if (deterministic
> 0)
353 obfd
->flags
|= BFD_DETERMINISTIC_OUTPUT
;
355 #if !defined (_WIN32) || defined (__CYGWIN32__)
356 /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
357 copy fallback to write the output. */
358 ofd
= dup (fileno ((FILE *) obfd
->iostream
));
359 if (lstat (real_name
, &target_stat
) != 0)
365 smart_rename (ofilename
, real_name
, ofd
,
366 skip_stat
? NULL
: &target_stat
, 0);
373 ar_replace (struct list
*list
)
377 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
384 /* Find this name in the archive. */
385 bfd
*member
= obfd
->archive_head
;
386 bfd
**prev
= &(obfd
->archive_head
);
391 if (FILENAME_CMP (bfd_get_filename (member
), list
->name
) == 0)
393 /* Found the one to replace. */
394 bfd
*abfd
= bfd_openr (list
->name
, NULL
);
398 fprintf (stderr
, _("%s: can't open file %s\n"),
399 program_name
, list
->name
);
405 abfd
->archive_next
= member
->archive_next
;
411 prev
= &(member
->archive_next
);
413 member
= member
->archive_next
;
418 bfd
*abfd
= bfd_openr (list
->name
, NULL
);
420 fprintf (stderr
,_("%s: can't find module file %s\n"),
421 program_name
, list
->name
);
424 fprintf (stderr
, _("%s: can't open file %s\n"),
425 program_name
, list
->name
);
437 /* And I added this one. */
443 fprintf (stderr
, _("%s: no open output archive\n"), program_name
);
452 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd
));
454 for (abfd
= obfd
->archive_head
;
456 abfd
= abfd
->archive_next
)
457 ar_directory_doer (abfd
, (bfd
*) NULL
);
466 bfd_cache_close (obfd
);
467 unlink (bfd_get_filename (obfd
));
472 ar_extract (struct list
*list
)
476 fprintf (stderr
, _("%s: no open archive\n"), program_name
);
483 /* Find this name in the archive. */
484 bfd
*member
= obfd
->archive_head
;
487 while (member
&& !found
)
489 if (FILENAME_CMP (bfd_get_filename (member
), list
->name
) == 0)
491 extract_file (member
);
495 member
= member
->archive_next
;
500 bfd_openr (list
->name
, NULL
);
501 fprintf (stderr
, _("%s: can't find module file %s\n"),
502 program_name
, list
->name
);