Fix v850 sanitization.
[deliverable/binutils-gdb.git] / binutils / strings.c
index f46335b0f67103713a649c6a0b00e5d15245d62e..02ef67ba37ac890bb5d1cdfdf41a74b4ae461825 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 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
@@ -37,6 +37,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.
 
@@ -50,7 +53,8 @@
 #include <getopt.h>
 #include <ctype.h>
 #include <errno.h>
-#include <bfd.h>
+#include "bfd.h"
+#include "bucomm.h"
 
 #ifdef isascii
 #define isgraphic(c) (isascii (c) && isprint (c))
@@ -83,10 +87,9 @@ 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;
+/* The BFD object file format.  */
+static char *target;
 
-extern char *program_name;
 extern char *program_version;
 
 static struct option long_options[] =
@@ -95,18 +98,18 @@ 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 boolean strings_file PARAMS ((char *file));
+static int integer_arg PARAMS ((char *s));
+static void print_strings PARAMS ((char *filename, FILE *stream,
+                                 file_ptr address, int stop_point,
+                                 int magiccount, char *magic));
+static void usage PARAMS ((FILE *stream, int status));
 \f
 void
 main (argc, argv)
@@ -122,8 +125,9 @@ main (argc, argv)
   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,8 +141,7 @@ main (argc, argv)
          break;
 
        case 'h':
-         usage (stdout);
-         exit (0);
+         usage (stdout, 0);
 
        case 'n':
          string_min = integer_arg (optarg);
@@ -158,7 +161,7 @@ main (argc, argv)
        case 't':
          print_addresses = true;
          if (optarg[1] != '\0')
-           usage ();
+           usage (stderr, 1);
          switch (optarg[0])
            {
            case 'o':
@@ -174,16 +177,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);
+         printf ("GNU %s version %s\n", program_name, program_version);
          exit (0);
 
        case '?':
-         usage (stderr);
+         usage (stderr, 1);
 
        default:
          if (string_min < 0)
@@ -198,13 +205,6 @@ main (argc, argv)
     string_min = 4;
 
   bfd_init ();
-  devnull = fopen ("/dev/null", "r");
-  if (devnull == NULL)
-    {
-      fprintf (stderr, "%s: ", program_name);
-      perror ("/dev/null");
-      exit (1);
-    }
 
   for (; optind < argc; ++optind)
     {
@@ -218,14 +218,14 @@ main (argc, argv)
     }
 
   if (files_given == false)
-    usage (stderr);
+    usage (stderr, 1);
 
   exit (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)
@@ -240,13 +240,15 @@ 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).  */
 
@@ -254,31 +256,29 @@ static boolean
 strings_object_file (file)
      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);
 
   if (!bfd_close (abfd))
     {
-      fprintf (stderr, "%s: ", program_name);
-      bfd_perror (file);
+      bfd_nonfatal (file);
       return false;
     }
 
@@ -307,7 +307,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)
        {
@@ -325,14 +325,15 @@ 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)
+print_strings (filename, stream, address, stop_point, magiccount, magic)
      char *filename;
      FILE *stream;
      file_ptr address;
@@ -361,6 +362,8 @@ dump_strings (filename, stream, address, stop_point, magiccount, magic)
            }
          else
            {
+             if (stream == NULL)
+               return;
              c = getc (stream);
              if (c < 0)
                return;
@@ -388,6 +391,8 @@ dump_strings (filename, stream, address, stop_point, magiccount, magic)
            }
          else
            {
+             if (stream == NULL)
+               return;
              c = getc (stream);
              if (c < 0)
                return;
@@ -409,15 +414,15 @@ dump_strings (filename, stream, address, stop_point, magiccount, magic)
        switch (address_radix)
          {
          case 8:
-           printf ("%7lo ", address - i - 1);
+           printf ("%7lo ", (unsigned long) (address - i - 1));
            break;
 
          case 10:
-           printf ("%7ld ", address - i - 1);
+           printf ("%7ld ", (long) (address - i - 1));
            break;
 
          case 16:
-           printf ("%7lx ", address - i - 1);
+           printf ("%7lx ", (unsigned long) (address - i - 1));
            break;
          }
 
@@ -495,13 +500,14 @@ integer_arg (s)
 }
 
 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\
+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);
+  exit (status);
 }
This page took 0.027493 seconds and 4 git commands to generate.