move remaining help text from include/usage.src.h
[platform/upstream/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
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:     "\nOptions:"
15 //usage:     "\n        -a      Scan whole file (default)"
16 //usage:     "\n        -f      Precede strings with filenames"
17 //usage:     "\n        -n LEN  At least LEN characters form a string (default 4)"
18 //usage:     "\n        -o      Precede strings with decimal offsets"
19
20 #include "libbb.h"
21
22 #define WHOLE_FILE    1
23 #define PRINT_NAME    2
24 #define PRINT_OFFSET  4
25 #define SIZE          8
26
27 int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
28 int strings_main(int argc UNUSED_PARAM, char **argv)
29 {
30         int n, c, status = EXIT_SUCCESS;
31         unsigned count;
32         off_t offset;
33         FILE *file;
34         char *string;
35         const char *fmt = "%s: ";
36         const char *n_arg = "4";
37
38         getopt32(argv, "afon:", &n_arg);
39         /* -a is our default behaviour */
40         /*argc -= optind;*/
41         argv += optind;
42
43         n = xatou_range(n_arg, 1, INT_MAX);
44         string = xzalloc(n + 1);
45         n--;
46
47         if (!*argv) {
48                 fmt = "{%s}: ";
49                 *--argv = (char *)bb_msg_standard_input;
50         }
51
52         do {
53                 file = fopen_or_warn_stdin(*argv);
54                 if (!file) {
55                         status = EXIT_FAILURE;
56                         continue;
57                 }
58                 offset = 0;
59                 count = 0;
60                 do {
61                         c = fgetc(file);
62                         if (isprint_asciionly(c) || c == '\t') {
63                                 if (count > n) {
64                                         bb_putchar(c);
65                                 } else {
66                                         string[count] = c;
67                                         if (count == n) {
68                                                 if (option_mask32 & PRINT_NAME) {
69                                                         printf(fmt, *argv);
70                                                 }
71                                                 if (option_mask32 & PRINT_OFFSET) {
72                                                         printf("%7"OFF_FMT"o ", offset - n);
73                                                 }
74                                                 fputs(string, stdout);
75                                         }
76                                         count++;
77                                 }
78                         } else {
79                                 if (count > n) {
80                                         bb_putchar('\n');
81                                 }
82                                 count = 0;
83                         }
84                         offset++;
85                 } while (c != EOF);
86                 fclose_if_not_stdin(file);
87         } while (*++argv);
88
89         if (ENABLE_FEATURE_CLEAN_UP)
90                 free(string);
91
92         fflush_stdout_and_exit(status);
93 }