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