1 /* vi: set sw=4 ts=4: */
3 * strings implementation for busybox
5 * Copyright 2003 Tito Ragusa <farmatito@tiscali.it>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9 //config:config STRINGS
10 //config: bool "strings (4.8 kb)"
13 //config: strings prints the printable character sequences for each file
16 //applet:IF_STRINGS(APPLET(strings, BB_DIR_USR_BIN, BB_SUID_DROP))
18 //kbuild:lib-$(CONFIG_STRINGS) += strings.o
20 //usage:#define strings_trivial_usage
21 //usage: "[-fo] [-t o|d|x] [-n LEN] [FILE]..."
22 //usage:#define strings_full_usage "\n\n"
23 //usage: "Display printable strings in a binary file\n"
24 //We usually don't bother user with "nop" options. They work, but are not shown:
25 ////usage: "\n -a Scan whole file (default)"
26 //unimplemented alternative is -d: Only strings from initialized, loaded data sections
27 //usage: "\n -f Precede strings with filenames"
28 //usage: "\n -o Precede strings with octal offsets"
29 //usage: "\n -t o|d|x Precede strings with offsets in base 8/10/16"
30 //usage: "\n -n LEN At least LEN characters form a string (default 4)"
36 #define PRINT_OFFSET 4
38 #define PRINT_RADIX 16
40 int strings_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
41 int strings_main(int argc UNUSED_PARAM
, char **argv
)
44 exitcode_t status
= EXIT_SUCCESS
;
49 const char *fmt
= "%s: ";
50 const char *n_arg
= "4";
52 const char *radix
= "o";
55 getopt32(argv
, "afon:t:", &n_arg
, &radix
);
56 /* -a is our default behaviour */
60 n
= xatou_range(n_arg
, 1, INT_MAX
);
61 string
= xzalloc(n
+ 1);
64 if ((radix
[0] != 'd' && radix
[0] != 'o' && radix
[0] != 'x') || radix
[1] != 0)
67 radix_fmt
= xasprintf("%%7"OFF_FMT
"%s ", radix
);
71 *--argv
= (char *)bb_msg_standard_input
;
75 file
= fopen_or_warn_stdin(*argv
);
77 status
= EXIT_FAILURE
;
84 if (isprint_asciionly(c
) || c
== '\t') {
90 if (option_mask32
& PRINT_NAME
) {
93 if (option_mask32
& (PRINT_OFFSET
| PRINT_RADIX
)) {
94 printf(radix_fmt
, offset
- n
);
108 fclose_if_not_stdin(file
);
111 if (ENABLE_FEATURE_CLEAN_UP
) {
116 fflush_stdout_and_exit(status
);