Use bool in binutils
[deliverable/binutils-gdb.git] / binutils / arsup.c
1 /* arsup.c - Archive support for MRI compatibility
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
3
4 This file is part of GNU Binutils.
5
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.
10
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.
15
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. */
20
21
22 /* Contributed by Steve Chamberlain
23 sac@cygnus.com
24
25 This file looks after requests from arparse.y, to provide the MRI
26 style librarian command syntax + 1 word LIST. */
27
28 #include "sysdep.h"
29 #include "bfd.h"
30 #include "libiberty.h"
31 #include "filenames.h"
32 #include "bucomm.h"
33 #include "arsup.h"
34
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 *);
39
40 extern int verbose;
41 extern int deterministic;
42
43 static bfd *obfd;
44 static char *real_name;
45 static char *temp_name;
46 static int temp_fd;
47 static FILE *outfile;
48
49 static void
50 map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list)
51 {
52 bfd *head;
53
54 if (list == NULL)
55 {
56 bfd *next;
57
58 head = arch->archive_next;
59 while (head != NULL)
60 {
61 next = head->archive_next;
62 function (head, (bfd *) NULL);
63 head = next;
64 }
65 }
66 else
67 {
68 struct list *ptr;
69
70 /* This may appear to be a baroque way of accomplishing what we
71 want. however we have to iterate over the filenames in order
72 to notice where a filename is requested but does not exist in
73 the archive. Ditto mapping over each file each time -- we
74 want to hack multiple references. */
75 for (ptr = list; ptr; ptr = ptr->next)
76 {
77 bool found = false;
78 bfd *prev = arch;
79
80 for (head = arch->archive_next; head; head = head->archive_next)
81 {
82 if (bfd_get_filename (head) != NULL
83 && FILENAME_CMP (ptr->name, bfd_get_filename (head)) == 0)
84 {
85 found = true;
86 function (head, prev);
87 }
88 prev = head;
89 }
90 if (! found)
91 fprintf (stderr, _("No entry %s in archive.\n"), ptr->name);
92 }
93 }
94 }
95
96
97
98 static void
99 ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED)
100 {
101 print_arelt_descr(outfile, abfd, verbose, false);
102 }
103
104 void
105 ar_directory (char *ar_name, struct list *list, char *output)
106 {
107 bfd *arch;
108
109 arch = open_inarch (ar_name, (char *) NULL);
110 if (output)
111 {
112 outfile = fopen(output,"w");
113 if (outfile == 0)
114 {
115 outfile = stdout;
116 fprintf (stderr,_("Can't open file %s\n"), output);
117 output = 0;
118 }
119 }
120 else
121 outfile = stdout;
122
123 map_over_list (arch, ar_directory_doer, list);
124
125 bfd_close (arch);
126
127 if (output)
128 fclose (outfile);
129 }
130
131 void
132 prompt (void)
133 {
134 extern int interactive;
135
136 if (interactive)
137 {
138 printf ("AR >");
139 fflush (stdout);
140 }
141 }
142
143 void
144 maybequit (void)
145 {
146 if (! interactive)
147 xexit (9);
148 }
149
150
151 void
152 ar_open (char *name, int t)
153 {
154 real_name = xstrdup (name);
155 temp_name = make_tempname (real_name, &temp_fd);
156
157 if (temp_name == NULL)
158 {
159 fprintf (stderr, _("%s: Can't open temporary file (%s)\n"),
160 program_name, strerror(errno));
161 maybequit ();
162 return;
163 }
164
165 obfd = bfd_fdopenw (temp_name, NULL, temp_fd);
166
167 if (!obfd)
168 {
169 fprintf (stderr,
170 _("%s: Can't open output archive %s\n"),
171 program_name, temp_name);
172
173 maybequit ();
174 }
175 else
176 {
177 if (!t)
178 {
179 bfd **ptr;
180 bfd *element;
181 bfd *ibfd;
182
183 ibfd = bfd_openr (name, NULL);
184
185 if (!ibfd)
186 {
187 fprintf (stderr,_("%s: Can't open input archive %s\n"),
188 program_name, name);
189 maybequit ();
190 return;
191 }
192
193 if (!bfd_check_format(ibfd, bfd_archive))
194 {
195 fprintf (stderr,
196 _("%s: file %s is not an archive\n"),
197 program_name, name);
198 maybequit ();
199 return;
200 }
201
202 ptr = &(obfd->archive_head);
203 element = bfd_openr_next_archived_file (ibfd, NULL);
204
205 while (element)
206 {
207 *ptr = element;
208 ptr = &element->archive_next;
209 element = bfd_openr_next_archived_file (ibfd, element);
210 }
211 }
212
213 bfd_set_format (obfd, bfd_archive);
214
215 obfd->has_armap = 1;
216 obfd->is_thin_archive = 0;
217 }
218 }
219
220 static void
221 ar_addlib_doer (bfd *abfd, bfd *prev)
222 {
223 /* Add this module to the output bfd. */
224 if (prev != NULL)
225 prev->archive_next = abfd->archive_next;
226
227 abfd->archive_next = obfd->archive_head;
228 obfd->archive_head = abfd;
229 }
230
231 void
232 ar_addlib (char *name, struct list *list)
233 {
234 if (obfd == NULL)
235 {
236 fprintf (stderr, _("%s: no output archive specified yet\n"), program_name);
237 maybequit ();
238 }
239 else
240 {
241 bfd *arch;
242
243 arch = open_inarch (name, (char *) NULL);
244 if (arch != NULL)
245 map_over_list (arch, ar_addlib_doer, list);
246
247 /* Don't close the bfd, since it will make the elements disappear. */
248 }
249 }
250
251 void
252 ar_addmod (struct list *list)
253 {
254 if (!obfd)
255 {
256 fprintf (stderr, _("%s: no open output archive\n"), program_name);
257 maybequit ();
258 }
259 else
260 {
261 while (list)
262 {
263 bfd *abfd;
264
265 #if BFD_SUPPORTS_PLUGINS
266 abfd = bfd_openr (list->name, "plugin");
267 #else
268 abfd = bfd_openr (list->name, NULL);
269 #endif
270 if (!abfd)
271 {
272 fprintf (stderr, _("%s: can't open file %s\n"),
273 program_name, list->name);
274 maybequit ();
275 }
276 else
277 {
278 abfd->archive_next = obfd->archive_head;
279 obfd->archive_head = abfd;
280 }
281 list = list->next;
282 }
283 }
284 }
285
286
287 void
288 ar_clear (void)
289 {
290 if (obfd)
291 obfd->archive_head = 0;
292 }
293
294 void
295 ar_delete (struct list *list)
296 {
297 if (!obfd)
298 {
299 fprintf (stderr, _("%s: no open output archive\n"), program_name);
300 maybequit ();
301 }
302 else
303 {
304 while (list)
305 {
306 /* Find this name in the archive. */
307 bfd *member = obfd->archive_head;
308 bfd **prev = &(obfd->archive_head);
309 int found = 0;
310
311 while (member)
312 {
313 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
314 {
315 *prev = member->archive_next;
316 found = 1;
317 }
318 else
319 prev = &(member->archive_next);
320
321 member = member->archive_next;
322 }
323
324 if (!found)
325 {
326 fprintf (stderr, _("%s: can't find module file %s\n"),
327 program_name, list->name);
328 maybequit ();
329 }
330
331 list = list->next;
332 }
333 }
334 }
335
336 void
337 ar_save (void)
338 {
339 if (!obfd)
340 {
341 fprintf (stderr, _("%s: no open output archive\n"), program_name);
342 maybequit ();
343 }
344 else
345 {
346 struct stat target_stat;
347
348 if (deterministic > 0)
349 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
350
351 temp_fd = dup (temp_fd);
352 bfd_close (obfd);
353
354 if (stat (real_name, &target_stat) != 0)
355 {
356 /* The temp file created in ar_open has mode 0600 as per mkstemp.
357 Create the real empty output file here so smart_rename will
358 update the mode according to the process umask. */
359 obfd = bfd_openw (real_name, NULL);
360 if (obfd != NULL)
361 {
362 bfd_set_format (obfd, bfd_archive);
363 bfd_close (obfd);
364 }
365 }
366
367 smart_rename (temp_name, real_name, temp_fd, NULL, false);
368 obfd = 0;
369 free (temp_name);
370 free (real_name);
371 }
372 }
373
374 void
375 ar_replace (struct list *list)
376 {
377 if (!obfd)
378 {
379 fprintf (stderr, _("%s: no open output archive\n"), program_name);
380 maybequit ();
381 }
382 else
383 {
384 while (list)
385 {
386 /* Find this name in the archive. */
387 bfd *member = obfd->archive_head;
388 bfd **prev = &(obfd->archive_head);
389 int found = 0;
390
391 while (member)
392 {
393 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
394 {
395 /* Found the one to replace. */
396 bfd *abfd = bfd_openr (list->name, NULL);
397
398 if (!abfd)
399 {
400 fprintf (stderr, _("%s: can't open file %s\n"),
401 program_name, list->name);
402 maybequit ();
403 }
404 else
405 {
406 *prev = abfd;
407 abfd->archive_next = member->archive_next;
408 found = 1;
409 }
410 }
411 else
412 {
413 prev = &(member->archive_next);
414 }
415 member = member->archive_next;
416 }
417
418 if (!found)
419 {
420 bfd *abfd = bfd_openr (list->name, NULL);
421
422 fprintf (stderr,_("%s: can't find module file %s\n"),
423 program_name, list->name);
424 if (!abfd)
425 {
426 fprintf (stderr, _("%s: can't open file %s\n"),
427 program_name, list->name);
428 maybequit ();
429 }
430 else
431 *prev = abfd;
432 }
433
434 list = list->next;
435 }
436 }
437 }
438
439 /* And I added this one. */
440 void
441 ar_list (void)
442 {
443 if (!obfd)
444 {
445 fprintf (stderr, _("%s: no open output archive\n"), program_name);
446 maybequit ();
447 }
448 else
449 {
450 bfd *abfd;
451
452 outfile = stdout;
453 verbose =1 ;
454 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
455
456 for (abfd = obfd->archive_head;
457 abfd != (bfd *)NULL;
458 abfd = abfd->archive_next)
459 ar_directory_doer (abfd, (bfd *) NULL);
460 }
461 }
462
463 void
464 ar_end (void)
465 {
466 if (obfd)
467 {
468 bfd_cache_close (obfd);
469 unlink (bfd_get_filename (obfd));
470 }
471 }
472
473 void
474 ar_extract (struct list *list)
475 {
476 if (!obfd)
477 {
478 fprintf (stderr, _("%s: no open archive\n"), program_name);
479 maybequit ();
480 }
481 else
482 {
483 while (list)
484 {
485 /* Find this name in the archive. */
486 bfd *member = obfd->archive_head;
487 int found = 0;
488
489 while (member && !found)
490 {
491 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
492 {
493 extract_file (member);
494 found = 1;
495 }
496
497 member = member->archive_next;
498 }
499
500 if (!found)
501 {
502 bfd_openr (list->name, NULL);
503 fprintf (stderr, _("%s: can't find module file %s\n"),
504 program_name, list->name);
505 }
506
507 list = list->next;
508 }
509 }
510 }
This page took 0.039005 seconds and 5 git commands to generate.