2011-05-27 06:13:38 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-01-30 10:52:44 -05:00
|
|
|
#include <limits.h>
|
2011-05-26 13:18:42 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-05-26 13:18:42 -04:00
|
|
|
#include "text.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-09 09:06:17 -05:00
|
|
|
static int fflag = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
dropinit(FILE *fp, const char *str, long n)
|
|
|
|
{
|
|
|
|
char *buf = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
ssize_t len;
|
|
|
|
unsigned long i = 0;
|
|
|
|
|
|
|
|
while (i < n && (len = getline(&buf, &size, fp)) != -1)
|
|
|
|
if (len > 0 && buf[len - 1] == '\n')
|
|
|
|
i++;
|
|
|
|
free(buf);
|
|
|
|
concat(fp, str, stdout, "<stdout>");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
taketail(FILE *fp, const char *str, long n)
|
|
|
|
{
|
|
|
|
char **ring = NULL;
|
|
|
|
long i, j;
|
|
|
|
size_t *size = NULL;
|
|
|
|
|
|
|
|
ring = ecalloc(n, sizeof *ring);
|
|
|
|
size = ecalloc(n, sizeof *size);
|
|
|
|
|
|
|
|
for (i = j = 0; getline(&ring[i], &size[i], fp) != -1; i = j = (i + 1) % n)
|
|
|
|
;
|
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("%s: read error:", str);
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (ring[j]) {
|
|
|
|
fputs(ring[j], stdout);
|
|
|
|
free(ring[j]);
|
|
|
|
}
|
|
|
|
} while ((j = (j + 1) % n) != i);
|
|
|
|
|
|
|
|
free(ring);
|
|
|
|
free(size);
|
|
|
|
}
|
2011-05-26 13:18:42 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-09 09:06:17 -05:00
|
|
|
eprintf("usage: %s [-f] [-n lines] [file ...]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-05-26 13:18:42 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-02-09 09:06:17 -05:00
|
|
|
size_t n = 10, tmpsize;
|
|
|
|
int ret = 0, newline, many;
|
|
|
|
char *lines, *tmp;
|
2011-05-26 13:29:22 -04:00
|
|
|
void (*tail)(FILE *, const char *, long) = taketail;
|
2011-05-26 13:18:42 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
ARGBEGIN {
|
2015-02-09 09:06:17 -05:00
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
case 'n':
|
2013-08-31 16:53:11 -04:00
|
|
|
lines = EARGF(usage());
|
2015-01-30 10:52:44 -05:00
|
|
|
n = estrtonum(lines, 0, LONG_MAX);
|
2014-11-13 12:29:30 -05:00
|
|
|
if (lines[0] == '+')
|
2013-06-14 14:20:47 -04:00
|
|
|
tail = dropinit;
|
|
|
|
break;
|
2013-11-11 14:53:01 -05:00
|
|
|
ARGNUM:
|
2015-01-30 11:45:44 -05:00
|
|
|
n = ARGNUMF();
|
2013-11-11 14:53:01 -05:00
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
2015-02-09 09:06:17 -05:00
|
|
|
|
|
|
|
if (argc == 0)
|
2011-05-26 13:18:42 -04:00
|
|
|
tail(stdin, "<stdin>", n);
|
2015-02-09 09:06:17 -05:00
|
|
|
else {
|
|
|
|
if ((many = argc > 1) && fflag)
|
|
|
|
usage();
|
2014-11-20 18:09:14 -05:00
|
|
|
for (newline = 0; argc > 0; argc--, argv++) {
|
2014-11-13 12:29:30 -05:00
|
|
|
if (!(fp = fopen(argv[0], "r"))) {
|
2013-11-13 06:39:24 -05:00
|
|
|
weprintf("fopen %s:", argv[0]);
|
2014-11-20 17:51:34 -05:00
|
|
|
ret = 1;
|
2013-11-12 05:45:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
2014-11-20 18:09:14 -05:00
|
|
|
if (many)
|
|
|
|
printf("%s==> %s <==\n",
|
|
|
|
newline ? "\n" : "", argv[0]);
|
|
|
|
newline = 1;
|
2013-11-12 05:45:18 -05:00
|
|
|
tail(fp, argv[0], n);
|
2015-02-09 09:06:17 -05:00
|
|
|
|
|
|
|
if (fflag && argc == 1) {
|
|
|
|
for(;; tmp = NULL, tmpsize = 0) {
|
|
|
|
while (getline(&tmp, &tmpsize, fp) != -1) {
|
|
|
|
fputs(tmp, stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
free(tmp);
|
|
|
|
tmp = NULL;
|
|
|
|
tmpsize = 0;
|
|
|
|
}
|
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("readline '%s':", argv[0]);
|
|
|
|
clearerr(fp);
|
|
|
|
}
|
|
|
|
}
|
2013-11-12 05:45:18 -05:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
}
|
2014-11-20 17:51:34 -05:00
|
|
|
return ret;
|
2011-05-26 13:18:42 -04:00
|
|
|
}
|