* ar.c (main): Split ranlib path.
[external/binutils.git] / binutils / ar.c
1 /* ar.c - Archive modify and extract.
2    Copyright 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3    2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5
6    This file is part of GNU Binutils.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21    MA 02110-1301, USA.  */
22 \f
23 /*
24    Bugs: should use getopt the way tar does (complete w/optional -) and
25    should have long options too. GNU ar used to check file against filesystem
26    in quick_update and replace operations (would check mtime). Doesn't warn
27    when name truncated. No way to specify pos_end. Error messages should be
28    more consistent.  */
29
30 #include "sysdep.h"
31 #include "bfd.h"
32 #include "libiberty.h"
33 #include "progress.h"
34 #include "aout/ar.h"
35 #include "libbfd.h"
36 #include "bucomm.h"
37 #include "arsup.h"
38 #include "filenames.h"
39 #include "binemul.h"
40 #include "plugin.h"
41 #include <sys/stat.h>
42
43 #ifdef __GO32___
44 #define EXT_NAME_LEN 3          /* Bufflen of addition to name if it's MS-DOS.  */
45 #else
46 #define EXT_NAME_LEN 6          /* Ditto for *NIX.  */
47 #endif
48
49 /* Static declarations.  */
50
51 static void mri_emul (void);
52 static const char *normalize (const char *, bfd *);
53 static void remove_output (void);
54 static void map_over_members (bfd *, void (*)(bfd *), char **, int);
55 static void print_contents (bfd * member);
56 static void delete_members (bfd *, char **files_to_delete);
57
58 static void move_members (bfd *, char **files_to_move);
59 static void replace_members
60   (bfd *, char **files_to_replace, bfd_boolean quick);
61 static void print_descr (bfd * abfd);
62 static void write_archive (bfd *);
63 static int  ranlib_only (const char *archname);
64 static int  ranlib_touch (const char *archname);
65 static void usage (int);
66 \f
67 /** Globals and flags.  */
68
69 static int mri_mode;
70
71 /* This flag distinguishes between ar and ranlib:
72    1 means this is 'ranlib'; 0 means this is 'ar'.
73    -1 means if we should use argv[0] to decide.  */
74 extern int is_ranlib;
75
76 /* Nonzero means don't warn about creating the archive file if necessary.  */
77 int silent_create = 0;
78
79 /* Nonzero means describe each action performed.  */
80 int verbose = 0;
81
82 /* Nonzero means preserve dates of members when extracting them.  */
83 int preserve_dates = 0;
84
85 /* Nonzero means don't replace existing members whose dates are more recent
86    than the corresponding files.  */
87 int newer_only = 0;
88
89 /* Controls the writing of an archive symbol table (in BSD: a __.SYMDEF
90    member).  -1 means we've been explicitly asked to not write a symbol table;
91    +1 means we've been explicitly asked to write it;
92    0 is the default.
93    Traditionally, the default in BSD has been to not write the table.
94    However, for POSIX.2 compliance the default is now to write a symbol table
95    if any of the members are object files.  */
96 int write_armap = 0;
97
98 /* Operate in deterministic mode: write zero for timestamps, uids,
99    and gids for archive members and the archive symbol table, and write
100    consistent file modes.  */
101 int deterministic = 0;
102
103 /* Nonzero means it's the name of an existing member; position new or moved
104    files with respect to this one.  */
105 char *posname = NULL;
106
107 /* Sez how to use `posname': pos_before means position before that member.
108    pos_after means position after that member. pos_end means always at end.
109    pos_default means default appropriately. For the latter two, `posname'
110    should also be zero.  */
111 enum pos
112   {
113     pos_default, pos_before, pos_after, pos_end
114   } postype = pos_default;
115
116 static bfd **
117 get_pos_bfd (bfd **, enum pos, const char *);
118
119 /* For extract/delete only.  If COUNTED_NAME_MODE is TRUE, we only
120    extract the COUNTED_NAME_COUNTER instance of that name.  */
121 static bfd_boolean counted_name_mode = 0;
122 static int counted_name_counter = 0;
123
124 /* Whether to truncate names of files stored in the archive.  */
125 static bfd_boolean ar_truncate = FALSE;
126
127 /* Whether to use a full file name match when searching an archive.
128    This is convenient for archives created by the Microsoft lib
129    program.  */
130 static bfd_boolean full_pathname = FALSE;
131
132 /* Whether to create a "thin" archive (symbol index only -- no files).  */
133 static bfd_boolean make_thin_archive = FALSE;
134
135 static const char *plugin_target = NULL;
136
137 int interactive = 0;
138
139 static void
140 mri_emul (void)
141 {
142   interactive = isatty (fileno (stdin));
143   yyparse ();
144 }
145
146 /* If COUNT is 0, then FUNCTION is called once on each entry.  If nonzero,
147    COUNT is the length of the FILES chain; FUNCTION is called on each entry
148    whose name matches one in FILES.  */
149
150 static void
151 map_over_members (bfd *arch, void (*function)(bfd *), char **files, int count)
152 {
153   bfd *head;
154   int match_count;
155
156   if (count == 0)
157     {
158       for (head = arch->archive_next; head; head = head->archive_next)
159         {
160           PROGRESS (1);
161           function (head);
162         }
163       return;
164     }
165
166   /* This may appear to be a baroque way of accomplishing what we want.
167      However we have to iterate over the filenames in order to notice where
168      a filename is requested but does not exist in the archive.  Ditto
169      mapping over each file each time -- we want to hack multiple
170      references.  */
171
172   for (; count > 0; files++, count--)
173     {
174       bfd_boolean found = FALSE;
175
176       match_count = 0;
177       for (head = arch->archive_next; head; head = head->archive_next)
178         {
179           const char * filename;
180
181           PROGRESS (1);
182           filename = head->filename;
183           if (filename == NULL)
184             {
185               /* Some archive formats don't get the filenames filled in
186                  until the elements are opened.  */
187               struct stat buf;
188               bfd_stat_arch_elt (head, &buf);
189             }
190           else if (bfd_is_thin_archive (arch))
191             {
192               /* Thin archives store full pathnames.  Need to normalize.  */
193               filename = normalize (filename, arch);
194             }
195
196           if ((filename != NULL) &&
197               (!FILENAME_CMP (normalize (*files, arch), filename)))
198             {
199               ++match_count;
200               if (counted_name_mode
201                   && match_count != counted_name_counter)
202                 {
203                   /* Counting, and didn't match on count; go on to the
204                      next one.  */
205                   continue;
206                 }
207
208               found = TRUE;
209               function (head);
210             }
211         }
212
213       if (!found)
214         /* xgettext:c-format */
215         fprintf (stderr, _("no entry %s in archive\n"), *files);
216     }
217 }
218 \f
219 bfd_boolean operation_alters_arch = FALSE;
220
221 static void
222 usage (int help)
223 {
224   FILE *s;
225
226   s = help ? stdout : stderr;
227
228   /* xgettext:c-format */
229   const char * command_line =
230 #if BFD_SUPPORTS_PLUGINS
231         _("Usage: %s [emulation options] [--plugin <name>] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
232 #else
233         _("Usage: %s [emulation options] [-]{dmpqrstx}[abcfilNoPsSuvV] [member-name] [count] archive-file file...\n");
234 #endif
235   fprintf (s, command_line, program_name);
236
237   /* xgettext:c-format */
238   fprintf (s, _("       %s -M [<mri-script]\n"), program_name);
239   fprintf (s, _(" commands:\n"));
240   fprintf (s, _("  d            - delete file(s) from the archive\n"));
241   fprintf (s, _("  m[ab]        - move file(s) in the archive\n"));
242   fprintf (s, _("  p            - print file(s) found in the archive\n"));
243   fprintf (s, _("  q[f]         - quick append file(s) to the archive\n"));
244   fprintf (s, _("  r[ab][f][u]  - replace existing or insert new file(s) into the archive\n"));
245   fprintf (s, _("  s            - act as ranlib\n"));
246   fprintf (s, _("  t            - display contents of archive\n"));
247   fprintf (s, _("  x[o]         - extract file(s) from the archive\n"));
248   fprintf (s, _(" command specific modifiers:\n"));
249   fprintf (s, _("  [a]          - put file(s) after [member-name]\n"));
250   fprintf (s, _("  [b]          - put file(s) before [member-name] (same as [i])\n"));
251   fprintf (s, _("  [D]          - use zero for timestamps and uids/gids\n"));
252   fprintf (s, _("  [N]          - use instance [count] of name\n"));
253   fprintf (s, _("  [f]          - truncate inserted file names\n"));
254   fprintf (s, _("  [P]          - use full path names when matching\n"));
255   fprintf (s, _("  [o]          - preserve original dates\n"));
256   fprintf (s, _("  [u]          - only replace files that are newer than current archive contents\n"));
257   fprintf (s, _(" generic modifiers:\n"));
258   fprintf (s, _("  [c]          - do not warn if the library had to be created\n"));
259   fprintf (s, _("  [s]          - create an archive index (cf. ranlib)\n"));
260   fprintf (s, _("  [S]          - do not build a symbol table\n"));
261   fprintf (s, _("  [T]          - make a thin archive\n"));
262   fprintf (s, _("  [v]          - be verbose\n"));
263   fprintf (s, _("  [V]          - display the version number\n"));
264   fprintf (s, _("  @<file>      - read options from <file>\n"));
265 #if BFD_SUPPORTS_PLUGINS
266   fprintf (s, _(" optional:\n"));
267   fprintf (s, _("  --plugin <p> - load the specified plugin\n"));
268 #endif
269
270   ar_emul_usage (s);
271
272   list_supported_targets (program_name, s);
273
274   if (REPORT_BUGS_TO[0] && help)
275     fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
276
277   xexit (help ? 0 : 1);
278 }
279
280 static void
281 ranlib_usage(int help)
282 {
283   FILE *s;
284
285   s = help ? stdout : stderr;
286
287   /* xgettext:c-format */
288   fprintf (s, _("Usage: %s [options] archive\n"), program_name);
289   fprintf (s, _(" Generate an index to speed access to archives\n"));
290   fprintf (s, _(" The options are:\n\
291   @<file>                      Read options from <file>\n"));
292 #if BFD_SUPPORTS_PLUGINS
293   fprintf (s, _("\
294   --plugin <name>              Load the specified plugin\n"));
295 #endif
296   fprintf (s, _("\
297   -t                           Update the archive's symbol map timestamp\n\
298   -h --help                    Print this help message\n\
299   -v --version                 Print version information\n"));
300
301   list_supported_targets (program_name, s);
302
303   if (REPORT_BUGS_TO[0] && help)
304     fprintf (s, _("Report bugs to %s\n"), REPORT_BUGS_TO);
305
306   xexit (help ? 0 : 1);
307 }
308
309 /* Normalize a file name specified on the command line into a file
310    name which we will use in an archive.  */
311
312 static const char *
313 normalize (const char *file, bfd *abfd)
314 {
315   const char *filename;
316
317   if (full_pathname)
318     return file;
319
320   filename = lbasename (file);
321
322   if (ar_truncate
323       && abfd != NULL
324       && strlen (filename) > abfd->xvec->ar_max_namelen)
325     {
326       char *s;
327
328       /* Space leak.  */
329       s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
330       memcpy (s, filename, abfd->xvec->ar_max_namelen);
331       s[abfd->xvec->ar_max_namelen] = '\0';
332       filename = s;
333     }
334
335   return filename;
336 }
337
338 /* Remove any output file.  This is only called via xatexit.  */
339
340 static const char *output_filename = NULL;
341 static FILE *output_file = NULL;
342 static bfd *output_bfd = NULL;
343
344 static void
345 remove_output (void)
346 {
347   if (output_filename != NULL)
348     {
349       if (output_bfd != NULL)
350         bfd_cache_close (output_bfd);
351       if (output_file != NULL)
352         fclose (output_file);
353       unlink_if_ordinary (output_filename);
354     }
355 }
356
357 static void
358 ranlib_main(int argc, char **argv)
359 {
360   int arg_index, status = 0;
361   bfd_boolean touch = FALSE;
362
363   if (argc > 1 && argv[1][0] == '-')
364     {
365       if (strcmp (argv[1], "--help") == 0)
366         ranlib_usage (1);
367       else if (strcmp (argv[1], "--version") == 0)
368         {
369           print_version ("ranlib");
370         }
371     }
372
373   if (argc < 2
374       || strcmp (argv[1], "--help") == 0
375       || strcmp (argv[1], "-h") == 0
376       || strcmp (argv[1], "-H") == 0)
377     ranlib_usage (0);
378
379   if (strcmp (argv[1], "-V") == 0
380       || strcmp (argv[1], "-v") == 0
381       || CONST_STRNEQ (argv[1], "--v"))
382     print_version ("ranlib");
383   arg_index = 1;
384
385   if (strcmp (argv[1], "-t") == 0)
386     {
387       ++arg_index;
388       touch = TRUE;
389     }
390
391   while (arg_index < argc)
392     {
393       if (! touch)
394         status |= ranlib_only (argv[arg_index]);
395       else
396         status |= ranlib_touch (argv[arg_index]);
397       ++arg_index;
398     }
399
400   xexit (status);
401 }
402
403 /* The option parsing should be in its own function.
404    It will be when I have getopt working.  */
405
406 int main (int, char **);
407
408 int
409 main (int argc, char **argv)
410 {
411   char *arg_ptr;
412   char c;
413   enum
414     {
415       none = 0, del, replace, print_table,
416       print_files, extract, move, quick_append
417     } operation = none;
418   int arg_index;
419   char **files;
420   int file_count;
421   char *inarch_filename;
422   int show_version;
423   int i;
424   int do_posix = 0;
425
426 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
427   setlocale (LC_MESSAGES, "");
428 #endif
429 #if defined (HAVE_SETLOCALE)
430   setlocale (LC_CTYPE, "");
431 #endif
432   bindtextdomain (PACKAGE, LOCALEDIR);
433   textdomain (PACKAGE);
434
435   program_name = argv[0];
436   xmalloc_set_program_name (program_name);
437 #if BFD_SUPPORTS_PLUGINS
438   bfd_plugin_set_program_name (program_name);
439 #endif
440
441   expandargv (&argc, &argv);
442
443   if (is_ranlib < 0)
444     {
445       const char *temp = lbasename (program_name);
446
447       if (strlen (temp) >= 6
448           && FILENAME_CMP (temp + strlen (temp) - 6, "ranlib") == 0)
449         is_ranlib = 1;
450       else
451         is_ranlib = 0;
452     }
453
454
455   START_PROGRESS (program_name, 0);
456
457   bfd_init ();
458   set_default_bfd_target ();
459
460   show_version = 0;
461
462   xatexit (remove_output);
463
464   for (i = 1; i < argc; i++)
465     if (! ar_emul_parse_arg (argv[i]))
466       break;
467   argv += (i - 1);
468   argc -= (i - 1);
469
470   if (is_ranlib)
471     ranlib_main(argc, argv);
472
473   if (argc > 1 && argv[1][0] == '-')
474     {
475       if (strcmp (argv[1], "--help") == 0)
476         usage (1);
477       else if (strcmp (argv[1], "--version") == 0)
478         print_version ("ar");
479     }
480
481   if (argc == 2 && strcmp (argv[1], "-M") == 0)
482     {
483       mri_emul ();
484       xexit (0);
485     }
486
487   if (argc < 2)
488     usage (0);
489
490   arg_index = 1;
491   arg_ptr = argv[arg_index];
492
493   if (strcmp (arg_ptr, "--plugin") == 0)
494     {
495 #if BFD_SUPPORTS_PLUGINS
496       if (argc < 4)
497         usage (1);
498
499       bfd_plugin_set_plugin (argv[2]);
500
501       arg_index += 2;
502       arg_ptr = argv[arg_index];
503
504       plugin_target = "plugin";
505 #else
506       fprintf (stderr, _("sorry - this program has been built without plugin support\n"));
507       xexit (1);
508 #endif
509     }
510
511   if (*arg_ptr == '-')
512     {
513       /* When the first option starts with '-' we support POSIX-compatible
514          option parsing.  */
515       do_posix = 1;
516       ++arg_ptr;                        /* compatibility */
517     }
518
519   do
520     {
521       while ((c = *arg_ptr++) != '\0')
522         {
523           switch (c)
524             {
525             case 'd':
526             case 'm':
527             case 'p':
528             case 'q':
529             case 'r':
530             case 't':
531             case 'x':
532               if (operation != none)
533                 fatal (_("two different operation options specified"));
534               switch (c)
535                 {
536                 case 'd':
537                   operation = del;
538                   operation_alters_arch = TRUE;
539                   break;
540                 case 'm':
541                   operation = move;
542                   operation_alters_arch = TRUE;
543                   break;
544                 case 'p':
545                   operation = print_files;
546                   break;
547                 case 'q':
548                   operation = quick_append;
549                   operation_alters_arch = TRUE;
550                   break;
551                 case 'r':
552                   operation = replace;
553                   operation_alters_arch = TRUE;
554                   break;
555                 case 't':
556                   operation = print_table;
557                   break;
558                 case 'x':
559                   operation = extract;
560                   break;
561                 }
562             case 'l':
563               break;
564             case 'c':
565               silent_create = 1;
566               break;
567             case 'o':
568               preserve_dates = 1;
569               break;
570             case 'V':
571               show_version = TRUE;
572               break;
573             case 's':
574               write_armap = 1;
575               break;
576             case 'S':
577               write_armap = -1;
578               break;
579             case 'u':
580               newer_only = 1;
581               break;
582             case 'v':
583               verbose = 1;
584               break;
585             case 'a':
586               postype = pos_after;
587               break;
588             case 'b':
589               postype = pos_before;
590               break;
591             case 'i':
592               postype = pos_before;
593               break;
594             case 'M':
595               mri_mode = 1;
596               break;
597             case 'N':
598               counted_name_mode = TRUE;
599               break;
600             case 'f':
601               ar_truncate = TRUE;
602               break;
603             case 'P':
604               full_pathname = TRUE;
605               break;
606             case 'T':
607               make_thin_archive = TRUE;
608               break;
609             case 'D':
610               deterministic = TRUE;
611               break;
612             default:
613               /* xgettext:c-format */
614               non_fatal (_("illegal option -- %c"), c);
615               usage (0);
616             }
617         }
618
619       /* With POSIX-compatible option parsing continue with the next
620          argument if it starts with '-'.  */
621       if (do_posix && arg_index + 1 < argc && argv[arg_index + 1][0] == '-')
622         arg_ptr = argv[++arg_index] + 1;
623       else
624         do_posix = 0;
625     }
626   while (do_posix);
627
628   if (show_version)
629     print_version ("ar");
630
631   ++arg_index;
632   if (arg_index >= argc)
633     usage (0);
634
635   if (mri_mode)
636     {
637       mri_emul ();
638     }
639   else
640     {
641       bfd *arch;
642
643       /* We don't use do_quick_append any more.  Too many systems
644          expect ar to always rebuild the symbol table even when q is
645          used.  */
646
647       /* We can't write an armap when using ar q, so just do ar r
648          instead.  */
649       if (operation == quick_append && write_armap)
650         operation = replace;
651
652       if ((operation == none || operation == print_table)
653           && write_armap == 1)
654         xexit (ranlib_only (argv[arg_index]));
655
656       if (operation == none)
657         fatal (_("no operation specified"));
658
659       if (newer_only && operation != replace)
660         fatal (_("`u' is only meaningful with the `r' option."));
661
662       if (newer_only && deterministic)
663         fatal (_("`u' is not meaningful with the `D' option."));
664
665       if (postype != pos_default)
666         posname = argv[arg_index++];
667
668       if (counted_name_mode)
669         {
670           if (operation != extract && operation != del)
671              fatal (_("`N' is only meaningful with the `x' and `d' options."));
672           counted_name_counter = atoi (argv[arg_index++]);
673           if (counted_name_counter <= 0)
674             fatal (_("Value for `N' must be positive."));
675         }
676
677       inarch_filename = argv[arg_index++];
678
679       files = arg_index < argc ? argv + arg_index : NULL;
680       file_count = argc - arg_index;
681
682       arch = open_inarch (inarch_filename,
683                           files == NULL ? (char *) NULL : files[0]);
684
685       if (operation == extract && bfd_is_thin_archive (arch))
686         fatal (_("`x' cannot be used on thin archives."));
687
688       switch (operation)
689         {
690         case print_table:
691           map_over_members (arch, print_descr, files, file_count);
692           break;
693
694         case print_files:
695           map_over_members (arch, print_contents, files, file_count);
696           break;
697
698         case extract:
699           map_over_members (arch, extract_file, files, file_count);
700           break;
701
702         case del:
703           if (files != NULL)
704             delete_members (arch, files);
705           else
706             output_filename = NULL;
707           break;
708
709         case move:
710           if (files != NULL)
711             move_members (arch, files);
712           else
713             output_filename = NULL;
714           break;
715
716         case replace:
717         case quick_append:
718           if (files != NULL || write_armap > 0)
719             replace_members (arch, files, operation == quick_append);
720           else
721             output_filename = NULL;
722           break;
723
724           /* Shouldn't happen! */
725         default:
726           /* xgettext:c-format */
727           fatal (_("internal error -- this option not implemented"));
728         }
729     }
730
731   END_PROGRESS (program_name);
732
733   xexit (0);
734   return 0;
735 }
736
737 bfd *
738 open_inarch (const char *archive_filename, const char *file)
739 {
740   const char *target;
741   bfd **last_one;
742   bfd *next_one;
743   struct stat sbuf;
744   bfd *arch;
745   char **matching;
746
747   bfd_set_error (bfd_error_no_error);
748
749   target = plugin_target;
750
751   if (stat (archive_filename, &sbuf) != 0)
752     {
753 #if !defined(__GO32__) || defined(__DJGPP__)
754
755       /* FIXME: I don't understand why this fragment was ifndef'ed
756          away for __GO32__; perhaps it was in the days of DJGPP v1.x.
757          stat() works just fine in v2.x, so I think this should be
758          removed.  For now, I enable it for DJGPP v2. -- EZ.  */
759
760 /* KLUDGE ALERT! Temporary fix until I figger why
761    stat() is wrong ... think it's buried in GO32's IDT - Jax */
762       if (errno != ENOENT)
763         bfd_fatal (archive_filename);
764 #endif
765
766       if (!operation_alters_arch)
767         {
768           fprintf (stderr, "%s: ", program_name);
769           perror (archive_filename);
770           maybequit ();
771           return NULL;
772         }
773
774       /* Try to figure out the target to use for the archive from the
775          first object on the list.  */
776       if (file != NULL)
777         {
778           bfd *obj;
779
780           obj = bfd_openr (file, target);
781           if (obj != NULL)
782             {
783               if (bfd_check_format (obj, bfd_object))
784                 target = bfd_get_target (obj);
785               (void) bfd_close (obj);
786             }
787         }
788
789       /* Create an empty archive.  */
790       arch = bfd_openw (archive_filename, target);
791       if (arch == NULL
792           || ! bfd_set_format (arch, bfd_archive)
793           || ! bfd_close (arch))
794         bfd_fatal (archive_filename);
795       else if (!silent_create)
796         non_fatal (_("creating %s"), archive_filename);
797
798       /* If we die creating a new archive, don't leave it around.  */
799       output_filename = archive_filename;
800     }
801
802   arch = bfd_openr (archive_filename, target);
803   if (arch == NULL)
804     {
805     bloser:
806       bfd_fatal (archive_filename);
807     }
808
809   if (! bfd_check_format_matches (arch, bfd_archive, &matching))
810     {
811       bfd_nonfatal (archive_filename);
812       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
813         {
814           list_matching_formats (matching);
815           free (matching);
816         }
817       xexit (1);
818     }
819
820   last_one = &(arch->archive_next);
821   /* Read all the contents right away, regardless.  */
822   for (next_one = bfd_openr_next_archived_file (arch, NULL);
823        next_one;
824        next_one = bfd_openr_next_archived_file (arch, next_one))
825     {
826       PROGRESS (1);
827       *last_one = next_one;
828       last_one = &next_one->archive_next;
829     }
830   *last_one = (bfd *) NULL;
831   if (bfd_get_error () != bfd_error_no_more_archived_files)
832     goto bloser;
833   return arch;
834 }
835
836 static void
837 print_contents (bfd *abfd)
838 {
839   size_t ncopied = 0;
840   char *cbuf = (char *) xmalloc (BUFSIZE);
841   struct stat buf;
842   size_t size;
843   if (bfd_stat_arch_elt (abfd, &buf) != 0)
844     /* xgettext:c-format */
845     fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
846
847   if (verbose)
848     printf ("\n<%s>\n\n", bfd_get_filename (abfd));
849
850   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
851
852   size = buf.st_size;
853   while (ncopied < size)
854     {
855
856       size_t nread;
857       size_t tocopy = size - ncopied;
858       if (tocopy > BUFSIZE)
859         tocopy = BUFSIZE;
860
861       nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
862       if (nread != tocopy)
863         /* xgettext:c-format */
864         fatal (_("%s is not a valid archive"),
865                bfd_get_filename (bfd_my_archive (abfd)));
866
867       /* fwrite in mingw32 may return int instead of size_t. Cast the
868          return value to size_t to avoid comparison between signed and
869          unsigned values.  */
870       if ((size_t) fwrite (cbuf, 1, nread, stdout) != nread)
871         fatal ("stdout: %s", strerror (errno));
872       ncopied += tocopy;
873     }
874   free (cbuf);
875 }
876
877 /* Extract a member of the archive into its own file.
878
879    We defer opening the new file until after we have read a BUFSIZ chunk of the
880    old one, since we know we have just read the archive header for the old
881    one.  Since most members are shorter than BUFSIZ, this means we will read
882    the old header, read the old data, write a new inode for the new file, and
883    write the new data, and be done. This 'optimization' is what comes from
884    sitting next to a bare disk and hearing it every time it seeks.  -- Gnu
885    Gilmore  */
886
887 void
888 extract_file (bfd *abfd)
889 {
890   FILE *ostream;
891   char *cbuf = (char *) xmalloc (BUFSIZE);
892   size_t nread, tocopy;
893   size_t ncopied = 0;
894   size_t size;
895   struct stat buf;
896
897   if (bfd_stat_arch_elt (abfd, &buf) != 0)
898     /* xgettext:c-format */
899     fatal (_("internal stat error on %s"), bfd_get_filename (abfd));
900   size = buf.st_size;
901
902   if (verbose)
903     printf ("x - %s\n", bfd_get_filename (abfd));
904
905   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
906
907   ostream = NULL;
908   if (size == 0)
909     {
910       /* Seems like an abstraction violation, eh?  Well it's OK! */
911       output_filename = bfd_get_filename (abfd);
912
913       ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
914       if (ostream == NULL)
915         {
916           perror (bfd_get_filename (abfd));
917           xexit (1);
918         }
919
920       output_file = ostream;
921     }
922   else
923     while (ncopied < size)
924       {
925         tocopy = size - ncopied;
926         if (tocopy > BUFSIZE)
927           tocopy = BUFSIZE;
928
929         nread = bfd_bread (cbuf, (bfd_size_type) tocopy, abfd);
930         if (nread != tocopy)
931           /* xgettext:c-format */
932           fatal (_("%s is not a valid archive"),
933                  bfd_get_filename (bfd_my_archive (abfd)));
934
935         /* See comment above; this saves disk arm motion */
936         if (ostream == NULL)
937           {
938             /* Seems like an abstraction violation, eh?  Well it's OK! */
939             output_filename = bfd_get_filename (abfd);
940
941             ostream = fopen (bfd_get_filename (abfd), FOPEN_WB);
942             if (ostream == NULL)
943               {
944                 perror (bfd_get_filename (abfd));
945                 xexit (1);
946               }
947
948             output_file = ostream;
949           }
950
951         /* fwrite in mingw32 may return int instead of size_t. Cast
952            the return value to size_t to avoid comparison between
953            signed and unsigned values.  */
954         if ((size_t) fwrite (cbuf, 1, nread, ostream) != nread)
955           fatal ("%s: %s", output_filename, strerror (errno));
956         ncopied += tocopy;
957       }
958
959   if (ostream != NULL)
960     fclose (ostream);
961
962   output_file = NULL;
963   output_filename = NULL;
964
965   chmod (bfd_get_filename (abfd), buf.st_mode);
966
967   if (preserve_dates)
968     {
969       /* Set access time to modification time.  Only st_mtime is
970          initialized by bfd_stat_arch_elt.  */
971       buf.st_atime = buf.st_mtime;
972       set_times (bfd_get_filename (abfd), &buf);
973     }
974
975   free (cbuf);
976 }
977
978 static void
979 write_archive (bfd *iarch)
980 {
981   bfd *obfd;
982   char *old_name, *new_name;
983   bfd *contents_head = iarch->archive_next;
984
985   old_name = (char *) xmalloc (strlen (bfd_get_filename (iarch)) + 1);
986   strcpy (old_name, bfd_get_filename (iarch));
987   new_name = make_tempname (old_name);
988
989   if (new_name == NULL)
990     bfd_fatal ("could not create temporary file whilst writing archive");
991
992   output_filename = new_name;
993
994   obfd = bfd_openw (new_name, bfd_get_target (iarch));
995
996   if (obfd == NULL)
997     bfd_fatal (old_name);
998
999   output_bfd = obfd;
1000
1001   bfd_set_format (obfd, bfd_archive);
1002
1003   /* Request writing the archive symbol table unless we've
1004      been explicitly requested not to.  */
1005   obfd->has_armap = write_armap >= 0;
1006
1007   if (ar_truncate)
1008     {
1009       /* This should really use bfd_set_file_flags, but that rejects
1010          archives.  */
1011       obfd->flags |= BFD_TRADITIONAL_FORMAT;
1012     }
1013
1014   if (deterministic)
1015     obfd->flags |= BFD_DETERMINISTIC_OUTPUT;
1016
1017   if (make_thin_archive || bfd_is_thin_archive (iarch))
1018     bfd_is_thin_archive (obfd) = 1;
1019
1020   if (!bfd_set_archive_head (obfd, contents_head))
1021     bfd_fatal (old_name);
1022
1023   if (!bfd_close (obfd))
1024     bfd_fatal (old_name);
1025
1026   output_bfd = NULL;
1027   output_filename = NULL;
1028
1029   /* We don't care if this fails; we might be creating the archive.  */
1030   bfd_close (iarch);
1031
1032   if (smart_rename (new_name, old_name, 0) != 0)
1033     xexit (1);
1034 }
1035
1036 /* Return a pointer to the pointer to the entry which should be rplacd'd
1037    into when altering.  DEFAULT_POS should be how to interpret pos_default,
1038    and should be a pos value.  */
1039
1040 static bfd **
1041 get_pos_bfd (bfd **contents, enum pos default_pos, const char *default_posname)
1042 {
1043   bfd **after_bfd = contents;
1044   enum pos realpos;
1045   const char *realposname;
1046
1047   if (postype == pos_default)
1048     {
1049       realpos = default_pos;
1050       realposname = default_posname;
1051     }
1052   else
1053     {
1054       realpos = postype;
1055       realposname = posname;
1056     }
1057
1058   if (realpos == pos_end)
1059     {
1060       while (*after_bfd)
1061         after_bfd = &((*after_bfd)->archive_next);
1062     }
1063   else
1064     {
1065       for (; *after_bfd; after_bfd = &(*after_bfd)->archive_next)
1066         if (FILENAME_CMP ((*after_bfd)->filename, realposname) == 0)
1067           {
1068             if (realpos == pos_after)
1069               after_bfd = &(*after_bfd)->archive_next;
1070             break;
1071           }
1072     }
1073   return after_bfd;
1074 }
1075
1076 static void
1077 delete_members (bfd *arch, char **files_to_delete)
1078 {
1079   bfd **current_ptr_ptr;
1080   bfd_boolean found;
1081   bfd_boolean something_changed = FALSE;
1082   int match_count;
1083
1084   for (; *files_to_delete != NULL; ++files_to_delete)
1085     {
1086       /* In a.out systems, the armap is optional.  It's also called
1087          __.SYMDEF.  So if the user asked to delete it, we should remember
1088          that fact. This isn't quite right for COFF systems (where
1089          __.SYMDEF might be regular member), but it's very unlikely
1090          to be a problem.  FIXME */
1091
1092       if (!strcmp (*files_to_delete, "__.SYMDEF"))
1093         {
1094           arch->has_armap = FALSE;
1095           write_armap = -1;
1096           continue;
1097         }
1098
1099       found = FALSE;
1100       match_count = 0;
1101       current_ptr_ptr = &(arch->archive_next);
1102       while (*current_ptr_ptr)
1103         {
1104           if (FILENAME_CMP (normalize (*files_to_delete, arch),
1105                             (*current_ptr_ptr)->filename) == 0)
1106             {
1107               ++match_count;
1108               if (counted_name_mode
1109                   && match_count != counted_name_counter)
1110                 {
1111                   /* Counting, and didn't match on count; go on to the
1112                      next one.  */
1113                 }
1114               else
1115                 {
1116                   found = TRUE;
1117                   something_changed = TRUE;
1118                   if (verbose)
1119                     printf ("d - %s\n",
1120                             *files_to_delete);
1121                   *current_ptr_ptr = ((*current_ptr_ptr)->archive_next);
1122                   goto next_file;
1123                 }
1124             }
1125
1126           current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1127         }
1128
1129       if (verbose && !found)
1130         {
1131           /* xgettext:c-format */
1132           printf (_("No member named `%s'\n"), *files_to_delete);
1133         }
1134     next_file:
1135       ;
1136     }
1137
1138   if (something_changed)
1139     write_archive (arch);
1140   else
1141     output_filename = NULL;
1142 }
1143
1144
1145 /* Reposition existing members within an archive */
1146
1147 static void
1148 move_members (bfd *arch, char **files_to_move)
1149 {
1150   bfd **after_bfd;              /* New entries go after this one */
1151   bfd **current_ptr_ptr;        /* cdr pointer into contents */
1152
1153   for (; *files_to_move; ++files_to_move)
1154     {
1155       current_ptr_ptr = &(arch->archive_next);
1156       while (*current_ptr_ptr)
1157         {
1158           bfd *current_ptr = *current_ptr_ptr;
1159           if (FILENAME_CMP (normalize (*files_to_move, arch),
1160                             current_ptr->filename) == 0)
1161             {
1162               /* Move this file to the end of the list - first cut from
1163                  where it is.  */
1164               bfd *link_bfd;
1165               *current_ptr_ptr = current_ptr->archive_next;
1166
1167               /* Now glue to end */
1168               after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1169               link_bfd = *after_bfd;
1170               *after_bfd = current_ptr;
1171               current_ptr->archive_next = link_bfd;
1172
1173               if (verbose)
1174                 printf ("m - %s\n", *files_to_move);
1175
1176               goto next_file;
1177             }
1178
1179           current_ptr_ptr = &((*current_ptr_ptr)->archive_next);
1180         }
1181       /* xgettext:c-format */
1182       fatal (_("no entry %s in archive %s!"), *files_to_move, arch->filename);
1183
1184     next_file:;
1185     }
1186
1187   write_archive (arch);
1188 }
1189
1190 /* Ought to default to replacing in place, but this is existing practice!  */
1191
1192 static void
1193 replace_members (bfd *arch, char **files_to_move, bfd_boolean quick)
1194 {
1195   bfd_boolean changed = FALSE;
1196   bfd **after_bfd;              /* New entries go after this one.  */
1197   bfd *current;
1198   bfd **current_ptr;
1199
1200   while (files_to_move && *files_to_move)
1201     {
1202       if (! quick)
1203         {
1204           current_ptr = &arch->archive_next;
1205           while (*current_ptr)
1206             {
1207               current = *current_ptr;
1208
1209               /* For compatibility with existing ar programs, we
1210                  permit the same file to be added multiple times.  */
1211               if (FILENAME_CMP (normalize (*files_to_move, arch),
1212                                 normalize (current->filename, arch)) == 0
1213                   && current->arelt_data != NULL)
1214                 {
1215                   if (newer_only)
1216                     {
1217                       struct stat fsbuf, asbuf;
1218
1219                       if (stat (*files_to_move, &fsbuf) != 0)
1220                         {
1221                           if (errno != ENOENT)
1222                             bfd_fatal (*files_to_move);
1223                           goto next_file;
1224                         }
1225                       if (bfd_stat_arch_elt (current, &asbuf) != 0)
1226                         /* xgettext:c-format */
1227                         fatal (_("internal stat error on %s"),
1228                                current->filename);
1229
1230                       if (fsbuf.st_mtime <= asbuf.st_mtime)
1231                         goto next_file;
1232                     }
1233
1234                   after_bfd = get_pos_bfd (&arch->archive_next, pos_after,
1235                                            current->filename);
1236                   if (ar_emul_replace (after_bfd, *files_to_move,
1237                                        plugin_target, verbose))
1238                     {
1239                       /* Snip out this entry from the chain.  */
1240                       *current_ptr = (*current_ptr)->archive_next;
1241                       changed = TRUE;
1242                     }
1243
1244                   goto next_file;
1245                 }
1246               current_ptr = &(current->archive_next);
1247             }
1248         }
1249
1250       /* Add to the end of the archive.  */
1251       after_bfd = get_pos_bfd (&arch->archive_next, pos_end, NULL);
1252
1253       if (ar_emul_append (after_bfd, *files_to_move, plugin_target,
1254                           verbose, make_thin_archive))
1255         changed = TRUE;
1256
1257     next_file:;
1258
1259       files_to_move++;
1260     }
1261
1262   if (changed)
1263     write_archive (arch);
1264   else
1265     output_filename = NULL;
1266 }
1267
1268 static int
1269 ranlib_only (const char *archname)
1270 {
1271   bfd *arch;
1272
1273   if (get_file_size (archname) < 1)
1274     return 1;
1275   write_armap = 1;
1276   arch = open_inarch (archname, (char *) NULL);
1277   if (arch == NULL)
1278     xexit (1);
1279   write_archive (arch);
1280   return 0;
1281 }
1282
1283 /* Update the timestamp of the symbol map of an archive.  */
1284
1285 static int
1286 ranlib_touch (const char *archname)
1287 {
1288 #ifdef __GO32__
1289   /* I don't think updating works on go32.  */
1290   ranlib_only (archname);
1291 #else
1292   int f;
1293   bfd *arch;
1294   char **matching;
1295
1296   if (get_file_size (archname) < 1)
1297     return 1;
1298   f = open (archname, O_RDWR | O_BINARY, 0);
1299   if (f < 0)
1300     {
1301       bfd_set_error (bfd_error_system_call);
1302       bfd_fatal (archname);
1303     }
1304
1305   arch = bfd_fdopenr (archname, (const char *) NULL, f);
1306   if (arch == NULL)
1307     bfd_fatal (archname);
1308   if (! bfd_check_format_matches (arch, bfd_archive, &matching))
1309     {
1310       bfd_nonfatal (archname);
1311       if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1312         {
1313           list_matching_formats (matching);
1314           free (matching);
1315         }
1316       xexit (1);
1317     }
1318
1319   if (! bfd_has_map (arch))
1320     /* xgettext:c-format */
1321     fatal (_("%s: no archive map to update"), archname);
1322
1323   bfd_update_armap_timestamp (arch);
1324
1325   if (! bfd_close (arch))
1326     bfd_fatal (archname);
1327 #endif
1328   return 0;
1329 }
1330
1331 /* Things which are interesting to map over all or some of the files: */
1332
1333 static void
1334 print_descr (bfd *abfd)
1335 {
1336   print_arelt_descr (stdout, abfd, verbose);
1337 }