pr27270 and pr27284, ar segfaults and wrong file mode
[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 real_ofd;
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 bfd_boolean 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, &real_ofd);
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, real_ofd);
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 bfd_boolean skip_stat = FALSE;
347 struct stat target_stat;
348 int ofd = real_ofd;
349
350 if (deterministic > 0)
351 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
352
353 #if !defined (_WIN32) || defined (__CYGWIN32__)
354 /* It's OK to fail; at worst it will result in SMART_RENAME using a slow
355 copy fallback to write the output. */
356 ofd = dup (ofd);
357 #endif
358 bfd_close (obfd);
359
360 if (lstat (real_name, &target_stat) != 0)
361 {
362 /* The temp file created in ar_open has mode 0600 as per mkstemp.
363 Create the real empty output file here so smart_rename will
364 update the mode according to the process umask. */
365 obfd = bfd_openw (real_name, NULL);
366 if (obfd == NULL
367 || bfd_stat (obfd, &target_stat) != 0)
368 skip_stat = TRUE;
369 if (obfd != NULL)
370 {
371 bfd_set_format (obfd, bfd_archive);
372 bfd_close (obfd);
373 }
374 }
375
376 smart_rename (temp_name, real_name, ofd,
377 skip_stat ? NULL : &target_stat, 0);
378 obfd = 0;
379 free (temp_name);
380 free (real_name);
381 }
382 }
383
384 void
385 ar_replace (struct list *list)
386 {
387 if (!obfd)
388 {
389 fprintf (stderr, _("%s: no open output archive\n"), program_name);
390 maybequit ();
391 }
392 else
393 {
394 while (list)
395 {
396 /* Find this name in the archive. */
397 bfd *member = obfd->archive_head;
398 bfd **prev = &(obfd->archive_head);
399 int found = 0;
400
401 while (member)
402 {
403 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
404 {
405 /* Found the one to replace. */
406 bfd *abfd = bfd_openr (list->name, NULL);
407
408 if (!abfd)
409 {
410 fprintf (stderr, _("%s: can't open file %s\n"),
411 program_name, list->name);
412 maybequit ();
413 }
414 else
415 {
416 *prev = abfd;
417 abfd->archive_next = member->archive_next;
418 found = 1;
419 }
420 }
421 else
422 {
423 prev = &(member->archive_next);
424 }
425 member = member->archive_next;
426 }
427
428 if (!found)
429 {
430 bfd *abfd = bfd_openr (list->name, NULL);
431
432 fprintf (stderr,_("%s: can't find module file %s\n"),
433 program_name, list->name);
434 if (!abfd)
435 {
436 fprintf (stderr, _("%s: can't open file %s\n"),
437 program_name, list->name);
438 maybequit ();
439 }
440 else
441 *prev = abfd;
442 }
443
444 list = list->next;
445 }
446 }
447 }
448
449 /* And I added this one. */
450 void
451 ar_list (void)
452 {
453 if (!obfd)
454 {
455 fprintf (stderr, _("%s: no open output archive\n"), program_name);
456 maybequit ();
457 }
458 else
459 {
460 bfd *abfd;
461
462 outfile = stdout;
463 verbose =1 ;
464 printf (_("Current open archive is %s\n"), bfd_get_filename (obfd));
465
466 for (abfd = obfd->archive_head;
467 abfd != (bfd *)NULL;
468 abfd = abfd->archive_next)
469 ar_directory_doer (abfd, (bfd *) NULL);
470 }
471 }
472
473 void
474 ar_end (void)
475 {
476 if (obfd)
477 {
478 bfd_cache_close (obfd);
479 unlink (bfd_get_filename (obfd));
480 }
481 }
482
483 void
484 ar_extract (struct list *list)
485 {
486 if (!obfd)
487 {
488 fprintf (stderr, _("%s: no open archive\n"), program_name);
489 maybequit ();
490 }
491 else
492 {
493 while (list)
494 {
495 /* Find this name in the archive. */
496 bfd *member = obfd->archive_head;
497 int found = 0;
498
499 while (member && !found)
500 {
501 if (FILENAME_CMP (bfd_get_filename (member), list->name) == 0)
502 {
503 extract_file (member);
504 found = 1;
505 }
506
507 member = member->archive_next;
508 }
509
510 if (!found)
511 {
512 bfd_openr (list->name, NULL);
513 fprintf (stderr, _("%s: can't find module file %s\n"),
514 program_name, list->name);
515 }
516
517 list = list->next;
518 }
519 }
520 }
This page took 0.088192 seconds and 5 git commands to generate.