Fixed time command segfault with no arguments
[busybox4maemo] / 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) MAIN_EXTERNALLY_VISIBLE;
20 int strings_main(int argc ATTRIBUTE_UNUSED, char **argv)
21 {
22         int n, c, status = EXIT_SUCCESS;
23         unsigned opt;
24         unsigned count;
25         off_t offset;
26         FILE *file;
27         char *string;
28         const char *fmt = "%s: ";
29         const char *n_arg = "4";
30
31         opt = getopt32(argv, "afon:", &n_arg);
32         /* -a is our default behaviour */
33         /*argc -= optind;*/
34         argv += optind;
35
36         n = xatou_range(n_arg, 1, INT_MAX);
37         string = xzalloc(n + 1);
38         n--;
39
40         if (!*argv) {
41                 fmt = "{%s}: ";
42                 *--argv = (char *)bb_msg_standard_input;
43         }
44
45         do {
46                 file = fopen_or_warn_stdin(*argv);
47                 if (!file) {
48                         status = EXIT_FAILURE;
49                         continue;
50                 }
51                 offset = 0;
52                 count = 0;
53                 do {
54                         c = fgetc(file);
55                         if (isprint(c) || c == '\t') {
56                                 if (count > n) {
57                                         bb_putchar(c);
58                                 } else {
59                                         string[count] = c;
60                                         if (count == n) {
61                                                 if (opt & PRINT_NAME) {
62                                                         printf(fmt, *argv);
63                                                 }
64                                                 if (opt & PRINT_OFFSET) {
65                                                         printf("%7"OFF_FMT"o ", offset - n);
66                                                 }
67                                                 fputs(string, stdout);
68                                         }
69                                         count++;
70                                 }
71                         } else {
72                                 if (count > n) {
73                                         bb_putchar('\n');
74                                 }
75                                 count = 0;
76                         }
77                         offset++;
78                 } while (c != EOF);
79                 fclose_if_not_stdin(file);
80         } while (*++argv);
81
82         if (ENABLE_FEATURE_CLEAN_UP)
83                 free(string);
84
85         fflush_stdout_and_exit(status);
86 }