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