Document the GDB 9.1 release in gdb/ChangeLog
[binutils-gdb.git] / binutils / arsup.c
blob00967c972cd67a1fb83b65d4ced9858bf44defed
1 /* arsup.c - Archive support for MRI compatibility
2 Copyright (C) 1992-2020 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
23 sac@cygnus.com
25 This file looks after requests from arparse.y, to provide the MRI
26 style librarian command syntax + 1 word LIST. */
28 #include "sysdep.h"
29 #include "bfd.h"
30 #include "libiberty.h"
31 #include "filenames.h"
32 #include "bucomm.h"
33 #include "arsup.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 *);
40 extern int verbose;
41 extern int deterministic;
43 static bfd *obfd;
44 static char *real_name;
45 static FILE *outfile;
47 static void
48 map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
50 bfd *head;
52 if (list == NULL)
54 bfd *next;
56 head = arch->archive_next;
57 while (head != NULL)
59 next = head->archive_next;
60 function (head, (bfd *) NULL);
61 head = next;
64 else
66 struct list *ptr;
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;
76 bfd *prev = arch;
78 for (head = arch->archive_next; head; head = head->archive_next)
80 if (head->filename != NULL
81 && FILENAME_CMP (ptr->name, head->filename) == 0)
83 found = TRUE;
84 function (head, prev);
86 prev = head;
88 if (! found)
89 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
96 static void
97 ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
99 print_arelt_descr(outfile, abfd, verbose, FALSE);
102 void
103 ar_directory (char *ar_name, struct list *list, char *output)
105 bfd *arch;
107 arch = open_inarch (ar_name, (char *) NULL);
108 if (output)
110 outfile = fopen(output,"w");
111 if (outfile == 0)
113 outfile = stdout;
114 fprintf (stderr,_("Can't open file %s\n"), output);
115 output = 0;
118 else
119 outfile = stdout;
121 map_over_list (arch, ar_directory_doer, list);
123 bfd_close (arch);
125 if (output)
126 fclose (outfile);
129 void
130 prompt (void)
132 extern int interactive;
134 if (interactive)
136 printf ("AR >");
137 fflush (stdout);
141 void
142 maybequit (void)
144 if (! interactive)
145 xexit (9);
149 void
150 ar_open (char *name, int t)
152 char *tname;
153 const char *bname = lbasename (name);
154 real_name = 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));
162 maybequit ();
163 return;
166 obfd = bfd_openw (tname, NULL);
168 if (!obfd)
170 fprintf (stderr,
171 _("%s: Can't open output archive %s\n"),
172 program_name, tname);
174 maybequit ();
176 else
178 if (!t)
180 bfd **ptr;
181 bfd *element;
182 bfd *ibfd;
184 ibfd = bfd_openr (name, NULL);
186 if (!ibfd)
188 fprintf (stderr,_("%s: Can't open input archive %s\n"),
189 program_name, name);
190 maybequit ();
191 return;
194 if (!bfd_check_format(ibfd, bfd_archive))
196 fprintf (stderr,
197 _("%s: file %s is not an archive\n"),
198 program_name, name);
199 maybequit ();
200 return;
203 ptr = &(obfd->archive_head);
204 element = bfd_openr_next_archived_file (ibfd, NULL);
206 while (element)
208 *ptr = element;
209 ptr = &element->archive_next;
210 element = bfd_openr_next_archived_file (ibfd, element);
214 bfd_set_format (obfd, bfd_archive);
216 obfd->has_armap = 1;
217 obfd->is_thin_archive = 0;
221 static void
222 ar_addlib_doer (bfd *abfd, bfd *prev)
224 /* Add this module to the output bfd. */
225 if (prev != NULL)
226 prev->archive_next = abfd->archive_next;
228 abfd->archive_next = obfd->archive_head;
229 obfd->archive_head = abfd;
232 void
233 ar_addlib (char *name, struct list *list)
235 if (obfd == NULL)
237 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
238 maybequit ();
240 else
242 bfd *arch;
244 arch = open_inarch (name, (char *) NULL);
245 if (arch != NULL)
246 map_over_list (arch, ar_addlib_doer, list);
248 /* Don't close the bfd, since it will make the elements disappear. */
252 void
253 ar_addmod (struct list *list)
255 if (!obfd)
257 fprintf (stderr, _("%s: no open output archive\n"), program_name);
258 maybequit ();
260 else
262 while (list)
264 bfd *abfd;
266 #if BFD_SUPPORTS_PLUGINS
267 abfd = bfd_openr (list->name, "plugin");
268 #else
269 abfd = bfd_openr (list->name, NULL);
270 #endif
271 if (!abfd)
273 fprintf (stderr, _("%s: can't open file %s\n"),
274 program_name, list->name);
275 maybequit ();
277 else
279 abfd->archive_next = obfd->archive_head;
280 obfd->archive_head = abfd;
282 list = list->next;
288 void
289 ar_clear (void)
291 if (obfd)
292 obfd->archive_head = 0;
295 void
296 ar_delete (struct list *list)
298 if (!obfd)
300 fprintf (stderr, _("%s: no open output archive\n"), program_name);
301 maybequit ();
303 else
305 while (list)
307 /* Find this name in the archive. */
308 bfd *member = obfd->archive_head;
309 bfd **prev = &(obfd->archive_head);
310 int found = 0;
312 while (member)
314 if (FILENAME_CMP(member->filename, list->name) == 0)
316 *prev = member->archive_next;
317 found = 1;
319 else
320 prev = &(member->archive_next);
322 member = member->archive_next;
325 if (!found)
327 fprintf (stderr, _("%s: can't find module file %s\n"),
328 program_name, list->name);
329 maybequit ();
332 list = list->next;
337 void
338 ar_save (void)
340 if (!obfd)
342 fprintf (stderr, _("%s: no open output archive\n"), program_name);
343 maybequit ();
345 else
347 char *ofilename = xstrdup (bfd_get_filename (obfd));
349 if (deterministic > 0)
350 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
352 bfd_close (obfd);
354 smart_rename (ofilename, real_name, 0);
355 obfd = 0;
356 free (ofilename);
360 void
361 ar_replace (struct list *list)
363 if (!obfd)
365 fprintf (stderr, _("%s: no open output archive\n"), program_name);
366 maybequit ();
368 else
370 while (list)
372 /* Find this name in the archive. */
373 bfd *member = obfd->archive_head;
374 bfd **prev = &(obfd->archive_head);
375 int found = 0;
377 while (member)
379 if (FILENAME_CMP (member->filename, list->name) == 0)
381 /* Found the one to replace. */
382 bfd *abfd = bfd_openr (list->name, NULL);
384 if (!abfd)
386 fprintf (stderr, _("%s: can't open file %s\n"),
387 program_name, list->name);
388 maybequit ();
390 else
392 *prev = abfd;
393 abfd->archive_next = member->archive_next;
394 found = 1;
397 else
399 prev = &(member->archive_next);
401 member = member->archive_next;
404 if (!found)
406 bfd *abfd = bfd_openr (list->name, NULL);
408 fprintf (stderr,_("%s: can't find module file %s\n"),
409 program_name, list->name);
410 if (!abfd)
412 fprintf (stderr, _("%s: can't open file %s\n"),
413 program_name, list->name);
414 maybequit ();
416 else
417 *prev = abfd;
420 list = list->next;
425 /* And I added this one. */
426 void
427 ar_list (void)
429 if (!obfd)
431 fprintf (stderr, _("%s: no open output archive\n"), program_name);
432 maybequit ();
434 else
436 bfd *abfd;
438 outfile = stdout;
439 verbose =1 ;
440 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
442 for (abfd = obfd->archive_head;
443 abfd != (bfd *)NULL;
444 abfd = abfd->archive_next)
445 ar_directory_doer (abfd, (bfd *) NULL);
449 void
450 ar_end (void)
452 if (obfd)
454 bfd_cache_close (obfd);
455 unlink (bfd_get_filename (obfd));
459 void
460 ar_extract (struct list *list)
462 if (!obfd)
464 fprintf (stderr, _("%s: no open archive\n"), program_name);
465 maybequit ();
467 else
469 while (list)
471 /* Find this name in the archive. */
472 bfd *member = obfd->archive_head;
473 int found = 0;
475 while (member && !found)
477 if (FILENAME_CMP (member->filename, list->name) == 0)
479 extract_file (member);
480 found = 1;
483 member = member->archive_next;
486 if (!found)
488 bfd_openr (list->name, NULL);
489 fprintf (stderr, _("%s: can't find module file %s\n"),
490 program_name, list->name);
493 list = list->next;