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.3 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)
43 int n, c, status = EXIT_SUCCESS;
48 const char *fmt = "%s: ";
49 const char *n_arg = "4";
51 const char *radix = "o";
54 getopt32(argv, "afon:t:", &n_arg, &radix);
55 /* -a is our default behaviour */
59 n = xatou_range(n_arg, 1, INT_MAX);
60 string = xzalloc(n + 1);
63 if ((radix[0] != 'd' && radix[0] != 'o' && radix[0] != 'x') || radix[1] != 0)
66 radix_fmt = xasprintf("%%7"OFF_FMT"%s ", radix);
70 *--argv = (char *)bb_msg_standard_input;
74 file = fopen_or_warn_stdin(*argv);
76 status = EXIT_FAILURE;
83 if (isprint_asciionly(c) || c == '\t') {
89 if (option_mask32 & PRINT_NAME) {
92 if (option_mask32 & (PRINT_OFFSET | PRINT_RADIX)) {
93 printf(radix_fmt, offset - n);
95 fputs(string, stdout);
107 fclose_if_not_stdin(file);
110 if (ENABLE_FEATURE_CLEAN_UP) {
115 fflush_stdout_and_exit(status);