aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--miscutils/strings.c69
1 files changed, 35 insertions, 34 deletions
diff --git a/miscutils/strings.c b/miscutils/strings.c
index eff1bd947..f0ef2a2e9 100644
--- a/miscutils/strings.c
+++ b/miscutils/strings.c
@@ -19,9 +19,10 @@
19int strings_main(int argc, char **argv); 19int strings_main(int argc, char **argv);
20int strings_main(int argc, char **argv) 20int strings_main(int argc, char **argv)
21{ 21{
22 int n, c, i = 0, status = EXIT_SUCCESS; 22 int n, c, status = EXIT_SUCCESS;
23 unsigned opt; 23 unsigned opt;
24 unsigned long count; 24 unsigned count;
25 off_t offset;
25 FILE *file = stdin; 26 FILE *file = stdin;
26 char *string; 27 char *string;
27 const char *fmt = "%s: "; 28 const char *fmt = "%s: ";
@@ -29,56 +30,56 @@ int strings_main(int argc, char **argv)
29 30
30 opt = getopt32(argc, argv, "afon:", &n_arg); 31 opt = getopt32(argc, argv, "afon:", &n_arg);
31 /* -a is our default behaviour */ 32 /* -a is our default behaviour */
32 33 /*argc -= optind;*/
33 argc -= optind;
34 argv += optind; 34 argv += optind;
35 35
36 n = xatoul_range(n_arg, 1, INT_MAX); 36 n = xatou_range(n_arg, 1, INT_MAX);
37 string = xzalloc(n + 1); 37 string = xzalloc(n + 1);
38 n--; 38 n--;
39 39
40 if (argc == 0) { 40 if (!*argv) {
41 fmt = "{%s}: "; 41 fmt = "{%s}: ";
42 *argv = (char *)bb_msg_standard_input; 42 *--argv = (char *)bb_msg_standard_input;
43 goto PIPE; 43 goto PIPE;
44 } 44 }
45 45
46 do { 46 do {
47 file = fopen_or_warn(*argv, "r"); 47 file = fopen_or_warn(*argv, "r");
48 if (file) { 48 if (!file) {
49PIPE: 49 status = EXIT_FAILURE;
50 count = 0; 50 continue;
51 do { 51 }
52 c = fgetc(file); 52 PIPE:
53 if (isprint(c) || c == '\t') { 53 offset = 0;
54 if (i <= n) { 54 count = 0;
55 string[i] = c; 55 do {
56 } else { 56 c = fgetc(file);
57 putchar(c); 57 if (isprint(c) || c == '\t') {
58 } 58 if (count > n) {
59 if (i == n) { 59 putchar(c);
60 } else {
61 string[count] = c;
62 if (count == n) {
60 if (opt & PRINT_NAME) { 63 if (opt & PRINT_NAME) {
61 printf(fmt, *argv); 64 printf(fmt, *argv);
62 } 65 }
63 if (opt & PRINT_OFFSET) { 66 if (opt & PRINT_OFFSET) {
64 printf("%7lo ", count - n); 67 printf("%7"OFF_FMT"o ", offset - n);
65 } 68 }
66 printf("%s", string); 69 fputs(string, stdout);
67 } 70 }
68 i++; 71 count++;
69 } else {
70 if (i > n) {
71 putchar('\n');
72 }
73 i = 0;
74 } 72 }
75 count++; 73 } else {
76 } while (c != EOF); 74 if (count > n) {
77 fclose_if_not_stdin(file); 75 putchar('\n');
78 } else { 76 }
79 status = EXIT_FAILURE; 77 count = 0;
80 } 78 }
81 } while (--argc > 0); 79 offset++;
80 } while (c != EOF);
81 fclose_if_not_stdin(file);
82 } while (*++argv);
82 83
83 if (ENABLE_FEATURE_CLEAN_UP) 84 if (ENABLE_FEATURE_CLEAN_UP)
84 free(string); 85 free(string);