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 normalize PARAMS ((const char *, bfd *));
68 remove_output PARAMS ((void));
71 map_over_members PARAMS ((bfd *, void (*)(bfd *), char **, int));
74 print_contents PARAMS ((bfd * member));
77 delete_members PARAMS ((bfd *, char **files_to_delete));
80 do_quick_append PARAMS ((const char *archive_filename,
81 char **files_to_append));
84 move_members PARAMS ((bfd *, char **files_to_move));
87 replace_members PARAMS ((bfd *, char **files_to_replace));
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));
101 /** Globals and flags */
105 /* This flag distinguishes between ar and ranlib:
106 1 means this is 'ranlib'; 0 means this is 'ar'.
107 -1 means if we should use argv[0] to decide. */
108 extern int is_ranlib;
110 /* Nonzero means don't warn about creating the archive file if necessary. */
111 int silent_create = 0;
113 /* Nonzero means describe each action performed. */
116 /* Nonzero means preserve dates of members when extracting them. */
117 int preserve_dates = 0;
119 /* Nonzero means don't replace existing members whose dates are more recent
120 than the corresponding files. */
123 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
124 member). -1 means we've been explicitly asked to not write a symbol table;
125 +1 means we've been explictly asked to write it;
127 Traditionally, the default in BSD has been to not write the table.
128 However, for POSIX.2 compliance the default is now to write a symbol table
129 if any of the members are object files. */
132 /* Nonzero means it's the name of an existing member; position new or moved
133 files with respect to this one. */
134 char *posname = NULL;
136 /* Sez how to use `posname': pos_before means position before that member.
137 pos_after means position after that member. pos_end means always at end.
138 pos_default means default appropriately. For the latter two, `posname'
139 should also be zero. */
142 pos_default, pos_before, pos_after, pos_end
143 } postype = pos_default;
145 /* Whether to truncate names of files stored in the archive. */
146 static boolean ar_truncate = false;
153 interactive = isatty (fileno (stdin));
157 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
158 COUNT is the length of the FILES chain; FUNCTION is called on each entry
159 whose name matches one in FILES. */
162 map_over_members (arch, function, files, count)
164 void (*function) PARAMS ((bfd *));
172 for (head = arch->next; head; head = head->next)
179 /* This may appear to be a baroque way of accomplishing what we want.
180 However we have to iterate over the filenames in order to notice where
181 a filename is requested but does not exist in the archive. Ditto
182 mapping over each file each time -- we want to hack multiple
185 for (; count > 0; files++, count--)
187 boolean found = false;
189 for (head = arch->next; head; head = head->next)
192 if (head->filename == NULL)
194 /* Some archive formats don't get the filenames filled in
195 until the elements are opened. */
197 bfd_stat_arch_elt (head, &buf);
199 if ((head->filename != NULL) &&
200 (!strcmp (*files, head->filename)))
207 fprintf (stderr, "no entry %s in archive\n", *files);
211 boolean operation_alters_arch = false;
213 extern char *program_version;
218 printf ("GNU %s version %s\n", program_name, program_version);
227 Usage: %s [-]{dmpqrtx}[abcilosuvV] [member-name] archive-file file...\n\
228 %s -M [<mri-script]\n",
229 program_name, program_name);
232 Usage: %s [-vV] archive\n", program_name);
233 list_supported_targets (program_name, stderr);
237 /* Normalize a file name specified on the command line into a file
238 name which we will use in an archive. */
241 normalize (file, abfd)
245 const char *filename;
247 filename = strrchr (file, '/');
248 if (filename != (char *) NULL)
255 && strlen (filename) > abfd->xvec->ar_max_namelen)
260 s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
261 memcpy (s, filename, abfd->xvec->ar_max_namelen);
262 s[abfd->xvec->ar_max_namelen] = '\0';
269 /* Remove any output file. This is only called via xatexit. */
271 static char *output_filename = NULL;
272 static FILE *output_file = NULL;
273 static bfd *output_bfd = NULL;
278 if (output_filename != NULL)
280 if (output_bfd != NULL && output_bfd->iostream != NULL)
281 fclose ((FILE *) (output_bfd->iostream));
282 if (output_file != NULL)
283 fclose (output_file);
284 unlink (output_filename);
288 /* The option parsing should be in its own function.
289 It will be when I have getopt working. */
300 none = 0, delete, replace, print_table,
301 print_files, extract, move, quick_append
305 char *inarch_filename;
309 program_name = argv[0];
310 xmalloc_set_program_name (program_name);
312 START_PROGRESS (program_name, 0);
317 xatexit (remove_output);
319 temp = strrchr (program_name, '/');
320 if (temp == (char *) NULL)
321 temp = program_name; /* shouldn't happen, but... */
324 if (is_ranlib > 0 || (is_ranlib < 0 && strcmp (temp, "ranlib") == 0))
326 boolean touch = false;
331 if (strcmp (argv[1], "-V") == 0
332 || strcmp (argv[1], "-v") == 0
333 || strncmp (argv[1], "--v", 3) == 0)
336 if (strcmp (argv[1], "-t") == 0)
341 while (arg_index < argc)
344 ranlib_only (argv[arg_index]);
346 ranlib_touch (argv[arg_index]);
354 if (argc == 2 && strcmp (argv[1], "-M") == 0)
366 ++arg_ptr; /* compatibility */
368 while ((c = *arg_ptr++) != '\0')
379 if (operation != none)
380 fatal ("two different operation options specified");
385 operation_alters_arch = true;
389 operation_alters_arch = true;
392 operation = print_files;
395 operation = quick_append;
396 operation_alters_arch = true;
400 operation_alters_arch = true;
403 operation = print_table;
433 postype = pos_before;
436 postype = pos_before;
445 fprintf (stderr, "%s: illegal option -- %c\n", program_name, c);
464 /* We can't write an armap when using ar q, so just do ar r
466 if (operation == quick_append && write_armap)
469 if ((operation == none || operation == print_table)
472 ranlib_only (argv[2]);
476 if (operation == none)
477 fatal ("no operation specified");
479 if (newer_only && operation != replace)
480 fatal ("`u' is only meaningful with the `r' option.");
484 if (postype != pos_default)
485 posname = argv[arg_index++];
487 inarch_filename = argv[arg_index++];
489 files = arg_index < argc ? argv + arg_index : NULL;
491 /* We can't do a quick append if we need to construct an
492 extended name table, because do_quick_append won't be able to
493 rebuild the name table. Unfortunately, at this point we
494 don't actually know the maximum name length permitted by this
495 object file format. So, we guess. FIXME. */
496 if (operation == quick_append && ! ar_truncate)
500 for (chk = files; chk != NULL && *chk != '\0'; chk++)
502 if (strlen (normalize (*chk, (bfd *) NULL)) > 14)
510 if (operation == quick_append)
512 /* Note that quick appending to a non-existent archive creates it,
513 even if there are no files to append. */
514 do_quick_append (inarch_filename, files);
518 arch = open_inarch (inarch_filename);
523 map_over_members (arch, print_descr, files, argc - 3);
527 map_over_members (arch, print_contents, files, argc - 3);
531 map_over_members (arch, extract_file, files, argc - 3);
536 delete_members (arch, files);
541 move_members (arch, files);
545 if (files != NULL || write_armap > 0)
546 replace_members (arch, files);
549 /* Shouldn't happen! */
551 fprintf (stderr, "%s: internal error -- this option not implemented\n",
557 END_PROGRESS (program_name);
564 open_inarch (archive_filename)
565 const char *archive_filename;
572 bfd_set_error (bfd_error_no_error);
574 if (stat (archive_filename, &sbuf) != 0)
579 /* KLUDGE ALERT! Temporary fix until I figger why
580 * stat() is wrong ... think it's buried in GO32's IDT
584 bfd_fatal (archive_filename);
587 if (!operation_alters_arch)
589 fprintf (stderr, "%s: ", program_name);
590 perror (archive_filename);
595 /* This routine is one way to forcibly create the archive. */
597 do_quick_append (archive_filename, 0);
600 arch = bfd_openr (archive_filename, NULL);
604 bfd_fatal (archive_filename);
607 if (bfd_check_format (arch, bfd_archive) != true)
608 fatal ("%s is not an archive", archive_filename);
609 last_one = &(arch->next);
610 /* Read all the contents right away, regardless. */
611 for (next_one = bfd_openr_next_archived_file (arch, NULL);
613 next_one = bfd_openr_next_archived_file (arch, next_one))
616 *last_one = next_one;
617 last_one = &next_one->next;
619 *last_one = (bfd *) NULL;
620 if (bfd_get_error () != bfd_error_no_more_archived_files)
626 print_contents (abfd)
630 char *cbuf = xmalloc (BUFSIZE);
633 if (bfd_stat_arch_elt (abfd, &buf) != 0)
634 fatal ("internal stat error on %s", bfd_get_filename (abfd));
637 printf ("\n<member %s>\n\n", bfd_get_filename (abfd));
639 bfd_seek (abfd, 0, SEEK_SET);
642 while (ncopied < size)
646 int tocopy = size - ncopied;
647 if (tocopy > BUFSIZE)
650 nread = bfd_read (cbuf, 1, tocopy, abfd); /* oops -- broke
653 fatal ("%s is not a valid archive",
654 bfd_get_filename (bfd_my_archive (abfd)));
655 fwrite (cbuf, 1, nread, stdout);
661 /* Extract a member of the archive into its own file.
663 We defer opening the new file until after we have read a BUFSIZ chunk of the
664 old one, since we know we have just read the archive header for the old
665 one. Since most members are shorter than BUFSIZ, this means we will read
666 the old header, read the old data, write a new inode for the new file, and
667 write the new data, and be done. This 'optimization' is what comes from
668 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
676 char *cbuf = xmalloc (BUFSIZE);
681 if (bfd_stat_arch_elt (abfd, &buf) != 0)
682 fatal ("internal stat error on %s", bfd_get_filename (abfd));
686 printf ("x - %s\n", bfd_get_filename (abfd));
688 bfd_seek (abfd, 0, SEEK_SET);
693 /* Seems like an abstraction violation, eh? Well it's OK! */
694 output_filename = bfd_get_filename (abfd);
696 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
699 perror (bfd_get_filename (abfd));
703 output_file = ostream;
706 while (ncopied < size)
708 tocopy = size - ncopied;
709 if (tocopy > BUFSIZE)
712 nread = bfd_read (cbuf, 1, tocopy, abfd);
714 fatal ("%s is not a valid archive",
715 bfd_get_filename (bfd_my_archive (abfd)));
717 /* See comment above; this saves disk arm motion */
720 /* Seems like an abstraction violation, eh? Well it's OK! */
721 output_filename = bfd_get_filename (abfd);
723 ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
726 perror (bfd_get_filename (abfd));
730 output_file = ostream;
732 fwrite (cbuf, 1, nread, ostream);
739 output_filename = NULL;
741 chmod (bfd_get_filename (abfd), buf.st_mode);
747 tb.actime = buf.st_mtime;
748 tb.modtime = buf.st_mtime;
749 utime (bfd_get_filename (abfd), &tb); /* FIXME check result */
750 #else /* ! POSIX_UTIME */
753 tb[0] = buf.st_mtime;
754 tb[1] = buf.st_mtime;
755 utime (bfd_get_filename (abfd), tb); /* FIXME check result */
756 #else /* ! USE_UTIME */
757 struct timeval tv[2];
758 tv[0].tv_sec = buf.st_mtime;
760 tv[1].tv_sec = buf.st_mtime;
762 utimes (bfd_get_filename (abfd), tv); /* FIXME check result */
763 #endif /* ! USE_UTIME */
764 #endif /* ! POSIX_UTIME */
769 /* Just do it quickly; don't worry about dups, armap, or anything like that */
772 do_quick_append (archive_filename, files_to_append)
773 const char *archive_filename;
774 char **files_to_append;
777 char *buf = xmalloc (BUFSIZE);
778 long tocopy, thistime;
781 boolean newfile = false;
782 bfd_set_error (bfd_error_no_error);
784 if (stat (archive_filename, &sbuf) != 0)
789 /* KLUDGE ALERT! Temporary fix until I figger why
790 * stat() is wrong ... think it's buried in GO32's IDT
795 bfd_fatal (archive_filename);
801 ofile = fopen (archive_filename, FOPEN_AUB);
804 perror (program_name);
808 temp = bfd_openr (archive_filename, NULL);
811 bfd_fatal (archive_filename);
813 if (newfile == false)
815 if (bfd_check_format (temp, bfd_archive) != true)
816 fatal ("%s is not an archive", archive_filename);
820 fwrite (ARMAG, 1, SARMAG, ofile);
822 fprintf (stderr, "%s: creating %s\n",
823 program_name, archive_filename);
827 temp->flags |= BFD_TRADITIONAL_FORMAT;
829 /* assume it's an achive, go straight to the end, sans $200 */
832 for (; files_to_append && *files_to_append; ++files_to_append)
834 struct ar_hdr *hdr = bfd_special_undocumented_glue (temp, *files_to_append);
837 bfd_fatal (*files_to_append);
840 BFD_SEND (temp, _bfd_truncate_arname, (temp, *files_to_append, (char *) hdr));
842 ifile = fopen (*files_to_append, FOPEN_RB);
845 bfd_nonfatal (*files_to_append);
848 if (stat (*files_to_append, &sbuf) != 0)
850 bfd_nonfatal (*files_to_append);
853 tocopy = sbuf.st_size;
855 /* XXX should do error-checking! */
856 fwrite (hdr, 1, sizeof (struct ar_hdr), ofile);
861 if (thistime > BUFSIZE)
863 fread (buf, 1, thistime, ifile);
864 fwrite (buf, 1, thistime, ofile);
868 if ((sbuf.st_size % 2) == 1)
869 putc ('\012', ofile);
878 write_archive (iarch)
882 char *old_name, *new_name;
883 bfd *contents_head = iarch->next;
885 old_name = xmalloc (strlen (bfd_get_filename (iarch)) + 1);
886 strcpy (old_name, bfd_get_filename (iarch));
887 new_name = make_tempname (old_name);
889 output_filename = new_name;
891 obfd = bfd_openw (new_name, bfd_get_target (iarch));
894 bfd_fatal (old_name);
898 bfd_set_format (obfd, bfd_archive);
900 /* Request writing the archive symbol table unless we've
901 been explicitly requested not to. */
902 obfd->has_armap = write_armap >= 0;
906 /* This should really use bfd_set_file_flags, but that rejects
908 obfd->flags |= BFD_TRADITIONAL_FORMAT;
911 if (bfd_set_archive_head (obfd, contents_head) != true)
912 bfd_fatal (old_name);
914 if (!bfd_close (obfd))
915 bfd_fatal (old_name);
918 output_filename = NULL;
920 /* We don't care if this fails; we might be creating the archive. */
924 if (rename (new_name, old_name) != 0)
925 bfd_fatal (old_name);
928 /* Return a pointer to the pointer to the entry which should be rplacd'd
929 into when altering. DEFAULT_POS should be how to interpret pos_default,
930 and should be a pos value. */
933 get_pos_bfd (contents, default_pos)
935 enum pos default_pos;
937 bfd **after_bfd = contents;
938 enum pos realpos = (postype == pos_default ? default_pos : postype);
940 if (realpos == pos_end)
943 after_bfd = &((*after_bfd)->next);
947 for (; *after_bfd; after_bfd = &(*after_bfd)->next)
948 if (!strcmp ((*after_bfd)->filename, posname))
950 if (realpos == pos_after)
951 after_bfd = &(*after_bfd)->next;
959 delete_members (arch, files_to_delete)
961 char **files_to_delete;
963 bfd **current_ptr_ptr;
965 boolean something_changed = false;
966 for (; *files_to_delete != NULL; ++files_to_delete)
968 /* In a.out systems, the armap is optional. It's also called
969 __.SYMDEF. So if the user asked to delete it, we should remember
970 that fact. This isn't quite right for COFF systems (where
971 __.SYMDEF might be regular member), but it's very unlikely
972 to be a problem. FIXME */
974 if (!strcmp (*files_to_delete, "__.SYMDEF"))
976 arch->has_armap = false;
982 current_ptr_ptr = &(arch->next);
983 while (*current_ptr_ptr)
985 if (strcmp (*files_to_delete, (*current_ptr_ptr)->filename) == 0)
988 something_changed = true;
992 *current_ptr_ptr = ((*current_ptr_ptr)->next);
997 current_ptr_ptr = &((*current_ptr_ptr)->next);
1001 if (verbose && found == false)
1003 printf ("No member named `%s'\n", *files_to_delete);
1009 if (something_changed == true)
1011 write_archive (arch);
1016 /* Reposition existing members within an archive */
1019 move_members (arch, files_to_move)
1021 char **files_to_move;
1023 bfd **after_bfd; /* New entries go after this one */
1024 bfd **current_ptr_ptr; /* cdr pointer into contents */
1026 for (; *files_to_move; ++files_to_move)
1028 current_ptr_ptr = &(arch->next);
1029 while (*current_ptr_ptr)
1031 bfd *current_ptr = *current_ptr_ptr;
1032 if (strcmp (normalize (*files_to_move, arch),
1033 current_ptr->filename) == 0)
1035 /* Move this file to the end of the list - first cut from
1038 *current_ptr_ptr = current_ptr->next;
1040 /* Now glue to end */
1041 after_bfd = get_pos_bfd (&arch->next, pos_end);
1043 *after_bfd = current_ptr;
1044 current_ptr->next = link;
1047 printf ("m - %s\n", *files_to_move);
1052 current_ptr_ptr = &((*current_ptr_ptr)->next);
1054 fprintf (stderr, "%s: no entry %s in archive %s!\n",
1055 program_name, *files_to_move, arch->filename);
1060 write_archive (arch);
1063 /* Ought to default to replacing in place, but this is existing practice! */
1066 replace_members (arch, files_to_move)
1068 char **files_to_move;
1070 bfd **after_bfd; /* New entries go after this one */
1075 while (files_to_move && *files_to_move)
1077 current_ptr = &arch->next;
1078 while (*current_ptr)
1080 current = *current_ptr;
1082 if (!strcmp (normalize (*files_to_move, arch),
1083 normalize (current->filename, arch)))
1087 struct stat fsbuf, asbuf;
1089 if (current->arelt_data == NULL)
1091 /* This can only happen if you specify a file on the
1092 command line more than once. */
1094 "%s: duplicate file specified: %s -- skipping\n",
1095 program_name, *files_to_move);
1099 if (stat (*files_to_move, &fsbuf) != 0)
1101 if (errno != ENOENT)
1102 bfd_fatal (*files_to_move);
1105 if (bfd_stat_arch_elt (current, &asbuf) != 0)
1106 fatal ("internal stat error on %s", current->filename);
1108 if (fsbuf.st_mtime <= asbuf.st_mtime)
1112 /* snip out this entry from the chain */
1113 *current_ptr = current->next;
1115 after_bfd = get_pos_bfd (&arch->next, pos_end);
1117 *after_bfd = bfd_openr (*files_to_move, NULL);
1118 if (*after_bfd == (bfd *) NULL)
1120 bfd_fatal (*files_to_move);
1122 (*after_bfd)->next = temp;
1126 printf ("r - %s\n", *files_to_move);
1130 current_ptr = &(current->next);
1133 /* It isn't in there, so add to end */
1135 after_bfd = get_pos_bfd (&arch->next, pos_end);
1137 *after_bfd = bfd_openr (*files_to_move, NULL);
1138 if (*after_bfd == (bfd *) NULL)
1140 bfd_fatal (*files_to_move);
1144 printf ("a - %s\n", *files_to_move);
1147 (*after_bfd)->next = temp;
1154 write_archive (arch);
1158 ranlib_only (archname)
1159 const char *archname;
1164 arch = open_inarch (archname);
1167 write_archive (arch);
1170 /* Update the timestamp of the symbol map of an archive. */
1173 ranlib_touch (archname)
1174 const char *archname;
1177 /* I don't think updating works on go32. */
1178 ranlib_only (archname);
1183 f = open (archname, O_RDWR, 0);
1186 bfd_set_error (bfd_error_system_call);
1187 bfd_fatal (archname);
1190 arch = bfd_fdopenr (archname, (const char *) NULL, f);
1192 || ! bfd_check_format (arch, bfd_archive))
1193 bfd_fatal (archname);
1195 if (! bfd_has_map (arch))
1196 fatal ("%s: no archive map to update", archname);
1198 bfd_update_armap_timestamp (arch);
1200 if (! bfd_close (arch))
1201 bfd_fatal (archname);
1205 /* Things which are interesting to map over all or some of the files: */
1211 print_arelt_descr (stdout, abfd, verbose);