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"
37 #else /* ! POSIX_UTIME */
40 #else /* ! USE_UTIME */
42 #endif /* ! USE_UTIME */
43 #endif /* ! POSIX_UTIME */
51 #define EXT_NAME_LEN 3 /* bufflen of addition to name if it's MS-DOS */
53 #define EXT_NAME_LEN 6 /* ditto for *NIX */
56 /* Kludge declaration from BFD! This is ugly! FIXME! XXX */
59 bfd_special_undocumented_glue
PARAMS ((bfd
* abfd
, char *filename
));
61 /* Forward declarations */
64 remove_output
PARAMS ((void));
67 map_over_members
PARAMS ((bfd
*, void (*)(bfd
*), char **, int));
70 print_contents
PARAMS ((bfd
* member
));
73 delete_members
PARAMS ((bfd
*, char **files_to_delete
));
76 do_quick_append
PARAMS ((const char *archive_filename
,
77 char **files_to_append
));
80 move_members
PARAMS ((bfd
*, char **files_to_move
));
83 replace_members
PARAMS ((bfd
*, char **files_to_replace
));
86 print_descr
PARAMS ((bfd
* abfd
));
89 write_archive
PARAMS ((bfd
*));
92 ranlib_only
PARAMS ((const char *archname
));
95 ranlib_touch
PARAMS ((const char *archname
));
97 /** Globals and flags */
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. */
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. */
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;
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. */
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. */
138 pos_default
, pos_before
, pos_after
, pos_end
139 } postype
= pos_default
;
146 interactive
= isatty (fileno (stdin
));
150 /* If COUNT is 0, then FUNCTION is called once on each entry. If nonzero,
151 COUNT is the length of the FILES chain; FUNCTION is called on each entry
152 whose name matches one in FILES. */
155 map_over_members (arch
, function
, files
, count
)
157 void (*function
) PARAMS ((bfd
*));
165 for (head
= arch
->next
; head
; head
= head
->next
)
169 /* This may appear to be a baroque way of accomplishing what we want.
170 However we have to iterate over the filenames in order to notice where
171 a filename is requested but does not exist in the archive. Ditto
172 mapping over each file each time -- we want to hack multiple
175 for (; count
> 0; files
++, count
--)
177 boolean found
= false;
178 for (head
= arch
->next
; head
; head
= head
->next
)
180 if (head
->filename
== NULL
)
182 /* Some archive formats don't get the filenames filled in
183 until the elements are opened. */
185 bfd_stat_arch_elt (head
, &buf
);
187 if ((head
->filename
!= NULL
) &&
188 (!strcmp (*files
, head
->filename
)))
195 fprintf (stderr
, "no entry %s in archive\n", *files
);
199 boolean operation_alters_arch
= false;
201 extern char *program_version
;
206 printf ("GNU %s version %s\n", program_name
, program_version
);
215 Usage: %s [-]{dmpqrtx}[abcilosuvV] [member-name] archive-file file...\n\
216 %s -M [<mri-script]\n",
217 program_name
, program_name
);
220 Usage: %s [-vV] archive\n", program_name
);
224 /* Normalize a file name specified on the command line into a file
225 name which we will use in an archive. */
231 char *filename
= strrchr (file
, '/');
232 if (filename
!= (char *) NULL
)
243 /* Remove any output file. This is only called via xatexit. */
245 static char *output_filename
= NULL
;
246 static FILE *output_file
= NULL
;
247 static bfd
*output_bfd
= NULL
;
252 if (output_filename
!= NULL
)
254 if (output_bfd
!= NULL
&& output_bfd
->iostream
!= NULL
)
255 fclose ((FILE *) (output_bfd
->iostream
));
256 if (output_file
!= NULL
)
257 fclose (output_file
);
258 unlink (output_filename
);
262 /* The option parsing should be in its own function.
263 It will be when I have getopt working. */
274 none
= 0, delete, replace
, print_table
,
275 print_files
, extract
, move
, quick_append
279 char *inarch_filename
;
283 program_name
= argv
[0];
284 xmalloc_set_program_name (program_name
);
289 xatexit (remove_output
);
291 temp
= strrchr (program_name
, '/');
292 if (temp
== (char *) NULL
)
293 temp
= program_name
; /* shouldn't happen, but... */
296 if (is_ranlib
> 0 || (is_ranlib
< 0 && strcmp (temp
, "ranlib") == 0))
298 boolean touch
= false;
303 if (strcmp (argv
[1], "-V") == 0
304 || strcmp (argv
[1], "-v") == 0
305 || strncmp (argv
[1], "--v", 3) == 0)
308 if (strcmp (argv
[1], "-t") == 0)
313 while (arg_index
< argc
)
316 ranlib_only (argv
[arg_index
]);
318 ranlib_touch (argv
[arg_index
]);
326 if (argc
== 2 && strcmp (argv
[1], "-M") == 0)
338 ++arg_ptr
; /* compatibility */
340 while ((c
= *arg_ptr
++) != '\0')
351 if (operation
!= none
)
352 fatal ("two different operation options specified");
357 operation_alters_arch
= true;
361 operation_alters_arch
= true;
364 operation
= print_files
;
367 operation
= quick_append
;
368 operation_alters_arch
= true;
372 operation_alters_arch
= true;
375 operation
= print_table
;
405 postype
= pos_before
;
408 postype
= pos_before
;
414 fprintf (stderr
, "%s: illegal option -- %c\n", program_name
, c
);
433 /* We can't write an armap when using ar q, so just do ar r
435 if (operation
== quick_append
&& write_armap
)
438 if ((operation
== none
|| operation
== print_table
)
441 ranlib_only (argv
[2]);
445 if (operation
== none
)
446 fatal ("no operation specified");
448 if (newer_only
&& operation
!= replace
)
449 fatal ("`u' is only meaningful with the `r' option.");
453 if (postype
!= pos_default
)
454 posname
= argv
[arg_index
++];
456 inarch_filename
= argv
[arg_index
++];
458 files
= arg_index
< argc
? argv
+ arg_index
: NULL
;
460 /* We can't do a quick append if we need to construct an
461 extended name table, because do_quick_append won't be able to
462 rebuild the name table. Unfortunately, at this point we
463 don't actually know the maximum name length permitted by this
464 object file format. So, we guess. FIXME. */
465 if (operation
== quick_append
)
469 for (chk
= files
; chk
!= NULL
&& *chk
!= '\0'; chk
++)
471 if (strlen (normalize (*chk
)) > 14)
479 if (operation
== quick_append
)
481 /* Note that quick appending to a non-existent archive creates it,
482 even if there are no files to append. */
483 do_quick_append (inarch_filename
, files
);
487 arch
= open_inarch (inarch_filename
);
492 map_over_members (arch
, print_descr
, files
, argc
- 3);
496 map_over_members (arch
, print_contents
, files
, argc
- 3);
500 map_over_members (arch
, extract_file
, files
, argc
- 3);
505 delete_members (arch
, files
);
510 move_members (arch
, files
);
514 if (files
!= NULL
|| write_armap
> 0)
515 replace_members (arch
, files
);
518 /* Shouldn't happen! */
520 fprintf (stderr
, "%s: internal error -- this option not implemented\n",
531 open_inarch (archive_filename
)
532 const char *archive_filename
;
539 bfd_set_error (bfd_error_no_error
);
541 if (stat (archive_filename
, &sbuf
) != 0)
546 /* KLUDGE ALERT! Temporary fix until I figger why
547 * stat() is wrong ... think it's buried in GO32's IDT
551 bfd_fatal (archive_filename
);
554 if (!operation_alters_arch
)
556 fprintf (stderr
, "%s: ", program_name
);
557 perror (archive_filename
);
562 /* This routine is one way to forcibly create the archive. */
564 do_quick_append (archive_filename
, 0);
567 arch
= bfd_openr (archive_filename
, NULL
);
571 bfd_fatal (archive_filename
);
574 if (bfd_check_format (arch
, bfd_archive
) != true)
575 fatal ("%s is not an archive", archive_filename
);
576 last_one
= &(arch
->next
);
577 /* Read all the contents right away, regardless. */
578 for (next_one
= bfd_openr_next_archived_file (arch
, NULL
);
580 next_one
= bfd_openr_next_archived_file (arch
, next_one
))
582 *last_one
= next_one
;
583 last_one
= &next_one
->next
;
585 *last_one
= (bfd
*) NULL
;
586 if (bfd_get_error () != bfd_error_no_more_archived_files
)
592 print_contents (abfd
)
598 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
599 fatal ("internal stat error on %s", bfd_get_filename (abfd
));
602 printf ("\n<member %s>\n\n", bfd_get_filename (abfd
));
604 bfd_seek (abfd
, 0, SEEK_SET
);
607 while (ncopied
< size
)
611 int tocopy
= size
- ncopied
;
612 if (tocopy
> BUFSIZE
)
615 nread
= bfd_read (cbuf
, 1, tocopy
, abfd
); /* oops -- broke
618 fatal ("%s is not a valid archive",
619 bfd_get_filename (bfd_my_archive (abfd
)));
620 fwrite (cbuf
, 1, nread
, stdout
);
625 /* Extract a member of the archive into its own file.
627 We defer opening the new file until after we have read a BUFSIZ chunk of the
628 old one, since we know we have just read the archive header for the old
629 one. Since most members are shorter than BUFSIZ, this means we will read
630 the old header, read the old data, write a new inode for the new file, and
631 write the new data, and be done. This 'optimization' is what comes from
632 sitting next to a bare disk and hearing it every time it seeks. -- Gnu
645 if (bfd_stat_arch_elt (abfd
, &buf
) != 0)
646 fatal ("internal stat error on %s", bfd_get_filename (abfd
));
650 printf ("x - %s\n", bfd_get_filename (abfd
));
652 bfd_seek (abfd
, 0, SEEK_SET
);
657 /* Seems like an abstraction violation, eh? Well it's OK! */
658 output_filename
= bfd_get_filename (abfd
);
660 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
663 perror (bfd_get_filename (abfd
));
667 output_file
= ostream
;
670 while (ncopied
< size
)
672 tocopy
= size
- ncopied
;
673 if (tocopy
> BUFSIZE
)
676 nread
= bfd_read (cbuf
, 1, tocopy
, abfd
);
678 fatal ("%s is not a valid archive",
679 bfd_get_filename (bfd_my_archive (abfd
)));
681 /* See comment above; this saves disk arm motion */
684 /* Seems like an abstraction violation, eh? Well it's OK! */
685 output_filename
= bfd_get_filename (abfd
);
687 ostream
= fopen (bfd_get_filename (abfd
), FOPEN_WB
);
690 perror (bfd_get_filename (abfd
));
694 output_file
= ostream
;
696 fwrite (cbuf
, 1, nread
, ostream
);
703 output_filename
= NULL
;
705 chmod (bfd_get_filename (abfd
), buf
.st_mode
);
711 tb
.actime
= buf
.st_mtime
;
712 tb
.modtime
= buf
.st_mtime
;
713 utime (bfd_get_filename (abfd
), &tb
); /* FIXME check result */
714 #else /* ! POSIX_UTIME */
717 tb
[0] = buf
.st_mtime
;
718 tb
[1] = buf
.st_mtime
;
719 utime (bfd_get_filename (abfd
), tb
); /* FIXME check result */
720 #else /* ! USE_UTIME */
721 struct timeval tv
[2];
722 tv
[0].tv_sec
= buf
.st_mtime
;
724 tv
[1].tv_sec
= buf
.st_mtime
;
726 utimes (bfd_get_filename (abfd
), tv
); /* FIXME check result */
727 #endif /* ! USE_UTIME */
728 #endif /* ! POSIX_UTIME */
732 /* Just do it quickly; don't worry about dups, armap, or anything like that */
735 do_quick_append (archive_filename
, files_to_append
)
736 const char *archive_filename
;
737 char **files_to_append
;
741 long tocopy
, thistime
;
744 boolean newfile
= false;
745 bfd_set_error (bfd_error_no_error
);
747 if (stat (archive_filename
, &sbuf
) != 0)
752 /* KLUDGE ALERT! Temporary fix until I figger why
753 * stat() is wrong ... think it's buried in GO32's IDT
758 bfd_fatal (archive_filename
);
764 ofile
= fopen (archive_filename
, FOPEN_AUB
);
767 perror (program_name
);
771 temp
= bfd_openr (archive_filename
, NULL
);
774 bfd_fatal (archive_filename
);
776 if (newfile
== false)
778 if (bfd_check_format (temp
, bfd_archive
) != true)
779 fatal ("%s is not an archive", archive_filename
);
783 fwrite (ARMAG
, 1, SARMAG
, ofile
);
785 fprintf (stderr
, "%s: creating %s\n",
786 program_name
, archive_filename
);
789 /* assume it's an achive, go straight to the end, sans $200 */
792 for (; files_to_append
&& *files_to_append
; ++files_to_append
)
794 struct ar_hdr
*hdr
= bfd_special_undocumented_glue (temp
, *files_to_append
);
797 bfd_fatal (*files_to_append
);
800 BFD_SEND (temp
, _bfd_truncate_arname
, (temp
, *files_to_append
, (char *) hdr
));
802 ifile
= fopen (*files_to_append
, FOPEN_RB
);
805 bfd_nonfatal (*files_to_append
);
808 if (stat (*files_to_append
, &sbuf
) != 0)
810 bfd_nonfatal (*files_to_append
);
813 tocopy
= sbuf
.st_size
;
815 /* XXX should do error-checking! */
816 fwrite (hdr
, 1, sizeof (struct ar_hdr
), ofile
);
821 if (thistime
> BUFSIZE
)
823 fread (buf
, 1, thistime
, ifile
);
824 fwrite (buf
, 1, thistime
, ofile
);
828 if ((sbuf
.st_size
% 2) == 1)
829 putc ('\012', ofile
);
837 write_archive (iarch
)
841 int namelen
= strlen (bfd_get_filename (iarch
));
842 char *old_name
= xmalloc (namelen
+ 1);
843 char *new_name
= xmalloc (namelen
+ EXT_NAME_LEN
);
844 bfd
*contents_head
= iarch
->next
;
846 strcpy (old_name
, bfd_get_filename (iarch
));
847 strcpy (new_name
, bfd_get_filename (iarch
));
849 #ifdef __GO32__ /* avoid long .extensions for MS-DOS */
850 strcpy (new_name
+ namelen
, "-a");
852 strcpy (new_name
+ namelen
, "-art");
855 output_filename
= new_name
;
857 obfd
= bfd_openw (new_name
, bfd_get_target (iarch
));
860 bfd_fatal (old_name
);
864 bfd_set_format (obfd
, bfd_archive
);
866 /* Request writing the archive symbol table unless we've
867 been explicitly requested not to. */
868 obfd
->has_armap
= write_armap
>= 0;
870 if (bfd_set_archive_head (obfd
, contents_head
) != true)
871 bfd_fatal (old_name
);
873 if (!bfd_close (obfd
))
874 bfd_fatal (old_name
);
877 output_filename
= NULL
;
879 /* We don't care if this fails; we might be creating the archive. */
883 if (rename (new_name
, old_name
) != 0)
884 bfd_fatal (old_name
);
887 /* Return a pointer to the pointer to the entry which should be rplacd'd
888 into when altering. DEFAULT_POS should be how to interpret pos_default,
889 and should be a pos value. */
892 get_pos_bfd (contents
, default_pos
)
894 enum pos default_pos
;
896 bfd
**after_bfd
= contents
;
897 enum pos realpos
= (postype
== pos_default
? default_pos
: postype
);
899 if (realpos
== pos_end
)
902 after_bfd
= &((*after_bfd
)->next
);
906 for (; *after_bfd
; after_bfd
= &(*after_bfd
)->next
)
907 if (!strcmp ((*after_bfd
)->filename
, posname
))
909 if (realpos
== pos_after
)
910 after_bfd
= &(*after_bfd
)->next
;
918 delete_members (arch
, files_to_delete
)
920 char **files_to_delete
;
922 bfd
**current_ptr_ptr
;
924 boolean something_changed
= false;
925 for (; *files_to_delete
!= NULL
; ++files_to_delete
)
927 /* In a.out systems, the armap is optional. It's also called
928 __.SYMDEF. So if the user asked to delete it, we should remember
929 that fact. This isn't quite right for COFF systems (where
930 __.SYMDEF might be regular member), but it's very unlikely
931 to be a problem. FIXME */
933 if (!strcmp (*files_to_delete
, "__.SYMDEF"))
935 arch
->has_armap
= false;
941 current_ptr_ptr
= &(arch
->next
);
942 while (*current_ptr_ptr
)
944 if (strcmp (*files_to_delete
, (*current_ptr_ptr
)->filename
) == 0)
947 something_changed
= true;
951 *current_ptr_ptr
= ((*current_ptr_ptr
)->next
);
956 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
960 if (verbose
&& found
== false)
962 printf ("No member named `%s'\n", *files_to_delete
);
968 if (something_changed
== true)
970 write_archive (arch
);
975 /* Reposition existing members within an archive */
978 move_members (arch
, files_to_move
)
980 char **files_to_move
;
982 bfd
**after_bfd
; /* New entries go after this one */
983 bfd
**current_ptr_ptr
; /* cdr pointer into contents */
985 for (; *files_to_move
; ++files_to_move
)
987 current_ptr_ptr
= &(arch
->next
);
988 while (*current_ptr_ptr
)
990 bfd
*current_ptr
= *current_ptr_ptr
;
991 if (strcmp (normalize (*files_to_move
), current_ptr
->filename
) == 0)
993 /* Move this file to the end of the list - first cut from
996 *current_ptr_ptr
= current_ptr
->next
;
998 /* Now glue to end */
999 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
);
1001 *after_bfd
= current_ptr
;
1002 current_ptr
->next
= link
;
1005 printf ("m - %s\n", *files_to_move
);
1010 current_ptr_ptr
= &((*current_ptr_ptr
)->next
);
1012 fprintf (stderr
, "%s: no entry %s in archive %s!\n",
1013 program_name
, *files_to_move
, arch
->filename
);
1018 write_archive (arch
);
1021 /* Ought to default to replacing in place, but this is existing practice! */
1024 replace_members (arch
, files_to_move
)
1026 char **files_to_move
;
1028 bfd
**after_bfd
; /* New entries go after this one */
1033 while (files_to_move
&& *files_to_move
)
1035 current_ptr
= &arch
->next
;
1036 while (*current_ptr
)
1038 current
= *current_ptr
;
1040 if (!strcmp (normalize (*files_to_move
), current
->filename
))
1044 struct stat fsbuf
, asbuf
;
1046 if (current
->arelt_data
== NULL
)
1048 /* This can only happen if you specify a file on the
1049 command line more than once. */
1051 "%s: duplicate file specified: %s -- skipping\n",
1052 program_name
, *files_to_move
);
1056 if (stat (*files_to_move
, &fsbuf
) != 0)
1058 if (errno
!= ENOENT
)
1059 bfd_fatal (*files_to_move
);
1062 if (bfd_stat_arch_elt (current
, &asbuf
) != 0)
1063 fatal ("internal stat error on %s", current
->filename
);
1065 if (fsbuf
.st_mtime
<= asbuf
.st_mtime
)
1069 /* snip out this entry from the chain */
1070 *current_ptr
= current
->next
;
1072 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
);
1074 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1075 if (*after_bfd
== (bfd
*) NULL
)
1077 bfd_fatal (*files_to_move
);
1079 (*after_bfd
)->next
= temp
;
1083 printf ("%c - %s\n", (postype
== pos_after
? 'r' : 'a'),
1088 current_ptr
= &(current
->next
);
1091 /* It isn't in there, so add to end */
1093 after_bfd
= get_pos_bfd (&arch
->next
, pos_end
);
1095 *after_bfd
= bfd_openr (*files_to_move
, NULL
);
1096 if (*after_bfd
== (bfd
*) NULL
)
1098 bfd_fatal (*files_to_move
);
1102 printf ("c - %s\n", *files_to_move
);
1105 (*after_bfd
)->next
= temp
;
1112 write_archive (arch
);
1116 ranlib_only (archname
)
1117 const char *archname
;
1122 arch
= open_inarch (archname
);
1125 write_archive (arch
);
1128 /* Update the timestamp of the symbol map of an archive. */
1131 ranlib_touch (archname
)
1132 const char *archname
;
1135 /* I don't think updating works on go32. */
1136 ranlib_only (archname
);
1141 f
= open (archname
, O_RDWR
, 0);
1144 bfd_set_error (bfd_error_system_call
);
1145 bfd_fatal (archname
);
1148 arch
= bfd_fdopenr (archname
, (const char *) NULL
, f
);
1150 || ! bfd_check_format (arch
, bfd_archive
))
1151 bfd_fatal (archname
);
1153 if (! bfd_has_map (arch
))
1154 fatal ("%s: no archive map to update", archname
);
1156 bfd_update_armap_timestamp (arch
);
1158 if (! bfd_close (arch
))
1159 bfd_fatal (archname
);
1163 /* Things which are interesting to map over all or some of the files: */
1169 print_arelt_descr (stdout
, abfd
, verbose
);