2011-05-27 10:13:38 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-09 14:41:49 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2015-02-09 14:30:23 +00:00
|
|
|
#include <stdint.h>
|
2011-05-26 17:18:42 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-05-26 17:18:42 +00:00
|
|
|
#include "text.h"
|
2015-02-09 17:38:41 +00:00
|
|
|
#include "utf.h"
|
2011-05-26 17:18:42 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-09 17:46:47 +00:00
|
|
|
static int fflag = 0;
|
|
|
|
static size_t num = 10;
|
|
|
|
static char mode = 'n';
|
2015-02-09 14:06:17 +00:00
|
|
|
|
|
|
|
static void
|
2015-02-09 17:46:47 +00:00
|
|
|
dropinit(FILE *fp, const char *str)
|
2015-02-09 14:06:17 +00:00
|
|
|
{
|
2015-02-09 17:38:41 +00:00
|
|
|
Rune r;
|
2015-02-09 14:06:17 +00:00
|
|
|
char *buf = NULL;
|
2015-02-09 14:47:08 +00:00
|
|
|
size_t size = 0, i = 0;
|
2015-02-09 14:06:17 +00:00
|
|
|
ssize_t len;
|
|
|
|
|
2015-02-09 17:38:41 +00:00
|
|
|
if (mode == 'n') {
|
2015-03-27 13:49:48 +00:00
|
|
|
while (i < num && (len = getline(&buf, &size, fp)) > 0)
|
2015-02-09 17:38:41 +00:00
|
|
|
if (len > 0 && buf[len - 1] == '\n')
|
|
|
|
i++;
|
|
|
|
} else {
|
2015-02-11 19:13:43 +00:00
|
|
|
while (i < num && (len = efgetrune(&r, fp, str)))
|
2015-02-09 14:06:17 +00:00
|
|
|
i++;
|
2015-02-09 17:38:41 +00:00
|
|
|
}
|
2015-02-09 14:06:17 +00:00
|
|
|
free(buf);
|
|
|
|
concat(fp, str, stdout, "<stdout>");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-02-09 17:46:47 +00:00
|
|
|
taketail(FILE *fp, const char *str)
|
2015-02-09 14:06:17 +00:00
|
|
|
{
|
2015-02-09 17:38:41 +00:00
|
|
|
Rune *r = NULL;
|
2015-02-09 14:06:17 +00:00
|
|
|
char **ring = NULL;
|
2015-02-09 14:47:08 +00:00
|
|
|
size_t i, j, *size = NULL;
|
2015-02-09 14:06:17 +00:00
|
|
|
|
2015-02-09 17:38:41 +00:00
|
|
|
if (mode == 'n') {
|
2015-02-09 17:46:47 +00:00
|
|
|
ring = ecalloc(num, sizeof *ring);
|
|
|
|
size = ecalloc(num, sizeof *size);
|
2015-02-09 17:38:41 +00:00
|
|
|
|
2015-03-27 13:49:48 +00:00
|
|
|
for (i = j = 0; getline(&ring[i], &size[i], fp) > 0; )
|
2015-02-09 17:46:47 +00:00
|
|
|
i = j = (i + 1) % num;
|
2015-02-09 17:38:41 +00:00
|
|
|
} else {
|
2015-02-09 17:46:47 +00:00
|
|
|
r = ecalloc(num, sizeof *r);
|
2015-02-09 14:06:17 +00:00
|
|
|
|
2015-02-11 19:13:43 +00:00
|
|
|
for (i = j = 0; efgetrune(&r[i], fp, str); )
|
2015-02-09 17:46:47 +00:00
|
|
|
i = j = (i + 1) % num;
|
2015-02-09 17:38:41 +00:00
|
|
|
}
|
2015-02-09 14:06:17 +00:00
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("%s: read error:", str);
|
|
|
|
|
|
|
|
do {
|
2015-02-09 17:38:41 +00:00
|
|
|
if (ring && ring[j]) {
|
2015-02-09 14:06:17 +00:00
|
|
|
fputs(ring[j], stdout);
|
|
|
|
free(ring[j]);
|
2015-02-09 17:46:47 +00:00
|
|
|
} else if (r) {
|
2015-02-11 19:58:00 +00:00
|
|
|
efputrune(&r[j], stdout, "<stdout>");
|
2015-02-09 17:38:41 +00:00
|
|
|
}
|
2015-02-09 17:46:47 +00:00
|
|
|
} while ((j = (j + 1) % num) != i);
|
2015-02-09 14:06:17 +00:00
|
|
|
|
|
|
|
free(ring);
|
|
|
|
free(size);
|
2015-02-09 17:46:47 +00:00
|
|
|
free(r);
|
2015-02-09 14:06:17 +00:00
|
|
|
}
|
2011-05-26 17:18:42 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-05 08:13:42 +00:00
|
|
|
eprintf("usage: %s [-f] [-c num | -n num | -num] [file ...]\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-05-26 17:18:42 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-02-09 14:41:49 +00:00
|
|
|
struct stat st1, st2;
|
2011-05-26 17:18:42 +00:00
|
|
|
FILE *fp;
|
2015-02-09 17:46:47 +00:00
|
|
|
size_t tmpsize;
|
2015-02-09 14:06:17 +00:00
|
|
|
int ret = 0, newline, many;
|
2015-02-09 17:46:47 +00:00
|
|
|
char *numstr, *tmp;
|
|
|
|
void (*tail)(FILE *, const char *) = taketail;
|
2011-05-26 17:18:42 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
2015-02-09 14:06:17 +00:00
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
2015-02-09 17:38:41 +00:00
|
|
|
case 'c':
|
2013-06-14 18:20:47 +00:00
|
|
|
case 'n':
|
2015-02-09 17:38:41 +00:00
|
|
|
mode = ARGC();
|
|
|
|
numstr = EARGF(usage());
|
|
|
|
num = MIN(llabs(estrtonum(numstr, LLONG_MIN + 1, MIN(LLONG_MAX, SIZE_MAX))), SIZE_MAX);
|
|
|
|
if (strchr(numstr, '+'))
|
2013-06-14 18:20:47 +00:00
|
|
|
tail = dropinit;
|
|
|
|
break;
|
2013-11-11 19:53:01 +00:00
|
|
|
ARGNUM:
|
2015-02-09 17:38:41 +00:00
|
|
|
num = ARGNUMF();
|
2013-11-11 19:53:01 +00:00
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2015-02-09 14:06:17 +00:00
|
|
|
|
2015-03-17 22:24:43 +00:00
|
|
|
if (!argc)
|
2015-02-09 17:46:47 +00:00
|
|
|
tail(stdin, "<stdin>");
|
2015-02-09 14:06:17 +00:00
|
|
|
else {
|
|
|
|
if ((many = argc > 1) && fflag)
|
|
|
|
usage();
|
2015-03-17 22:24:43 +00:00
|
|
|
for (newline = 0; *argv; argc--, argv++) {
|
|
|
|
if (!(fp = fopen(*argv, "r"))) {
|
|
|
|
weprintf("fopen %s:", *argv);
|
2014-11-20 22:51:34 +00:00
|
|
|
ret = 1;
|
2013-11-12 10:45:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-20 23:09:14 +00:00
|
|
|
if (many)
|
2015-03-17 22:24:43 +00:00
|
|
|
printf("%s==> %s <==\n", newline ? "\n" : "", *argv);
|
|
|
|
if (stat(*argv, &st1) < 0)
|
|
|
|
eprintf("stat %s:", *argv);
|
2015-02-09 15:03:35 +00:00
|
|
|
if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
|
|
|
|
fflag = 0;
|
2014-11-20 23:09:14 +00:00
|
|
|
newline = 1;
|
2015-03-17 22:24:43 +00:00
|
|
|
tail(fp, *argv);
|
|
|
|
|
|
|
|
if (!fflag) {
|
|
|
|
fclose(fp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (tmp = NULL, tmpsize = 0;;) {
|
2015-03-27 13:49:48 +00:00
|
|
|
while (getline(&tmp, &tmpsize, fp) > 0) {
|
2015-03-17 22:24:43 +00:00
|
|
|
fputs(tmp, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("readline %s:", *argv);
|
|
|
|
clearerr(fp);
|
|
|
|
/* ignore error in case file was removed, we continue
|
|
|
|
* tracking the existing open file descriptor */
|
|
|
|
if (!stat(*argv, &st2)) {
|
|
|
|
if (st2.st_size < st1.st_size) {
|
|
|
|
fprintf(stderr, "%s: file truncated\n", *argv);
|
|
|
|
rewind(fp);
|
2015-02-09 14:41:49 +00:00
|
|
|
}
|
2015-03-17 22:24:43 +00:00
|
|
|
st1 = st2;
|
2015-02-09 14:06:17 +00:00
|
|
|
}
|
2015-03-17 22:24:43 +00:00
|
|
|
sleep(1);
|
2015-02-09 14:06:17 +00:00
|
|
|
}
|
2013-11-12 10:45:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 22:24:43 +00:00
|
|
|
|
2014-11-20 22:51:34 +00:00
|
|
|
return ret;
|
2011-05-26 17:18:42 +00:00
|
|
|
}
|