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