revert previous delta
[deliverable/binutils-gdb.git] / binutils / strings.c
index 7655ab6152b18af557b70f62666839e48aaa8570..44a8e1da644d1ac2e4d5c7606b084d7c8b3e600a 100644 (file)
@@ -1,5 +1,5 @@
 /* strings -- print the strings of printable characters in files
-   Copyright (C) 1993-2018 Free Software Foundation, Inc.
+   Copyright (C) 1993-2021 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
@@ -97,16 +97,16 @@ static int address_radix;
 static int string_min;
 
 /* Whether or not we include all whitespace as a graphic char.   */
-static bfd_boolean include_all_whitespace;
+static bool include_all_whitespace;
 
 /* TRUE means print address within file for each string.  */
-static bfd_boolean print_addresses;
+static bool print_addresses;
 
 /* TRUE means print filename for each string.  */
-static bfd_boolean print_filenames;
+static bool print_filenames;
 
 /* TRUE means for object files scan only the data section.  */
-static bfd_boolean datasection_only;
+static bool datasection_only;
 
 /* The BFD object file format.  */
 static char *target;
@@ -134,7 +134,7 @@ static struct option long_options[] =
   {NULL, 0, NULL, 0}
 };
 
-static bfd_boolean strings_file (char *);
+static bool strings_file (char *);
 static void print_strings (const char *, FILE *, file_ptr, int, int, char *);
 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
 \f
@@ -145,13 +145,11 @@ main (int argc, char **argv)
 {
   int optc;
   int exit_status = 0;
-  bfd_boolean files_given = FALSE;
+  bool files_given = false;
   char *s;
   int numeric_opt = 0;
 
-#if defined (HAVE_SETLOCALE)
   setlocale (LC_ALL, "");
-#endif
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
@@ -162,13 +160,13 @@ main (int argc, char **argv)
   expandargv (&argc, &argv);
 
   string_min = 4;
-  include_all_whitespace = FALSE;
-  print_addresses = FALSE;
-  print_filenames = FALSE;
+  include_all_whitespace = false;
+  print_addresses = false;
+  print_filenames = false;
   if (DEFAULT_STRINGS_ALL)
-    datasection_only = FALSE;
+    datasection_only = false;
   else
-    datasection_only = TRUE;
+    datasection_only = true;
   target = NULL;
   encoding = 's';
   output_separator = NULL;
@@ -179,15 +177,15 @@ main (int argc, char **argv)
       switch (optc)
        {
        case 'a':
-         datasection_only = FALSE;
+         datasection_only = false;
          break;
 
        case 'd':
-         datasection_only = TRUE;
+         datasection_only = true;
          break;
 
        case 'f':
-         print_filenames = TRUE;
+         print_filenames = true;
          break;
 
        case 'H':
@@ -201,16 +199,16 @@ main (int argc, char **argv)
          break;
 
        case 'w':
-         include_all_whitespace = TRUE;
+         include_all_whitespace = true;
          break;
 
        case 'o':
-         print_addresses = TRUE;
+         print_addresses = true;
          address_radix = 8;
          break;
 
        case 't':
-         print_addresses = TRUE;
+         print_addresses = true;
          if (optarg[1] != '\0')
            usage (stderr, 1);
          switch (optarg[0])
@@ -287,25 +285,26 @@ main (int argc, char **argv)
       usage (stderr, 1);
     }
 
-  bfd_init ();
+  if (bfd_init () != BFD_INIT_MAGIC)
+    fatal (_("fatal error: libbfd ABI mismatch"));
   set_default_bfd_target ();
 
   if (optind >= argc)
     {
-      datasection_only = FALSE;
+      datasection_only = false;
       SET_BINARY (fileno (stdin));
       print_strings ("{standard input}", stdin, 0, 0, 0, (char *) NULL);
-      files_given = TRUE;
+      files_given = true;
     }
   else
     {
       for (; optind < argc; ++optind)
        {
          if (strcmp (argv[optind], "-") == 0)
-           datasection_only = FALSE;
+           datasection_only = false;
          else
            {
-             files_given = TRUE;
+             files_given = true;
              exit_status |= !strings_file (argv[optind]);
            }
        }
@@ -323,7 +322,7 @@ main (int argc, char **argv)
 
 static void
 strings_a_section (bfd *abfd, asection *sect, const char *filename,
-                  bfd_boolean *got_a_section)
+                  bool *got_a_section)
 {
   bfd_size_type sectsize;
   bfd_byte *mem;
@@ -331,7 +330,7 @@ strings_a_section (bfd *abfd, asection *sect, const char *filename,
   if ((sect->flags & DATA_FLAGS) != DATA_FLAGS)
     return;
 
-  sectsize = bfd_get_section_size (sect);
+  sectsize = bfd_section_size (sect);
   if (sectsize == 0)
     return;
 
@@ -342,7 +341,7 @@ strings_a_section (bfd *abfd, asection *sect, const char *filename,
       return;
     }
 
-  *got_a_section = TRUE;
+  *got_a_section = true;
   print_strings (filename, NULL, sect->filepos, 0, sectsize, (char *) mem);
   free (mem);
 }
@@ -353,18 +352,18 @@ strings_a_section (bfd *abfd, asection *sect, const char *filename,
    Return TRUE if successful,
    FALSE if not (such as if FILE is not an object file).  */
 
-static bfd_boolean
+static bool
 strings_object_file (const char *file)
 {
   bfd *abfd;
   asection *s;
-  bfd_boolean got_a_section;
+  bool got_a_section;
 
   abfd = bfd_openr (file, target);
 
   if (abfd == NULL)
     /* Treat the file as a non-object file.  */
-    return FALSE;
+    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
@@ -372,17 +371,17 @@ strings_object_file (const char *file)
   if (!bfd_check_format (abfd, bfd_object))
     {
       bfd_close (abfd);
-      return FALSE;
+      return false;
     }
 
-  got_a_section = FALSE;
+  got_a_section = false;
   for (s = abfd->sections; s != NULL; s = s->next)
     strings_a_section (abfd, s, file, &got_a_section);
 
   if (!bfd_close (abfd))
     {
       bfd_nonfatal (file);
-      return FALSE;
+      return false;
     }
 
   return got_a_section;
@@ -390,7 +389,7 @@ strings_object_file (const char *file)
 
 /* Print the strings in FILE.  Return TRUE if ok, FALSE if an error occurs.  */
 
-static bfd_boolean
+static bool
 strings_file (char *file)
 {
   struct stat st;
@@ -404,12 +403,12 @@ strings_file (char *file)
       else
        non_fatal (_("Warning: could not locate '%s'.  reason: %s"),
                   file, strerror (errno));
-      return FALSE;
+      return false;
     }
   else if (S_ISDIR (st.st_mode))
     {
       non_fatal (_("Warning: '%s' is a directory"), file);
-      return FALSE;
+      return false;
     }
 
   /* If we weren't told to scan the whole file,
@@ -425,7 +424,7 @@ strings_file (char *file)
        {
          fprintf (stderr, "%s: ", program_name);
          perror (file);
-         return FALSE;
+         return false;
        }
 
       print_strings (file, stream, (file_ptr) 0, 0, 0, (char *) 0);
@@ -434,11 +433,11 @@ strings_file (char *file)
        {
          fprintf (stderr, "%s: ", program_name);
          perror (file);
-         return FALSE;
+         return false;
        }
     }
 
-  return TRUE;
+  return true;
 }
 \f
 /* Read the next character, return EOF if none available.
@@ -501,6 +500,57 @@ get_char (FILE *stream, file_ptr *address, int *magiccount, char **magic)
 
   return r;
 }
+
+/* Throw away one byte of a (possibly) multi-byte char C, updating
+   address and buffer to suit.  */
+
+static void
+unget_part_char (long c, file_ptr *address, int *magiccount, char **magic)
+{
+  static char tmp[4];
+
+  if (encoding_bytes > 1)
+    {
+      *address -= encoding_bytes - 1;
+
+      if (*magiccount == 0)
+       {
+         /* If no magic buffer exists, use temp buffer.  */
+         switch (encoding)
+           {
+           default:
+             break;
+           case 'b':
+             tmp[0] = c & 0xff;
+             *magiccount = 1;
+             break;
+           case 'l':
+             tmp[0] = (c >> 8) & 0xff;
+             *magiccount = 1;
+             break;
+           case 'B':
+             tmp[0] = (c >> 16) & 0xff;
+             tmp[1] = (c >> 8) & 0xff;
+             tmp[2] = c & 0xff;
+             *magiccount = 3;
+             break;
+           case 'L':
+             tmp[0] = (c >> 8) & 0xff;
+             tmp[1] = (c >> 16) & 0xff;
+             tmp[2] = (c >> 24) & 0xff;
+             *magiccount = 3;
+             break;
+           }
+         *magic = tmp;
+       }
+      else
+       {
+         /* If magic buffer exists, rewind.  */
+         *magic -= encoding_bytes - 1;
+         *magiccount += encoding_bytes - 1;
+       }
+    }
+}
 \f
 /* Find the strings in file FILENAME, read from STREAM.
    Assume that STREAM is positioned so that the next byte read
@@ -539,9 +589,13 @@ print_strings (const char *filename, FILE *stream, file_ptr address,
              free (buf);
              return;
            }
+
          if (! STRING_ISGRAPHIC (c))
-           /* Found a non-graphic.  Try again starting with next char.  */
-           goto tryline;
+           {
+             /* Found a non-graphic.  Try again starting with next byte.  */
+             unget_part_char (c, &address, &magiccount, &magic);
+             goto tryline;
+           }
          buf[i] = c;
        }
 
@@ -554,60 +608,41 @@ print_strings (const char *filename, FILE *stream, file_ptr address,
        switch (address_radix)
          {
          case 8:
-#ifdef HAVE_LONG_LONG
            if (sizeof (start) > sizeof (long))
              {
-# ifndef __MSVCRT__
-               printf ("%7llo ", (unsigned long long) start);
-# else
-               printf ("%7I64o ", (unsigned long long) start);
-# endif
+#ifndef __MSVCRT__
+               printf ("%7llo ", (unsigned long long) start);
+#else
+               printf ("%7I64o ", (unsigned long long) start);
+#endif
              }
            else
-#elif !BFD_HOST_64BIT_LONG
-           if (start != (unsigned long) start)
-             printf ("++%7lo ", (unsigned long) start);
-           else
-#endif
              printf ("%7lo ", (unsigned long) start);
            break;
 
          case 10:
-#ifdef HAVE_LONG_LONG
            if (sizeof (start) > sizeof (long))
              {
-# ifndef __MSVCRT__
-               printf ("%7lld ", (unsigned long long) start);
-# else
-               printf ("%7I64d ", (unsigned long long) start);
-# endif
+#ifndef __MSVCRT__
+               printf ("%7llu ", (unsigned long long) start);
+#else
+               printf ("%7I64d ", (unsigned long long) start);
+#endif
              }
            else
-#elif !BFD_HOST_64BIT_LONG
-           if (start != (unsigned long) start)
-             printf ("++%7ld ", (unsigned long) start);
-           else
-#endif
              printf ("%7ld ", (long) start);
            break;
 
          case 16:
-#ifdef HAVE_LONG_LONG
            if (sizeof (start) > sizeof (long))
              {
-# ifndef __MSVCRT__
-               printf ("%7llx ", (unsigned long long) start);
-# else
-               printf ("%7I64x ", (unsigned long long) start);
-# endif
+#ifndef __MSVCRT__
+               printf ("%7llx ", (unsigned long long) start);
+#else
+               printf ("%7I64x ", (unsigned long long) start);
+#endif
              }
            else
-#elif !BFD_HOST_64BIT_LONG
-           if (start != (unsigned long) start)
-             printf ("%lx%8.8lx ", (unsigned long) (start >> 32),
-                     (unsigned long) (start & 0xffffffff));
-           else
-#endif
              printf ("%7lx ", (unsigned long) start);
            break;
          }
@@ -621,14 +656,17 @@ print_strings (const char *filename, FILE *stream, file_ptr address,
          if (c == EOF)
            break;
          if (! STRING_ISGRAPHIC (c))
-           break;
+           {
+             unget_part_char (c, &address, &magiccount, &magic);
+             break;
+           }
          putchar (c);
        }
 
       if (output_separator)
-        fputs (output_separator, stdout);
+       fputs (output_separator, stdout);
       else
-        putchar ('\n');
+       putchar ('\n');
     }
   free (buf);
 }
This page took 0.030406 seconds and 4 git commands to generate.