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