usage.c: remove reference to busybox.h
[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 <getopt.h>
11
12 #include "libbb.h"
13
14 #define WHOLE_FILE              1
15 #define PRINT_NAME              2
16 #define PRINT_OFFSET    4
17 #define SIZE                    8
18
19 int strings_main(int argc, char **argv);
20 int strings_main(int argc, char **argv)
21 {
22         int n, c, i = 0, status = EXIT_SUCCESS;
23         unsigned opt;
24         unsigned long count;
25         FILE *file = stdin;
26         char *string;
27         const char *fmt = "%s: ";
28         const char *n_arg = "4";
29
30         opt = getopt32(argc, argv, "afon:", &n_arg);
31         /* -a is our default behaviour */
32
33         argc -= optind;
34         argv += optind;
35
36         n = xatoul_range(n_arg, 1, INT_MAX);
37         string = xzalloc(n + 1);
38         n--;
39
40         if (argc == 0) {
41                 fmt = "{%s}: ";
42                 *argv = (char *)bb_msg_standard_input;
43                 goto PIPE;
44         }
45
46         do {
47                 file = fopen_or_warn(*argv, "r");
48                 if (file) {
49 PIPE:
50                         count = 0;
51                         do {
52                                 c = fgetc(file);
53                                 if (isprint(c) || c == '\t') {
54                                         if (i <= n) {
55                                                 string[i] = c;
56                                         } else {
57                                                 putchar(c);
58                                         }
59                                         if (i == n) {
60                                                 if (opt & PRINT_NAME) {
61                                                         printf(fmt, *argv);
62                                                 }
63                                                 if (opt & PRINT_OFFSET) {
64                                                         printf("%7lo ", count - n);
65                                                 }
66                                                 printf("%s", string);
67                                         }
68                                         i++;
69                                 } else {
70                                         if (i > n) {
71                                                 putchar('\n');
72                                         }
73                                         i = 0;
74                                 }
75                                 count++;
76                         } while (c != EOF);
77                         fclose_if_not_stdin(file);
78                 } else {
79                         status = EXIT_FAILURE;
80                 }
81         } while (--argc > 0);
82
83         if (ENABLE_FEATURE_CLEAN_UP)
84                 free(string);
85
86         fflush_stdout_and_exit(status);
87 }