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