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