f46335b0f67103713a649c6a0b00e5d15245d62e
1 /* strings -- print the strings of printable characters in files
2 Copyright (C) 1993 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
18 /* Usage: strings [options] file...
23 - Do not scan only the initialized data section of object files.
26 -f Print the name of the file before each string.
30 -min-len Print graphic char sequences, MIN-LEN or more bytes long,
31 that are followed by a NUL or a newline. Default is 4.
34 -t {o,x,d} Print the offset within the file before each string,
37 -o Like -to. (Some other implementations have -o like -to,
38 others like -td. We chose one arbitrarily.)
41 -h Print the usage message on the standard output.
44 -v Print the program version number.
46 Written by Richard Stallman <rms@gnu.ai.mit.edu>
47 and David MacKenzie <djm@gnu.ai.mit.edu>. */
56 #define isgraphic(c) (isascii (c) && isprint (c))
58 #define isgraphic(c) (isprint (c))
65 /* The BFD section flags that identify an initialized data section. */
66 #define DATA_FLAGS (SEC_ALLOC | SEC_LOAD | SEC_DATA | SEC_HAS_CONTENTS)
68 /* Radix for printing addresses (must be 8, 10 or 16). */
69 static int address_radix
;
71 /* Minimum length of sequence of graphic chars to trigger output. */
72 static int string_min
;
74 /* true means print address within file for each string. */
75 static boolean print_addresses
;
77 /* true means print filename for each string. */
78 static boolean print_filenames
;
80 /* true means for object files scan only the data section. */
81 static boolean datasection_only
;
83 /* true if we found an initialized data section in the current file. */
84 static boolean got_a_section
;
86 /* Opened to /dev/null for reading from a BFD. */
89 extern char *program_name
;
90 extern char *program_version
;
92 static struct option long_options
[] =
94 {"all", no_argument
, NULL
, 'a'},
95 {"print-file-name", no_argument
, NULL
, 'f'},
96 {"bytes", required_argument
, NULL
, 'n'},
97 {"radix", required_argument
, NULL
, 't'},
98 {"help", no_argument
, NULL
, 'h'},
99 {"version", no_argument
, NULL
, 'v'},
106 static boolean
strings_file ();
107 static int integer_arg ();
108 static void dump_strings ();
109 static void usage ();
118 boolean files_given
= false;
120 program_name
= argv
[0];
122 print_addresses
= false;
123 print_filenames
= false;
124 datasection_only
= true;
126 while ((optc
= getopt_long (argc
, argv
, "afhn:ot:v0123456789",
127 long_options
, (int *) 0)) != EOF
)
132 datasection_only
= false;
136 print_filenames
= true;
144 string_min
= integer_arg (optarg
);
147 fprintf (stderr
, "%s: invalid number %s\n",
148 program_name
, optarg
);
154 print_addresses
= true;
159 print_addresses
= true;
160 if (optarg
[1] != '\0')
182 printf ("%s version %s\n", program_name
, program_version
);
192 string_min
= string_min
* 10 + optc
- '0';
201 devnull
= fopen ("/dev/null", "r");
204 fprintf (stderr
, "%s: ", program_name
);
205 perror ("/dev/null");
209 for (; optind
< argc
; ++optind
)
211 if (!strcmp (argv
[optind
], "-"))
212 datasection_only
= false;
216 exit_status
|= (strings_file (argv
[optind
]) == false);
220 if (files_given
== false)
226 /* Scan the sections of the file ABFD, whose printable name is FILE.
227 If any of them contain initialized data,
228 set `got_a_section' and print the strings in them. */
231 strings_a_section (abfd
, sect
, file
)
236 if ((sect
->flags
& DATA_FLAGS
) == DATA_FLAGS
)
238 bfd_size_type sz
= bfd_get_section_size_before_reloc (sect
);
239 PTR mem
= xmalloc (sz
);
240 if (bfd_get_section_contents (abfd
, sect
, mem
, (file_ptr
) 0, sz
))
242 got_a_section
= true;
243 dump_strings (file
, devnull
, sect
->filepos
, 0, sz
, mem
);
249 /* Print the strings in the initialized data section of FILE.
250 Return true if successful,
251 false if not (such as if FILE is not an object file). */
254 strings_object_file (file
)
257 bfd
*abfd
= bfd_openr (file
, NULL
);
261 if (bfd_error
!= system_call_error
)
263 /* Out of memory, or an invalid target is specified by the
264 GNUTARGET environment variable. */
265 fprintf (stderr
, "%s: ", program_name
);
271 /* For some reason, without this call, the BFD has no sections.
272 This call is only for the side effect of reading in the sections. */
273 bfd_check_format (abfd
, bfd_object
);
275 got_a_section
= false;
276 bfd_map_over_sections (abfd
, strings_a_section
, file
);
278 if (!bfd_close (abfd
))
280 fprintf (stderr
, "%s: ", program_name
);
285 return got_a_section
;
288 /* Print the strings in FILE. Return true if ok, false if an error occurs. */
294 /* If we weren't told to scan the whole file,
295 try to open it as an object file and only look at
296 initialized data sections. If that fails, fall back to the
298 if (!datasection_only
|| !strings_object_file (file
))
302 stream
= fopen (file
, "r");
305 fprintf (stderr
, "%s: ", program_name
);
310 dump_strings (file
, stream
, (file_ptr
) 0, 0, 0, (char *) 0);
312 if (fclose (stream
) == EOF
)
314 fprintf (stderr
, "%s: ", program_name
);
323 /* Find the strings in file FILENAME, read from STREAM.
324 Assume that STREAM is positioned so that the next byte read
325 is at address ADDRESS in the file.
326 Stop reading at address STOP_POINT in the file, if nonzero.
328 Optionally the caller can supply a buffer of characters
329 to be processed before the data in STREAM.
330 MAGIC is the address of the buffer and
331 MAGICCOUNT is how many characters are in it.
332 Those characters come at address ADDRESS and the data in STREAM follow. */
335 dump_strings (filename
, stream
, address
, stop_point
, magiccount
, magic
)
344 char *buf
= (char *) xmalloc (bufsize
);
351 /* See if the next `string_min' chars are all graphic chars. */
353 if (stop_point
&& address
>= stop_point
)
355 for (i
= 0; i
< string_min
; i
++)
370 /* Found a non-graphic. Try again starting with next char. */
375 /* We found a run of `string_min' graphic characters.
376 Now see if it is terminated with a NUL byte or a newline. */
382 buf
= (char *) xrealloc (buf
, bufsize
);
396 if (c
== '\0' || c
== '\n')
397 break; /* It is; print this string. */
399 goto tryline
; /* It isn't; give up on this string. */
400 buf
[i
++] = c
; /* The string continues; store it all. */
403 /* If we get here, the string is all graphics and properly terminated,
404 so print it. It is all in `buf' and `i' is its length. */
407 printf ("%s: ", filename
);
409 switch (address_radix
)
412 printf ("%7lo ", address
- i
- 1);
416 printf ("%7ld ", address
- i
- 1);
420 printf ("%7lx ", address
- i
- 1);
424 for (i
= 0; (c
= buf
[i
]) != '\0'; i
++)
449 /* Parse string S as an integer, using decimal radix by default,
450 but allowing octal and hex numbers as in C. */
463 else if (*++p
== 'x')
472 while (((c
= *p
++) >= '0' && c
<= '9')
473 || (radix
== 16 && (c
& ~40) >= 'A' && (c
& ~40) <= 'Z'))
476 if (c
>= '0' && c
<= '9')
479 value
+= (c
& ~40) - 'A';
491 fprintf (stderr
, "%s: invalid integer argument %s\n", program_name
, s
);
502 Usage: %s [-afhov] [-n min-len] [-min-len] [-t {o,x,d}] [-]\n\
503 [--all] [--print-file-name] [--bytes=min-len] [--radix={o,x,d}]\n\
504 [--help] [--version] file...\n",
This page took 0.718381 seconds and 4 git commands to generate.