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