PR binutils/13482
[deliverable/binutils-gdb.git] / binutils / ar.c
1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
5
6 This file is part of GNU Binutils.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
22 \f
23 /*
24 Bugs: GNU ar used to check file against filesystem in quick_update and
25 replace operations (would check mtime). Doesn't warn when name truncated.
26 No way to specify pos_end. Error messages should be more consistent. */
27
28 #include "sysdep.h"
29 #include "bfd.h"
30 #include "libiberty.h"
31 #include "progress.h"
32 #include "getopt.h"
33 #include "aout/ar.h"
34 #include "libbfd.h"
35 #include "bucomm.h"
36 #include "arsup.h"
37 #include "filenames.h"
38 #include "binemul.h"
39 #include "plugin.h"
40 #include <sys/stat.h>
41
42 #ifdef __GO32___
43 #define EXT_NAME_LEN 3 /* Bufflen of addition to name if it's MS-DOS. */
44 #else
45 #define EXT_NAME_LEN 6 /* Ditto for *NIX. */
46 #endif
47
48 /* Static declarations. */
49
50 static void mri_emul (void);
51 static const char *normalize (const char *, bfd *);
52 static void remove_output (void);
53 static void map_over_members (bfd *, void (*)(bfd *), char **, int);
54 static void print_contents (bfd * member);
55 static void delete_members (bfd *, char **files_to_delete);
56
57 static void move_members (bfd *, char **files_to_move);
58 static void replace_members
59 (bfd *, char **files_to_replace, bfd_boolean quick);
60 static void print_descr (bfd * abfd);
61 static void write_archive (bfd *);
62 static int ranlib_only (const char *archname);
63 static int ranlib_touch (const char *archname);
64 static void usage (int);
65 \f
66 /** Globals and flags. */
67
68 static int mri_mode;
69
70 /* This flag distinguishes between ar and ranlib:
71 1 means this is 'ranlib'; 0 means this is 'ar'.
72 -1 means if we should use argv[0] to decide. */
73 extern int is_ranlib;
74
75 /* Nonzero means don't warn about creating the archive file if necessary. */
76 int silent_create = 0;
77
78 /* Nonzero means describe each action performed. */
79 int verbose = 0;
80
81 /* Nonzero means preserve dates of members when extracting them. */
82 int preserve_dates = 0;
83
84 /* Nonzero means don't replace existing members whose dates are more recent
85 than the corresponding files. */
86 int newer_only = 0;
87
88 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
89 member). -1 means we've been explicitly asked to not write a symbol table;
90 +1 means we've been explicitly asked to write it;
91 0 is the default.
92 Traditionally, the default in BSD has been to not write the table.
93 However, for POSIX.2 compliance the default is now to write a symbol table
94 if any of the members are object files. */
95 int write_armap = 0;
96
97 /* Operate in deterministic mode: write zero for timestamps, uids,
98 and gids for archive members and the archive symbol table, and write
99 consistent file modes. */
100 int deterministic = -1; /* Determinism indeterminate. */
101
102 /* Nonzero means it's the name of an existing member; position new or moved
103 files with respect to this one. */
104 char *posname = NULL;
105
106 /* Sez how to use `posname': pos_before means position before that member.
107 pos_after means position after that member. pos_end means always at end.
108 pos_default means default appropriately. For the latter two, `posname'
109 should also be zero. */
110 enum pos
111 {
112 pos_default, pos_before, pos_after, pos_end
113 } postype = pos_default;
114
115 enum operations
116 {
117 none = 0, del, replace, print_table,
118 print_files, extract, move, quick_append
119 } operation = none;
120
121 static bfd **
122 get_pos_bfd (bfd **, enum pos, const char *);
123
124 /* For extract/delete only. If COUNTED_NAME_MODE is TRUE, we only
125 extract the COUNTED_NAME_COUNTER instance of that name. */
126 static bfd_boolean counted_name_mode = 0;
127 static int counted_name_counter = 0;
128
129 /* Whether to truncate names of files stored in the archive. */
130 static bfd_boolean ar_truncate = FALSE;
131
132 /* Whether to use a full file name match when searching an archive.
133 This is convenient for archives created by the Microsoft lib
134 program. */
135 static bfd_boolean full_pathname = FALSE;
136
137 /* Whether to create a "thin" archive (symbol index only -- no files). */
138 static bfd_boolean make_thin_archive = FALSE;
139
140 static int show_version = 0;
141
142 static int show_help = 0;
143
144 static const char *plugin_target = NULL;
145
146 static const char *target = NULL;
147
148 #define OPTION_PLUGIN 201
149 #define OPTION_TARGET 202
150
151 static struct option long_options[] =
152 {
153 {"help", no_argument, &show_help, 1},
154 {"plugin", required_argument, NULL, OPTION_PLUGIN},
155 {"target", required_argument, NULL, OPTION_TARGET},
156 {"version", no_argument, &show_version, 1},
157 {NULL, no_argument, NULL, 0}
158 };
159
160 int interactive = 0;
161
162 static void
163 mri_emul (void)
164 {
165 interactive = isatty (fileno (stdin));
166 yyparse ();
167 }
168
169 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
170 COUNT is the length of the FILES chain; FUNCTION is called on each entry
171 whose name matches one in FILES. */
172
173 static void
174 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
175 {
176 bfd *head;
177 int match_count;
178
179 if (count == 0)
180 {
181 for (head = arch->archive_next; head; head = head->archive_next)
182 {
183 PROGRESS (1);
184 function (head);
185 }
186 return;
187 }
188
189 /* This may appear to be a baroque way of accomplishing what we want.
190 However we have to iterate over the filenames in order to notice where
191 a filename is requested but does not exist in the archive. Ditto
192 mapping over each file each time -- we want to hack multiple
193 references. */
194
195 for (; count > 0; files++, count--)
196 {
197 bfd_boolean found = FALSE;
198
199 match_count = 0;
200 for (head = arch->archive_next; head; head = head->archive_next)
201 {
202 const char * filename;
203
204 PROGRESS (1);
205 filename = head->filename;
206 if (filename == NULL)
207 {
208 /* Some archive formats don't get the filenames filled in
209 until the elements are opened. */
210 struct stat buf;
211 bfd_stat_arch_elt (head, &buf);
212 }
213 else if (bfd_is_thin_archive (arch))
214 {
215 /* Thin archives store full pathnames. Need to normalize. */
216 filename = normalize (filename, arch);
217 }
218
219 if (filename != NULL
220 && !FILENAME_CMP (normalize (*files, arch), filename))
221 {
222 ++match_count;
223 if (counted_name_mode
224 && match_count != counted_name_counter)
225 {
226 /* Counting, and didn't match on count; go on to the
227 next one. */
228 continue;
229 }
230
231 found = TRUE;
232 function (head);
233 }
234 }
235
236 if (!found)
237 /* xgettext:c-format */
238 fprintf (stderr, _("no entry %s in archive\n"), *files);
239 }
240 }
241 \f
242 bfd_boolean operation_alters_arch = FALSE;
243
244 static void
245 usage (int help)
246 {
247 FILE *s;
248
249 s = help ? stdout : stderr;
250
251 #if BFD_SUPPORTS_PLUGINS
252 /* xgettext:c-format */
253 const char *command_line
254 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
255 " [--plugin <name>] [member-name] [count] archive-file file...\n");
256
257 #else
258 /* xgettext:c-format */
259 const char *command_line
260 = _("Usage: %s [emulation options] [-]{dmpqrstx}[abcDfilMNoPsSTuvV]"
261 " [member-name] [count] archive-file file...\n");
262 #endif
263 fprintf (s, command_line, program_name);
264
265 /* xgettext:c-format */
266 fprintf (s, _(" %s -M [<mri-script]\n"), program_name);
267 fprintf (s, _(" commands:\n"));
268 fprintf (s, _(" d - delete file(s) from the archive\n"));
269 fprintf (s, _(" m[ab] - move file(s) in the archive\n"));
270 fprintf (s, _(" p - print file(s) found in the archive\n"));
271 fprintf (s, _(" q[f] - quick append file(s) to the archive\n"));
272 fprintf (s, _(" r[ab][f][u] - replace existing or insert new file(s) into the archive\n"));
273 fprintf (s, _(" s - act as ranlib\n"));
274 fprintf (s, _(" t - display contents of archive\n"));
275 fprintf (s, _(" x[o] - extract file(s) from the archive\n"));
276 fprintf (s, _(" command specific modifiers:\n"));
277 fprintf (s, _(" [a] - put file(s) after [member-name]\n"));
278 fprintf (s, _(" [b] - put file(s) before [member-name] (same as [i])\n"));
279 if (DEFAULT_AR_DETERMINISTIC)
280 {
281 fprintf (s, _("\
282 [D] - use zero for timestamps and uids/gids (default)\n"));
283 fprintf (s, _("\
284 [U] - use actual timestamps and uids/gids\n"));
285 }
286 else
287 {
288 fprintf (s, _("\
289 [D] - use zero for timestamps and uids/gids\n"));
290 fprintf (s, _("\
291 [U] - use actual timestamps and uids/gids (default)\n"));
292 }
293 fprintf (s, _(" [N] - use instance [count] of name\n"));
294 fprintf (s, _(" [f] - truncate inserted file names\n"));
295 fprintf (s, _(" [P] - use full path names when matching\n"));
296 fprintf (s, _(" [o] - preserve original dates\n"));
297 fprintf (s, _(" [u] - only replace files that are newer than current archive contents\n"));
298 fprintf (s, _(" generic modifiers:\n"));
299 fprintf (s, _(" [c] - do not warn if the library had to be created\n"));
300 fprintf (s, _(" [s] - create an archive index (cf. ranlib)\n"));
301 fprintf (s, _(" [S] - do not build a symbol table\n"));
302 fprintf (s, _(" [T] - make a thin archive\n"));
303 fprintf (s, _(" [v] - be verbose\n"));
304 fprintf (s, _(" [V] - display the version number\n"));
305 fprintf (s, _(" @<file> - read options from <file>\n"));
306 fprintf (s, _(" --target=BFDNAME - specify the target object format as BFDNAME\n"));
307 #if BFD_SUPPORTS_PLUGINS
308 fprintf (s, _(" optional:\n"));
309 fprintf (s, _(" --plugin <p> - load the specified plugin\n"));
310 #endif
311
312 ar_emul_usage (s);
313
314 list_supported_targets (program_name, s);
315
316 if (REPORT_BUGS_TO[0] && help)
317 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
318
319 xexit (help ? 0 : 1);
320 }
321
322 static void
323 ranlib_usage (int help)
324 {
325 FILE *s;
326
327 s = help ? stdout : stderr;
328
329 /* xgettext:c-format */
330 fprintf (s, _("Usage: %s [options] archive\n"), program_name);
331 fprintf (s, _(" Generate an index to speed access to archives\n"));
332 fprintf (s, _(" The options are:\n\
333 @<file> Read options from <file>\n"));
334 #if BFD_SUPPORTS_PLUGINS
335 fprintf (s, _("\
336 --plugin <name> Load the specified plugin\n"));
337 #endif
338 if (DEFAULT_AR_DETERMINISTIC)
339 fprintf (s, _("\
340 -D Use zero for symbol map timestamp (default)\n\
341 -U Use an actual symbol map timestamp\n"));
342 else
343 fprintf (s, _("\
344 -D Use zero for symbol map timestamp\n\
345 -U Use actual symbol map timestamp (default)\n"));
346 fprintf (s, _("\
347 -t Update the archive's symbol map timestamp\n\
348 -h --help Print this help message\n\
349 -v --version Print version information\n"));
350
351 list_supported_targets (program_name, s);
352
353 if (REPORT_BUGS_TO[0] && help)
354 fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
355
356 xexit (help ? 0 : 1);
357 }
358
359 /* Normalize a file name specified on the command line into a file
360 name which we will use in an archive. */
361
362 static const char *
363 normalize (const char *file, bfd *abfd)
364 {
365 const char *filename;
366
367 if (full_pathname)
368 return file;
369
370 filename = lbasename (file);
371
372 if (ar_truncate
373 && abfd != NULL
374 && strlen (filename) > abfd->xvec->ar_max_namelen)
375 {
376 char *s;
377
378 /* Space leak. */
379 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
380 memcpy (s, filename, abfd->xvec->ar_max_namelen);
381 s[abfd->xvec->ar_max_namelen] = '\0';
382 filename = s;
383 }
384
385 return filename;
386 }
387
388 /* Remove any output file. This is only called via xatexit. */
389
390 static const char *output_filename = NULL;
391 static FILE *output_file = NULL;
392 static bfd *output_bfd = NULL;
393
394 static void
395 remove_output (void)
396 {
397 if (output_filename != NULL)
398 {
399 if (output_bfd != NULL)
400 bfd_cache_close (output_bfd);
401 if (output_file != NULL)
402 fclose (output_file);
403 unlink_if_ordinary (output_filename);
404 }
405 }
406
407 static char **
408 decode_options (int argc, char **argv)
409 {
410 int c;
411
412 /* Convert old-style tar call by exploding option element and rearranging
413 options accordingly. */
414
415 if (argc > 1 && argv[1][0] != '-')
416 {
417 int new_argc; /* argc value for rearranged arguments */
418 char **new_argv; /* argv value for rearranged arguments */
419 char *const *in; /* cursor into original argv */
420 char **out; /* cursor into rearranged argv */
421 const char *letter; /* cursor into old option letters */
422 char buffer[3]; /* constructed option buffer */
423
424 /* Initialize a constructed option. */
425
426 buffer[0] = '-';
427 buffer[2] = '\0';
428
429 /* Allocate a new argument array, and copy program name in it. */
430
431 new_argc = argc - 1 + strlen (argv[1]);
432 new_argv = xmalloc ((new_argc + 1) * sizeof (*argv));
433 in = argv;
434 out = new_argv;
435 *out++ = *in++;
436
437 /* Copy each old letter option as a separate option. */
438
439 for (letter = *in++; *letter; letter++)
440 {
441 buffer[1] = *letter;
442 *out++ = xstrdup (buffer);
443 }
444
445 /* Copy all remaining options. */
446
447 while (in < argv + argc)
448 *out++ = *in++;
449 *out = NULL;
450
451 /* Replace the old option list by the new one. */
452
453 argc = new_argc;
454 argv = new_argv;
455 }
456
457 while ((c = getopt_long (argc, argv, "hdmpqrtxlcoVsSuvabiMNfPTDU",
458 long_options, NULL)) != EOF)
459 {
460 switch (c)
461 {
462 case 'd':
463 case 'm':
464 case 'p':
465 case 'q':
466 case 'r':
467 case 't':
468 case 'x':
469 if (operation != none)
470 fatal (_("two different operation options specified"));
471 break;
472 }
473
474 switch (c)
475 {
476 case 'h':
477 show_help = 1;
478 break;
479 case 'd':
480 operation = del;
481 operation_alters_arch = TRUE;
482 break;
483 case 'm':
484 operation = move;
485 operation_alters_arch = TRUE;
486 break;
487 case 'p':
488 operation = print_files;
489 break;
490 case 'q':
491 operation = quick_append;
492 operation_alters_arch = TRUE;
493 break;
494 case 'r':
495 operation = replace;
496 operation_alters_arch = TRUE;
497 break;
498 case 't':
499 operation = print_table;
500 break;
501 case 'x':
502 operation = extract;
503 break;
504 case 'l':
505 break;
506 case 'c':
507 silent_create = 1;
508 break;
509 case 'o':
510 preserve_dates = 1;
511 break;
512 case 'V':
513 show_version = TRUE;
514 break;
515 case 's':
516 write_armap = 1;
517 break;
518 case 'S':
519 write_armap = -1;
520 break;
521 case 'u':
522 newer_only = 1;
523 break;
524 case 'v':
525 verbose = 1;
526 break;
527 case 'a':
528 postype = pos_after;
529 break;
530 case 'b':
531 postype = pos_before;
532 break;
533 case 'i':
534 postype = pos_before;
535 break;
536 case 'M':
537 mri_mode = 1;
538 break;
539 case 'N':
540 counted_name_mode = TRUE;
541 break;
542 case 'f':
543 ar_truncate = TRUE;
544 break;
545 case 'P':
546 full_pathname = TRUE;
547 break;
548 case 'T':
549 make_thin_archive = TRUE;
550 break;
551 case 'D':
552 deterministic = TRUE;
553 break;
554 case 'U':
555 deterministic = FALSE;
556 break;
557 case OPTION_PLUGIN:
558 #if BFD_SUPPORTS_PLUGINS
559 plugin_target = "plugin";
560 bfd_plugin_set_plugin (optarg);
561 #else
562 fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
563 xexit (1);
564 #endif
565 break;
566 case OPTION_TARGET:
567 target = optarg;
568 break;
569 case 0: /* A long option that just sets a flag. */
570 break;
571 default:
572 usage (0);
573 }
574 }
575
576 return &argv[optind];
577 }
578
579 /* If neither -D nor -U was not specified explicitly,
580 then use the configured default. */
581 static void
582 default_deterministic (void)
583 {
584 if (deterministic < 0)
585 deterministic = DEFAULT_AR_DETERMINISTIC;
586 }
587
588 static void
589 ranlib_main (int argc, char **argv)
590 {
591 int arg_index, status = 0;
592 bfd_boolean touch = FALSE;
593 int c;
594
595 while ((c = getopt_long (argc, argv, "DhHUvVt", long_options, NULL)) != EOF)
596 {
597 switch (c)
598 {
599 case 'D':
600 deterministic = TRUE;
601 break;
602 case 'U':
603 deterministic = FALSE;
604 break;
605 case 'h':
606 case 'H':
607 show_help = 1;
608 break;
609 case 't':
610 touch = TRUE;
611 break;
612 case 'v':
613 case 'V':
614 show_version = 1;
615 break;
616 }
617 }
618
619 if (argc < 2)
620 ranlib_usage (0);
621
622 if (show_help)
623 ranlib_usage (1);
624
625 if (show_version)
626 print_version ("ranlib");
627
628 default_deterministic ();
629
630 arg_index = optind;
631
632 while (arg_index < argc)
633 {
634 if (! touch)
635 status |= ranlib_only (argv[arg_index]);
636 else
637 status |= ranlib_touch (argv[arg_index]);
638 ++arg_index;
639 }
640
641 xexit (status);
642 }
643
644 int main (int, char **);
645
646 int
647 main (int argc, char **argv)
648 {
649 int arg_index;
650 char **files;
651 int file_count;
652 char *inarch_filename;
653 int i;
654
655 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
656 setlocale (LC_MESSAGES, "");
657 #endif
658 #if defined (HAVE_SETLOCALE)
659 setlocale (LC_CTYPE, "");
660 #endif
661 bindtextdomain (PACKAGE, LOCALEDIR);
662 textdomain (PACKAGE);
663
664 program_name = argv[0];
665 xmalloc_set_program_name (program_name);
666 #if BFD_SUPPORTS_PLUGINS
667 bfd_plugin_set_program_name (program_name);
668 #endif
669
670 expandargv (&argc, &argv);
671
672 if (is_ranlib < 0)
673 {
674 const char *temp = lbasename (program_name);
675
676 if (strlen (temp) >= 6
677 && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
678 is_ranlib = 1;
679 else
680 is_ranlib = 0;
681 }
682
683 START_PROGRESS (program_name, 0);
684
685 bfd_init ();
686 set_default_bfd_target ();
687
688 xatexit (remove_output);
689
690 for (i = 1; i < argc; i++)
691 if (! ar_emul_parse_arg (argv[i]))
692 break;
693 argv += (i - 1);
694 argc -= (i - 1);
695
696 if (is_ranlib)
697 ranlib_main (argc, argv);
698
699 if (argc < 2)
700 usage (0);
701
702 argv = decode_options (argc, argv);
703
704 if (show_help)
705 usage (1);
706
707 if (show_version)
708 print_version ("ar");
709
710 arg_index = 0;
711
712 if (mri_mode)
713 {
714 mri_emul ();
715 }
716 else
717 {
718 bfd *arch;
719
720 /* We don't use do_quick_append any more. Too many systems
721 expect ar to always rebuild the symbol table even when q is
722 used. */
723
724 /* We can't write an armap when using ar q, so just do ar r
725 instead. */
726 if (operation == quick_append && write_armap)
727 operation = replace;
728
729 if ((operation == none || operation == print_table)
730 && write_armap == 1)
731 xexit (ranlib_only (argv[arg_index]));
732
733 if (operation == none)
734 fatal (_("no operation specified"));
735
736 if (newer_only && operation != replace)
737 fatal (_("`u' is only meaningful with the `r' option."));
738
739 if (newer_only && deterministic > 0)
740 fatal (_("`u' is not meaningful with the `D' option."));
741
742 if (newer_only && deterministic < 0 && DEFAULT_AR_DETERMINISTIC)
743 non_fatal (_("\
744 `u' modifier ignored since `D' is the default (see `U')"));
745
746 default_deterministic ();
747
748 if (postype != pos_default)
749 posname = argv[arg_index++];
750
751 if (counted_name_mode)
752 {
753 if (operation != extract && operation != del)
754 fatal (_("`N' is only meaningful with the `x' and `d' options."));
755 counted_name_counter = atoi (argv[arg_index++]);
756 if (counted_name_counter <= 0)
757 fatal (_("Value for `N' must be positive."));
758 }
759
760 inarch_filename = argv[arg_index++];
761
762 for (file_count = 0; argv[arg_index + file_count] != NULL; file_count++)
763 continue;
764
765 files = (file_count > 0) ? argv + arg_index : NULL;
766
767 arch = open_inarch (inarch_filename,
768 files == NULL ? (char *) NULL : files[0]);
769
770 if (operation == extract && bfd_is_thin_archive (arch))
771 fatal (_("`x' cannot be used on thin archives."));
772
773 switch (operation)
774 {
775 case print_table:
776 map_over_members (arch, print_descr, files, file_count);
777 break;
778
779 case print_files:
780 map_over_members (arch, print_contents, files, file_count);
781 break;
782
783 case extract:
784 map_over_members (arch, extract_file, files, file_count);
785 break;
786
787 case del:
788 if (files != NULL)
789 delete_members (arch, files);
790 else
791 output_filename = NULL;
792 break;
793
794 case move:
795 /* PR 12558: Creating and moving at the same time does
796 not make sense. Just create the archive instead. */
797 if (! silent_create)
798 {
799 if (files != NULL)
800 move_members (arch, files);
801 else
802 output_filename = NULL;
803 break;
804 }
805 /* Fall through. */
806
807 case replace:
808 case quick_append:
809 if (files != NULL || write_armap > 0)
810 replace_members (arch, files, operation == quick_append);
811 else
812 output_filename = NULL;
813 break;
814
815 /* Shouldn't happen! */
816 default:
817 /* xgettext:c-format */
818 fatal (_("internal error -- this option not implemented"));
819 }
820 }
821
822 END_PROGRESS (program_name);
823
824 xexit (0);
825 return 0;
826 }
827
828 bfd *
829 open_inarch (const char *archive_filename, const char *file)
830 {
831 bfd **last_one;
832 bfd *next_one;
833 struct stat sbuf;
834 bfd *arch;
835 char **matching;
836
837 bfd_set_error (bfd_error_no_error);
838
839 if (target == NULL)
840 target = plugin_target;
841
842 if (stat (archive_filename, &sbuf) != 0)
843 {
844 #if !defined(__GO32__) || defined(__DJGPP__)
845
846 /* FIXME: I don't understand why this fragment was ifndef'ed
847 away for __GO32__; perhaps it was in the days of DJGPP v1.x.
848 stat() works just fine in v2.x, so I think this should be
849 removed. For now, I enable it for DJGPP v2. -- EZ. */
850
851 /* KLUDGE ALERT! Temporary fix until I figger why
852 stat() is wrong ... think it's buried in GO32's IDT - Jax */
853 if (errno != ENOENT)
854 bfd_fatal (archive_filename);
855 #endif
856
857 if (!operation_alters_arch)
858 {
859 fprintf (stderr, "%s: ", program_name);
860 perror (archive_filename);
861 maybequit ();
862 return NULL;
863 }
864
865 /* If the target isn't set, try to figure out the target to use
866 for the archive from the first object on the list. */
867 if (target == NULL && file != NULL)
868 {
869 bfd *obj;
870
871 obj = bfd_openr (file, target);
872 if (obj != NULL)
873 {
874 if (bfd_check_format (obj, bfd_object))
875 target = bfd_get_target (obj);
876 (void) bfd_close (obj);
877 }
878 }
879
880 /* Create an empty archive. */
881 arch = bfd_openw (archive_filename, target);
882 if (arch == NULL
883 || ! bfd_set_format (arch, bfd_archive)
884 || ! bfd_close (arch))
885 bfd_fatal (archive_filename);
886 else if (!silent_create)
887 non_fatal (_("creating %s"), archive_filename);
888
889 /* If we die creating a new archive, don't leave it around. */
890 output_filename = archive_filename;
891 }
892
893 arch = bfd_openr (archive_filename, target);
894 if (arch == NULL)
895 {
896 bloser:
897 bfd_fatal (archive_filename);
898 }
899
900 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
901 {
902 bfd_nonfatal (archive_filename);
903 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
904 {
905 list_matching_formats (matching);
906 free (matching);
907 }
908 xexit (1);
909 }
910
911 last_one = &(arch->archive_next);
912 /* Read all the contents right away, regardless. */
913 for (next_one = bfd_openr_next_archived_file (arch, NULL);
914 next_one;
915 next_one = bfd_openr_next_archived_file (arch, next_one))
916 {
917 PROGRESS (1);
918 *last_one = next_one;
919 last_one = &next_one->archive_next;
920 }
921 *last_one = (bfd *) NULL;
922 if (bfd_get_error () != bfd_error_no_more_archived_files)
923 goto bloser;
924 return arch;
925 }
926
927 static void
928 print_contents (bfd *abfd)
929 {
930 size_t ncopied = 0;
931 char *cbuf = (char *) xmalloc (BUFSIZE);
932 struct stat buf;
933 size_t size;
934 if (bfd_stat_arch_elt (abfd, &buf) != 0)
935 /* xgettext:c-format */
936 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
937
938 if (verbose)
939 printf ("\n<%s>\n\n", bfd_get_filename (abfd));
940
941 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
942
943 size = buf.st_size;
944 while (ncopied < size)
945 {
946
947 size_t nread;
948 size_t tocopy = size - ncopied;
949 if (tocopy > BUFSIZE)
950 tocopy = BUFSIZE;
951
952 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
953 if (nread != tocopy)
954 /* xgettext:c-format */
955 fatal (_("%s is not a valid archive"),
956 bfd_get_filename (bfd_my_archive (abfd)));
957
958 /* fwrite in mingw32 may return int instead of size_t. Cast the
959 return value to size_t to avoid comparison between signed and
960 unsigned values. */
961 if ((size_t) fwrite (cbuf, 1, nread, stdout) != nread)
962 fatal ("stdout: %s", strerror (errno));
963 ncopied += tocopy;
964 }
965 free (cbuf);
966 }
967
968 /* Extract a member of the archive into its own file.
969
970 We defer opening the new file until after we have read a BUFSIZ chunk of the
971 old one, since we know we have just read the archive header for the old
972 one. Since most members are shorter than BUFSIZ, this means we will read
973 the old header, read the old data, write a new inode for the new file, and
974 write the new data, and be done. This 'optimization' is what comes from
975 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
976 Gilmore */
977
978 void
979 extract_file (bfd *abfd)
980 {
981 FILE *ostream;
982 char *cbuf = (char *) xmalloc (BUFSIZE);
983 size_t nread, tocopy;
984 size_t ncopied = 0;
985 size_t size;
986 struct stat buf;
987
988 if (bfd_stat_arch_elt (abfd, &buf) != 0)
989 /* xgettext:c-format */
990 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
991 size = buf.st_size;
992
993 if (verbose)
994 printf ("x - %s\n", bfd_get_filename (abfd));
995
996 bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
997
998 ostream = NULL;
999 if (size == 0)
1000 {
1001 /* Seems like an abstraction violation, eh? Well it's OK! */
1002 output_filename = bfd_get_filename (abfd);
1003
1004 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
1005 if (ostream == NULL)
1006 {
1007 perror (bfd_get_filename (abfd));
1008 xexit (1);
1009 }
1010
1011 output_file = ostream;
1012 }
1013 else
1014 while (ncopied < size)
1015 {
1016 tocopy = size - ncopied;
1017 if (tocopy > BUFSIZE)
1018 tocopy = BUFSIZE;
1019
1020 nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
1021 if (nread != tocopy)
1022 /* xgettext:c-format */
1023 fatal (_("%s is not a valid archive"),
1024 bfd_get_filename (bfd_my_archive (abfd)));
1025
1026 /* See comment above; this saves disk arm motion */
1027 if (ostream == NULL)
1028 {
1029 /* Seems like an abstraction violation, eh? Well it's OK! */
1030 output_filename = bfd_get_filename (abfd);
1031
1032 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
1033 if (ostream == NULL)
1034 {
1035 perror (bfd_get_filename (abfd));
1036 xexit (1);
1037 }
1038
1039 output_file = ostream;
1040 }
1041
1042 /* fwrite in mingw32 may return int instead of size_t. Cast
1043 the return value to size_t to avoid comparison between
1044 signed and unsigned values. */
1045 if ((size_t) fwrite (cbuf, 1, nread, ostream) != nread)
1046 fatal ("%s: %s", output_filename, strerror (errno));
1047 ncopied += tocopy;
1048 }
1049
1050 if (ostream != NULL)
1051 fclose (ostream);
1052
1053 output_file = NULL;
1054 output_filename = NULL;
1055
1056 chmod (bfd_get_filename (abfd), buf.st_mode);
1057
1058 if (preserve_dates)
1059 {
1060 /* Set access time to modification time. Only st_mtime is
1061 initialized by bfd_stat_arch_elt. */
1062 buf.st_atime = buf.st_mtime;
1063 set_times (bfd_get_filename (abfd), &buf);
1064 }
1065
1066 free (cbuf);
1067 }
1068
1069 static void
1070 write_archive (bfd *iarch)
1071 {
1072 bfd *obfd;
1073 char *old_name, *new_name;
1074 bfd *contents_head = iarch->archive_next;
1075
1076 old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
1077 strcpy (old_name, bfd_get_filename (iarch));
1078 new_name = make_tempname (old_name);
1079
1080 if (new_name == NULL)
1081 bfd_fatal (_("could not create temporary file whilst writing archive"));
1082
1083 output_filename = new_name;
1084
1085 obfd = bfd_openw (new_name, bfd_get_target (iarch));
1086
1087 if (obfd == NULL)
1088 bfd_fatal (old_name);
1089
1090 output_bfd = obfd;
1091
1092 bfd_set_format (obfd, bfd_archive);
1093
1094 /* Request writing the archive symbol table unless we've
1095 been explicitly requested not to. */
1096 obfd->has_armap = write_armap >= 0;
1097
1098 if (ar_truncate)
1099 {
1100 /* This should really use bfd_set_file_flags, but that rejects
1101 archives. */
1102 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1103 }
1104
1105 if (deterministic)
1106 obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
1107
1108 if (make_thin_archive || bfd_is_thin_archive (iarch))
1109 bfd_is_thin_archive (obfd) = 1;
1110
1111 if (!bfd_set_archive_head (obfd, contents_head))
1112 bfd_fatal (old_name);
1113
1114 if (!bfd_close (obfd))
1115 bfd_fatal (old_name);
1116
1117 output_bfd = NULL;
1118 output_filename = NULL;
1119
1120 /* We don't care if this fails; we might be creating the archive. */
1121 bfd_close (iarch);
1122
1123 if (smart_rename (new_name, old_name, 0) != 0)
1124 xexit (1);
1125 free (old_name);
1126 }
1127
1128 /* Return a pointer to the pointer to the entry which should be rplacd'd
1129 into when altering. DEFAULT_POS should be how to interpret pos_default,
1130 and should be a pos value. */
1131
1132 static bfd **
1133 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
1134 {
1135 bfd **after_bfd = contents;
1136 enum pos realpos;
1137 const char *realposname;
1138
1139 if (postype == pos_default)
1140 {
1141 realpos = default_pos;
1142 realposname = default_posname;
1143 }
1144 else
1145 {
1146 realpos = postype;
1147 realposname = posname;
1148 }
1149
1150 if (realpos == pos_end)
1151 {
1152 while (*after_bfd)
1153 after_bfd = &((*after_bfd)->archive_next);
1154 }
1155 else
1156 {
1157 for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
1158 if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1159 {
1160 if (realpos == pos_after)
1161 after_bfd = &(*after_bfd)->archive_next;
1162 break;
1163 }
1164 }
1165 return after_bfd;
1166 }
1167
1168 static void
1169 delete_members (bfd *arch, char **files_to_delete)
1170 {
1171 bfd **current_ptr_ptr;
1172 bfd_boolean found;
1173 bfd_boolean something_changed = FALSE;
1174 int match_count;
1175
1176 for (; *files_to_delete != NULL; ++files_to_delete)
1177 {
1178 /* In a.out systems, the armap is optional. It's also called
1179 __.SYMDEF. So if the user asked to delete it, we should remember
1180 that fact. This isn't quite right for COFF systems (where
1181 __.SYMDEF might be regular member), but it's very unlikely
1182 to be a problem. FIXME */
1183
1184 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1185 {
1186 arch->has_armap = FALSE;
1187 write_armap = -1;
1188 continue;
1189 }
1190
1191 found = FALSE;
1192 match_count = 0;
1193 current_ptr_ptr = &(arch->archive_next);
1194 while (*current_ptr_ptr)
1195 {
1196 if (FILENAME_CMP (normalize (*files_to_delete, arch),
1197 (*current_ptr_ptr)->filename) == 0)
1198 {
1199 ++match_count;
1200 if (counted_name_mode
1201 && match_count != counted_name_counter)
1202 {
1203 /* Counting, and didn't match on count; go on to the
1204 next one. */
1205 }
1206 else
1207 {
1208 found = TRUE;
1209 something_changed = TRUE;
1210 if (verbose)
1211 printf ("d - %s\n",
1212 *files_to_delete);
1213 *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
1214 goto next_file;
1215 }
1216 }
1217
1218 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1219 }
1220
1221 if (verbose && !found)
1222 {
1223 /* xgettext:c-format */
1224 printf (_("No member named `%s'\n"), *files_to_delete);
1225 }
1226 next_file:
1227 ;
1228 }
1229
1230 if (something_changed)
1231 write_archive (arch);
1232 else
1233 output_filename = NULL;
1234 }
1235
1236
1237 /* Reposition existing members within an archive */
1238
1239 static void
1240 move_members (bfd *arch, char **files_to_move)
1241 {
1242 bfd **after_bfd; /* New entries go after this one */
1243 bfd **current_ptr_ptr; /* cdr pointer into contents */
1244
1245 for (; *files_to_move; ++files_to_move)
1246 {
1247 current_ptr_ptr = &(arch->archive_next);
1248 while (*current_ptr_ptr)
1249 {
1250 bfd *current_ptr = *current_ptr_ptr;
1251 if (FILENAME_CMP (normalize (*files_to_move, arch),
1252 current_ptr->filename) == 0)
1253 {
1254 /* Move this file to the end of the list - first cut from
1255 where it is. */
1256 bfd *link_bfd;
1257 *current_ptr_ptr = current_ptr->archive_next;
1258
1259 /* Now glue to end */
1260 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1261 link_bfd = *after_bfd;
1262 *after_bfd = current_ptr;
1263 current_ptr->archive_next = link_bfd;
1264
1265 if (verbose)
1266 printf ("m - %s\n", *files_to_move);
1267
1268 goto next_file;
1269 }
1270
1271 current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1272 }
1273 /* xgettext:c-format */
1274 fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1275
1276 next_file:;
1277 }
1278
1279 write_archive (arch);
1280 }
1281
1282 /* Ought to default to replacing in place, but this is existing practice! */
1283
1284 static void
1285 replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
1286 {
1287 bfd_boolean changed = FALSE;
1288 bfd **after_bfd; /* New entries go after this one. */
1289 bfd *current;
1290 bfd **current_ptr;
1291
1292 while (files_to_move && *files_to_move)
1293 {
1294 if (! quick)
1295 {
1296 current_ptr = &arch->archive_next;
1297 while (*current_ptr)
1298 {
1299 current = *current_ptr;
1300
1301 /* For compatibility with existing ar programs, we
1302 permit the same file to be added multiple times. */
1303 if (FILENAME_CMP (normalize (*files_to_move, arch),
1304 normalize (current->filename, arch)) == 0
1305 && current->arelt_data != NULL)
1306 {
1307 if (newer_only)
1308 {
1309 struct stat fsbuf, asbuf;
1310
1311 if (stat (*files_to_move, &fsbuf) != 0)
1312 {
1313 if (errno != ENOENT)
1314 bfd_fatal (*files_to_move);
1315 goto next_file;
1316 }
1317 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1318 /* xgettext:c-format */
1319 fatal (_("internal stat error on %s"),
1320 current->filename);
1321
1322 if (fsbuf.st_mtime <= asbuf.st_mtime)
1323 goto next_file;
1324 }
1325
1326 after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
1327 current->filename);
1328 if (ar_emul_replace (after_bfd, *files_to_move,
1329 target, verbose))
1330 {
1331 /* Snip out this entry from the chain. */
1332 *current_ptr = (*current_ptr)->archive_next;
1333 changed = TRUE;
1334 }
1335
1336 goto next_file;
1337 }
1338 current_ptr = &(current->archive_next);
1339 }
1340 }
1341
1342 /* Add to the end of the archive. */
1343 after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1344
1345 if (ar_emul_append (after_bfd, *files_to_move, target,
1346 verbose, make_thin_archive))
1347 changed = TRUE;
1348
1349 next_file:;
1350
1351 files_to_move++;
1352 }
1353
1354 if (changed)
1355 write_archive (arch);
1356 else
1357 output_filename = NULL;
1358 }
1359
1360 static int
1361 ranlib_only (const char *archname)
1362 {
1363 bfd *arch;
1364
1365 if (get_file_size (archname) < 1)
1366 return 1;
1367 write_armap = 1;
1368 arch = open_inarch (archname, (char *) NULL);
1369 if (arch == NULL)
1370 xexit (1);
1371 write_archive (arch);
1372 return 0;
1373 }
1374
1375 /* Update the timestamp of the symbol map of an archive. */
1376
1377 static int
1378 ranlib_touch (const char *archname)
1379 {
1380 #ifdef __GO32__
1381 /* I don't think updating works on go32. */
1382 ranlib_only (archname);
1383 #else
1384 int f;
1385 bfd *arch;
1386 char **matching;
1387
1388 if (get_file_size (archname) < 1)
1389 return 1;
1390 f = open (archname, O_RDWR | O_BINARY, 0);
1391 if (f < 0)
1392 {
1393 bfd_set_error (bfd_error_system_call);
1394 bfd_fatal (archname);
1395 }
1396
1397 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1398 if (arch == NULL)
1399 bfd_fatal (archname);
1400 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1401 {
1402 bfd_nonfatal (archname);
1403 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1404 {
1405 list_matching_formats (matching);
1406 free (matching);
1407 }
1408 xexit (1);
1409 }
1410
1411 if (! bfd_has_map (arch))
1412 /* xgettext:c-format */
1413 fatal (_("%s: no archive map to update"), archname);
1414
1415 if (deterministic)
1416 arch->flags |= BFD_DETERMINISTIC_OUTPUT;
1417
1418 bfd_update_armap_timestamp (arch);
1419
1420 if (! bfd_close (arch))
1421 bfd_fatal (archname);
1422 #endif
1423 return 0;
1424 }
1425
1426 /* Things which are interesting to map over all or some of the files: */
1427
1428 static void
1429 print_descr (bfd *abfd)
1430 {
1431 print_arelt_descr (stdout, abfd, verbose);
1432 }
This page took 0.059753 seconds and 4 git commands to generate.