Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * strings implementation for busybox |
| 4 | * |
Denis Vlasenko | 4b57094 | 2008-11-23 14:58:14 +0000 | [diff] [blame] | 5 | * Copyright 2003 Tito Ragusa <farmatito@tiscali.it> |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 6 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 9 | //config:config STRINGS |
| 10 | //config: bool "strings" |
| 11 | //config: default y |
| 12 | //config: help |
| 13 | //config: strings prints the printable character sequences for each file |
| 14 | //config: specified. |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 15 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame^] | 16 | //applet:IF_STRINGS(APPLET(strings, BB_DIR_USR_BIN, BB_SUID_DROP)) |
| 17 | |
| 18 | //kbuild:lib-$(CONFIG_STRINGS) += strings.o |
| 19 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 20 | //usage:#define strings_trivial_usage |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 21 | //usage: "[-fo] [-t o/d/x] [-n LEN] [FILE]..." |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 22 | //usage:#define strings_full_usage "\n\n" |
| 23 | //usage: "Display printable strings in a binary file\n" |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 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)" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 31 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 32 | #include "libbb.h" |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 33 | |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 34 | #define WHOLE_FILE 1 |
| 35 | #define PRINT_NAME 2 |
| 36 | #define PRINT_OFFSET 4 |
| 37 | #define SIZE 8 |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 38 | #define PRINT_RADIX 16 |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 40 | int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 41 | int strings_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 42 | { |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 43 | int n, c, status = EXIT_SUCCESS; |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 44 | unsigned count; |
| 45 | off_t offset; |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 46 | FILE *file; |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 47 | char *string; |
| 48 | const char *fmt = "%s: "; |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 49 | const char *n_arg = "4"; |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 50 | /* default for -o */ |
| 51 | const char *radix = "o"; |
| 52 | char *radix_fmt; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 53 | |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 54 | getopt32(argv, "afon:t:", &n_arg, &radix); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 55 | /* -a is our default behaviour */ |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 56 | /*argc -= optind;*/ |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 57 | argv += optind; |
| 58 | |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 59 | n = xatou_range(n_arg, 1, INT_MAX); |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 60 | string = xzalloc(n + 1); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 61 | n--; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 62 | |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 63 | if ((radix[0] != 'd' && radix[0] != 'o' && radix[0] != 'x') || radix[1] != 0) |
| 64 | bb_show_usage(); |
| 65 | |
| 66 | radix_fmt = xasprintf("%%7"OFF_FMT"%s ", radix); |
| 67 | |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 68 | if (!*argv) { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 69 | fmt = "{%s}: "; |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 70 | *--argv = (char *)bb_msg_standard_input; |
Eric Andersen | 65ddf77 | 2003-01-13 23:19:31 +0000 | [diff] [blame] | 71 | } |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 72 | |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 73 | do { |
Denis Vlasenko | 62a90cd | 2008-03-17 09:07:36 +0000 | [diff] [blame] | 74 | file = fopen_or_warn_stdin(*argv); |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 75 | if (!file) { |
| 76 | status = EXIT_FAILURE; |
| 77 | continue; |
| 78 | } |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 79 | offset = 0; |
| 80 | count = 0; |
| 81 | do { |
| 82 | c = fgetc(file); |
Denys Vlasenko | 8684cbb | 2009-11-18 11:34:43 +0100 | [diff] [blame] | 83 | if (isprint_asciionly(c) || c == '\t') { |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 84 | if (count > n) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 85 | bb_putchar(c); |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 86 | } else { |
| 87 | string[count] = c; |
| 88 | if (count == n) { |
Denis Vlasenko | 4b57094 | 2008-11-23 14:58:14 +0000 | [diff] [blame] | 89 | if (option_mask32 & PRINT_NAME) { |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 90 | printf(fmt, *argv); |
| 91 | } |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 92 | if (option_mask32 & (PRINT_OFFSET | PRINT_RADIX)) { |
| 93 | printf(radix_fmt, offset - n); |
Rob Landley | 16cd02e | 2005-06-07 03:21:20 +0000 | [diff] [blame] | 94 | } |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 95 | fputs(string, stdout); |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 96 | } |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 97 | count++; |
Eric Andersen | 92b7e7b | 2003-03-13 18:49:45 +0000 | [diff] [blame] | 98 | } |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 99 | } else { |
| 100 | if (count > n) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 101 | bb_putchar('\n'); |
Denis Vlasenko | 787d926 | 2007-06-17 12:19:07 +0000 | [diff] [blame] | 102 | } |
| 103 | count = 0; |
| 104 | } |
| 105 | offset++; |
| 106 | } while (c != EOF); |
| 107 | fclose_if_not_stdin(file); |
| 108 | } while (*++argv); |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 109 | |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 110 | if (ENABLE_FEATURE_CLEAN_UP) { |
Bernhard Reutner-Fischer | 0817313 | 2006-02-27 22:34:41 +0000 | [diff] [blame] | 111 | free(string); |
Tito Ragusa | 69312e8 | 2016-10-24 21:52:10 +0200 | [diff] [blame] | 112 | free(radix_fmt); |
| 113 | } |
Rob Landley | 658d2cf | 2005-09-08 03:11:58 +0000 | [diff] [blame] | 114 | |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 115 | fflush_stdout_and_exit(status); |
Eric Andersen | ef5e8f8 | 2002-11-07 02:09:37 +0000 | [diff] [blame] | 116 | } |