1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 92, 93, 94 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
21 Bugs: should use getopt the way tar does (complete w/optional -) and
22 should have long options too. GNU ar used to check file against filesystem
23 in quick_update and replace operations (would check mtime). Doesn't warn
24 when name truncated. No way to specify pos_end. Error messages should be
29 #include "libiberty.h"
38 #else /* ! POSIX_UTIME */
41 #else /* ! USE_UTIME */
43 #endif /* ! USE_UTIME */
44 #endif /* ! POSIX_UTIME */
52 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
54 #define EXT_NAME_LEN 6 /* ditto for *NIX */
57 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
60 bfd_special_undocumented_glue PARAMS ((bfd * abfd, char *filename));
62 /* Forward declarations */
65 remove_output PARAMS ((void));
68 map_over_members PARAMS ((bfd *, void (*)(bfd *), char **, int));
71 print_contents PARAMS ((bfd * member));
74 delete_members PARAMS ((bfd *, char **files_to_delete));
77 do_quick_append PARAMS ((const char *archive_filename,
78 char **files_to_append));
81 move_members PARAMS ((bfd *, char **files_to_move));
84 replace_members PARAMS ((bfd *, char **files_to_replace));
87 print_descr PARAMS ((bfd * abfd));
90 write_archive PARAMS ((bfd *));
93 ranlib_only PARAMS ((const char *archname));
96 ranlib_touch PARAMS ((const char *archname));
98 /** Globals and flags */
102 /* This flag distinguishes between ar and ranlib:
103 1 means this is 'ranlib'; 0 means this is 'ar'.
104 -1 means if we should use argv[0] to decide. */
105 extern int is_ranlib;
107 /* Nonzero means don't warn about creating the archive file if necessary. */
108 int silent_create = 0;
110 /* Nonzero means describe each action performed. */
113 /* Nonzero means preserve dates of members when extracting them. */
114 int preserve_dates = 0;
116 /* Nonzero means don't replace existing members whose dates are more recent
117 than the corresponding files. */
120 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
121 member). -1 means we've been explicitly asked to not write a symbol table;
122 +1 means we've been explictly asked to write it;
124 Traditionally, the default in BSD has been to not write the table.
125 However, for POSIX.2 compliance the default is now to write a symbol table
126 if any of the members are object files. */
129 /* Nonzero means it's the name of an existing member; position new or moved
130 files with respect to this one. */
131 char *posname = NULL;
133 /* Sez how to use `posname': pos_before means position before that member.
134 pos_after means position after that member. pos_end means always at end.
135 pos_default means default appropriately. For the latter two, `posname'
136 should also be zero. */
139 pos_default, pos_before, pos_after, pos_end
140 } postype = pos_default;
147 interactive = isatty (fileno (stdin));
151 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
152 COUNT is the length of the FILES chain; FUNCTION is called on each entry
153 whose name matches one in FILES. */
156 map_over_members (arch, function, files, count)
158 void (*function) PARAMS ((bfd *));
166 for (head = arch->next; head; head = head->next)
173 /* This may appear to be a baroque way of accomplishing what we want.
174 However we have to iterate over the filenames in order to notice where
175 a filename is requested but does not exist in the archive. Ditto
176 mapping over each file each time -- we want to hack multiple
179 for (; count > 0; files++, count--)
181 boolean found = false;
183 for (head = arch->next; head; head = head->next)
186 if (head->filename == NULL)
188 /* Some archive formats don't get the filenames filled in
189 until the elements are opened. */
191 bfd_stat_arch_elt (head, &buf);
193 if ((head->filename != NULL) &&
194 (!strcmp (*files, head->filename)))
201 fprintf (stderr, "no entry %s in archive\n", *files);
205 boolean operation_alters_arch = false;
207 extern char *program_version;
212 printf ("GNU %s version %s\n", program_name, program_version);
221 Usage: %s [-]{dmpqrtx}[abcilosuvV] [member-name] archive-file file...\n\
222 %s -M [<mri-script]\n",
223 program_name, program_name);
226 Usage: %s [-vV] archive\n", program_name);
227 list_supported_targets (program_name, stderr);
231 /* Normalize a file name specified on the command line into a file
232 name which we will use in an archive. */
238 char *filename = strrchr (file, '/');
239 if (filename != (char *) NULL)
250 /* Remove any output file. This is only called via xatexit. */
252 static char *output_filename = NULL;
253 static FILE *output_file = NULL;
254 static bfd *output_bfd = NULL;
259 if (output_filename != NULL)
261 if (output_bfd != NULL && output_bfd->iostream != NULL)
262 fclose ((FILE *) (output_bfd->iostream));
263 if (output_file != NULL)
264 fclose (output_file);
265 unlink (output_filename);
269 /* The option parsing should be in its own function.
270 It will be when I have getopt working. */
281 none = 0, delete, replace, print_table,
282 print_files, extract, move, quick_append
286 char *inarch_filename;
290 program_name = argv[0];
291 xmalloc_set_program_name (program_name);
293 START_PROGRESS (program_name, 0);
298 xatexit (remove_output);
300 temp = strrchr (program_name, '/');
301 if (temp == (char *) NULL)
302 temp = program_name; /* shouldn't happen, but... */
305 if (is_ranlib > 0 || (is_ranlib < 0 && strcmp (temp, "ranlib") == 0))
307 boolean touch = false;
312 if (strcmp (argv[1], "-V") == 0
313 || strcmp (argv[1], "-v") == 0
314 || strncmp (argv[1], "--v", 3) == 0)
317 if (strcmp (argv[1], "-t") == 0)
322 while (arg_index < argc)
325 ranlib_only (argv[arg_index]);
327 ranlib_touch (argv[arg_index]);
335 if (argc == 2 && strcmp (argv[1], "-M") == 0)
347 ++arg_ptr; /* compatibility */
349 while ((c = *arg_ptr++) != '\0')
360 if (operation != none)
361 fatal ("two different operation options specified");
366 operation_alters_arch = true;
370 operation_alters_arch = true;
373 operation = print_files;
376 operation = quick_append;
377 operation_alters_arch = true;
381 operation_alters_arch = true;
384 operation = print_table;
414 postype = pos_before;
417 postype = pos_before;
423 /* On HP/UX 9, the f modifier means to truncate names to 14
424 characters when comparing them to existing names. We
425 always use an extended name table, so the truncation has
426 no purpose for us. We ignore the modifier for
427 compatibility with the AR_FLAGS definition in make. */
430 fprintf (stderr, "%s: illegal option -- %c\n", program_name, c);
449 /* We can't write an armap when using ar q, so just do ar r
451 if (operation == quick_append && write_armap)
454 if ((operation == none || operation == print_table)
457 ranlib_only (argv[2]);
461 if (operation == none)
462 fatal ("no operation specified");
464 if (newer_only && operation != replace)
465 fatal ("`u' is only meaningful with the `r' option.");
469 if (postype != pos_default)
470 posname = argv[arg_index++];
472 inarch_filename = argv[arg_index++];
474 files = arg_index < argc ? argv + arg_index : NULL;
476 /* We can't do a quick append if we need to construct an
477 extended name table, because do_quick_append won't be able to
478 rebuild the name table. Unfortunately, at this point we
479 don't actually know the maximum name length permitted by this
480 object file format. So, we guess. FIXME. */
481 if (operation == quick_append)
485 for (chk = files; chk != NULL && *chk != '\0'; chk++)
487 if (strlen (normalize (*chk)) > 14)
495 if (operation == quick_append)
497 /* Note that quick appending to a non-existent archive creates it,
498 even if there are no files to append. */
499 do_quick_append (inarch_filename, files);
503 arch = open_inarch (inarch_filename);
508 map_over_members (arch, print_descr, files, argc - 3);
512 map_over_members (arch, print_contents, files, argc - 3);
516 map_over_members (arch, extract_file, files, argc - 3);
521 delete_members (arch, files);
526 move_members (arch, files);
530 if (files != NULL || write_armap > 0)
531 replace_members (arch, files);
534 /* Shouldn't happen! */
536 fprintf (stderr, "%s: internal error -- this option not implemented\n",
542 END_PROGRESS (program_name);
549 open_inarch (archive_filename)
550 const char *archive_filename;
557 bfd_set_error (bfd_error_no_error);
559 if (stat (archive_filename, &sbuf) != 0)
564 /* KLUDGE ALERT! Temporary fix until I figger why
565 * stat() is wrong ... think it's buried in GO32's IDT
569 bfd_fatal (archive_filename);
572 if (!operation_alters_arch)
574 fprintf (stderr, "%s: ", program_name);
575 perror (archive_filename);
580 /* This routine is one way to forcibly create the archive. */
582 do_quick_append (archive_filename, 0);
585 arch = bfd_openr (archive_filename, NULL);
589 bfd_fatal (archive_filename);
592 if (bfd_check_format (arch, bfd_archive) != true)
593 fatal ("%s is not an archive", archive_filename);
594 last_one = &(arch->next);
595 /* Read all the contents right away, regardless. */
596 for (next_one = bfd_openr_next_archived_file (arch, NULL);
598 next_one = bfd_openr_next_archived_file (arch, next_one))
601 *last_one = next_one;
602 last_one = &next_one->next;
604 *last_one = (bfd *) NULL;
605 if (bfd_get_error () != bfd_error_no_more_archived_files)
611 print_contents (abfd)
615 char *cbuf = xmalloc (BUFSIZE);
618 if (bfd_stat_arch_elt (abfd, &buf) != 0)
619 fatal ("internal stat error on %s", bfd_get_filename (abfd));
622 printf ("\n<member %s>\n\n", bfd_get_filename (abfd));
624 bfd_seek (abfd, 0, SEEK_SET);
627 while (ncopied < size)
631 int tocopy = size - ncopied;
632 if (tocopy > BUFSIZE)
635 nread = bfd_read (cbuf, 1, tocopy, abfd); /* oops -- broke
638 fatal ("%s is not a valid archive",
639 bfd_get_filename (bfd_my_archive (abfd)));
640 fwrite (cbuf, 1, nread, stdout);
646 /* Extract a member of the archive into its own file.
648 We defer opening the new file until after we have read a BUFSIZ chunk of the
649 old one, since we know we have just read the archive header for the old
650 one. Since most members are shorter than BUFSIZ, this means we will read
651 the old header, read the old data, write a new inode for the new file, and
652 write the new data, and be done. This 'optimization' is what comes from
653 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
661 char *cbuf = xmalloc (BUFSIZE);
666 if (bfd_stat_arch_elt (abfd, &buf) != 0)
667 fatal ("internal stat error on %s", bfd_get_filename (abfd));
671 printf ("x - %s\n", bfd_get_filename (abfd));
673 bfd_seek (abfd, 0, SEEK_SET);
678 /* Seems like an abstraction violation, eh? Well it's OK! */
679 output_filename = bfd_get_filename (abfd);
681 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
684 perror (bfd_get_filename (abfd));
688 output_file = ostream;
691 while (ncopied < size)
693 tocopy = size - ncopied;
694 if (tocopy > BUFSIZE)
697 nread = bfd_read (cbuf, 1, tocopy, abfd);
699 fatal ("%s is not a valid archive",
700 bfd_get_filename (bfd_my_archive (abfd)));
702 /* See comment above; this saves disk arm motion */
705 /* Seems like an abstraction violation, eh? Well it's OK! */
706 output_filename = bfd_get_filename (abfd);
708 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
711 perror (bfd_get_filename (abfd));
715 output_file = ostream;
717 fwrite (cbuf, 1, nread, ostream);
724 output_filename = NULL;
726 chmod (bfd_get_filename (abfd), buf.st_mode);
732 tb.actime = buf.st_mtime;
733 tb.modtime = buf.st_mtime;
734 utime (bfd_get_filename (abfd), &tb); /* FIXME check result */
735 #else /* ! POSIX_UTIME */
738 tb[0] = buf.st_mtime;
739 tb[1] = buf.st_mtime;
740 utime (bfd_get_filename (abfd), tb); /* FIXME check result */
741 #else /* ! USE_UTIME */
742 struct timeval tv[2];
743 tv[0].tv_sec = buf.st_mtime;
745 tv[1].tv_sec = buf.st_mtime;
747 utimes (bfd_get_filename (abfd), tv); /* FIXME check result */
748 #endif /* ! USE_UTIME */
749 #endif /* ! POSIX_UTIME */
754 /* Just do it quickly; don't worry about dups, armap, or anything like that */
757 do_quick_append (archive_filename, files_to_append)
758 const char *archive_filename;
759 char **files_to_append;
762 char *buf = xmalloc (BUFSIZE);
763 long tocopy, thistime;
766 boolean newfile = false;
767 bfd_set_error (bfd_error_no_error);
769 if (stat (archive_filename, &sbuf) != 0)
774 /* KLUDGE ALERT! Temporary fix until I figger why
775 * stat() is wrong ... think it's buried in GO32's IDT
780 bfd_fatal (archive_filename);
786 ofile = fopen (archive_filename, FOPEN_AUB);
789 perror (program_name);
793 temp = bfd_openr (archive_filename, NULL);
796 bfd_fatal (archive_filename);
798 if (newfile == false)
800 if (bfd_check_format (temp, bfd_archive) != true)
801 fatal ("%s is not an archive", archive_filename);
805 fwrite (ARMAG, 1, SARMAG, ofile);
807 fprintf (stderr, "%s: creating %s\n",
808 program_name, archive_filename);
811 /* assume it's an achive, go straight to the end, sans $200 */
814 for (; files_to_append && *files_to_append; ++files_to_append)
816 struct ar_hdr *hdr = bfd_special_undocumented_glue (temp, *files_to_append);
819 bfd_fatal (*files_to_append);
822 BFD_SEND (temp, _bfd_truncate_arname, (temp, *files_to_append, (char *) hdr));
824 ifile = fopen (*files_to_append, FOPEN_RB);
827 bfd_nonfatal (*files_to_append);
830 if (stat (*files_to_append, &sbuf) != 0)
832 bfd_nonfatal (*files_to_append);
835 tocopy = sbuf.st_size;
837 /* XXX should do error-checking! */
838 fwrite (hdr, 1, sizeof (struct ar_hdr), ofile);
843 if (thistime > BUFSIZE)
845 fread (buf, 1, thistime, ifile);
846 fwrite (buf, 1, thistime, ofile);
850 if ((sbuf.st_size % 2) == 1)
851 putc ('\012', ofile);
860 write_archive (iarch)
864 char *old_name, *new_name;
865 bfd *contents_head = iarch->next;
867 old_name = xmalloc (strlen (bfd_get_filename (iarch)) + 1);
868 strcpy (old_name, bfd_get_filename (iarch));
869 new_name = make_tempname (old_name);
871 output_filename = new_name;
873 obfd = bfd_openw (new_name, bfd_get_target (iarch));
876 bfd_fatal (old_name);
880 bfd_set_format (obfd, bfd_archive);
882 /* Request writing the archive symbol table unless we've
883 been explicitly requested not to. */
884 obfd->has_armap = write_armap >= 0;
886 if (bfd_set_archive_head (obfd, contents_head) != true)
887 bfd_fatal (old_name);
889 if (!bfd_close (obfd))
890 bfd_fatal (old_name);
893 output_filename = NULL;
895 /* We don't care if this fails; we might be creating the archive. */
899 if (rename (new_name, old_name) != 0)
900 bfd_fatal (old_name);
903 /* Return a pointer to the pointer to the entry which should be rplacd'd
904 into when altering. DEFAULT_POS should be how to interpret pos_default,
905 and should be a pos value. */
908 get_pos_bfd (contents, default_pos)
910 enum pos default_pos;
912 bfd **after_bfd = contents;
913 enum pos realpos = (postype == pos_default ? default_pos : postype);
915 if (realpos == pos_end)
918 after_bfd = &((*after_bfd)->next);
922 for (; *after_bfd; after_bfd = &(*after_bfd)->next)
923 if (!strcmp ((*after_bfd)->filename, posname))
925 if (realpos == pos_after)
926 after_bfd = &(*after_bfd)->next;
934 delete_members (arch, files_to_delete)
936 char **files_to_delete;
938 bfd **current_ptr_ptr;
940 boolean something_changed = false;
941 for (; *files_to_delete != NULL; ++files_to_delete)
943 /* In a.out systems, the armap is optional. It's also called
944 __.SYMDEF. So if the user asked to delete it, we should remember
945 that fact. This isn't quite right for COFF systems (where
946 __.SYMDEF might be regular member), but it's very unlikely
947 to be a problem. FIXME */
949 if (!strcmp (*files_to_delete, "__.SYMDEF"))
951 arch->has_armap = false;
957 current_ptr_ptr = &(arch->next);
958 while (*current_ptr_ptr)
960 if (strcmp (*files_to_delete, (*current_ptr_ptr)->filename) == 0)
963 something_changed = true;
967 *current_ptr_ptr = ((*current_ptr_ptr)->next);
972 current_ptr_ptr = &((*current_ptr_ptr)->next);
976 if (verbose && found == false)
978 printf ("No member named `%s'\n", *files_to_delete);
984 if (something_changed == true)
986 write_archive (arch);
991 /* Reposition existing members within an archive */
994 move_members (arch, files_to_move)
996 char **files_to_move;
998 bfd **after_bfd; /* New entries go after this one */
999 bfd **current_ptr_ptr; /* cdr pointer into contents */
1001 for (; *files_to_move; ++files_to_move)
1003 current_ptr_ptr = &(arch->next);
1004 while (*current_ptr_ptr)
1006 bfd *current_ptr = *current_ptr_ptr;
1007 if (strcmp (normalize (*files_to_move), current_ptr->filename) == 0)
1009 /* Move this file to the end of the list - first cut from
1012 *current_ptr_ptr = current_ptr->next;
1014 /* Now glue to end */
1015 after_bfd = get_pos_bfd (&arch->next, pos_end);
1017 *after_bfd = current_ptr;
1018 current_ptr->next = link;
1021 printf ("m - %s\n", *files_to_move);
1026 current_ptr_ptr = &((*current_ptr_ptr)->next);
1028 fprintf (stderr, "%s: no entry %s in archive %s!\n",
1029 program_name, *files_to_move, arch->filename);
1034 write_archive (arch);
1037 /* Ought to default to replacing in place, but this is existing practice! */
1040 replace_members (arch, files_to_move)
1042 char **files_to_move;
1044 bfd **after_bfd; /* New entries go after this one */
1049 while (files_to_move && *files_to_move)
1051 current_ptr = &arch->next;
1052 while (*current_ptr)
1054 current = *current_ptr;
1056 if (!strcmp (normalize (*files_to_move),
1057 normalize (current->filename)))
1061 struct stat fsbuf, asbuf;
1063 if (current->arelt_data == NULL)
1065 /* This can only happen if you specify a file on the
1066 command line more than once. */
1068 "%s: duplicate file specified: %s -- skipping\n",
1069 program_name, *files_to_move);
1073 if (stat (*files_to_move, &fsbuf) != 0)
1075 if (errno != ENOENT)
1076 bfd_fatal (*files_to_move);
1079 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1080 fatal ("internal stat error on %s", current->filename);
1082 if (fsbuf.st_mtime <= asbuf.st_mtime)
1086 /* snip out this entry from the chain */
1087 *current_ptr = current->next;
1089 after_bfd = get_pos_bfd (&arch->next, pos_end);
1091 *after_bfd = bfd_openr (*files_to_move, NULL);
1092 if (*after_bfd == (bfd *) NULL)
1094 bfd_fatal (*files_to_move);
1096 (*after_bfd)->next = temp;
1100 printf ("%c - %s\n", (postype == pos_after ? 'r' : 'a'),
1105 current_ptr = &(current->next);
1108 /* It isn't in there, so add to end */
1110 after_bfd = get_pos_bfd (&arch->next, pos_end);
1112 *after_bfd = bfd_openr (*files_to_move, NULL);
1113 if (*after_bfd == (bfd *) NULL)
1115 bfd_fatal (*files_to_move);
1119 printf ("c - %s\n", *files_to_move);
1122 (*after_bfd)->next = temp;
1129 write_archive (arch);
1133 ranlib_only (archname)
1134 const char *archname;
1139 arch = open_inarch (archname);
1142 write_archive (arch);
1145 /* Update the timestamp of the symbol map of an archive. */
1148 ranlib_touch (archname)
1149 const char *archname;
1152 /* I don't think updating works on go32. */
1153 ranlib_only (archname);
1158 f = open (archname, O_RDWR, 0);
1161 bfd_set_error (bfd_error_system_call);
1162 bfd_fatal (archname);
1165 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1167 || ! bfd_check_format (arch, bfd_archive))
1168 bfd_fatal (archname);
1170 if (! bfd_has_map (arch))
1171 fatal ("%s: no archive map to update", archname);
1173 bfd_update_armap_timestamp (arch);
1175 if (! bfd_close (arch))
1176 bfd_fatal (archname);
1180 /* Things which are interesting to map over all or some of the files: */
1186 print_arelt_descr (stdout, abfd, verbose);