1 /* strings -- print the strings of printable characters in files
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 /* Usage: strings [options] file...
25 - Do not scan only the initialized data section of object files.
28 -f Print the name of the file before each string.
32 -min-len Print graphic char sequences, MIN-LEN or more bytes long,
33 that are followed by a NUL or a newline. Default is 4.
36 -t {o,x,d} Print the offset within the file before each string,
39 -o Like -to. (Some other implementations have -o like -to,
40 others like -td. We chose one arbitrarily.)
42 --encoding={s,b,l,B,L}
44 Select character encoding: single-byte, bigendian 16-bit,
45 littleendian 16-bit, bigendian 32-bit, littleendian 32-bit
48 Specify a non-default object file format.
51 -h Print the usage message on the standard output.
54 -v Print the program version number.
56 Written by Richard Stallman <rms@gnu.ai.mit.edu>
57 and David MacKenzie <djm@gnu.ai.mit.edu>. */
65 #include "libiberty.h"
67 /* Some platforms need to put stdin into binary mode, to read
72 #define O_BINARY _O_BINARY
73 #define setmode _setmode
80 #define SET_BINARY(f) do { if (!isatty(f)) setmode(f,O_BINARY); } while (0)
84 /* Not all printable characters have ASCII codes (depending upon the
85 LOCALE set) but on some older systems it is not safe to test isprint
86 without first testing isascii... */
87 #if defined isascii && !defined HAVE_LOCALE_H
88 #define isgraphic(c) (isascii (c) && (isprint (c) || (c) == '\t'))
90 #define isgraphic(c) (isprint (c) || (c) == '\t')
97 /* The BFD section flags that identify an initialized data section. */
98 #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
100 /* Radix for printing addresses (must be 8, 10 or 16). */
101 static int address_radix
;
103 /* Minimum length of sequence of graphic chars to trigger output. */
104 static int string_min
;
106 /* true means print address within file for each string. */
107 static boolean print_addresses
;
109 /* true means print filename for each string. */
110 static boolean print_filenames
;
112 /* true means for object files scan only the data section. */
113 static boolean datasection_only
;
115 /* true if we found an initialized data section in the current file. */
116 static boolean got_a_section
;
118 /* The BFD object file format. */
121 /* The character encoding format. */
122 static char encoding
;
123 static int encoding_bytes
;
125 static struct option long_options
[] =
127 {"all", no_argument
, NULL
, 'a'},
128 {"print-file-name", no_argument
, NULL
, 'f'},
129 {"bytes", required_argument
, NULL
, 'n'},
130 {"radix", required_argument
, NULL
, 't'},
131 {"encoding", required_argument
, NULL
, 'e'},
132 {"target", required_argument
, NULL
, 'T'},
133 {"help", no_argument
, NULL
, 'h'},
134 {"version", no_argument
, NULL
, 'v'},
138 static void strings_a_section
PARAMS ((bfd
*, asection
*, PTR
));
139 static boolean strings_object_file
PARAMS ((const char *));
140 static boolean strings_file
PARAMS ((char *file
));
141 static int integer_arg
PARAMS ((char *s
));
142 static void print_strings
PARAMS ((const char *filename
, FILE *stream
,
143 file_ptr address
, int stop_point
,
144 int magiccount
, char *magic
));
145 static void usage
PARAMS ((FILE *stream
, int status
));
154 boolean files_given
= false;
156 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
157 setlocale (LC_ALL
, "");
159 bindtextdomain (PACKAGE
, LOCALEDIR
);
160 textdomain (PACKAGE
);
162 program_name
= argv
[0];
163 xmalloc_set_program_name (program_name
);
165 print_addresses
= false;
166 print_filenames
= false;
167 datasection_only
= true;
171 while ((optc
= getopt_long (argc
, argv
, "afn:ot:e:v0123456789",
172 long_options
, (int *) 0)) != EOF
)
177 datasection_only
= false;
181 print_filenames
= true;
188 string_min
= integer_arg (optarg
);
191 fatal (_("invalid number %s"), optarg
);
196 print_addresses
= true;
201 print_addresses
= true;
202 if (optarg
[1] != '\0')
228 if (optarg
[1] != '\0')
230 encoding
= optarg
[0];
234 print_version ("strings");
242 string_min
= optc
- '0';
244 string_min
= string_min
* 10 + optc
- '0';
270 set_default_bfd_target ();
274 datasection_only
= false;
276 SET_BINARY (fileno (stdin
));
278 print_strings ("{standard input}", stdin
, 0, 0, 0, (char *) NULL
);
283 for (; optind
< argc
; ++optind
)
285 if (strcmp (argv
[optind
], "-") == 0)
286 datasection_only
= false;
290 exit_status
|= (strings_file (argv
[optind
]) == false);
295 if (files_given
== false)
298 return (exit_status
);
301 /* Scan section SECT of the file ABFD, whose printable name is FILE.
302 If it contains initialized data,
303 set `got_a_section' and print the strings in it. */
306 strings_a_section (abfd
, sect
, filearg
)
311 const char *file
= (const char *) filearg
;
313 if ((sect
->flags
& DATA_FLAGS
) == DATA_FLAGS
)
315 bfd_size_type sz
= bfd_get_section_size_before_reloc (sect
);
316 PTR mem
= xmalloc (sz
);
317 if (bfd_get_section_contents (abfd
, sect
, mem
, (file_ptr
) 0, sz
))
319 got_a_section
= true;
320 print_strings (file
, (FILE *) NULL
, sect
->filepos
, 0, sz
, mem
);
326 /* Scan all of the sections in FILE, and print the strings
327 in the initialized data section(s).
329 Return true if successful,
330 false if not (such as if FILE is not an object file). */
333 strings_object_file (file
)
336 bfd
*abfd
= bfd_openr (file
, target
);
340 /* Treat the file as a non-object file. */
344 /* This call is mainly for its side effect of reading in the sections.
345 We follow the traditional behavior of `strings' in that we don't
346 complain if we don't recognize a file to be an object file. */
347 if (bfd_check_format (abfd
, bfd_object
) == false)
353 got_a_section
= false;
354 bfd_map_over_sections (abfd
, strings_a_section
, (PTR
) file
);
356 if (!bfd_close (abfd
))
362 return got_a_section
;
365 /* Print the strings in FILE. Return true if ok, false if an error occurs. */
371 /* If we weren't told to scan the whole file,
372 try to open it as an object file and only look at
373 initialized data sections. If that fails, fall back to the
375 if (!datasection_only
|| !strings_object_file (file
))
379 stream
= fopen (file
, "rb");
380 /* Not all systems permit "rb", so try "r" if it failed. */
382 stream
= fopen (file
, "r");
385 fprintf (stderr
, "%s: ", program_name
);
390 print_strings (file
, stream
, (file_ptr
) 0, 0, 0, (char *) 0);
392 if (fclose (stream
) == EOF
)
394 fprintf (stderr
, "%s: ", program_name
);
403 /* Read the next character, return EOF if none available.
404 Assume that STREAM is positioned so that the next byte read
405 is at address ADDRESS in the file.
407 If STREAM is NULL, do not read from it.
408 The caller can supply a buffer of characters
409 to be processed before the data in STREAM.
410 MAGIC is the address of the buffer and
411 MAGICCOUNT is how many characters are in it. */
414 get_char (stream
, address
, magiccount
, magic
)
422 unsigned char buf
[4];
424 for (i
= 0; i
< encoding_bytes
; i
++)
450 r
= (buf
[0] << 8) | buf
[1];
453 r
= buf
[0] | (buf
[1] << 8);
456 r
= ((long) buf
[0] << 24) | ((long) buf
[1] << 16) |
457 ((long) buf
[2] << 8) | buf
[3];
460 r
= buf
[0] | ((long) buf
[1] << 8) | ((long) buf
[2] << 16) |
461 ((long) buf
[3] << 24);
471 /* Find the strings in file FILENAME, read from STREAM.
472 Assume that STREAM is positioned so that the next byte read
473 is at address ADDRESS in the file.
474 Stop reading at address STOP_POINT in the file, if nonzero.
476 If STREAM is NULL, do not read from it.
477 The caller can supply a buffer of characters
478 to be processed before the data in STREAM.
479 MAGIC is the address of the buffer and
480 MAGICCOUNT is how many characters are in it.
481 Those characters come at address ADDRESS and the data in STREAM follow. */
484 print_strings (filename
, stream
, address
, stop_point
, magiccount
, magic
)
485 const char *filename
;
492 char *buf
= (char *) xmalloc (sizeof (char) * (string_min
+ 1));
500 /* See if the next `string_min' chars are all graphic chars. */
502 if (stop_point
&& address
>= stop_point
)
505 for (i
= 0; i
< string_min
; i
++)
507 c
= get_char (stream
, &address
, &magiccount
, &magic
);
510 if (c
> 255 || c
< 0 || !isgraphic (c
))
511 /* Found a non-graphic. Try again starting with next char. */
516 /* We found a run of `string_min' graphic characters. Print up
517 to the next non-graphic character. */
520 printf ("%s: ", filename
);
522 switch (address_radix
)
525 printf ("%7lo ", (unsigned long) start
);
529 printf ("%7ld ", (long) start
);
533 printf ("%7lx ", (unsigned long) start
);
542 c
= get_char (stream
, &address
, &magiccount
, &magic
);
545 if (c
> 255 || c
< 0 || !isgraphic (c
))
554 /* Parse string S as an integer, using decimal radix by default,
555 but allowing octal and hex numbers as in C. */
568 else if (*++p
== 'x')
577 while (((c
= *p
++) >= '0' && c
<= '9')
578 || (radix
== 16 && (c
& ~40) >= 'A' && (c
& ~40) <= 'Z'))
581 if (c
>= '0' && c
<= '9')
584 value
+= (c
& ~40) - 'A';
596 fatal (_("invalid integer argument %s"), s
);
602 usage (stream
, status
)
606 fprintf (stream
, _("\
607 Usage: %s [-afov] [-n min-len] [-min-len] [-t {o,x,d}] [-e {s,b,l,B,L}]\n\
608 [-] [--all] [--print-file-name] [--bytes=min-len] [--radix={o,x,d}]\n\
609 [--target=bfdname] [--encoding {s,b,l,B,L}] [--help] [--version] file...\n"),
611 list_supported_targets (program_name
, stream
);
613 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);