1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#define FOR_strings
32#include "toys.h"
33
34GLOBALS(
35 long n;
36 char *t;
37)
38
39static void do_strings(int fd, char *filename)
40{
41 int nread, i, wlen = TT.n, count = 0;
42 off_t offset = 0;
43 char *string = 0, pattern[8];
44
45 if (TT.t) if (!(string = strchr("oxd", *TT.t))) error_exit("-t needs oxd");
46 sprintf(pattern, "%%7ll%c ", string ? *string : 'd');
47
48
49
50 string = xzalloc(wlen+1);
51
52 for (i = nread = 0; ;i++) {
53 if (i >= nread) {
54 nread = read(fd, toybuf, sizeof(toybuf));
55 i = 0;
56 if (nread < 0) perror_msg_raw(filename);
57 if (nread < 1) {
58 if (count) goto flush;
59 break;
60 }
61 }
62
63 offset++;
64 if ((toybuf[i]>=32 && toybuf[i]<=126) || toybuf[i]=='\t') {
65 if (count == wlen) fputc(toybuf[i], stdout);
66 else {
67 string[count++] = toybuf[i];
68 if (count == wlen) {
69 if (FLAG(f)) printf("%s: ", filename);
70 if (FLAG(o) || FLAG(t)) printf(pattern, (long long)(offset - wlen));
71 printf("%s", string);
72 }
73 }
74 continue;
75 }
76flush:
77
78 if (count == wlen) xputc('\n');
79 count = 0;
80 }
81 xclose(fd);
82 free(string);
83}
84
85void strings_main(void)
86{
87 loopfiles(toys.optargs, do_strings);
88}
89