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