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"
13 //config: strings prints the printable character sequences for each file
16 //usage:#define strings_trivial_usage
17 //usage: "[-fo] [-t o/d/x] [-n LEN] [FILE]..."
18 //usage:#define strings_full_usage "\n\n"
19 //usage: "Display printable strings in a binary file\n"
20 //We usually don't bother user with "nop" options. They work, but are not shown:
21 ////usage: "\n -a Scan whole file (default)"
22 //unimplemented alternative is -d: Only strings from initialized, loaded data sections
23 //usage: "\n -f Precede strings with filenames"
24 //usage: "\n -o Precede strings with octal offsets"
25 //usage: "\n -t o/d/x Precede strings with offsets in base 8/10/16"
26 //usage: "\n -n LEN At least LEN characters form a string (default 4)"
32 #define PRINT_OFFSET 4
34 #define PRINT_RADIX 16
36 int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
37 int strings_main(int argc UNUSED_PARAM, char **argv)
39 int n, c, status = EXIT_SUCCESS;
44 const char *fmt = "%s: ";
45 const char *n_arg = "4";
47 const char *radix = "o";
50 getopt32(argv, "afon:t:", &n_arg, &radix);
51 /* -a is our default behaviour */
55 n = xatou_range(n_arg, 1, INT_MAX);
56 string = xzalloc(n + 1);
59 if ((radix[0] != 'd' && radix[0] != 'o' && radix[0] != 'x') || radix[1] != 0)
62 radix_fmt = xasprintf("%%7"OFF_FMT"%s ", radix);
66 *--argv = (char *)bb_msg_standard_input;
70 file = fopen_or_warn_stdin(*argv);
72 status = EXIT_FAILURE;
79 if (isprint_asciionly(c) || c == '\t') {
85 if (option_mask32 & PRINT_NAME) {
88 if (option_mask32 & (PRINT_OFFSET | PRINT_RADIX)) {
89 printf(radix_fmt, offset - n);
91 fputs(string, stdout);
103 fclose_if_not_stdin(file);
106 if (ENABLE_FEATURE_CLEAN_UP) {
111 fflush_stdout_and_exit(status);