1 /* strings -- print the strings of printable characters in files
2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
3 2002, 2003, 2004 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., 51 Franklin Street - Fifth Floor, 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,S,b,l,B,L}
44 Select character encoding: 7-bit-character, 8-bit-character,
45 bigendian 16-bit, littleendian 16-bit, bigendian 32-bit,
49 Specify a non-default object file format.
52 -h Print the usage message on the standard output.
55 -v Print the program version number.
57 Written by Richard Stallman <rms@gnu.ai.mit.edu>
58 and David MacKenzie <djm@gnu.ai.mit.edu>. */
68 #include "libiberty.h"
69 #include "safe-ctype.h"
72 /* Some platforms need to put stdin into binary mode, to read
77 #define O_BINARY _O_BINARY
78 #define setmode _setmode
85 #define SET_BINARY(f) do { if (!isatty (f)) setmode (f,O_BINARY); } while (0)
89 #define STRING_ISGRAPHIC(c) \
92 && ((c) == '\t' || ISPRINT (c) || (encoding == 'S' && (c) > 127)))
98 /* The BFD section flags that identify an initialized data section. */
99 #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS)
102 typedef off64_t file_off
;
103 #define file_open(s,m) fopen64(s, m)
105 typedef off_t file_off
;
106 #define file_open(s,m) fopen(s, m)
109 typedef struct stat64 statbuf
;
110 #define file_stat(f,s) stat64(f, s)
112 typedef struct stat statbuf
;
113 #define file_stat(f,s) stat(f, s)
116 /* Radix for printing addresses (must be 8, 10 or 16). */
117 static int address_radix
;
119 /* Minimum length of sequence of graphic chars to trigger output. */
120 static int string_min
;
122 /* TRUE means print address within file for each string. */
123 static bfd_boolean print_addresses
;
125 /* TRUE means print filename for each string. */
126 static bfd_boolean print_filenames
;
128 /* TRUE means for object files scan only the data section. */
129 static bfd_boolean datasection_only
;
131 /* TRUE if we found an initialized data section in the current file. */
132 static bfd_boolean got_a_section
;
134 /* The BFD object file format. */
137 /* The character encoding format. */
138 static char encoding
;
139 static int encoding_bytes
;
141 static struct option long_options
[] =
143 {"all", no_argument
, NULL
, 'a'},
144 {"print-file-name", no_argument
, NULL
, 'f'},
145 {"bytes", required_argument
, NULL
, 'n'},
146 {"radix", required_argument
, NULL
, 't'},
147 {"encoding", required_argument
, NULL
, 'e'},
148 {"target", required_argument
, NULL
, 'T'},
149 {"help", no_argument
, NULL
, 'h'},
150 {"version", no_argument
, NULL
, 'v'},
154 static void strings_a_section (bfd
*, asection
*, void *);
155 static bfd_boolean
strings_object_file (const char *);
156 static bfd_boolean
strings_file (char *file
);
157 static int integer_arg (char *s
);
158 static void print_strings (const char *, FILE *, file_off
, int, int, char *);
159 static void usage (FILE *, int);
160 static long get_char (FILE *, file_off
*, int *, char **);
162 int main (int, char **);
165 main (int argc
, char **argv
)
169 bfd_boolean files_given
= FALSE
;
171 #if defined (HAVE_SETLOCALE)
172 setlocale (LC_ALL
, "");
174 bindtextdomain (PACKAGE
, LOCALEDIR
);
175 textdomain (PACKAGE
);
177 program_name
= argv
[0];
178 xmalloc_set_program_name (program_name
);
180 print_addresses
= FALSE
;
181 print_filenames
= FALSE
;
182 datasection_only
= TRUE
;
186 while ((optc
= getopt_long (argc
, argv
, "afhHn:ot:e:Vv0123456789",
187 long_options
, (int *) 0)) != EOF
)
192 datasection_only
= FALSE
;
196 print_filenames
= TRUE
;
204 string_min
= integer_arg (optarg
);
206 fatal (_("invalid number %s"), optarg
);
210 print_addresses
= TRUE
;
215 print_addresses
= TRUE
;
216 if (optarg
[1] != '\0')
242 if (optarg
[1] != '\0')
244 encoding
= optarg
[0];
249 print_version ("strings");
257 string_min
= optc
- '0';
259 string_min
= string_min
* 10 + optc
- '0';
286 set_default_bfd_target ();
290 datasection_only
= FALSE
;
292 SET_BINARY (fileno (stdin
));
294 print_strings ("{standard input}", stdin
, 0, 0, 0, (char *) NULL
);
299 for (; optind
< argc
; ++optind
)
301 if (strcmp (argv
[optind
], "-") == 0)
302 datasection_only
= FALSE
;
306 exit_status
|= strings_file (argv
[optind
]) == FALSE
;
314 return (exit_status
);
317 /* Scan section SECT of the file ABFD, whose printable name is FILE.
318 If it contains initialized data,
319 set `got_a_section' and print the strings in it. */
322 strings_a_section (bfd
*abfd
, asection
*sect
, void *filearg
)
324 const char *file
= (const char *) filearg
;
326 if ((sect
->flags
& DATA_FLAGS
) == DATA_FLAGS
)
328 bfd_size_type sz
= bfd_get_section_size (sect
);
329 void *mem
= xmalloc (sz
);
331 if (bfd_get_section_contents (abfd
, sect
, mem
, (file_ptr
) 0, sz
))
333 got_a_section
= TRUE
;
334 print_strings (file
, (FILE *) NULL
, sect
->filepos
, 0, sz
, mem
);
340 /* Scan all of the sections in FILE, and print the strings
341 in the initialized data section(s).
343 Return TRUE if successful,
344 FALSE if not (such as if FILE is not an object file). */
347 strings_object_file (const char *file
)
349 bfd
*abfd
= bfd_openr (file
, target
);
352 /* Treat the file as a non-object file. */
355 /* This call is mainly for its side effect of reading in the sections.
356 We follow the traditional behavior of `strings' in that we don't
357 complain if we don't recognize a file to be an object file. */
358 if (!bfd_check_format (abfd
, bfd_object
))
364 got_a_section
= FALSE
;
365 bfd_map_over_sections (abfd
, strings_a_section
, (void *) file
);
367 if (!bfd_close (abfd
))
373 return got_a_section
;
376 /* Print the strings in FILE. Return TRUE if ok, FALSE if an error occurs. */
379 strings_file (char *file
)
383 if (file_stat (file
, &st
) < 0)
386 non_fatal (_("'%s': No such file"), file
);
388 non_fatal (_("Warning: could not locate '%s'. reason: %s"),
389 file
, strerror (errno
));
393 /* If we weren't told to scan the whole file,
394 try to open it as an object file and only look at
395 initialized data sections. If that fails, fall back to the
397 if (!datasection_only
|| !strings_object_file (file
))
401 stream
= file_open (file
, FOPEN_RB
);
404 fprintf (stderr
, "%s: ", program_name
);
409 print_strings (file
, stream
, (file_off
) 0, 0, 0, (char *) 0);
411 if (fclose (stream
) == EOF
)
413 fprintf (stderr
, "%s: ", program_name
);
422 /* Read the next character, return EOF if none available.
423 Assume that STREAM is positioned so that the next byte read
424 is at address ADDRESS in the file.
426 If STREAM is NULL, do not read from it.
427 The caller can supply a buffer of characters
428 to be processed before the data in STREAM.
429 MAGIC is the address of the buffer and
430 MAGICCOUNT is how many characters are in it. */
433 get_char (FILE *stream
, file_off
*address
, int *magiccount
, char **magic
)
437 unsigned char buf
[4];
439 for (i
= 0; i
< encoding_bytes
; i
++)
450 #ifdef HAVE_GETC_UNLOCKED
451 c
= getc_unlocked (stream
);
470 r
= (buf
[0] << 8) | buf
[1];
473 r
= buf
[0] | (buf
[1] << 8);
476 r
= ((long) buf
[0] << 24) | ((long) buf
[1] << 16) |
477 ((long) buf
[2] << 8) | buf
[3];
480 r
= buf
[0] | ((long) buf
[1] << 8) | ((long) buf
[2] << 16) |
481 ((long) buf
[3] << 24);
491 /* Find the strings in file FILENAME, read from STREAM.
492 Assume that STREAM is positioned so that the next byte read
493 is at address ADDRESS in the file.
494 Stop reading at address STOP_POINT in the file, if nonzero.
496 If STREAM is NULL, do not read from it.
497 The caller can supply a buffer of characters
498 to be processed before the data in STREAM.
499 MAGIC is the address of the buffer and
500 MAGICCOUNT is how many characters are in it.
501 Those characters come at address ADDRESS and the data in STREAM follow. */
504 print_strings (const char *filename
, FILE *stream
, file_off address
,
505 int stop_point
, int magiccount
, char *magic
)
507 char *buf
= (char *) xmalloc (sizeof (char) * (string_min
+ 1));
515 /* See if the next `string_min' chars are all graphic chars. */
517 if (stop_point
&& address
>= stop_point
)
520 for (i
= 0; i
< string_min
; i
++)
522 c
= get_char (stream
, &address
, &magiccount
, &magic
);
525 if (! STRING_ISGRAPHIC (c
))
526 /* Found a non-graphic. Try again starting with next char. */
531 /* We found a run of `string_min' graphic characters. Print up
532 to the next non-graphic character. */
535 printf ("%s: ", filename
);
537 switch (address_radix
)
540 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
541 if (sizeof (start
) > sizeof (long))
542 printf ("%7Lo ", (unsigned long long) start
);
545 # if !BFD_HOST_64BIT_LONG
546 if (start
!= (unsigned long) start
)
547 printf ("++%7lo ", (unsigned long) start
);
551 printf ("%7lo ", (unsigned long) start
);
555 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
556 if (sizeof (start
) > sizeof (long))
557 printf ("%7Ld ", (unsigned long long) start
);
560 # if !BFD_HOST_64BIT_LONG
561 if (start
!= (unsigned long) start
)
562 printf ("++%7ld ", (unsigned long) start
);
566 printf ("%7ld ", (long) start
);
570 #if __STDC_VERSION__ >= 199901L || (defined(__GNUC__) && __GNUC__ >= 2)
571 if (sizeof (start
) > sizeof (long))
572 printf ("%7Lx ", (unsigned long long) start
);
575 # if !BFD_HOST_64BIT_LONG
576 if (start
!= (unsigned long) start
)
577 printf ("%lx%8.8lx ", (unsigned long) (start
>> 32),
578 (unsigned long) (start
& 0xffffffff));
582 printf ("%7lx ", (unsigned long) start
);
591 c
= get_char (stream
, &address
, &magiccount
, &magic
);
594 if (! STRING_ISGRAPHIC (c
))
603 /* Parse string S as an integer, using decimal radix by default,
604 but allowing octal and hex numbers as in C. */
607 integer_arg (char *s
)
616 else if (*++p
== 'x')
625 while (((c
= *p
++) >= '0' && c
<= '9')
626 || (radix
== 16 && (c
& ~40) >= 'A' && (c
& ~40) <= 'Z'))
629 if (c
>= '0' && c
<= '9')
632 value
+= (c
& ~40) - 'A';
643 fatal (_("invalid integer argument %s"), s
);
649 usage (FILE *stream
, int status
)
651 fprintf (stream
, _("Usage: %s [option(s)] [file(s)]\n"), program_name
);
652 fprintf (stream
, _(" Display printable strings in [file(s)] (stdin by default)\n"));
653 fprintf (stream
, _(" The options are:\n\
654 -a - --all Scan the entire file, not just the data section\n\
655 -f --print-file-name Print the name of the file before each string\n\
656 -n --bytes=[number] Locate & print any NUL-terminated sequence of at\n\
657 -<number> least [number] characters (default 4).\n\
658 -t --radix={o,d,x} Print the location of the string in base 8, 10 or 16\n\
659 -o An alias for --radix=o\n\
660 -T --target=<BFDNAME> Specify the binary file format\n\
661 -e --encoding={s,S,b,l,B,L} Select character size and endianness:\n\
662 s = 7-bit, S = 8-bit, {b,l} = 16-bit, {B,L} = 32-bit\n\
663 -h --help Display this information\n\
664 -v --version Print the program's version number\n"));
665 list_supported_targets (program_name
, stream
);
667 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);