* ld-scripts/script.t: Put .pr in .text, and .rw in .data, for
[deliverable/binutils-gdb.git] / binutils / ar.c
index a7cad3960af0f2d48d92c87da08890e30a444be0..eb98ab395c38df427b762f322c18077547f5a8e6 100644 (file)
@@ -27,6 +27,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #include "bfd.h"
 #include "sysdep.h"
 #include "libiberty.h"
+#include "progress.h"
 #include "bucomm.h"
 #include "aout/ar.h"
 #include "libbfd.h"
@@ -60,6 +61,9 @@ struct ar_hdr *
 
 /* Forward declarations */
 
+static const char *
+normalize PARAMS ((const char *, bfd *));
+
 static void
 remove_output PARAMS ((void));
 
@@ -138,6 +142,9 @@ enum pos
     pos_default, pos_before, pos_after, pos_end
   } postype = pos_default;
 
+/* Whether to truncate names of files stored in the archive.  */
+static boolean ar_truncate = false;
+
 int interactive = 0;
 
 void
@@ -163,7 +170,10 @@ map_over_members (arch, function, files, count)
   if (count == 0)
     {
       for (head = arch->next; head; head = head->next)
-       function (head);
+       {
+         PROGRESS (1);
+         function (head);
+       }
       return;
     }
   /* This may appear to be a baroque way of accomplishing what we want.
@@ -175,8 +185,10 @@ map_over_members (arch, function, files, count)
   for (; count > 0; files++, count--)
     {
       boolean found = false;
+
       for (head = arch->next; head; head = head->next)
        {
+         PROGRESS (1);
          if (head->filename == NULL)
            {
              /* Some archive formats don't get the filenames filled in
@@ -218,25 +230,39 @@ Usage: %s [-]{dmpqrtx}[abcilosuvV] [member-name] archive-file file...\n\
   else
     fprintf (stderr, "\
 Usage: %s [-vV] archive\n", program_name);
+  list_supported_targets (program_name, stderr);
   xexit (1);
 }
 
 /* Normalize a file name specified on the command line into a file
    name which we will use in an archive.  */
 
-static char *
-normalize (file)
-     char *file;
+static const char *
+normalize (file, abfd)
+     const char *file;
+     bfd *abfd;
 {
-  char *filename = strrchr (file, '/');
+  const char *filename;
+
+  filename = strrchr (file, '/');
   if (filename != (char *) NULL)
-    {
-      filename++;
-    }
+    filename++;
   else
+    filename = file;
+
+  if (ar_truncate
+      && abfd != NULL
+      && strlen (filename) > abfd->xvec->ar_max_namelen)
     {
-      filename = file;
+      char *s;
+
+      /* Space leak.  */
+      s = (char *) xmalloc (abfd->xvec->ar_max_namelen + 1);
+      memcpy (s, filename, abfd->xvec->ar_max_namelen);
+      s[abfd->xvec->ar_max_namelen] = '\0';
+      filename = s;
     }
+
   return filename;
 }
 
@@ -283,6 +309,8 @@ main (argc, argv)
   program_name = argv[0];
   xmalloc_set_program_name (program_name);
 
+  START_PROGRESS (program_name, 0);
+
   bfd_init ();
   show_version = 0;
 
@@ -410,6 +438,9 @@ main (argc, argv)
        case 'M':
          mri_mode = 1;
          break;
+       case 'f':
+         ar_truncate = true;
+         break;
        default:
          fprintf (stderr, "%s: illegal option -- %c\n", program_name, c);
          usage ();
@@ -462,13 +493,13 @@ main (argc, argv)
         rebuild the name table.  Unfortunately, at this point we
         don't actually know the maximum name length permitted by this
         object file format.  So, we guess.  FIXME.  */
-      if (operation == quick_append)
+      if (operation == quick_append && ! ar_truncate)
        {
          char **chk;
 
          for (chk = files; chk != NULL && *chk != '\0'; chk++)
            {
-             if (strlen (normalize (*chk)) > 14)
+             if (strlen (normalize (*chk, (bfd *) NULL)) > 14)
                {
                  operation = replace;
                  break;
@@ -523,6 +554,8 @@ main (argc, argv)
        }
     }
 
+  END_PROGRESS (program_name);
+
   xexit (0);
   return 0;
 }
@@ -579,6 +612,7 @@ open_inarch (archive_filename)
        next_one;
        next_one = bfd_openr_next_archived_file (arch, next_one))
     {
+      PROGRESS (1);
       *last_one = next_one;
       last_one = &next_one->next;
     }
@@ -593,6 +627,7 @@ print_contents (abfd)
      bfd *abfd;
 {
   int ncopied = 0;
+  char *cbuf = xmalloc (BUFSIZE);
   struct stat buf;
   long size;
   if (bfd_stat_arch_elt (abfd, &buf) != 0)
@@ -606,7 +641,7 @@ print_contents (abfd)
   size = buf.st_size;
   while (ncopied < size)
     {
-      char cbuf[BUFSIZE];
+
       int nread;
       int tocopy = size - ncopied;
       if (tocopy > BUFSIZE)
@@ -620,6 +655,7 @@ print_contents (abfd)
       fwrite (cbuf, 1, nread, stdout);
       ncopied += tocopy;
     }
+  free (cbuf);
 }
 
 /* Extract a member of the archive into its own file.
@@ -637,7 +673,7 @@ extract_file (abfd)
      bfd *abfd;
 {
   FILE *ostream;
-  char cbuf[BUFSIZE];
+  char *cbuf = xmalloc (BUFSIZE);
   int nread, tocopy;
   int ncopied = 0;
   long size;
@@ -727,6 +763,7 @@ extract_file (abfd)
 #endif /* ! USE_UTIME */
 #endif /* ! POSIX_UTIME */
     }
+free (cbuf);
 }
 
 /* Just do it quickly; don't worry about dups, armap, or anything like that */
@@ -737,7 +774,7 @@ do_quick_append (archive_filename, files_to_append)
      char **files_to_append;
 {
   FILE *ofile, *ifile;
-  char buf[BUFSIZE];
+  char *buf = xmalloc (BUFSIZE);
   long tocopy, thistime;
   bfd *temp;
   struct stat sbuf;
@@ -786,6 +823,9 @@ do_quick_append (archive_filename, files_to_append)
                 program_name, archive_filename);
     }
 
+  if (ar_truncate)
+    temp->flags |= BFD_TRADITIONAL_FORMAT;
+
   /* assume it's an achive, go straight to the end, sans $200 */
   fseek (ofile, 0, 2);
 
@@ -830,6 +870,7 @@ do_quick_append (archive_filename, files_to_append)
     }
   fclose (ofile);
   bfd_close (temp);
+  free (buf);
 }
 
 
@@ -838,19 +879,12 @@ write_archive (iarch)
      bfd *iarch;
 {
   bfd *obfd;
-  int namelen = strlen (bfd_get_filename (iarch));
-  char *old_name = xmalloc (namelen + 1);
-  char *new_name = xmalloc (namelen + EXT_NAME_LEN);
+  char *old_name, *new_name;
   bfd *contents_head = iarch->next;
 
+  old_name = xmalloc (strlen (bfd_get_filename (iarch)) + 1);
   strcpy (old_name, bfd_get_filename (iarch));
-  strcpy (new_name, bfd_get_filename (iarch));
-
-#ifdef __GO32__                        /* avoid long .extensions for MS-DOS */
-  strcpy (new_name + namelen, "-a");
-#else
-  strcpy (new_name + namelen, "-art");
-#endif
+  new_name = make_tempname (old_name);
 
   output_filename = new_name;
 
@@ -867,6 +901,13 @@ write_archive (iarch)
      been explicitly requested not to.  */
   obfd->has_armap = write_armap >= 0;
 
+  if (ar_truncate)
+    {
+      /* This should really use bfd_set_file_flags, but that rejects
+         archives.  */
+      obfd->flags |= BFD_TRADITIONAL_FORMAT;
+    }
+
   if (bfd_set_archive_head (obfd, contents_head) != true)
     bfd_fatal (old_name);
 
@@ -988,7 +1029,8 @@ move_members (arch, files_to_move)
       while (*current_ptr_ptr)
        {
          bfd *current_ptr = *current_ptr_ptr;
-         if (strcmp (normalize (*files_to_move), current_ptr->filename) == 0)
+         if (strcmp (normalize (*files_to_move, arch),
+                     current_ptr->filename) == 0)
            {
              /* Move this file to the end of the list - first cut from
                 where it is.  */
@@ -1037,7 +1079,8 @@ replace_members (arch, files_to_move)
        {
          current = *current_ptr;
 
-         if (!strcmp (normalize (*files_to_move), current->filename))
+         if (!strcmp (normalize (*files_to_move, arch),
+                      normalize (current->filename, arch)))
            {
              if (newer_only)
                {
@@ -1080,8 +1123,7 @@ replace_members (arch, files_to_move)
 
              if (verbose)
                {
-                 printf ("%c - %s\n", (postype == pos_after ? 'r' : 'a'),
-                         *files_to_move);
+                 printf ("r - %s\n", *files_to_move);
                }
              goto next_file;
            }
@@ -1099,7 +1141,7 @@ replace_members (arch, files_to_move)
        }
       if (verbose)
        {
-         printf ("c - %s\n", *files_to_move);
+         printf ("a - %s\n", *files_to_move);
        }
 
       (*after_bfd)->next = temp;
This page took 0.025877 seconds and 4 git commands to generate.