Split miscutils/Config.src items into miscutils/*.c files
[oweals/busybox.git] / miscutils / strings.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * strings implementation for busybox
4  *
5  * Copyright 2003 Tito Ragusa <farmatito@tiscali.it>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
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.
15
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)"
27
28 #include "libbb.h"
29
30 #define WHOLE_FILE    1
31 #define PRINT_NAME    2
32 #define PRINT_OFFSET  4
33 #define SIZE          8
34 #define PRINT_RADIX  16
35
36 int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
37 int strings_main(int argc UNUSED_PARAM, char **argv)
38 {
39         int n, c, status = EXIT_SUCCESS;
40         unsigned count;
41         off_t offset;
42         FILE *file;
43         char *string;
44         const char *fmt = "%s: ";
45         const char *n_arg = "4";
46         /* default for -o */
47         const char *radix = "o";
48         char *radix_fmt;
49
50         getopt32(argv, "afon:t:", &n_arg, &radix);
51         /* -a is our default behaviour */
52         /*argc -= optind;*/
53         argv += optind;
54
55         n = xatou_range(n_arg, 1, INT_MAX);
56         string = xzalloc(n + 1);
57         n--;
58
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
64         if (!*argv) {
65                 fmt = "{%s}: ";
66                 *--argv = (char *)bb_msg_standard_input;
67         }
68
69         do {
70                 file = fopen_or_warn_stdin(*argv);
71                 if (!file) {
72                         status = EXIT_FAILURE;
73                         continue;
74                 }
75                 offset = 0;
76                 count = 0;
77                 do {
78                         c = fgetc(file);
79                         if (isprint_asciionly(c) || c == '\t') {
80                                 if (count > n) {
81                                         bb_putchar(c);
82                                 } else {
83                                         string[count] = c;
84                                         if (count == n) {
85                                                 if (option_mask32 & PRINT_NAME) {
86                                                         printf(fmt, *argv);
87                                                 }
88                                                 if (option_mask32 & (PRINT_OFFSET | PRINT_RADIX)) {
89                                                         printf(radix_fmt, offset - n);
90                                                 }
91                                                 fputs(string, stdout);
92                                         }
93                                         count++;
94                                 }
95                         } else {
96                                 if (count > n) {
97                                         bb_putchar('\n');
98                                 }
99                                 count = 0;
100                         }
101                         offset++;
102                 } while (c != EOF);
103                 fclose_if_not_stdin(file);
104         } while (*++argv);
105
106         if (ENABLE_FEATURE_CLEAN_UP) {
107                 free(string);
108                 free(radix_fmt);
109         }
110
111         fflush_stdout_and_exit(status);
112 }