Annotation of /tags/mkinitrd-6_1_12/busybox/miscutils/strings.c
Parent Directory | Revision Log
Revision 939 -
(hide annotations)
(download)
Tue Nov 17 21:24:51 2009 UTC (14 years, 10 months ago) by niro
File MIME type: text/plain
File size: 1598 byte(s)
Tue Nov 17 21:24:51 2009 UTC (14 years, 10 months ago) by niro
File MIME type: text/plain
File size: 1598 byte(s)
tagged 'mkinitrd-6_1_12'
1 | niro | 532 | /* 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 | niro | 816 | #include "libbb.h" |
11 | niro | 532 | |
12 | #define WHOLE_FILE 1 | ||
13 | #define PRINT_NAME 2 | ||
14 | #define PRINT_OFFSET 4 | ||
15 | #define SIZE 8 | ||
16 | |||
17 | niro | 816 | int strings_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
18 | int strings_main(int argc UNUSED_PARAM, char **argv) | ||
19 | niro | 532 | { |
20 | niro | 816 | int n, c, status = EXIT_SUCCESS; |
21 | niro | 532 | unsigned opt; |
22 | niro | 816 | unsigned count; |
23 | off_t offset; | ||
24 | FILE *file; | ||
25 | niro | 532 | char *string; |
26 | const char *fmt = "%s: "; | ||
27 | niro | 816 | const char *n_arg = "4"; |
28 | niro | 532 | |
29 | niro | 816 | opt = getopt32(argv, "afon:", &n_arg); |
30 | niro | 532 | /* -a is our default behaviour */ |
31 | niro | 816 | /*argc -= optind;*/ |
32 | niro | 532 | argv += optind; |
33 | |||
34 | niro | 816 | n = xatou_range(n_arg, 1, INT_MAX); |
35 | niro | 532 | string = xzalloc(n + 1); |
36 | n--; | ||
37 | |||
38 | niro | 816 | if (!*argv) { |
39 | niro | 532 | fmt = "{%s}: "; |
40 | niro | 816 | *--argv = (char *)bb_msg_standard_input; |
41 | niro | 532 | } |
42 | |||
43 | do { | ||
44 | niro | 816 | 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 | niro | 532 | if (opt & PRINT_NAME) { |
60 | printf(fmt, *argv); | ||
61 | } | ||
62 | if (opt & PRINT_OFFSET) { | ||
63 | niro | 816 | printf("%7"OFF_FMT"o ", offset - n); |
64 | niro | 532 | } |
65 | niro | 816 | fputs(string, stdout); |
66 | niro | 532 | } |
67 | niro | 816 | count++; |
68 | niro | 532 | } |
69 | niro | 816 | } 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 | niro | 532 | |
80 | if (ENABLE_FEATURE_CLEAN_UP) | ||
81 | free(string); | ||
82 | |||
83 | fflush_stdout_and_exit(status); | ||
84 | } |