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