diff options
author | Tito Ragusa <farmatito@tiscali.it> | 2016-10-24 21:52:10 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2016-10-24 21:52:10 +0200 |
commit | 69312e87b008363575a6d6603f54f94d8150e1cc (patch) | |
tree | d123bc013216a2b2611a987127bded20c14b35ae | |
parent | db74c6caedf752623820cd09f7fbeaf4152e1de8 (diff) | |
download | busybox-w32-69312e87b008363575a6d6603f54f94d8150e1cc.tar.gz busybox-w32-69312e87b008363575a6d6603f54f94d8150e1cc.tar.bz2 busybox-w32-69312e87b008363575a6d6603f54f94d8150e1cc.zip |
strings: implement -t radix
v2: minor code cleanup, no changes.
v1: Implement -t radix option.
Fix help text for -o option.
Signed-off-by: Tito Ragusa <farmatito@tiscali.it>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | miscutils/strings.c | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/miscutils/strings.c b/miscutils/strings.c index 9f5018244..ee6649625 100644 --- a/miscutils/strings.c +++ b/miscutils/strings.c | |||
@@ -8,13 +8,16 @@ | |||
8 | */ | 8 | */ |
9 | 9 | ||
10 | //usage:#define strings_trivial_usage | 10 | //usage:#define strings_trivial_usage |
11 | //usage: "[-afo] [-n LEN] [FILE]..." | 11 | //usage: "[-fo] [-t o/d/x] [-n LEN] [FILE]..." |
12 | //usage:#define strings_full_usage "\n\n" | 12 | //usage:#define strings_full_usage "\n\n" |
13 | //usage: "Display printable strings in a binary file\n" | 13 | //usage: "Display printable strings in a binary file\n" |
14 | //usage: "\n -a Scan whole file (default)" | 14 | //We usually don't bother user with "nop" options. They work, but are not shown: |
15 | //usage: "\n -f Precede strings with filenames" | 15 | ////usage: "\n -a Scan whole file (default)" |
16 | //usage: "\n -n LEN At least LEN characters form a string (default 4)" | 16 | //unimplemented alternative is -d: Only strings from initialized, loaded data sections |
17 | //usage: "\n -o Precede strings with decimal offsets" | 17 | //usage: "\n -f Precede strings with filenames" |
18 | //usage: "\n -o Precede strings with octal offsets" | ||
19 | //usage: "\n -t o/d/x Precede strings with offsets in base 8/10/16" | ||
20 | //usage: "\n -n LEN At least LEN characters form a string (default 4)" | ||
18 | 21 | ||
19 | #include "libbb.h" | 22 | #include "libbb.h" |
20 | 23 | ||
@@ -22,6 +25,7 @@ | |||
22 | #define PRINT_NAME 2 | 25 | #define PRINT_NAME 2 |
23 | #define PRINT_OFFSET 4 | 26 | #define PRINT_OFFSET 4 |
24 | #define SIZE 8 | 27 | #define SIZE 8 |
28 | #define PRINT_RADIX 16 | ||
25 | 29 | ||
26 | int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 30 | int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
27 | int strings_main(int argc UNUSED_PARAM, char **argv) | 31 | int strings_main(int argc UNUSED_PARAM, char **argv) |
@@ -33,8 +37,11 @@ int strings_main(int argc UNUSED_PARAM, char **argv) | |||
33 | char *string; | 37 | char *string; |
34 | const char *fmt = "%s: "; | 38 | const char *fmt = "%s: "; |
35 | const char *n_arg = "4"; | 39 | const char *n_arg = "4"; |
40 | /* default for -o */ | ||
41 | const char *radix = "o"; | ||
42 | char *radix_fmt; | ||
36 | 43 | ||
37 | getopt32(argv, "afon:", &n_arg); | 44 | getopt32(argv, "afon:t:", &n_arg, &radix); |
38 | /* -a is our default behaviour */ | 45 | /* -a is our default behaviour */ |
39 | /*argc -= optind;*/ | 46 | /*argc -= optind;*/ |
40 | argv += optind; | 47 | argv += optind; |
@@ -43,6 +50,11 @@ int strings_main(int argc UNUSED_PARAM, char **argv) | |||
43 | string = xzalloc(n + 1); | 50 | string = xzalloc(n + 1); |
44 | n--; | 51 | n--; |
45 | 52 | ||
53 | if ((radix[0] != 'd' && radix[0] != 'o' && radix[0] != 'x') || radix[1] != 0) | ||
54 | bb_show_usage(); | ||
55 | |||
56 | radix_fmt = xasprintf("%%7"OFF_FMT"%s ", radix); | ||
57 | |||
46 | if (!*argv) { | 58 | if (!*argv) { |
47 | fmt = "{%s}: "; | 59 | fmt = "{%s}: "; |
48 | *--argv = (char *)bb_msg_standard_input; | 60 | *--argv = (char *)bb_msg_standard_input; |
@@ -67,8 +79,8 @@ int strings_main(int argc UNUSED_PARAM, char **argv) | |||
67 | if (option_mask32 & PRINT_NAME) { | 79 | if (option_mask32 & PRINT_NAME) { |
68 | printf(fmt, *argv); | 80 | printf(fmt, *argv); |
69 | } | 81 | } |
70 | if (option_mask32 & PRINT_OFFSET) { | 82 | if (option_mask32 & (PRINT_OFFSET | PRINT_RADIX)) { |
71 | printf("%7"OFF_FMT"o ", offset - n); | 83 | printf(radix_fmt, offset - n); |
72 | } | 84 | } |
73 | fputs(string, stdout); | 85 | fputs(string, stdout); |
74 | } | 86 | } |
@@ -85,8 +97,10 @@ int strings_main(int argc UNUSED_PARAM, char **argv) | |||
85 | fclose_if_not_stdin(file); | 97 | fclose_if_not_stdin(file); |
86 | } while (*++argv); | 98 | } while (*++argv); |
87 | 99 | ||
88 | if (ENABLE_FEATURE_CLEAN_UP) | 100 | if (ENABLE_FEATURE_CLEAN_UP) { |
89 | free(string); | 101 | free(string); |
102 | free(radix_fmt); | ||
103 | } | ||
90 | 104 | ||
91 | fflush_stdout_and_exit(status); | 105 | fflush_stdout_and_exit(status); |
92 | } | 106 | } |