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