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