* readelf.c (get_segment_type): Display "GNU_STACK", not just
[deliverable/binutils-gdb.git] / binutils / strings.c
index f1222e0c3f32f9b684845994fe102d9f51c8f14d..8a27fa800e4b1f3b3d1e89e2f86fb5baed1f8fb3 100644 (file)
@@ -1,6 +1,6 @@
 /* strings -- print the strings of printable characters in files
    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
-   2002 Free Software Foundation, Inc.
+   2002, 2003, 2004 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
    -o          Like -to.  (Some other implementations have -o like -to,
                others like -td.  We chose one arbitrarily.)
 
-   --encoding={s,b,l,B,L}
-   -e {s,b,l,B,L}
-               Select character encoding: single-byte, bigendian 16-bit,
-               littleendian 16-bit, bigendian 32-bit, littleendian 32-bit
+   --encoding={s,S,b,l,B,L}
+   -e {s,S,b,l,B,L}
+               Select character encoding: 7-bit-character, 8-bit-character,
+               bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
+               littleendian 32-bit.
 
    --target=BFDNAME
                Specify a non-default object file format.
 #endif
 #if O_BINARY
 #include <io.h>
-#define SET_BINARY(f) do { if (!isatty(f)) setmode(f,O_BINARY); } while (0)
+#define SET_BINARY(f) do { if (!isatty (f)) setmode (f,O_BINARY); } while (0)
 #endif
 #endif
 
-#define isgraphic(c) (ISPRINT (c) || (c) == '\t')
+#define STRING_ISGRAPHIC(c) \
+      (   (c) >= 0 \
+       && (c) <= 255 \
+       && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127)))
 
 #ifndef errno
 extern int errno;
@@ -95,10 +99,10 @@ extern int errno;
 
 #ifdef HAVE_FOPEN64
 typedef off64_t file_off;
-#define file_open(s,m) fopen64(s,m)
+#define file_open(s,m) fopen64(s, m)
 #else
 typedef off_t file_off;
-#define file_open(s,m) fopen(s,m)
+#define file_open(s,m) fopen(s, m)
 #endif
 
 /* Radix for printing addresses (must be 8, 10 or 16).  */
@@ -139,29 +143,18 @@ static struct option long_options[] =
   {NULL, 0, NULL, 0}
 };
 
-static void strings_a_section
-  PARAMS ((bfd *, asection *, PTR));
-static bfd_boolean strings_object_file
-  PARAMS ((const char *));
-static bfd_boolean strings_file
-  PARAMS ((char *file));
-static int integer_arg
-  PARAMS ((char *s));
-static void print_strings
-  PARAMS ((const char *filename, FILE *stream, file_off address,
-          int stop_point, int magiccount, char *magic));
-static void usage
-  PARAMS ((FILE *stream, int status));
-static long get_char
-  PARAMS ((FILE *stream, file_off *address, int *magiccount, char **magic));
+static void strings_a_section (bfd *, asection *, void *);
+static bfd_boolean strings_object_file (const char *);
+static bfd_boolean strings_file (char *file);
+static int integer_arg (char *s);
+static void print_strings (const char *, FILE *, file_off, int, int, char *);
+static void usage (FILE *, int);
+static long get_char (FILE *, file_off *, int *, char **);
 \f
-int main
-  PARAMS ((int, char **));
+int main (int, char **);
 
 int
-main (argc, argv)
-     int argc;
-     char **argv;
+main (int argc, char **argv)
 {
   int optc;
   int exit_status = 0;
@@ -202,9 +195,7 @@ main (argc, argv)
        case 'n':
          string_min = integer_arg (optarg);
          if (string_min < 1)
-           {
-             fatal (_("invalid number %s"), optarg);
-           }
+           fatal (_("invalid number %s"), optarg);
          break;
 
        case 'o':
@@ -267,6 +258,7 @@ main (argc, argv)
 
   switch (encoding)
     {
+    case 'S':
     case 's':
       encoding_bytes = 1;
       break;
@@ -319,17 +311,15 @@ main (argc, argv)
    set `got_a_section' and print the strings in it.  */
 
 static void
-strings_a_section (abfd, sect, filearg)
-     bfd *abfd;
-     asection *sect;
-     PTR filearg;
+strings_a_section (bfd *abfd, asection *sect, void *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);
-      PTR mem = xmalloc (sz);
+      bfd_size_type sz = bfd_get_section_size (sect);
+      void *mem = xmalloc (sz);
+
       if (bfd_get_section_contents (abfd, sect, mem, (file_ptr) 0, sz))
        {
          got_a_section = TRUE;
@@ -346,16 +336,13 @@ strings_a_section (abfd, sect, filearg)
    FALSE if not (such as if FILE is not an object file).  */
 
 static bfd_boolean
-strings_object_file (file)
-     const char *file;
+strings_object_file (const char *file)
 {
   bfd *abfd = bfd_openr (file, target);
 
   if (abfd == NULL)
-    {
-      /* Treat the file as a non-object file.  */
-      return FALSE;
-    }
+    /* Treat the file as a non-object file.  */
+    return FALSE;
 
   /* 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
@@ -367,7 +354,7 @@ strings_object_file (file)
     }
 
   got_a_section = FALSE;
-  bfd_map_over_sections (abfd, strings_a_section, (PTR) file);
+  bfd_map_over_sections (abfd, strings_a_section, (void *) file);
 
   if (!bfd_close (abfd))
     {
@@ -381,9 +368,11 @@ strings_object_file (file)
 /* Print the strings in FILE.  Return TRUE if ok, FALSE if an error occurs.  */
 
 static bfd_boolean
-strings_file (file)
-     char *file;
+strings_file (char *file)
 {
+  if (get_file_size (file) < 1)
+    return FALSE;
+
   /* If we weren't told to scan the whole file,
      try to open it as an object file and only look at
      initialized data sections.  If that fails, fall back to the
@@ -424,11 +413,7 @@ strings_file (file)
    MAGICCOUNT is how many characters are in it.  */
 
 static long
-get_char (stream, address, magiccount, magic)
-     FILE *stream;
-     file_off *address;
-     int *magiccount;
-     char **magic;
+get_char (FILE *stream, file_off *address, int *magiccount, char **magic)
 {
   int c, i;
   long r = EOF;
@@ -460,6 +445,7 @@ get_char (stream, address, magiccount, magic)
 
   switch (encoding)
     {
+    case 'S':
     case 's':
       r = buf[0];
       break;
@@ -498,13 +484,8 @@ get_char (stream, address, magiccount, magic)
    Those characters come at address ADDRESS and the data in STREAM follow.  */
 
 static void
-print_strings (filename, stream, address, stop_point, magiccount, magic)
-     const char *filename;
-     FILE *stream;
-     file_off address;
-     int stop_point;
-     int magiccount;
-     char *magic;
+print_strings (const char *filename, FILE *stream, file_off address,
+              int stop_point, int magiccount, char *magic)
 {
   char *buf = (char *) xmalloc (sizeof (char) * (string_min + 1));
 
@@ -524,14 +505,14 @@ print_strings (filename, stream, address, stop_point, magiccount, magic)
          c = get_char (stream, &address, &magiccount, &magic);
          if (c == EOF)
            return;
-         if (c > 255 || c < 0 || !isgraphic (c))
+         if (! STRING_ISGRAPHIC (c))
            /* Found a non-graphic.  Try again starting with next char.  */
            goto tryline;
          buf[i] = c;
        }
 
       /* We found a run of `string_min' graphic characters.  Print up
-         to the next non-graphic character.  */
+        to the next non-graphic character.  */
 
       if (print_filenames)
        printf ("%s: ", filename);
@@ -576,7 +557,8 @@ print_strings (filename, stream, address, stop_point, magiccount, magic)
 #else
 # if !BFD_HOST_64BIT_LONG
            if (start != (unsigned long) start)
-             printf ("%lx%8.8lx ", start >> 32, start & 0xffffffff);
+             printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
+                     (unsigned long) (start & 0xffffffff));
            else
 # endif
 #endif
@@ -592,7 +574,7 @@ print_strings (filename, stream, address, stop_point, magiccount, magic)
          c = get_char (stream, &address, &magiccount, &magic);
          if (c == EOF)
            break;
-         if (c > 255 || c < 0 || !isgraphic (c))
+         if (! STRING_ISGRAPHIC (c))
            break;
          putchar (c);
        }
@@ -605,8 +587,7 @@ print_strings (filename, stream, address, stop_point, magiccount, magic)
    but allowing octal and hex numbers as in C.  */
 
 static int
-integer_arg (s)
-     char *s;
+integer_arg (char *s)
 {
   int value;
   int radix = 10;
@@ -642,16 +623,13 @@ integer_arg (s)
     p--;
 
   if (*p)
-    {
-      fatal (_("invalid integer argument %s"), s);
-    }
+    fatal (_("invalid integer argument %s"), s);
+
   return value;
 }
 
 static void
-usage (stream, status)
-     FILE *stream;
-     int status;
+usage (FILE *stream, int status)
 {
   fprintf (stream, _("Usage: %s [option(s)] [file(s)]\n"), program_name);
   fprintf (stream, _(" Display printable strings in [file(s)] (stdin by default)\n"));
@@ -663,8 +641,8 @@ usage (stream, status)
   -t --radix={o,x,d}        Print the location of the string in base 8, 10 or 16\n\
   -o                        An alias for --radix=o\n\
   -T --target=<BFDNAME>     Specify the binary file format\n\
-  -e --encoding={s,b,l,B,L} Select character size and endianness:\n\
-                            s = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
+  -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
+                            s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
   -h --help                 Display this information\n\
   -v --version              Print the program's version number\n"));
   list_supported_targets (program_name, stream);
This page took 0.0267 seconds and 4 git commands to generate.