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