This is the mail archive of the mailing list for the binutils project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

PATCH: strings: Add --output-separator to specify custom output record separator

I can rename to output-delimiter if people like that more.

As stated in the patch, because --include-all-whitespace allows '\n'
to appear inside strings, it is no longer sufficient to separate
output strings. With this, the user can specify any custom string.
From d332a0013b87568e1898cee9d9fe41a041fc0da6 Mon Sep 17 00:00:00 2001
From: Erik Ackermann <>
Date: Thu, 16 Jul 2015 21:15:15 -0700
Subject: [PATCH] Adds the ability in strings to specify an output separator to
 delimit output records. Because of the --include-all-whitespace option,
 strings may contain new-lines, so the default newline separator is

	* strings.c: Add -s/--output-separator option to specify custom separator string
	* NEWS: Mention the new feature.
	* doc/binutils.text (strings): Document the new command line option
 binutils/NEWS              |  2 ++
 binutils/doc/binutils.texi |  8 ++++++++
 binutils/strings.c         | 23 ++++++++++++++++++++---
 3 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/binutils/NEWS b/binutils/NEWS
index 051fe48..bebd727 100644
--- a/binutils/NEWS
+++ b/binutils/NEWS
@@ -6,6 +6,8 @@
 * Add --update-section option to objcopy.
+* Add --output-separator option to strings.
 Changes in 2.25:
 * Add --data option to strings to only print strings in loadable, initialized
diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi
index 466f125..744cda5 100644
--- a/binutils/doc/binutils.texi
+++ b/binutils/doc/binutils.texi
@@ -2785,6 +2785,7 @@ strings [@option{-afovV}] [@option{-}@var{min-len}]
         [@option{-}] [@option{--all}] [@option{--print-file-name}]
         [@option{-T} @var{bfdname}] [@option{--target=}@var{bfdname}]
         [@option{-w}] [@option{--include-all-whitespace}]
+        [@option{-s}] [@option{--output-separator}@var{sep_string}]
         [@option{--help}] [@option{--version}] @var{file}@dots{}
 @c man end
 @end smallexample
@@ -2889,6 +2890,13 @@ By default tab and space characters are included in the strings that
 are displayed, but other whitespace characters, such a newlines and
 carriage returns, are not.  The @option{-w} option changes this so
 that all whitespace characters are considered to be part of a string.
+@item -s
+@itemx --output-separator
+By default, output strings are delimited by a new-line. This option
+allows you to supply any string to be used as the output record
+separator. Useful with --include-all-whitespace where strings
+may contain new-lines internally.
 @end table
 @c man end
diff --git a/binutils/strings.c b/binutils/strings.c
index 0e2c9a8..3875131 100644
--- a/binutils/strings.c
+++ b/binutils/strings.c
@@ -55,6 +55,10 @@
    -T {bfdname}
 		Specify a non-default object file format.
+  --output-separator=sep_string
+  -s sep_string	String used to separate parsed strings in output.
+		Default is newline.
    -h		Print the usage message on the standard output.
@@ -114,6 +118,9 @@ static char *target;
 static char encoding;
 static int encoding_bytes;
+/* Output string used to separate parsed strings  */
+static char *output_separator;
 static struct option long_options[] =
   {"all", no_argument, NULL, 'a'},
@@ -124,6 +131,7 @@ static struct option long_options[] =
   {"include-all-whitespace", required_argument, NULL, 'w'},
   {"encoding", required_argument, NULL, 'e'},
   {"target", required_argument, NULL, 'T'},
+  {"output-separator", required_argument, NULL, 's'},
   {"help", no_argument, NULL, 'h'},
   {"version", no_argument, NULL, 'v'},
   {NULL, 0, NULL, 0}
@@ -140,7 +148,7 @@ typedef struct
 static void strings_a_section (bfd *, asection *, void *);
 static bfd_boolean strings_object_file (const char *);
-static bfd_boolean strings_file (char *);
+static bfd_boolean strings_file (char *file);
 static void print_strings (const char *, FILE *, file_ptr, int, int, char *);
 static void usage (FILE *, int);
 static long get_char (FILE *, file_ptr *, int *, char **);
@@ -178,8 +186,9 @@ main (int argc, char **argv)
     datasection_only = TRUE;
   target = NULL;
   encoding = 's';
+  output_separator = NULL;
-  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:Vv0123456789",
+  while ((optc = getopt_long (argc, argv, "adfhHn:wot:e:T:s:Vv0123456789",
 			      long_options, (int *) 0)) != EOF)
       switch (optc)
@@ -248,6 +257,10 @@ main (int argc, char **argv)
 	  encoding = optarg[0];
+	case 's':
+	  output_separator = optarg;
+          break;
 	case 'V':
 	case 'v':
 	  print_version ("strings");
@@ -650,7 +663,10 @@ print_strings (const char *filename, FILE *stream, file_ptr address,
 	  putchar (c);
-      putchar ('\n');
+      if (output_separator)
+        fputs(output_separator, stdout);
+      else
+        putchar ('\n');
   free (buf);
@@ -681,6 +697,7 @@ usage (FILE *stream, int status)
   -T --target=<BFDNAME>     Specify the binary file format\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\
+  -s --output-separator=<string> String used to separate strings in output.\n\
   @<file>                   Read options from <file>\n\
   -h --help                 Display this information\n\
   -v -V --version           Print the program's version number\n"));

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]