ash: some beautification work, no code changes
[oweals/busybox.git] / miscutils / strings.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * strings implementation for busybox
4  *
5  * Copyright Tito Ragusa <farmatito@tiscali.it>
6  *
7  * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
8  */
9
10 #include "libbb.h"
11
12 #define WHOLE_FILE              1
13 #define PRINT_NAME              2
14 #define PRINT_OFFSET    4
15 #define SIZE                    8
16
17 int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
18 int strings_main(int argc UNUSED_PARAM, char **argv)
19 {
20         int n, c, status = EXIT_SUCCESS;
21         unsigned opt;
22         unsigned count;
23         off_t offset;
24         FILE *file;
25         char *string;
26         const char *fmt = "%s: ";
27         const char *n_arg = "4";
28
29         opt = getopt32(argv, "afon:", &n_arg);
30         /* -a is our default behaviour */
31         /*argc -= optind;*/
32         argv += optind;
33
34         n = xatou_range(n_arg, 1, INT_MAX);
35         string = xzalloc(n + 1);
36         n--;
37
38         if (!*argv) {
39                 fmt = "{%s}: ";
40                 *--argv = (char *)bb_msg_standard_input;
41         }
42
43         do {
44                 file = fopen_or_warn_stdin(*argv);
45                 if (!file) {
46                         status = EXIT_FAILURE;
47                         continue;
48                 }
49                 offset = 0;
50                 count = 0;
51                 do {
52                         c = fgetc(file);
53                         if (isprint(c) || c == '\t') {
54                                 if (count > n) {
55                                         bb_putchar(c);
56                                 } else {
57                                         string[count] = c;
58                                         if (count == n) {
59                                                 if (opt & PRINT_NAME) {
60                                                         printf(fmt, *argv);
61                                                 }
62                                                 if (opt & PRINT_OFFSET) {
63                                                         printf("%7"OFF_FMT"o ", offset - n);
64                                                 }
65                                                 fputs(string, stdout);
66                                         }
67                                         count++;
68                                 }
69                         } else {
70                                 if (count > n) {
71                                         bb_putchar('\n');
72                                 }
73                                 count = 0;
74                         }
75                         offset++;
76                 } while (c != EOF);
77                 fclose_if_not_stdin(file);
78         } while (*++argv);
79
80         if (ENABLE_FEATURE_CLEAN_UP)
81                 free(string);
82
83         fflush_stdout_and_exit(status);
84 }