2011-05-27 06:13:38 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-09 09:41:49 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2017-07-03 17:58:49 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2015-02-09 09:30:23 -05:00
|
|
|
#include <stdint.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
|
|
|
|
2015-02-09 12:38:41 -05:00
|
|
|
#include "utf.h"
|
2011-05-26 13:18:42 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-05-20 11:49:56 -04:00
|
|
|
static char mode = 'n';
|
2015-02-09 09:06:17 -05:00
|
|
|
|
2017-07-03 17:58:49 -04:00
|
|
|
static int
|
|
|
|
dropinit(int fd, const char *fname, size_t count)
|
2015-02-09 09:06:17 -05:00
|
|
|
{
|
2015-02-09 12:38:41 -05:00
|
|
|
Rune r;
|
2017-07-03 17:58:49 -04:00
|
|
|
char buf[BUFSIZ], *p;
|
|
|
|
ssize_t n;
|
|
|
|
int nr;
|
|
|
|
|
|
|
|
if (count < 2)
|
|
|
|
goto copy;
|
|
|
|
count--; /* numbering starts at 1 */
|
|
|
|
while (count && (n = read(fd, buf, sizeof(buf))) > 0) {
|
2017-07-03 17:58:52 -04:00
|
|
|
switch (mode) {
|
|
|
|
case 'n': /* lines */
|
2017-07-03 17:58:49 -04:00
|
|
|
for (p = buf; count && n > 0; p++, n--) {
|
|
|
|
if (*p == '\n')
|
|
|
|
count--;
|
|
|
|
}
|
2017-07-03 17:58:52 -04:00
|
|
|
break;
|
|
|
|
case 'c': /* bytes */
|
|
|
|
if (count > n) {
|
|
|
|
count -= n;
|
|
|
|
} else {
|
|
|
|
p = buf + count;
|
|
|
|
n -= count;
|
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm': /* runes */
|
2017-07-03 17:58:49 -04:00
|
|
|
for (p = buf; count && n > 0; p += nr, n -= nr, count--) {
|
|
|
|
nr = charntorune(&r, p, n);
|
|
|
|
if (!nr) {
|
|
|
|
/* we don't have a full rune, move
|
|
|
|
* remaining data to beginning and read
|
|
|
|
* again */
|
|
|
|
memmove(buf, p, n);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 17:58:52 -04:00
|
|
|
break;
|
2017-07-03 17:58:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count) {
|
|
|
|
if (n < 0)
|
|
|
|
weprintf("read %s:", fname);
|
|
|
|
if (n <= 0)
|
|
|
|
return n;
|
|
|
|
}
|
2015-02-09 09:06:17 -05:00
|
|
|
|
2017-07-03 17:58:49 -04:00
|
|
|
/* write the rest of the buffer */
|
|
|
|
if (writeall(1, p, n) < 0)
|
|
|
|
eprintf("write:");
|
|
|
|
copy:
|
|
|
|
switch (concat(fd, fname, 1, "<stdout>")) {
|
|
|
|
case -1: /* read error */
|
|
|
|
return -1;
|
|
|
|
case -2: /* write error */
|
|
|
|
exit(1);
|
|
|
|
default:
|
|
|
|
return 0;
|
2015-02-09 12:38:41 -05:00
|
|
|
}
|
2015-02-09 09:06:17 -05:00
|
|
|
}
|
|
|
|
|
2017-07-03 17:58:49 -04:00
|
|
|
static int
|
|
|
|
taketail(int fd, const char *fname, size_t count)
|
2015-02-09 09:06:17 -05:00
|
|
|
{
|
2017-07-03 17:58:49 -04:00
|
|
|
static char *buf = NULL;
|
|
|
|
static size_t size = 0;
|
|
|
|
char *p;
|
|
|
|
size_t len = 0, left;
|
|
|
|
ssize_t n;
|
|
|
|
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
for (;;) {
|
|
|
|
if (len + BUFSIZ > size) {
|
|
|
|
/* make sure we have at least BUFSIZ to read */
|
|
|
|
size += 2 * BUFSIZ;
|
|
|
|
buf = erealloc(buf, size);
|
2016-03-07 04:20:40 -05:00
|
|
|
}
|
2017-07-03 17:58:49 -04:00
|
|
|
n = read(fd, buf + len, size - len);
|
|
|
|
if (n < 0) {
|
|
|
|
weprintf("read %s:", fname);
|
|
|
|
return -1;
|
2015-02-09 12:38:41 -05:00
|
|
|
}
|
2017-07-03 17:58:49 -04:00
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
len += n;
|
2017-07-03 17:58:52 -04:00
|
|
|
switch (mode) {
|
|
|
|
case 'n': /* lines */
|
2017-07-03 17:58:49 -04:00
|
|
|
/* ignore the last character; if it is a newline, it
|
|
|
|
* ends the last line */
|
|
|
|
for (p = buf + len - 2, left = count; p >= buf; p--) {
|
|
|
|
if (*p != '\n')
|
|
|
|
continue;
|
|
|
|
left--;
|
|
|
|
if (!left) {
|
|
|
|
p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-07-03 17:58:52 -04:00
|
|
|
break;
|
|
|
|
case 'c': /* bytes */
|
|
|
|
p = count < len ? buf + len - count : buf;
|
|
|
|
break;
|
|
|
|
case 'm': /* runes */
|
2017-07-03 17:58:49 -04:00
|
|
|
for (p = buf + len - 1, left = count; p >= buf; p--) {
|
|
|
|
/* skip utf-8 continuation bytes */
|
|
|
|
if ((*p & 0xc0) == 0x80)
|
|
|
|
continue;
|
|
|
|
left--;
|
|
|
|
if (!left)
|
|
|
|
break;
|
|
|
|
}
|
2017-07-03 17:58:52 -04:00
|
|
|
break;
|
2017-07-03 17:58:49 -04:00
|
|
|
}
|
|
|
|
if (p > buf) {
|
|
|
|
len -= p - buf;
|
|
|
|
memmove(buf, p, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (writeall(1, buf, len) < 0)
|
|
|
|
eprintf("write:");
|
|
|
|
return 0;
|
2015-02-09 09:06:17 -05:00
|
|
|
}
|
2011-05-26 13:18:42 -04:00
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2017-07-03 17:58:52 -04:00
|
|
|
eprintf("usage: %s [-f] [-c num | -m num | -n num | -num] [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[])
|
|
|
|
{
|
2015-02-09 09:41:49 -05:00
|
|
|
struct stat st1, st2;
|
2017-07-03 17:58:49 -04:00
|
|
|
int fd;
|
|
|
|
size_t n = 10;
|
2015-05-20 11:49:56 -04:00
|
|
|
int fflag = 0, ret = 0, newline = 0, many = 0;
|
2017-07-03 17:58:49 -04:00
|
|
|
char *numstr;
|
|
|
|
int (*tail)(int, const char *, size_t) = 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;
|
2015-02-09 12:38:41 -05:00
|
|
|
case 'c':
|
2017-07-03 17:58:52 -04:00
|
|
|
case 'm':
|
2013-06-14 14:20:47 -04:00
|
|
|
case 'n':
|
2015-02-09 12:38:41 -05:00
|
|
|
mode = ARGC();
|
|
|
|
numstr = EARGF(usage());
|
2016-03-07 04:20:40 -05:00
|
|
|
n = MIN(llabs(estrtonum(numstr, LLONG_MIN + 1,
|
|
|
|
MIN(LLONG_MAX, SIZE_MAX))), SIZE_MAX);
|
2015-02-09 12:38:41 -05:00
|
|
|
if (strchr(numstr, '+'))
|
2013-06-14 14:20:47 -04:00
|
|
|
tail = dropinit;
|
|
|
|
break;
|
2013-11-11 14:53:01 -05:00
|
|
|
ARGNUM:
|
2015-05-20 11:49:56 -04:00
|
|
|
n = ARGNUMF();
|
2013-11-11 14:53:01 -05:00
|
|
|
break;
|
2013-06-14 14:20:47 -04:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 05:16:49 -05:00
|
|
|
} ARGEND
|
2015-02-09 09:06:17 -05:00
|
|
|
|
2017-07-03 17:58:49 -04:00
|
|
|
if (!argc) {
|
|
|
|
if (tail(0, "<stdin>", n) < 0)
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
2015-02-09 09:06:17 -05:00
|
|
|
if ((many = argc > 1) && fflag)
|
|
|
|
usage();
|
2015-03-17 18:24:43 -04:00
|
|
|
for (newline = 0; *argv; argc--, argv++) {
|
2015-05-19 11:44:15 -04:00
|
|
|
if (!strcmp(*argv, "-")) {
|
2015-05-15 07:28:39 -04:00
|
|
|
*argv = "<stdin>";
|
2017-07-03 17:58:49 -04:00
|
|
|
fd = 0;
|
|
|
|
} else if ((fd = open(*argv, O_RDONLY)) < 0) {
|
|
|
|
weprintf("open %s:", *argv);
|
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)
|
2015-03-17 18:24:43 -04:00
|
|
|
printf("%s==> %s <==\n", newline ? "\n" : "", *argv);
|
2017-07-03 17:58:51 -04:00
|
|
|
if (fstat(fd, &st1) < 0)
|
|
|
|
eprintf("fstat %s:", *argv);
|
2015-02-09 10:03:35 -05:00
|
|
|
if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
|
|
|
|
fflag = 0;
|
2014-11-20 18:09:14 -05:00
|
|
|
newline = 1;
|
2017-07-03 17:58:49 -04:00
|
|
|
if (tail(fd, *argv, n) < 0) {
|
|
|
|
ret = 1;
|
|
|
|
fflag = 0;
|
|
|
|
}
|
2015-03-17 18:24:43 -04:00
|
|
|
|
|
|
|
if (!fflag) {
|
2017-07-03 17:58:49 -04:00
|
|
|
if (fd != 0)
|
|
|
|
close(fd);
|
2015-03-17 18:24:43 -04:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-03 17:58:49 -04:00
|
|
|
for (;;) {
|
|
|
|
if (concat(fd, *argv, 1, "<stdout>") < 0)
|
|
|
|
exit(1);
|
2017-07-03 17:58:51 -04:00
|
|
|
if (fstat(fd, &st2) < 0)
|
|
|
|
eprintf("fstat %s:", *argv);
|
|
|
|
if (st2.st_size < st1.st_size) {
|
|
|
|
fprintf(stderr, "%s: file truncated\n", *argv);
|
|
|
|
if (lseek(fd, SEEK_SET, 0) < 0)
|
|
|
|
eprintf("lseek:");
|
2015-02-09 09:06:17 -05:00
|
|
|
}
|
2017-07-03 17:58:51 -04:00
|
|
|
st1 = st2;
|
2015-03-17 18:24:43 -04:00
|
|
|
sleep(1);
|
2015-02-09 09:06:17 -05:00
|
|
|
}
|
2013-11-12 05:45:18 -05:00
|
|
|
}
|
|
|
|
}
|
2015-03-17 18:24:43 -04:00
|
|
|
|
2015-05-24 19:33:19 -04:00
|
|
|
return ret;
|
2011-05-26 13:18:42 -04:00
|
|
|
}
|