[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * strings implementation for busybox
|
---|
| 4 | *
|
---|
[2725] | 5 | * Copyright 2003 Tito Ragusa <farmatito@tiscali.it>
|
---|
[821] | 6 | *
|
---|
[2725] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 8 | */
|
---|
| 9 |
|
---|
[3232] | 10 | //usage:#define strings_trivial_usage
|
---|
| 11 | //usage: "[-afo] [-n LEN] [FILE]..."
|
---|
| 12 | //usage:#define strings_full_usage "\n\n"
|
---|
| 13 | //usage: "Display printable strings in a binary file\n"
|
---|
| 14 | //usage: "\n -a Scan whole file (default)"
|
---|
| 15 | //usage: "\n -f Precede strings with filenames"
|
---|
| 16 | //usage: "\n -n LEN At least LEN characters form a string (default 4)"
|
---|
| 17 | //usage: "\n -o Precede strings with decimal offsets"
|
---|
| 18 |
|
---|
[1765] | 19 | #include "libbb.h"
|
---|
| 20 |
|
---|
[2725] | 21 | #define WHOLE_FILE 1
|
---|
| 22 | #define PRINT_NAME 2
|
---|
| 23 | #define PRINT_OFFSET 4
|
---|
| 24 | #define SIZE 8
|
---|
[821] | 25 |
|
---|
[2725] | 26 | int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 27 | int strings_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 28 | {
|
---|
[1765] | 29 | int n, c, status = EXIT_SUCCESS;
|
---|
| 30 | unsigned count;
|
---|
| 31 | off_t offset;
|
---|
[2725] | 32 | FILE *file;
|
---|
[821] | 33 | char *string;
|
---|
| 34 | const char *fmt = "%s: ";
|
---|
[1765] | 35 | const char *n_arg = "4";
|
---|
[821] | 36 |
|
---|
[2725] | 37 | getopt32(argv, "afon:", &n_arg);
|
---|
[821] | 38 | /* -a is our default behaviour */
|
---|
[1765] | 39 | /*argc -= optind;*/
|
---|
[821] | 40 | argv += optind;
|
---|
| 41 |
|
---|
[1765] | 42 | n = xatou_range(n_arg, 1, INT_MAX);
|
---|
[821] | 43 | string = xzalloc(n + 1);
|
---|
| 44 | n--;
|
---|
| 45 |
|
---|
[1765] | 46 | if (!*argv) {
|
---|
[821] | 47 | fmt = "{%s}: ";
|
---|
[1765] | 48 | *--argv = (char *)bb_msg_standard_input;
|
---|
[821] | 49 | }
|
---|
| 50 |
|
---|
| 51 | do {
|
---|
[2725] | 52 | file = fopen_or_warn_stdin(*argv);
|
---|
[1765] | 53 | if (!file) {
|
---|
| 54 | status = EXIT_FAILURE;
|
---|
| 55 | continue;
|
---|
| 56 | }
|
---|
| 57 | offset = 0;
|
---|
| 58 | count = 0;
|
---|
| 59 | do {
|
---|
| 60 | c = fgetc(file);
|
---|
[2725] | 61 | if (isprint_asciionly(c) || c == '\t') {
|
---|
[1765] | 62 | if (count > n) {
|
---|
[2725] | 63 | bb_putchar(c);
|
---|
[1765] | 64 | } else {
|
---|
| 65 | string[count] = c;
|
---|
| 66 | if (count == n) {
|
---|
[2725] | 67 | if (option_mask32 & PRINT_NAME) {
|
---|
[821] | 68 | printf(fmt, *argv);
|
---|
| 69 | }
|
---|
[2725] | 70 | if (option_mask32 & PRINT_OFFSET) {
|
---|
[1765] | 71 | printf("%7"OFF_FMT"o ", offset - n);
|
---|
[821] | 72 | }
|
---|
[1765] | 73 | fputs(string, stdout);
|
---|
[821] | 74 | }
|
---|
[1765] | 75 | count++;
|
---|
[821] | 76 | }
|
---|
[1765] | 77 | } else {
|
---|
| 78 | if (count > n) {
|
---|
[2725] | 79 | bb_putchar('\n');
|
---|
[1765] | 80 | }
|
---|
| 81 | count = 0;
|
---|
| 82 | }
|
---|
| 83 | offset++;
|
---|
| 84 | } while (c != EOF);
|
---|
| 85 | fclose_if_not_stdin(file);
|
---|
| 86 | } while (*++argv);
|
---|
[821] | 87 |
|
---|
| 88 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 89 | free(string);
|
---|
| 90 |
|
---|
[1765] | 91 | fflush_stdout_and_exit(status);
|
---|
[821] | 92 | }
|
---|