Subversion Repositories Kolibri OS

Rev

Rev 5221 | Blame | Compare with Previous | Last modification | View Log | RSS feed

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