2013-08-14 05:41:55 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-17 08:50:10 -05:00
|
|
|
#include <limits.h>
|
2013-08-14 05:41:55 -04:00
|
|
|
#include <stdio.h>
|
2015-02-17 08:46:48 -05:00
|
|
|
#include <stdlib.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2015-02-17 11:04:36 -05:00
|
|
|
#include "utf.h"
|
2013-08-14 05:41:55 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-17 11:04:36 -05:00
|
|
|
static char *format = "";
|
|
|
|
|
2015-02-17 08:46:48 -05:00
|
|
|
static void
|
2015-02-17 11:04:36 -05:00
|
|
|
strings(FILE *fp, const char *fname, size_t len)
|
2015-02-17 08:46:48 -05:00
|
|
|
{
|
2015-03-10 16:16:21 -04:00
|
|
|
Rune r, *rbuf = NULL;
|
2015-02-17 11:04:36 -05:00
|
|
|
size_t i, bread;
|
|
|
|
off_t off;
|
|
|
|
|
2015-03-10 16:16:21 -04:00
|
|
|
rbuf = ereallocarray(rbuf, len, sizeof(*rbuf));
|
2015-02-17 08:46:48 -05:00
|
|
|
|
2015-02-17 11:04:36 -05:00
|
|
|
for (off = 0, i = 0; (bread = efgetrune(&r, fp, fname)); ) {
|
|
|
|
off += bread;
|
|
|
|
if (r == Runeerror)
|
|
|
|
continue;
|
2015-02-17 12:10:22 -05:00
|
|
|
if (!isprintrune(r)) {
|
2015-02-17 11:04:36 -05:00
|
|
|
if (i > len)
|
|
|
|
putchar('\n');
|
2015-02-17 08:46:48 -05:00
|
|
|
i = 0;
|
2015-02-17 11:04:36 -05:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-17 12:18:54 -05:00
|
|
|
if (i < len) {
|
2015-02-17 11:04:36 -05:00
|
|
|
rbuf[i++] = r;
|
2015-02-17 12:18:54 -05:00
|
|
|
continue;
|
|
|
|
} else if (i > len) {
|
2015-02-17 11:04:36 -05:00
|
|
|
efputrune(&r, stdout, "<stdout>");
|
|
|
|
continue;
|
2015-02-17 08:46:48 -05:00
|
|
|
}
|
2015-02-17 11:04:36 -05:00
|
|
|
printf(format, (long)off - i);
|
2015-02-17 12:10:22 -05:00
|
|
|
for (i = 0; i < len; i++)
|
2015-02-17 11:04:36 -05:00
|
|
|
efputrune(rbuf + i, stdout, "<stdout>");
|
2015-02-17 12:18:54 -05:00
|
|
|
efputrune(&r, stdout, "<stdout>");
|
2015-02-17 11:04:36 -05:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
free(rbuf);
|
2015-02-17 08:46:48 -05:00
|
|
|
}
|
2013-08-14 05:41:55 -04:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-17 11:04:36 -05:00
|
|
|
eprintf("usage: %s [-a] [-n num] [-t format] [file ...]\n", argv0);
|
2013-08-14 05:41:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-02-17 11:04:36 -05:00
|
|
|
size_t len = 4;
|
2014-11-23 07:44:07 -05:00
|
|
|
int ret = 0;
|
2015-02-17 11:04:36 -05:00
|
|
|
char f;
|
2013-08-14 05:41:55 -04:00
|
|
|
|
|
|
|
ARGBEGIN {
|
2015-02-17 08:39:17 -05:00
|
|
|
case 'a':
|
|
|
|
break;
|
2015-02-17 08:46:48 -05:00
|
|
|
case 'n':
|
2015-02-17 11:04:36 -05:00
|
|
|
len = 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();
|
2015-02-17 08:46:48 -05:00
|
|
|
break;
|
2013-08-14 05:41:55 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2013-10-10 11:04:15 -04:00
|
|
|
if (argc == 0) {
|
2015-02-17 08:46:48 -05:00
|
|
|
strings(stdin, "<stdin>", len);
|
2013-10-10 11:04:15 -04:00
|
|
|
} else {
|
|
|
|
for (; argc > 0; argc--, argv++) {
|
2013-11-13 06:39:24 -05:00
|
|
|
if (!(fp = fopen(argv[0], "r"))) {
|
|
|
|
weprintf("fopen %s:", argv[0]);
|
2014-11-23 07:44:07 -05:00
|
|
|
ret = 1;
|
2013-11-13 06:39:24 -05:00
|
|
|
continue;
|
|
|
|
}
|
2015-02-17 08:46:48 -05:00
|
|
|
strings(fp, argv[0], len);
|
2013-10-10 11:04:15 -04:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2013-08-14 05:41:55 -04:00
|
|
|
}
|
2014-11-23 07:44:07 -05:00
|
|
|
return ret;
|
2013-08-14 05:41:55 -04:00
|
|
|
}
|