Subversion Repositories Kolibri OS

Rev

Rev 5221 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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