Add dv-mn103iop.c to list of files to sanitize out.
[deliverable/binutils-gdb.git] / binutils / strings.c
index aea9934cf39f92bbaafd81f1bccf1be0aa2ef039..20fa6c8a9349a5e428b401ddfd0dfabb42802317 100644 (file)
@@ -1,5 +1,5 @@
 /* strings -- print the strings of printable characters in files
-   Copyright (C) 1993 Free Software Foundation, Inc.
+   Copyright (C) 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -13,7 +13,8 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
+   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+   02111-1307, USA.  */
 \f
 /* Usage: strings [options] file...
 
@@ -37,6 +38,9 @@
    -o          Like -to.  (Some other implementations have -o like -to,
                others like -td.  We chose one arbitrarily.)
 
+   --target=BFDNAME
+               Specify a non-default object file format.
+
    --help
    -h          Print the usage message on the standard output.
 
    Written by Richard Stallman <rms@gnu.ai.mit.edu>
    and David MacKenzie <djm@gnu.ai.mit.edu>.  */
 
+#include "bfd.h"
 #include <stdio.h>
 #include <getopt.h>
 #include <ctype.h>
 #include <errno.h>
-#include <bfd.h>
+#include "bucomm.h"
+#include "libiberty.h"
 
 #ifdef isascii
 #define isgraphic(c) (isascii (c) && isprint (c))
@@ -63,7 +69,7 @@ extern int errno;
 #endif
 
 /* The BFD section flags that identify an initialized data section.  */
-#define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS)
+#define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
 
 /* Radix for printing addresses (must be 8, 10 or 16).  */
 static int address_radix;
@@ -83,11 +89,8 @@ static boolean datasection_only;
 /* true if we found an initialized data section in the current file.  */
 static boolean got_a_section;
 
-/* Opened to /dev/null for reading from a BFD.  */
-static FILE *devnull;
-
-extern char *program_name;
-extern char *program_version;
+/* The BFD object file format.  */
+static char *target;
 
 static struct option long_options[] =
 {
@@ -95,35 +98,43 @@ static struct option long_options[] =
   {"print-file-name", no_argument, NULL, 'f'},
   {"bytes", required_argument, NULL, 'n'},
   {"radix", required_argument, NULL, 't'},
+  {"target", required_argument, NULL, 'T'},
   {"help", no_argument, NULL, 'h'},
   {"version", no_argument, NULL, 'v'},
   {NULL, 0, NULL, 0}
 };
 
-char *xmalloc ();
-char *xrealloc ();
-
-static boolean strings_file ();
-static int integer_arg ();
-static void dump_strings ();
-static void usage ();
+static void strings_a_section PARAMS ((bfd *, asection *, PTR));
+static boolean strings_object_file PARAMS ((const char *));
+static boolean strings_file PARAMS ((char *file));
+static int integer_arg PARAMS ((char *s));
+static void print_strings PARAMS ((const char *filename, FILE *stream,
+                                 file_ptr address, int stop_point,
+                                 int magiccount, char *magic));
+static void usage PARAMS ((FILE *stream, int status));
 \f
-void
+int
 main (argc, argv)
      int argc;
      char **argv;
 {
   int optc;
   int exit_status = 0;
-  boolean files_given = false; /* false if any files were given.  */
+  boolean files_given = false;
+
+  setlocale (LC_MESSAGES, "");
+  bindtextdomain (PACKAGE, LOCALEDIR);
+  textdomain (PACKAGE);
 
   program_name = argv[0];
+  xmalloc_set_program_name (program_name);
   string_min = -1;
   print_addresses = false;
   print_filenames = false;
   datasection_only = true;
+  target = NULL;
 
-  while ((optc = getopt_long (argc, argv, "afhn:ot:v0123456789",
+  while ((optc = getopt_long (argc, argv, "afn:ot:v0123456789",
                              long_options, (int *) 0)) != EOF)
     {
       switch (optc)
@@ -137,14 +148,14 @@ main (argc, argv)
          break;
 
        case 'h':
-         usage (stdout);
-         exit (0);
+         usage (stdout, 0);
 
        case 'n':
          string_min = integer_arg (optarg);
          if (string_min < 1)
            {
-             fprintf (stderr, "%s: invalid number %s\n", program_name, optarg);
+             fprintf (stderr, _("%s: invalid number %s\n"),
+                      program_name, optarg);
              exit (1);
            }
          break;
@@ -157,7 +168,7 @@ main (argc, argv)
        case 't':
          print_addresses = true;
          if (optarg[1] != '\0')
-           usage ();
+           usage (stderr, 1);
          switch (optarg[0])
            {
            case 'o':
@@ -173,16 +184,20 @@ main (argc, argv)
              break;
 
            default:
-             usage ();
+             usage (stderr, 1);
            }
          break;
 
+       case 'T':
+         target = optarg;
+         break;
+
        case 'v':
-         printf ("%s version %s\n", program_name, program_version);
-         exit (0);
+         print_version ("strings");
+         break;
 
        case '?':
-         usage (stderr);
+         usage (stderr, 1);
 
        default:
          if (string_min < 0)
@@ -197,41 +212,46 @@ main (argc, argv)
     string_min = 4;
 
   bfd_init ();
-  devnull = fopen ("/dev/null", "r");
-  if (devnull == NULL)
+  set_default_bfd_target ();
+
+  if (optind >= argc)
     {
-      fprintf (stderr, "%s: ", program_name);
-      perror ("/dev/null");
-      exit (1);
+      datasection_only = false;
+      print_strings ("{standard input}", stdin, 0, 0, 0, (char *) NULL);
+      files_given = true;
     }
-
-  for (; optind < argc; ++optind)
+  else
     {
-      if (!strcmp (argv[optind], "-"))
-       datasection_only = false;
-      else
+      for (; optind < argc; ++optind)
        {
-         files_given = true;
-         exit_status |= (strings_file (argv[optind]) == false);
+         if (strcmp (argv[optind], "-") == 0)
+           datasection_only = false;
+         else
+           {
+             files_given = true;
+             exit_status |= (strings_file (argv[optind]) == false);
+           }
        }
     }
 
   if (files_given == false)
-    usage (stderr);
+    usage (stderr, 1);
 
-  exit (exit_status);
+  return (exit_status);
 }
 \f
-/* Scan the sections of the file ABFD, whose printable name is FILE.
-   If any of them contain initialized data,
-   set `got_a_section' and print the strings in them.  */
+/* Scan section SECT of the file ABFD, whose printable name is FILE.
+   If it contains initialized data,
+   set `got_a_section' and print the strings in it.  */
 
 static void
-strings_a_section (abfd, sect, file)
+strings_a_section (abfd, sect, filearg)
      bfd *abfd;
      asection *sect;
-     PTR file;
+     PTR filearg;
 {
+  const char *file = (const char *) filearg;
+
   if ((sect->flags & DATA_FLAGS) == DATA_FLAGS)
     {
       bfd_size_type sz = bfd_get_section_size_before_reloc (sect);
@@ -239,45 +259,45 @@ strings_a_section (abfd, sect, file)
       if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sz))
        {
          got_a_section = true;
-         dump_strings (file, devnull, sect->filepos, 0, sz, mem);
+         print_strings (file, (FILE *) NULL, sect->filepos, 0, sz, mem);
        }
       free (mem);
     }
 }
 
-/* Print the strings in the initialized data section of FILE.
+/* Scan all of the sections in FILE, and print the strings
+   in the initialized data section(s).
+
    Return true if successful,
    false if not (such as if FILE is not an object file).  */
 
 static boolean
 strings_object_file (file)
-     char *file;
+     const char *file;
 {
-  bfd *abfd = bfd_openr (file, NULL);
+  bfd *abfd = bfd_openr (file, target);
 
   if (abfd == NULL)
     {
-      if (bfd_error != system_call_error)
-       {
-         /* Out of memory, or an invalid target is specified by the
-            GNUTARGET environment variable.  */
-         fprintf (stderr, "%s: ", program_name);
-         bfd_perror (file);
-       }
+      /* Treat the file as a non-object file.  */
       return false;
     }
 
-  /* For some reason, without this call, the BFD has no sections.
-     This call is only for the side effect of reading in the sections.  */
-  bfd_check_format (abfd, bfd_object);
+  /* This call is mainly for its side effect of reading in the sections.
+     We follow the traditional behavior of `strings' in that we don't
+     complain if we don't recognize a file to be an object file.  */
+  if (bfd_check_format (abfd, bfd_object) == false)
+    {
+      bfd_close (abfd);
+      return false;
+    }
 
   got_a_section = false;
-  bfd_map_over_sections (abfd, strings_a_section, file);
+  bfd_map_over_sections (abfd, strings_a_section, (PTR) file);
 
   if (!bfd_close (abfd))
     {
-      fprintf (stderr, "%s: ", program_name);
-      bfd_perror (file);
+      bfd_nonfatal (file);
       return false;
     }
 
@@ -298,7 +318,10 @@ strings_file (file)
     {
       FILE *stream;
 
-      stream = fopen (file, "r");
+      stream = fopen (file, "rb");
+      /* Not all systems permit "rb", so try "r" if it failed.  */
+      if (stream == NULL)
+       stream = fopen (file, "r");
       if (stream == NULL)
        {
          fprintf (stderr, "%s: ", program_name);
@@ -306,7 +329,7 @@ strings_file (file)
          return false;
        }
 
-      dump_strings (file, stream, (file_ptr) 0, 0, 0, (char *) 0);
+      print_strings (file, stream, (file_ptr) 0, 0, 0, (char *) 0);
 
       if (fclose (stream) == EOF)
        {
@@ -324,26 +347,27 @@ strings_file (file)
    is at address ADDRESS in the file.
    Stop reading at address STOP_POINT in the file, if nonzero.
 
-   Optionally the caller can supply a buffer of characters
+   If STREAM is NULL, do not read from it.
+   The caller can supply a buffer of characters
    to be processed before the data in STREAM.
    MAGIC is the address of the buffer and
    MAGICCOUNT is how many characters are in it.
    Those characters come at address ADDRESS and the data in STREAM follow.  */
 
 static void
-dump_strings (filename, stream, address, stop_point, magiccount, magic)
-     char *filename;
+print_strings (filename, stream, address, stop_point, magiccount, magic)
+     const char *filename;
      FILE *stream;
      file_ptr address;
      int stop_point;
      int magiccount;
      char *magic;
 {
-  int bufsize = 100;
-  char *buf = (char *) xmalloc (bufsize);
+  char *buf = (char *) xmalloc (string_min + 1);
 
   while (1)
     {
+      file_ptr start;
       int i;
       int c;
 
@@ -351,6 +375,7 @@ dump_strings (filename, stream, address, stop_point, magiccount, magic)
     tryline:
       if (stop_point && address >= stop_point)
        break;
+      start = address;
       for (i = 0; i < string_min; i++)
        {
          if (magiccount)
@@ -360,8 +385,10 @@ dump_strings (filename, stream, address, stop_point, magiccount, magic)
            }
          else
            {
+             if (stream == NULL)
+               return;
              c = getc (stream);
-             if (c < 0)
+             if (c == EOF)
                return;
            }
          address++;
@@ -371,76 +398,51 @@ dump_strings (filename, stream, address, stop_point, magiccount, magic)
          buf[i] = c;
        }
 
-      /* We found a run of `string_min' graphic characters.
-        Now see if it is terminated with a NUL byte or a newline.   */
-      while (1)
-       {
-         if (i == bufsize)
-           {
-             bufsize *= 2;
-             buf = (char *) xrealloc (buf, bufsize);
-           }
-         if (magiccount)
-           {
-             magiccount--;
-             c = *magic++;
-           }
-         else
-           {
-             c = getc (stream);
-             if (c < 0)
-               return;
-           }
-         address++;
-         if (c == '\0' || c == '\n')
-           break;              /* It is; print this string.  */
-         if (!isgraphic (c))
-           goto tryline;       /* It isn't; give up on this string.  */
-         buf[i++] = c;         /* The string continues; store it all.  */
-       }
+      /* We found a run of `string_min' graphic characters.  Print up
+         to the next non-graphic character.  */
 
-      /* If we get here, the string is all graphics and properly terminated,
-        so print it.  It is all in `buf' and `i' is its length.  */
-      buf[i] = '\0';
       if (print_filenames)
        printf ("%s: ", filename);
       if (print_addresses)
        switch (address_radix)
          {
          case 8:
-           printf ("%7lo ", address - i - 1);
+           printf ("%7lo ", (unsigned long) start);
            break;
 
          case 10:
-           printf ("%7ld ", address - i - 1);
+           printf ("%7ld ", (long) start);
            break;
 
          case 16:
-           printf ("%7lx ", address - i - 1);
+           printf ("%7lx ", (unsigned long) start);
            break;
          }
 
-      for (i = 0; (c = buf[i]) != '\0'; i++)
-       switch (c)
-         {
-         case '\n':
-           printf ("\\n");
-           break;
-         case '\t':
-           printf ("\\t");
-           break;
-         case '\f':
-           printf ("\\f");
-           break;
-         case '\b':
-           printf ("\\b");
-           break;
-         case '\r':
-           printf ("\\r");
+      buf[i] = '\0';
+      fputs (buf, stdout);
+
+      while (1)
+       {
+         if (magiccount)
+           {
+             magiccount--;
+             c = *magic++;
+           }
+         else
+           {
+             if (stream == NULL)
+               break;
+             c = getc (stream);
+             if (c == EOF)
+               break;
+           }
+         address++;
+         if (! isgraphic (c))
            break;
-         default:
-           putchar (c);
-         }
+         putchar (c);
+       }
+
       putchar ('\n');
     }
 }
@@ -487,20 +489,24 @@ integer_arg (s)
 
   if (*p)
     {
-      fprintf (stderr, "%s: invalid integer argument %s\n", program_name, s);
+      fprintf (stderr, _("%s: invalid integer argument %s\n"), program_name, s);
       exit (1);
     }
   return value;
 }
 
 static void
-usage (stream)
+usage (stream, status)
      FILE *stream;
+     int status;
 {
-  fprintf (stream, "\
-Usage: %s [-afhov] [-n min-len] [-min-len] [-t {o,x,d}] [-]\n\
+  fprintf (stream, _("\
+Usage: %s [-afov] [-n min-len] [-min-len] [-t {o,x,d}] [-]\n\
        [--all] [--print-file-name] [--bytes=min-len] [--radix={o,x,d}]\n\
-       [--help] [--version] file...\n",
+       [--target=bfdname] [--help] [--version] file...\n"),
           program_name);
-  exit (1);
+  list_supported_targets (program_name, stream);
+  if (status == 0)
+    fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
+  exit (status);
 }
This page took 0.040234 seconds and 4 git commands to generate.