1 /* ar.c - Archive modify and extract.
2 Copyright 1991, 92, 93, 94, 95, 96, 97, 1998 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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
28 #include "libiberty.h"
36 #ifdef HAVE_GOOD_UTIME_H
38 #else /* ! HAVE_GOOD_UTIME_H */
41 #endif /* HAVE_UTIMES */
42 #endif /* ! HAVE_GOOD_UTIME_H */
45 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
47 #define EXT_NAME_LEN 6 /* ditto for *NIX */
52 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
55 bfd_special_undocumented_glue PARAMS ((bfd * abfd, const char *filename));
57 /* Static declarations */
60 mri_emul PARAMS ((void));
63 normalize PARAMS ((const char *, bfd *));
66 remove_output PARAMS ((void));
69 map_over_members PARAMS ((bfd *, void (*)(bfd *), char **, int));
72 print_contents PARAMS ((bfd * member));
75 delete_members PARAMS ((bfd *, char **files_to_delete));
79 do_quick_append PARAMS ((const char *archive_filename,
80 char **files_to_append));
84 move_members PARAMS ((bfd *, char **files_to_move));
87 replace_members PARAMS ((bfd *, char **files_to_replace, boolean quick));
90 print_descr PARAMS ((bfd * abfd));
93 write_archive PARAMS ((bfd *));
96 ranlib_only PARAMS ((const char *archname));
99 ranlib_touch PARAMS ((const char *archname));
102 usage PARAMS ((int));
104 /** Globals and flags */
108 /* This flag distinguishes between ar and ranlib:
109 1 means this is 'ranlib'; 0 means this is 'ar'.
110 -1 means if we should use argv[0] to decide. */
111 extern int is_ranlib;
113 /* Nonzero means don't warn about creating the archive file if necessary. */
114 int silent_create = 0;
116 /* Nonzero means describe each action performed. */
119 /* Nonzero means preserve dates of members when extracting them. */
120 int preserve_dates = 0;
122 /* Nonzero means don't replace existing members whose dates are more recent
123 than the corresponding files. */
126 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
127 member). -1 means we've been explicitly asked to not write a symbol table;
128 +1 means we've been explictly asked to write it;
130 Traditionally, the default in BSD has been to not write the table.
131 However, for POSIX.2 compliance the default is now to write a symbol table
132 if any of the members are object files. */
135 /* Nonzero means it's the name of an existing member; position new or moved
136 files with respect to this one. */
137 char *posname = NULL;
139 /* Sez how to use `posname': pos_before means position before that member.
140 pos_after means position after that member. pos_end means always at end.
141 pos_default means default appropriately. For the latter two, `posname'
142 should also be zero. */
145 pos_default, pos_before, pos_after, pos_end
146 } postype = pos_default;
149 get_pos_bfd PARAMS ((bfd **, enum pos, const char *));
151 /* Whether to truncate names of files stored in the archive. */
152 static boolean ar_truncate = false;
159 interactive = isatty (fileno (stdin));
163 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
164 COUNT is the length of the FILES chain; FUNCTION is called on each entry
165 whose name matches one in FILES. */
168 map_over_members (arch, function, files, count)
170 void (*function) PARAMS ((bfd *));
178 for (head = arch->next; head; head = head->next)
185 /* This may appear to be a baroque way of accomplishing what we want.
186 However we have to iterate over the filenames in order to notice where
187 a filename is requested but does not exist in the archive. Ditto
188 mapping over each file each time -- we want to hack multiple
191 for (; count > 0; files++, count--)
193 boolean found = false;
195 for (head = arch->next; head; head = head->next)
198 if (head->filename == NULL)
200 /* Some archive formats don't get the filenames filled in
201 until the elements are opened. */
203 bfd_stat_arch_elt (head, &buf);
205 if ((head->filename != NULL) &&
206 (!strcmp (*files, head->filename)))
213 fprintf (stderr, _("no entry %s in archive\n"), *files);
217 boolean operation_alters_arch = false;
225 s = help ? stdout : stderr;
228 Usage: %s [-]{dmpqrtx}[abcilosSuvV] [member-name] archive-file file...\n\
229 %s -M [<mri-script]\n"),
230 program_name, program_name);
233 Usage: %s [-vV] archive\n"), program_name);
235 list_supported_targets (program_name, stderr);
238 fprintf (s, _("Report bugs to bug-gnu-utils@gnu.org\n"));
240 xexit (help ? 0 : 1);
243 /* Normalize a file name specified on the command line into a file
244 name which we will use in an archive. */
247 normalize (file, abfd)
251 const char *filename;
253 filename = strrchr (file, '/');
254 if (filename != (char *) NULL)
261 && strlen (filename) > abfd->xvec->ar_max_namelen)
266 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
267 memcpy (s, filename, abfd->xvec->ar_max_namelen);
268 s[abfd->xvec->ar_max_namelen] = '\0';
275 /* Remove any output file. This is only called via xatexit. */
277 static char *output_filename = NULL;
278 static FILE *output_file = NULL;
279 static bfd *output_bfd = NULL;
284 if (output_filename != NULL)
286 if (output_bfd != NULL && output_bfd->iostream != NULL)
287 fclose ((FILE *) (output_bfd->iostream));
288 if (output_file != NULL)
289 fclose (output_file);
290 unlink (output_filename);
294 /* The option parsing should be in its own function.
295 It will be when I have getopt working. */
306 none = 0, delete, replace, print_table,
307 print_files, extract, move, quick_append
311 char *inarch_filename;
314 program_name = argv[0];
315 xmalloc_set_program_name (program_name);
321 temp = strrchr (program_name, '/');
326 if (strlen (temp) >= 6
327 && strcmp (temp + strlen (temp) - 6, "ranlib") == 0)
333 if (argc > 1 && argv[1][0] == '-')
335 if (strcmp (argv[1], "--help") == 0)
337 else if (strcmp (argv[1], "--version") == 0)
340 print_version ("ranlib");
342 print_version ("ar");
346 START_PROGRESS (program_name, 0);
349 set_default_bfd_target ();
353 xatexit (remove_output);
357 boolean touch = false;
359 if (argc < 2 || strcmp (argv[1], "--help") == 0)
361 if (strcmp (argv[1], "-V") == 0
362 || strcmp (argv[1], "-v") == 0
363 || strncmp (argv[1], "--v", 3) == 0)
364 print_version ("ranlib");
366 if (strcmp (argv[1], "-t") == 0)
371 while (arg_index < argc)
374 ranlib_only (argv[arg_index]);
376 ranlib_touch (argv[arg_index]);
382 if (argc == 2 && strcmp (argv[1], "-M") == 0)
394 ++arg_ptr; /* compatibility */
396 while ((c = *arg_ptr++) != '\0')
407 if (operation != none)
408 fatal (_("two different operation options specified"));
413 operation_alters_arch = true;
417 operation_alters_arch = true;
420 operation = print_files;
423 operation = quick_append;
424 operation_alters_arch = true;
428 operation_alters_arch = true;
431 operation = print_table;
464 postype = pos_before;
467 postype = pos_before;
476 fprintf (stderr, _("%s: illegal option -- %c\n"), program_name, c);
482 print_version ("ar");
495 /* We can't write an armap when using ar q, so just do ar r
497 if (operation == quick_append && write_armap)
500 if ((operation == none || operation == print_table)
503 ranlib_only (argv[2]);
507 if (operation == none)
508 fatal (_("no operation specified"));
510 if (newer_only && operation != replace)
511 fatal (_("`u' is only meaningful with the `r' option."));
515 if (postype != pos_default)
516 posname = argv[arg_index++];
518 inarch_filename = argv[arg_index++];
520 files = arg_index < argc ? argv + arg_index : NULL;
523 /* We don't use do_quick_append any more. Too many systems
524 expect ar to always rebuild the symbol table even when q is
527 /* We can't do a quick append if we need to construct an
528 extended name table, because do_quick_append won't be able to
529 rebuild the name table. Unfortunately, at this point we
530 don't actually know the maximum name length permitted by this
531 object file format. So, we guess. FIXME. */
532 if (operation == quick_append && ! ar_truncate)
536 for (chk = files; chk != NULL && *chk != '\0'; chk++)
538 if (strlen (normalize (*chk, (bfd *) NULL)) > 14)
546 if (operation == quick_append)
548 /* Note that quick appending to a non-existent archive creates it,
549 even if there are no files to append. */
550 do_quick_append (inarch_filename, files);
555 arch = open_inarch (inarch_filename,
556 files == NULL ? (char *) NULL : files[0]);
561 map_over_members (arch, print_descr, files, argc - 3);
565 map_over_members (arch, print_contents, files, argc - 3);
569 map_over_members (arch, extract_file, files, argc - 3);
574 delete_members (arch, files);
579 move_members (arch, files);
584 if (files != NULL || write_armap > 0)
585 replace_members (arch, files, operation == quick_append);
588 /* Shouldn't happen! */
590 fprintf (stderr, _("%s: internal error -- this option not implemented\n"),
596 END_PROGRESS (program_name);
603 open_inarch (archive_filename, file)
604 const char *archive_filename;
614 bfd_set_error (bfd_error_no_error);
618 if (stat (archive_filename, &sbuf) != 0)
622 /* KLUDGE ALERT! Temporary fix until I figger why
623 * stat() is wrong ... think it's buried in GO32's IDT
627 bfd_fatal (archive_filename);
630 if (!operation_alters_arch)
632 fprintf (stderr, "%s: ", program_name);
633 perror (archive_filename);
638 /* Try to figure out the target to use for the archive from the
639 first object on the list. */
644 obj = bfd_openr (file, NULL);
647 if (bfd_check_format (obj, bfd_object))
648 target = bfd_get_target (obj);
649 (void) bfd_close (obj);
653 /* Create an empty archive. */
654 arch = bfd_openw (archive_filename, target);
656 || ! bfd_set_format (arch, bfd_archive)
657 || ! bfd_close (arch))
658 bfd_fatal (archive_filename);
661 arch = bfd_openr (archive_filename, target);
665 bfd_fatal (archive_filename);
668 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
670 bfd_nonfatal (archive_filename);
671 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
673 list_matching_formats (matching);
679 last_one = &(arch->next);
680 /* Read all the contents right away, regardless. */
681 for (next_one = bfd_openr_next_archived_file (arch, NULL);
683 next_one = bfd_openr_next_archived_file (arch, next_one))
686 *last_one = next_one;
687 last_one = &next_one->next;
689 *last_one = (bfd *) NULL;
690 if (bfd_get_error () != bfd_error_no_more_archived_files)
696 print_contents (abfd)
700 char *cbuf = xmalloc (BUFSIZE);
703 if (bfd_stat_arch_elt (abfd, &buf) != 0)
704 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
707 printf (_("\n<member %s>\n\n"), bfd_get_filename (abfd));
709 bfd_seek (abfd, 0, SEEK_SET);
712 while (ncopied < size)
716 int tocopy = size - ncopied;
717 if (tocopy > BUFSIZE)
720 nread = bfd_read (cbuf, 1, tocopy, abfd); /* oops -- broke
723 fatal (_("%s is not a valid archive"),
724 bfd_get_filename (bfd_my_archive (abfd)));
725 fwrite (cbuf, 1, nread, stdout);
731 /* Extract a member of the archive into its own file.
733 We defer opening the new file until after we have read a BUFSIZ chunk of the
734 old one, since we know we have just read the archive header for the old
735 one. Since most members are shorter than BUFSIZ, this means we will read
736 the old header, read the old data, write a new inode for the new file, and
737 write the new data, and be done. This 'optimization' is what comes from
738 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
746 char *cbuf = xmalloc (BUFSIZE);
751 if (bfd_stat_arch_elt (abfd, &buf) != 0)
752 fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
756 printf ("x - %s\n", bfd_get_filename (abfd));
758 bfd_seek (abfd, 0, SEEK_SET);
763 /* Seems like an abstraction violation, eh? Well it's OK! */
764 output_filename = bfd_get_filename (abfd);
766 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
769 perror (bfd_get_filename (abfd));
773 output_file = ostream;
776 while (ncopied < size)
778 tocopy = size - ncopied;
779 if (tocopy > BUFSIZE)
782 nread = bfd_read (cbuf, 1, tocopy, abfd);
784 fatal (_("%s is not a valid archive"),
785 bfd_get_filename (bfd_my_archive (abfd)));
787 /* See comment above; this saves disk arm motion */
790 /* Seems like an abstraction violation, eh? Well it's OK! */
791 output_filename = bfd_get_filename (abfd);
793 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
796 perror (bfd_get_filename (abfd));
800 output_file = ostream;
802 fwrite (cbuf, 1, nread, ostream);
809 output_filename = NULL;
811 chmod (bfd_get_filename (abfd), buf.st_mode);
815 #ifdef HAVE_GOOD_UTIME_H
817 tb.actime = buf.st_mtime;
818 tb.modtime = buf.st_mtime;
819 utime (bfd_get_filename (abfd), &tb); /* FIXME check result */
820 #else /* ! HAVE_GOOD_UTIME_H */
823 tb[0] = buf.st_mtime;
824 tb[1] = buf.st_mtime;
825 utime (bfd_get_filename (abfd), tb); /* FIXME check result */
826 #else /* HAVE_UTIMES */
827 struct timeval tv[2];
828 tv[0].tv_sec = buf.st_mtime;
830 tv[1].tv_sec = buf.st_mtime;
832 utimes (bfd_get_filename (abfd), tv); /* FIXME check result */
833 #endif /* HAVE_UTIMES */
834 #endif /* ! HAVE_GOOD_UTIME_H */
841 /* We don't use this anymore. Too many systems expect ar to rebuild
842 the symbol table even when q is used. */
844 /* Just do it quickly; don't worry about dups, armap, or anything like that */
847 do_quick_append (archive_filename, files_to_append)
848 const char *archive_filename;
849 char **files_to_append;
852 char *buf = xmalloc (BUFSIZE);
853 long tocopy, thistime;
856 boolean newfile = false;
857 bfd_set_error (bfd_error_no_error);
859 if (stat (archive_filename, &sbuf) != 0)
864 /* KLUDGE ALERT! Temporary fix until I figger why
865 * stat() is wrong ... think it's buried in GO32's IDT
870 bfd_fatal (archive_filename);
876 ofile = fopen (archive_filename, FOPEN_AUB);
879 perror (program_name);
883 temp = bfd_openr (archive_filename, NULL);
886 bfd_fatal (archive_filename);
888 if (newfile == false)
890 if (bfd_check_format (temp, bfd_archive) != true)
891 fatal (_("%s is not an archive"), archive_filename);
895 fwrite (ARMAG, 1, SARMAG, ofile);
897 fprintf (stderr, _("%s: creating %s\n"),
898 program_name, archive_filename);
902 temp->flags |= BFD_TRADITIONAL_FORMAT;
904 /* assume it's an achive, go straight to the end, sans $200 */
907 for (; files_to_append && *files_to_append; ++files_to_append)
909 struct ar_hdr *hdr = bfd_special_undocumented_glue (temp, *files_to_append);
912 bfd_fatal (*files_to_append);
915 BFD_SEND (temp, _bfd_truncate_arname, (temp, *files_to_append, (char *) hdr));
917 ifile = fopen (*files_to_append, FOPEN_RB);
920 bfd_nonfatal (*files_to_append);
923 if (stat (*files_to_append, &sbuf) != 0)
925 bfd_nonfatal (*files_to_append);
928 tocopy = sbuf.st_size;
930 /* XXX should do error-checking! */
931 fwrite (hdr, 1, sizeof (struct ar_hdr), ofile);
936 if (thistime > BUFSIZE)
938 fread (buf, 1, thistime, ifile);
939 fwrite (buf, 1, thistime, ofile);
943 if ((sbuf.st_size % 2) == 1)
944 putc ('\012', ofile);
954 write_archive (iarch)
958 char *old_name, *new_name;
959 bfd *contents_head = iarch->next;
961 old_name = xmalloc (strlen (bfd_get_filename (iarch)) + 1);
962 strcpy (old_name, bfd_get_filename (iarch));
963 new_name = make_tempname (old_name);
965 output_filename = new_name;
967 obfd = bfd_openw (new_name, bfd_get_target (iarch));
970 bfd_fatal (old_name);
974 bfd_set_format (obfd, bfd_archive);
976 /* Request writing the archive symbol table unless we've
977 been explicitly requested not to. */
978 obfd->has_armap = write_armap >= 0;
982 /* This should really use bfd_set_file_flags, but that rejects
984 obfd->flags |= BFD_TRADITIONAL_FORMAT;
987 if (bfd_set_archive_head (obfd, contents_head) != true)
988 bfd_fatal (old_name);
990 if (!bfd_close (obfd))
991 bfd_fatal (old_name);
994 output_filename = NULL;
996 /* We don't care if this fails; we might be creating the archive. */
1000 if (rename (new_name, old_name) != 0)
1001 bfd_fatal (old_name);
1004 /* Return a pointer to the pointer to the entry which should be rplacd'd
1005 into when altering. DEFAULT_POS should be how to interpret pos_default,
1006 and should be a pos value. */
1009 get_pos_bfd (contents, default_pos, default_posname)
1011 enum pos default_pos;
1012 const char *default_posname;
1014 bfd **after_bfd = contents;
1016 const char *realposname;
1018 if (postype == pos_default)
1020 realpos = default_pos;
1021 realposname = default_posname;
1026 realposname = posname;
1029 if (realpos == pos_end)
1032 after_bfd = &((*after_bfd)->next);
1036 for (; *after_bfd; after_bfd = &(*after_bfd)->next)
1037 if (strcmp ((*after_bfd)->filename, realposname) == 0)
1039 if (realpos == pos_after)
1040 after_bfd = &(*after_bfd)->next;
1048 delete_members (arch, files_to_delete)
1050 char **files_to_delete;
1052 bfd **current_ptr_ptr;
1054 boolean something_changed = false;
1055 for (; *files_to_delete != NULL; ++files_to_delete)
1057 /* In a.out systems, the armap is optional. It's also called
1058 __.SYMDEF. So if the user asked to delete it, we should remember
1059 that fact. This isn't quite right for COFF systems (where
1060 __.SYMDEF might be regular member), but it's very unlikely
1061 to be a problem. FIXME */
1063 if (!strcmp (*files_to_delete, "__.SYMDEF"))
1065 arch->has_armap = false;
1071 current_ptr_ptr = &(arch->next);
1072 while (*current_ptr_ptr)
1074 if (strcmp (*files_to_delete, (*current_ptr_ptr)->filename) == 0)
1077 something_changed = true;
1081 *current_ptr_ptr = ((*current_ptr_ptr)->next);
1086 current_ptr_ptr = &((*current_ptr_ptr)->next);
1090 if (verbose && found == false)
1092 printf (_("No member named `%s'\n"), *files_to_delete);
1098 if (something_changed == true)
1100 write_archive (arch);
1105 /* Reposition existing members within an archive */
1108 move_members (arch, files_to_move)
1110 char **files_to_move;
1112 bfd **after_bfd; /* New entries go after this one */
1113 bfd **current_ptr_ptr; /* cdr pointer into contents */
1115 for (; *files_to_move; ++files_to_move)
1117 current_ptr_ptr = &(arch->next);
1118 while (*current_ptr_ptr)
1120 bfd *current_ptr = *current_ptr_ptr;
1121 if (strcmp (normalize (*files_to_move, arch),
1122 current_ptr->filename) == 0)
1124 /* Move this file to the end of the list - first cut from
1127 *current_ptr_ptr = current_ptr->next;
1129 /* Now glue to end */
1130 after_bfd = get_pos_bfd (&arch->next, pos_end, NULL);
1132 *after_bfd = current_ptr;
1133 current_ptr->next = link;
1136 printf ("m - %s\n", *files_to_move);
1141 current_ptr_ptr = &((*current_ptr_ptr)->next);
1143 fprintf (stderr, _("%s: no entry %s in archive %s!\n"),
1144 program_name, *files_to_move, arch->filename);
1149 write_archive (arch);
1152 /* Ought to default to replacing in place, but this is existing practice! */
1155 replace_members (arch, files_to_move, quick)
1157 char **files_to_move;
1160 boolean changed = false;
1161 bfd **after_bfd; /* New entries go after this one */
1166 while (files_to_move && *files_to_move)
1170 current_ptr = &arch->next;
1171 while (*current_ptr)
1173 current = *current_ptr;
1175 /* For compatibility with existing ar programs, we
1176 permit the same file to be added multiple times. */
1177 if (strcmp (normalize (*files_to_move, arch),
1178 normalize (current->filename, arch)) == 0
1179 && current->arelt_data != NULL)
1183 struct stat fsbuf, asbuf;
1185 if (stat (*files_to_move, &fsbuf) != 0)
1187 if (errno != ENOENT)
1188 bfd_fatal (*files_to_move);
1191 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1192 fatal (_("internal stat error on %s"), current->filename);
1194 if (fsbuf.st_mtime <= asbuf.st_mtime)
1198 after_bfd = get_pos_bfd (&arch->next, pos_after,
1202 *after_bfd = bfd_openr (*files_to_move, NULL);
1203 if (*after_bfd == (bfd *) NULL)
1205 bfd_fatal (*files_to_move);
1207 (*after_bfd)->next = temp;
1209 /* snip out this entry from the chain */
1210 *current_ptr = (*current_ptr)->next;
1214 printf ("r - %s\n", *files_to_move);
1221 current_ptr = &(current->next);
1225 /* Add to the end of the archive. */
1227 after_bfd = get_pos_bfd (&arch->next, pos_end, NULL);
1229 *after_bfd = bfd_openr (*files_to_move, NULL);
1230 if (*after_bfd == (bfd *) NULL)
1232 bfd_fatal (*files_to_move);
1236 printf ("a - %s\n", *files_to_move);
1239 (*after_bfd)->next = temp;
1249 write_archive (arch);
1253 ranlib_only (archname)
1254 const char *archname;
1259 arch = open_inarch (archname, (char *) NULL);
1262 write_archive (arch);
1265 /* Update the timestamp of the symbol map of an archive. */
1268 ranlib_touch (archname)
1269 const char *archname;
1272 /* I don't think updating works on go32. */
1273 ranlib_only (archname);
1279 f = open (archname, O_RDWR, 0);
1282 bfd_set_error (bfd_error_system_call);
1283 bfd_fatal (archname);
1286 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1288 bfd_fatal (archname);
1289 if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1291 bfd_nonfatal (archname);
1292 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1294 list_matching_formats (matching);
1300 if (! bfd_has_map (arch))
1301 fatal (_("%s: no archive map to update"), archname);
1303 bfd_update_armap_timestamp (arch);
1305 if (! bfd_close (arch))
1306 bfd_fatal (archname);
1310 /* Things which are interesting to map over all or some of the files: */
1316 print_arelt_descr (stdout, abfd, verbose);