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