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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/* See LICENSE file for copyright and license details. */
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utf.h"
#include "util.h"
static char *format = "";
static void
strings(FILE *fp, const char *fname, size_t min)
{
Rune r, *rbuf;
size_t i, bread;
off_t off;
rbuf = ereallocarray(NULL, min, sizeof(*rbuf));
for (off = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) {
off += bread;
if (r == Runeerror)
continue;
if (!isprintrune(r)) {
if (i == min)
putchar('\n');
i = 0;
continue;
}
if (i == min) {
efputrune(&r, stdout, "<stdout>");
continue;
}
rbuf[i++] = r;
if (i < min)
continue;
printf(format, (long)off - i);
for (i = 0; i < min; i++)
efputrune(rbuf + i, stdout, "<stdout>");
}
free(rbuf);
}
static void
usage(void)
{
eprintf("usage: %s [-a] [-n num] [-t format] [file ...]\n", argv0);
}
int
main(int argc, char *argv[])
{
FILE *fp;
size_t min = 4;
int ret = 0;
char f;
ARGBEGIN {
case 'a':
break;
case 'n':
min = estrtonum(EARGF(usage()), 1, LLONG_MAX);
break;
case 't':
format = estrdup("%8l#: ");
f = *EARGF(usage());
if (f == 'd' || f == 'o' || f == 'x')
format[3] = f;
else
usage();
break;
default:
usage();
} ARGEND
if (!argc) {
strings(stdin, "<stdin>", min);
} else {
for (; *argv; argc--, argv++) {
if (!strcmp(*argv, "-")) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv);
ret = 1;
continue;
}
strings(fp, *argv, min);
if (fp != stdin && fshut(fp, *argv))
ret = 1;
}
}
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
return ret;
}
|