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