Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* arsup.c - Archive support for MRI compatibility |
250d07de | 2 | Copyright (C) 1992-2021 Free Software Foundation, Inc. |
252b5132 | 3 | |
3a1a2036 | 4 | This file is part of GNU Binutils. |
252b5132 | 5 | |
3a1a2036 NC |
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 | |
32866df7 | 8 | the Free Software Foundation; either version 3 of the License, or |
3a1a2036 | 9 | (at your option) any later version. |
252b5132 | 10 | |
3a1a2036 NC |
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. | |
252b5132 | 15 | |
3a1a2036 NC |
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 | |
32866df7 NC |
18 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
19 | MA 02110-1301, USA. */ | |
252b5132 RH |
20 | |
21 | ||
22 | /* Contributed by Steve Chamberlain | |
3a1a2036 | 23 | sac@cygnus.com |
252b5132 | 24 | |
3a1a2036 NC |
25 | This file looks after requests from arparse.y, to provide the MRI |
26 | style librarian command syntax + 1 word LIST. */ | |
252b5132 | 27 | |
3db64b00 | 28 | #include "sysdep.h" |
252b5132 | 29 | #include "bfd.h" |
252b5132 | 30 | #include "libiberty.h" |
5af11cab | 31 | #include "filenames.h" |
3db64b00 AM |
32 | #include "bucomm.h" |
33 | #include "arsup.h" | |
252b5132 RH |
34 | |
35 | static void map_over_list | |
2da42df6 AJ |
36 | (bfd *, void (*function) (bfd *, bfd *), struct list *); |
37 | static void ar_directory_doer (bfd *, bfd *); | |
38 | static void ar_addlib_doer (bfd *, bfd *); | |
252b5132 RH |
39 | |
40 | extern int verbose; | |
5cd84a72 | 41 | extern int deterministic; |
252b5132 | 42 | |
85b1c36d BE |
43 | static bfd *obfd; |
44 | static char *real_name; | |
95b91a04 | 45 | static char *temp_name; |
c42c71a1 | 46 | static int temp_fd; |
85b1c36d BE |
47 | static FILE *outfile; |
48 | ||
252b5132 | 49 | static void |
2da42df6 | 50 | map_over_list (bfd *arch, void (*function) (bfd *, bfd *), struct list *list) |
252b5132 RH |
51 | { |
52 | bfd *head; | |
53 | ||
54 | if (list == NULL) | |
55 | { | |
56 | bfd *next; | |
57 | ||
cc481421 | 58 | head = arch->archive_next; |
252b5132 RH |
59 | while (head != NULL) |
60 | { | |
cc481421 | 61 | next = head->archive_next; |
252b5132 RH |
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 | { | |
b34976b6 | 77 | bfd_boolean found = FALSE; |
252b5132 RH |
78 | bfd *prev = arch; |
79 | ||
cc481421 | 80 | for (head = arch->archive_next; head; head = head->archive_next) |
252b5132 | 81 | { |
c177f377 AM |
82 | if (bfd_get_filename (head) != NULL |
83 | && FILENAME_CMP (ptr->name, bfd_get_filename (head)) == 0) | |
252b5132 | 84 | { |
b34976b6 | 85 | found = TRUE; |
252b5132 RH |
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 | ||
252b5132 | 97 | |
252b5132 | 98 | static void |
2da42df6 | 99 | ar_directory_doer (bfd *abfd, bfd *ignore ATTRIBUTE_UNUSED) |
252b5132 | 100 | { |
1869e86f | 101 | print_arelt_descr(outfile, abfd, verbose, FALSE); |
252b5132 RH |
102 | } |
103 | ||
104 | void | |
2da42df6 | 105 | ar_directory (char *ar_name, struct list *list, char *output) |
252b5132 RH |
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 | } | |
f462a9ea | 120 | else |
252b5132 RH |
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 | |
2da42df6 | 132 | prompt (void) |
252b5132 RH |
133 | { |
134 | extern int interactive; | |
3a1a2036 | 135 | |
f462a9ea | 136 | if (interactive) |
3a1a2036 NC |
137 | { |
138 | printf ("AR >"); | |
139 | fflush (stdout); | |
140 | } | |
252b5132 RH |
141 | } |
142 | ||
143 | void | |
2da42df6 | 144 | maybequit (void) |
252b5132 | 145 | { |
f462a9ea | 146 | if (! interactive) |
252b5132 RH |
147 | xexit (9); |
148 | } | |
149 | ||
150 | ||
3a1a2036 | 151 | void |
2da42df6 | 152 | ar_open (char *name, int t) |
252b5132 | 153 | { |
95b91a04 | 154 | real_name = xstrdup (name); |
c42c71a1 | 155 | temp_name = make_tempname (real_name, &temp_fd); |
3a1a2036 | 156 | |
95b91a04 | 157 | if (temp_name == NULL) |
2e02f296 | 158 | { |
95b91a04 | 159 | fprintf (stderr, _("%s: Can't open temporary file (%s)\n"), |
2e02f296 CZ |
160 | program_name, strerror(errno)); |
161 | maybequit (); | |
162 | return; | |
163 | } | |
164 | ||
c42c71a1 | 165 | obfd = bfd_fdopenw (temp_name, NULL, temp_fd); |
3a1a2036 NC |
166 | |
167 | if (!obfd) | |
168 | { | |
169 | fprintf (stderr, | |
170 | _("%s: Can't open output archive %s\n"), | |
95b91a04 | 171 | program_name, temp_name); |
3a1a2036 NC |
172 | |
173 | maybequit (); | |
252b5132 | 174 | } |
3a1a2036 NC |
175 | else |
176 | { | |
177 | if (!t) | |
178 | { | |
179 | bfd **ptr; | |
180 | bfd *element; | |
181 | bfd *ibfd; | |
252b5132 | 182 | |
3a1a2036 | 183 | ibfd = bfd_openr (name, NULL); |
252b5132 | 184 | |
3a1a2036 NC |
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 | ||
b34976b6 | 193 | if (!bfd_check_format(ibfd, bfd_archive)) |
3a1a2036 NC |
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; | |
cc481421 | 208 | ptr = &element->archive_next; |
3a1a2036 NC |
209 | element = bfd_openr_next_archived_file (ibfd, element); |
210 | } | |
211 | } | |
212 | ||
213 | bfd_set_format (obfd, bfd_archive); | |
252b5132 | 214 | |
3a1a2036 | 215 | obfd->has_armap = 1; |
a8da6403 | 216 | obfd->is_thin_archive = 0; |
3a1a2036 NC |
217 | } |
218 | } | |
252b5132 RH |
219 | |
220 | static void | |
2da42df6 | 221 | ar_addlib_doer (bfd *abfd, bfd *prev) |
252b5132 | 222 | { |
3a1a2036 | 223 | /* Add this module to the output bfd. */ |
252b5132 | 224 | if (prev != NULL) |
cc481421 | 225 | prev->archive_next = abfd->archive_next; |
3a1a2036 | 226 | |
cc481421 | 227 | abfd->archive_next = obfd->archive_head; |
252b5132 RH |
228 | obfd->archive_head = abfd; |
229 | } | |
230 | ||
231 | void | |
2da42df6 | 232 | ar_addlib (char *name, struct list *list) |
252b5132 RH |
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 | ||
50c2245b | 247 | /* Don't close the bfd, since it will make the elements disappear. */ |
252b5132 RH |
248 | } |
249 | } | |
250 | ||
251 | void | |
2da42df6 | 252 | ar_addmod (struct list *list) |
252b5132 | 253 | { |
3a1a2036 NC |
254 | if (!obfd) |
255 | { | |
256 | fprintf (stderr, _("%s: no open output archive\n"), program_name); | |
257 | maybequit (); | |
258 | } | |
f462a9ea | 259 | else |
3a1a2036 NC |
260 | { |
261 | while (list) | |
262 | { | |
70b0cf90 | 263 | bfd *abfd; |
3a1a2036 | 264 | |
70b0cf90 NC |
265 | #if BFD_SUPPORTS_PLUGINS |
266 | abfd = bfd_openr (list->name, "plugin"); | |
267 | #else | |
268 | abfd = bfd_openr (list->name, NULL); | |
269 | #endif | |
3a1a2036 NC |
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 | { | |
cc481421 | 278 | abfd->archive_next = obfd->archive_head; |
3a1a2036 NC |
279 | obfd->archive_head = abfd; |
280 | } | |
281 | list = list->next; | |
282 | } | |
252b5132 | 283 | } |
252b5132 RH |
284 | } |
285 | ||
286 | ||
252b5132 | 287 | void |
2da42df6 | 288 | ar_clear (void) |
252b5132 | 289 | { |
3a1a2036 NC |
290 | if (obfd) |
291 | obfd->archive_head = 0; | |
252b5132 RH |
292 | } |
293 | ||
294 | void | |
2da42df6 | 295 | ar_delete (struct list *list) |
252b5132 | 296 | { |
3a1a2036 NC |
297 | if (!obfd) |
298 | { | |
299 | fprintf (stderr, _("%s: no open output archive\n"), program_name); | |
300 | maybequit (); | |
301 | } | |
f462a9ea | 302 | else |
3a1a2036 NC |
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 | { | |
c177f377 | 313 | if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0) |
3a1a2036 | 314 | { |
cc481421 | 315 | *prev = member->archive_next; |
3a1a2036 NC |
316 | found = 1; |
317 | } | |
318 | else | |
cc481421 | 319 | prev = &(member->archive_next); |
3a1a2036 | 320 | |
cc481421 | 321 | member = member->archive_next; |
3a1a2036 NC |
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; | |
252b5132 | 332 | } |
252b5132 | 333 | } |
252b5132 RH |
334 | } |
335 | ||
252b5132 | 336 | void |
2da42df6 | 337 | ar_save (void) |
252b5132 | 338 | { |
3a1a2036 NC |
339 | if (!obfd) |
340 | { | |
341 | fprintf (stderr, _("%s: no open output archive\n"), program_name); | |
342 | maybequit (); | |
343 | } | |
344 | else | |
345 | { | |
014cc7f8 | 346 | struct stat target_stat; |
252b5132 | 347 | |
5cd84a72 NC |
348 | if (deterministic > 0) |
349 | obfd->flags |= BFD_DETERMINISTIC_OUTPUT; | |
350 | ||
c42c71a1 | 351 | temp_fd = dup (temp_fd); |
3a1a2036 | 352 | bfd_close (obfd); |
252b5132 | 353 | |
c180f095 | 354 | if (stat (real_name, &target_stat) != 0) |
95b91a04 AM |
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); | |
95b91a04 AM |
360 | if (obfd != NULL) |
361 | { | |
362 | bfd_set_format (obfd, bfd_archive); | |
363 | bfd_close (obfd); | |
364 | } | |
365 | } | |
366 | ||
c42c71a1 | 367 | smart_rename (temp_name, real_name, temp_fd, NULL, FALSE); |
3a1a2036 | 368 | obfd = 0; |
95b91a04 AM |
369 | free (temp_name); |
370 | free (real_name); | |
3a1a2036 NC |
371 | } |
372 | } | |
252b5132 RH |
373 | |
374 | void | |
2da42df6 | 375 | ar_replace (struct list *list) |
252b5132 | 376 | { |
3a1a2036 NC |
377 | if (!obfd) |
378 | { | |
379 | fprintf (stderr, _("%s: no open output archive\n"), program_name); | |
380 | maybequit (); | |
381 | } | |
f462a9ea | 382 | else |
3a1a2036 NC |
383 | { |
384 | while (list) | |
252b5132 | 385 | { |
3a1a2036 NC |
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 | { | |
c177f377 | 393 | if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0) |
3a1a2036 NC |
394 | { |
395 | /* Found the one to replace. */ | |
70b0cf90 | 396 | bfd *abfd = bfd_openr (list->name, NULL); |
3a1a2036 NC |
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; | |
cc481421 | 407 | abfd->archive_next = member->archive_next; |
3a1a2036 NC |
408 | found = 1; |
409 | } | |
410 | } | |
411 | else | |
412 | { | |
cc481421 | 413 | prev = &(member->archive_next); |
3a1a2036 | 414 | } |
cc481421 | 415 | member = member->archive_next; |
3a1a2036 NC |
416 | } |
417 | ||
418 | if (!found) | |
419 | { | |
70b0cf90 | 420 | bfd *abfd = bfd_openr (list->name, NULL); |
252b5132 | 421 | |
3a1a2036 NC |
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 | } | |
252b5132 | 436 | } |
252b5132 RH |
437 | } |
438 | ||
3a1a2036 | 439 | /* And I added this one. */ |
252b5132 | 440 | void |
2da42df6 | 441 | ar_list (void) |
252b5132 | 442 | { |
f462a9ea | 443 | if (!obfd) |
252b5132 | 444 | { |
3a1a2036 NC |
445 | fprintf (stderr, _("%s: no open output archive\n"), program_name); |
446 | maybequit (); | |
252b5132 | 447 | } |
3a1a2036 NC |
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)); | |
252b5132 | 455 | |
3a1a2036 NC |
456 | for (abfd = obfd->archive_head; |
457 | abfd != (bfd *)NULL; | |
cc481421 | 458 | abfd = abfd->archive_next) |
3a1a2036 NC |
459 | ar_directory_doer (abfd, (bfd *) NULL); |
460 | } | |
461 | } | |
252b5132 | 462 | |
f462a9ea | 463 | void |
2da42df6 | 464 | ar_end (void) |
252b5132 RH |
465 | { |
466 | if (obfd) | |
3a1a2036 | 467 | { |
c92c35e7 | 468 | bfd_cache_close (obfd); |
3a1a2036 NC |
469 | unlink (bfd_get_filename (obfd)); |
470 | } | |
252b5132 | 471 | } |
3a1a2036 | 472 | |
252b5132 | 473 | void |
2da42df6 | 474 | ar_extract (struct list *list) |
252b5132 | 475 | { |
f462a9ea | 476 | if (!obfd) |
3a1a2036 NC |
477 | { |
478 | fprintf (stderr, _("%s: no open archive\n"), program_name); | |
479 | maybequit (); | |
480 | } | |
f462a9ea | 481 | else |
3a1a2036 NC |
482 | { |
483 | while (list) | |
252b5132 | 484 | { |
3a1a2036 NC |
485 | /* Find this name in the archive. */ |
486 | bfd *member = obfd->archive_head; | |
487 | int found = 0; | |
488 | ||
489 | while (member && !found) | |
490 | { | |
c177f377 | 491 | if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0) |
3a1a2036 NC |
492 | { |
493 | extract_file (member); | |
494 | found = 1; | |
495 | } | |
496 | ||
cc481421 | 497 | member = member->archive_next; |
3a1a2036 NC |
498 | } |
499 | ||
500 | if (!found) | |
501 | { | |
70b0cf90 | 502 | bfd_openr (list->name, NULL); |
3a1a2036 NC |
503 | fprintf (stderr, _("%s: can't find module file %s\n"), |
504 | program_name, list->name); | |
505 | } | |
506 | ||
507 | list = list->next; | |
508 | } | |
252b5132 | 509 | } |
252b5132 | 510 | } |