*** empty log message ***
[binutils.git] / binutils / ar.c
blobc951ef7b45079a8d0fba5751b74e3e385898dae4
1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
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.
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.
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. */
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.
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>
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
43 #define BUFSIZE 8192
45 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
47 struct ar_hdr *
48 bfd_special_undocumented_glue PARAMS ((bfd * abfd, const char *filename));
50 /* Static declarations */
52 static void
53 mri_emul PARAMS ((void));
55 static const char *
56 normalize PARAMS ((const char *, bfd *));
58 static void
59 remove_output PARAMS ((void));
61 static void
62 map_over_members PARAMS ((bfd *, void (*)(bfd *), char **, int));
64 static void
65 print_contents PARAMS ((bfd * member));
67 static void
68 delete_members PARAMS ((bfd *, char **files_to_delete));
70 #if 0
71 static void
72 do_quick_append PARAMS ((const char *archive_filename,
73 char **files_to_append));
74 #endif
76 static void
77 move_members PARAMS ((bfd *, char **files_to_move));
79 static void
80 replace_members PARAMS ((bfd *, char **files_to_replace, boolean quick));
82 static void
83 print_descr PARAMS ((bfd * abfd));
85 static void
86 write_archive PARAMS ((bfd *));
88 static void
89 ranlib_only PARAMS ((const char *archname));
91 static void
92 ranlib_touch PARAMS ((const char *archname));
94 static void
95 usage PARAMS ((int));
97 /** Globals and flags */
99 int mri_mode;
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;
106 /* Nonzero means don't warn about creating the archive file if necessary. */
107 int silent_create = 0;
109 /* Nonzero means describe each action performed. */
110 int verbose = 0;
112 /* Nonzero means preserve dates of members when extracting them. */
113 int preserve_dates = 0;
115 /* Nonzero means don't replace existing members whose dates are more recent
116 than the corresponding files. */
117 int newer_only = 0;
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;
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;
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
138 pos_default, pos_before, pos_after, pos_end
139 } postype = pos_default;
141 static bfd **
142 get_pos_bfd PARAMS ((bfd **, enum pos, const char *));
144 /* Whether to truncate names of files stored in the archive. */
145 static boolean ar_truncate = false;
147 int interactive = 0;
149 static void
150 mri_emul ()
152 interactive = isatty (fileno (stdin));
153 yyparse ();
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. */
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;
167 bfd *head;
169 if (count == 0)
171 for (head = arch->next; head; head = head->next)
173 PROGRESS (1);
174 function (head);
176 return;
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. */
184 for (; count > 0; files++, count--)
186 boolean found = false;
188 for (head = arch->next; head; head = head->next)
190 PROGRESS (1);
191 if (head->filename == NULL)
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);
198 if ((head->filename != NULL) &&
199 (!strcmp (*files, head->filename)))
201 found = true;
202 function (head);
205 if (!found)
206 /* xgettext:c-format */
207 fprintf (stderr, _("no entry %s in archive\n"), *files);
211 boolean operation_alters_arch = false;
213 static void
214 usage (help)
215 int help;
217 FILE *s;
219 s = help ? stdout : stderr;
221 if (! is_ranlib)
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"));
248 else
249 /* xgettext:c-format */
250 fprintf (s, _("Usage: %s [-vV] archive\n"), program_name);
252 list_supported_targets (program_name, stderr);
254 if (help)
255 fprintf (s, _("Report bugs to bug-gnu-utils@gnu.org\n"));
257 xexit (help ? 0 : 1);
260 /* Normalize a file name specified on the command line into a file
261 name which we will use in an archive. */
263 static const char *
264 normalize (file, abfd)
265 const char *file;
266 bfd *abfd;
268 const char *filename;
270 filename = strrchr (file, '/');
271 if (filename != (char *) NULL)
272 filename++;
273 else
274 filename = file;
276 if (ar_truncate
277 && abfd != NULL
278 && strlen (filename) > abfd->xvec->ar_max_namelen)
280 char *s;
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;
289 return filename;
292 /* Remove any output file. This is only called via xatexit. */
294 static char *output_filename = NULL;
295 static FILE *output_file = NULL;
296 static bfd *output_bfd = NULL;
298 static void
299 remove_output ()
301 if (output_filename != NULL)
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);
311 /* The option parsing should be in its own function.
312 It will be when I have getopt working. */
315 main (argc, argv)
316 int argc;
317 char **argv;
319 char *arg_ptr;
320 char c;
321 enum
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;
331 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
332 setlocale (LC_MESSAGES, "");
333 #endif
334 bindtextdomain (PACKAGE, LOCALEDIR);
335 textdomain (PACKAGE);
337 program_name = argv[0];
338 xmalloc_set_program_name (program_name);
340 if (is_ranlib < 0)
342 char *temp;
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;
356 if (argc > 1 && argv[1][0] == '-')
358 if (strcmp (argv[1], "--help") == 0)
359 usage (1);
360 else if (strcmp (argv[1], "--version") == 0)
362 if (is_ranlib)
363 print_version ("ranlib");
364 else
365 print_version ("ar");
369 START_PROGRESS (program_name, 0);
371 bfd_init ();
372 set_default_bfd_target ();
374 show_version = 0;
376 xatexit (remove_output);
378 if (is_ranlib)
380 boolean touch = false;
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)
391 ++arg_index;
392 touch = true;
394 while (arg_index < argc)
396 if (! touch)
397 ranlib_only (argv[arg_index]);
398 else
399 ranlib_touch (argv[arg_index]);
400 ++arg_index;
402 xexit (0);
405 if (argc == 2 && strcmp (argv[1], "-M") == 0)
407 mri_emul ();
408 xexit (0);
411 if (argc < 2)
412 usage (0);
414 arg_ptr = argv[1];
416 if (*arg_ptr == '-')
417 ++arg_ptr; /* compatibility */
419 while ((c = *arg_ptr++) != '\0')
421 switch (c)
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)
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;
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);
505 if (show_version)
506 print_version ("ar");
508 if (argc < 3)
509 usage (0);
511 if (mri_mode)
513 mri_emul ();
515 else
517 bfd *arch;
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;
524 if ((operation == none || operation == print_table)
525 && write_armap == 1)
527 ranlib_only (argv[2]);
528 xexit (0);
531 if (operation == none)
532 fatal (_("no operation specified"));
534 if (newer_only && operation != replace)
535 fatal (_("`u' is only meaningful with the `r' option."));
537 arg_index = 2;
539 if (postype != pos_default)
540 posname = argv[arg_index++];
542 inarch_filename = argv[arg_index++];
544 files = arg_index < argc ? argv + arg_index : NULL;
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. */
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)
558 char **chk;
560 for (chk = files; chk != NULL && *chk != '\0'; chk++)
562 if (strlen (normalize (*chk, (bfd *) NULL)) > 14)
564 operation = replace;
565 break;
570 if (operation == quick_append)
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);
577 #endif
579 arch = open_inarch (inarch_filename,
580 files == NULL ? (char *) NULL : files[0]);
582 switch (operation)
584 case print_table:
585 map_over_members (arch, print_descr, files, argc - 3);
586 break;
588 case print_files:
589 map_over_members (arch, print_contents, files, argc - 3);
590 break;
592 case extract:
593 map_over_members (arch, extract_file, files, argc - 3);
594 break;
596 case delete:
597 if (files != NULL)
598 delete_members (arch, files);
599 break;
601 case move:
602 if (files != NULL)
603 move_members (arch, files);
604 break;
606 case replace:
607 case quick_append:
608 if (files != NULL || write_armap > 0)
609 replace_members (arch, files, operation == quick_append);
610 break;
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);
621 END_PROGRESS (program_name);
623 xexit (0);
624 return 0;
627 bfd *
628 open_inarch (archive_filename, file)
629 const char *archive_filename;
630 const char *file;
632 const char *target;
633 bfd **last_one;
634 bfd *next_one;
635 struct stat sbuf;
636 bfd *arch;
637 char **matching;
639 bfd_set_error (bfd_error_no_error);
641 target = NULL;
643 if (stat (archive_filename, &sbuf) != 0)
645 #ifndef __GO32__
647 /* KLUDGE ALERT! Temporary fix until I figger why
648 * stat() is wrong ... think it's buried in GO32's IDT
649 * - Jax
651 if (errno != ENOENT)
652 bfd_fatal (archive_filename);
653 #endif
655 if (!operation_alters_arch)
657 fprintf (stderr, "%s: ", program_name);
658 perror (archive_filename);
659 maybequit ();
660 return NULL;
663 /* Try to figure out the target to use for the archive from the
664 first object on the list. */
665 if (file != NULL)
667 bfd *obj;
669 obj = bfd_openr (file, NULL);
670 if (obj != NULL)
672 if (bfd_check_format (obj, bfd_object))
673 target = bfd_get_target (obj);
674 (void) bfd_close (obj);
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);
686 arch = bfd_openr (archive_filename, target);
687 if (arch == NULL)
689 bloser:
690 bfd_fatal (archive_filename);
693 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
695 bfd_nonfatal (archive_filename);
696 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
698 list_matching_formats (matching);
699 free (matching);
701 xexit (1);
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))
710 PROGRESS (1);
711 *last_one = next_one;
712 last_one = &next_one->next;
714 *last_one = (bfd *) NULL;
715 if (bfd_get_error () != bfd_error_no_more_archived_files)
716 goto bloser;
717 return arch;
720 static void
721 print_contents (abfd)
722 bfd *abfd;
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));
732 if (verbose)
733 /* xgettext:c-format */
734 printf (_("\n<member %s>\n\n"), bfd_get_filename (abfd));
736 bfd_seek (abfd, 0, SEEK_SET);
738 size = buf.st_size;
739 while (ncopied < size)
742 int nread;
743 int tocopy = size - ncopied;
744 if (tocopy > BUFSIZE)
745 tocopy = BUFSIZE;
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;
756 free (cbuf);
759 /* Extract a member of the archive into its own file.
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 */
769 void
770 extract_file (abfd)
771 bfd *abfd;
773 FILE *ostream;
774 char *cbuf = xmalloc (BUFSIZE);
775 int nread, tocopy;
776 long ncopied = 0;
777 long size;
778 struct stat buf;
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;
785 if (size < 0)
786 /* xgettext:c-format */
787 fatal (_("stat returns negative size for %s"), bfd_get_filename (abfd));
789 if (verbose)
790 printf ("x - %s\n", bfd_get_filename (abfd));
792 bfd_seek (abfd, 0, SEEK_SET);
794 ostream = NULL;
795 if (size == 0)
797 /* Seems like an abstraction violation, eh? Well it's OK! */
798 output_filename = bfd_get_filename (abfd);
800 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
801 if (ostream == NULL)
803 perror (bfd_get_filename (abfd));
804 xexit (1);
807 output_file = ostream;
809 else
810 while (ncopied < size)
812 tocopy = size - ncopied;
813 if (tocopy > BUFSIZE)
814 tocopy = BUFSIZE;
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)));
822 /* See comment above; this saves disk arm motion */
823 if (ostream == NULL)
825 /* Seems like an abstraction violation, eh? Well it's OK! */
826 output_filename = bfd_get_filename (abfd);
828 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
829 if (ostream == NULL)
831 perror (bfd_get_filename (abfd));
832 xexit (1);
835 output_file = ostream;
837 fwrite (cbuf, 1, nread, ostream);
838 ncopied += tocopy;
841 if (ostream != NULL)
842 fclose (ostream);
844 output_file = NULL;
845 output_filename = NULL;
847 chmod (bfd_get_filename (abfd), buf.st_mode);
849 if (preserve_dates)
850 set_times (bfd_get_filename (abfd), &buf);
852 free (cbuf);
855 #if 0
857 /* We don't use this anymore. Too many systems expect ar to rebuild
858 the symbol table even when q is used. */
860 /* Just do it quickly; don't worry about dups, armap, or anything like that */
862 static void
863 do_quick_append (archive_filename, files_to_append)
864 const char *archive_filename;
865 char **files_to_append;
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);
875 if (stat (archive_filename, &sbuf) != 0)
878 #ifndef __GO32__
880 /* KLUDGE ALERT! Temporary fix until I figger why
881 * stat() is wrong ... think it's buried in GO32's IDT
882 * - Jax
885 if (errno != ENOENT)
886 bfd_fatal (archive_filename);
887 #endif
889 newfile = true;
892 ofile = fopen (archive_filename, FOPEN_AUB);
893 if (ofile == NULL)
895 perror (program_name);
896 xexit (1);
899 temp = bfd_openr (archive_filename, NULL);
900 if (temp == NULL)
902 bfd_fatal (archive_filename);
904 if (newfile == false)
906 if (bfd_check_format (temp, bfd_archive) != true)
907 /* xgettext:c-format */
908 fatal (_("%s is not an archive"), archive_filename);
910 else
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);
919 if (ar_truncate)
920 temp->flags |= BFD_TRADITIONAL_FORMAT;
922 /* assume it's an achive, go straight to the end, sans $200 */
923 fseek (ofile, 0, 2);
925 for (; files_to_append && *files_to_append; ++files_to_append)
927 struct ar_hdr *hdr = bfd_special_undocumented_glue (temp, *files_to_append);
928 if (hdr == NULL)
930 bfd_fatal (*files_to_append);
933 BFD_SEND (temp, _bfd_truncate_arname, (temp, *files_to_append, (char *) hdr));
935 ifile = fopen (*files_to_append, FOPEN_RB);
936 if (ifile == NULL)
938 bfd_nonfatal (*files_to_append);
941 if (stat (*files_to_append, &sbuf) != 0)
943 bfd_nonfatal (*files_to_append);
946 tocopy = sbuf.st_size;
948 /* XXX should do error-checking! */
949 fwrite (hdr, 1, sizeof (struct ar_hdr), ofile);
951 while (tocopy > 0)
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;
960 fclose (ifile);
961 if ((sbuf.st_size % 2) == 1)
962 putc ('\012', ofile);
964 fclose (ofile);
965 bfd_close (temp);
966 free (buf);
969 #endif /* 0 */
971 static void
972 write_archive (iarch)
973 bfd *iarch;
975 bfd *obfd;
976 char *old_name, *new_name;
977 bfd *contents_head = iarch->next;
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);
983 output_filename = new_name;
985 obfd = bfd_openw (new_name, bfd_get_target (iarch));
987 if (obfd == NULL)
988 bfd_fatal (old_name);
990 output_bfd = obfd;
992 bfd_set_format (obfd, bfd_archive);
994 /* Request writing the archive symbol table unless we've
995 been explicitly requested not to. */
996 obfd->has_armap = write_armap >= 0;
998 if (ar_truncate)
1000 /* This should really use bfd_set_file_flags, but that rejects
1001 archives. */
1002 obfd->flags |= BFD_TRADITIONAL_FORMAT;
1005 if (bfd_set_archive_head (obfd, contents_head) != true)
1006 bfd_fatal (old_name);
1008 if (!bfd_close (obfd))
1009 bfd_fatal (old_name);
1011 output_bfd = NULL;
1012 output_filename = NULL;
1014 /* We don't care if this fails; we might be creating the archive. */
1015 bfd_close (iarch);
1017 if (smart_rename (new_name, old_name, 0) != 0)
1018 xexit (1);
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. */
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;
1031 bfd **after_bfd = contents;
1032 enum pos realpos;
1033 const char *realposname;
1035 if (postype == pos_default)
1037 realpos = default_pos;
1038 realposname = default_posname;
1040 else
1042 realpos = postype;
1043 realposname = posname;
1046 if (realpos == pos_end)
1048 while (*after_bfd)
1049 after_bfd = &((*after_bfd)->next);
1051 else
1053 for (; *after_bfd; after_bfd = &(*after_bfd)->next)
1054 if (strcmp ((*after_bfd)->filename, realposname) == 0)
1056 if (realpos == pos_after)
1057 after_bfd = &(*after_bfd)->next;
1058 break;
1061 return after_bfd;
1064 static void
1065 delete_members (arch, files_to_delete)
1066 bfd *arch;
1067 char **files_to_delete;
1069 bfd **current_ptr_ptr;
1070 boolean found;
1071 boolean something_changed = false;
1072 for (; *files_to_delete != NULL; ++files_to_delete)
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 */
1080 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1082 arch->has_armap = false;
1083 write_armap = -1;
1084 continue;
1087 found = false;
1088 current_ptr_ptr = &(arch->next);
1089 while (*current_ptr_ptr)
1091 if (strcmp (*files_to_delete, (*current_ptr_ptr)->filename) == 0)
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;
1101 else
1103 current_ptr_ptr = &((*current_ptr_ptr)->next);
1107 if (verbose && found == false)
1109 /* xgettext:c-format */
1110 printf (_("No member named `%s'\n"), *files_to_delete);
1112 next_file:
1116 if (something_changed == true)
1118 write_archive (arch);
1123 /* Reposition existing members within an archive */
1125 static void
1126 move_members (arch, files_to_move)
1127 bfd *arch;
1128 char **files_to_move;
1130 bfd **after_bfd; /* New entries go after this one */
1131 bfd **current_ptr_ptr; /* cdr pointer into contents */
1133 for (; *files_to_move; ++files_to_move)
1135 current_ptr_ptr = &(arch->next);
1136 while (*current_ptr_ptr)
1138 bfd *current_ptr = *current_ptr_ptr;
1139 if (strcmp (normalize (*files_to_move, arch),
1140 current_ptr->filename) == 0)
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;
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;
1153 if (verbose)
1154 printf ("m - %s\n", *files_to_move);
1156 goto next_file;
1159 current_ptr_ptr = &((*current_ptr_ptr)->next);
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:;
1168 write_archive (arch);
1171 /* Ought to default to replacing in place, but this is existing practice! */
1173 static void
1174 replace_members (arch, files_to_move, quick)
1175 bfd *arch;
1176 char **files_to_move;
1177 boolean quick;
1179 boolean changed = false;
1180 bfd **after_bfd; /* New entries go after this one */
1181 bfd *current;
1182 bfd **current_ptr;
1183 bfd *temp;
1185 while (files_to_move && *files_to_move)
1187 if (! quick)
1189 current_ptr = &arch->next;
1190 while (*current_ptr)
1192 current = *current_ptr;
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)
1200 if (newer_only)
1202 struct stat fsbuf, asbuf;
1204 if (stat (*files_to_move, &fsbuf) != 0)
1206 if (errno != ENOENT)
1207 bfd_fatal (*files_to_move);
1208 goto next_file;
1210 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1211 /* xgettext:c-format */
1212 fatal (_("internal stat error on %s"), current->filename);
1214 if (fsbuf.st_mtime <= asbuf.st_mtime)
1215 goto next_file;
1218 after_bfd = get_pos_bfd (&arch->next, pos_after,
1219 current->filename);
1220 temp = *after_bfd;
1222 *after_bfd = bfd_openr (*files_to_move, NULL);
1223 if (*after_bfd == (bfd *) NULL)
1225 bfd_fatal (*files_to_move);
1227 (*after_bfd)->next = temp;
1229 /* snip out this entry from the chain */
1230 *current_ptr = (*current_ptr)->next;
1232 if (verbose)
1234 printf ("r - %s\n", *files_to_move);
1237 changed = true;
1239 goto next_file;
1241 current_ptr = &(current->next);
1245 /* Add to the end of the archive. */
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)
1252 bfd_fatal (*files_to_move);
1254 if (verbose)
1256 printf ("a - %s\n", *files_to_move);
1259 (*after_bfd)->next = temp;
1261 changed = true;
1263 next_file:;
1265 files_to_move++;
1268 if (changed)
1269 write_archive (arch);
1272 static void
1273 ranlib_only (archname)
1274 const char *archname;
1276 bfd *arch;
1278 write_armap = 1;
1279 arch = open_inarch (archname, (char *) NULL);
1280 if (arch == NULL)
1281 xexit (1);
1282 write_archive (arch);
1285 /* Update the timestamp of the symbol map of an archive. */
1287 static void
1288 ranlib_touch (archname)
1289 const char *archname;
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;
1299 f = open (archname, O_RDWR, 0);
1300 if (f < 0)
1302 bfd_set_error (bfd_error_system_call);
1303 bfd_fatal (archname);
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))
1311 bfd_nonfatal (archname);
1312 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1314 list_matching_formats (matching);
1315 free (matching);
1317 xexit (1);
1320 if (! bfd_has_map (arch))
1321 /* xgettext:c-format */
1322 fatal (_("%s: no archive map to update"), archname);
1324 bfd_update_armap_timestamp (arch);
1326 if (! bfd_close (arch))
1327 bfd_fatal (archname);
1328 #endif
1331 /* Things which are interesting to map over all or some of the files: */
1333 static void
1334 print_descr (abfd)
1335 bfd *abfd;
1337 print_arelt_descr (stdout, abfd, verbose);