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>
|
|
|
|
|
2017-07-03 21:58:49 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.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
|
|
|
|
2015-02-09 17:38:41 +00:00
|
|
|
#include "utf.h"
|
2011-05-26 17:18:42 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-05-20 15:49:56 +00:00
|
|
|
static char mode = 'n';
|
2015-02-09 14:06:17 +00:00
|
|
|
|
2017-07-03 21:58:49 +00:00
|
|
|
static int
|
|
|
|
dropinit(int fd, const char *fname, size_t count)
|
2015-02-09 14:06:17 +00:00
|
|
|
{
|
2015-02-09 17:38:41 +00:00
|
|
|
Rune r;
|
2017-07-03 21:58:49 +00: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 21:58:52 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 'n': /* lines */
|
2017-07-03 21:58:49 +00:00
|
|
|
for (p = buf; count && n > 0; p++, n--) {
|
|
|
|
if (*p == '\n')
|
|
|
|
count--;
|
|
|
|
}
|
2017-07-03 21:58:52 +00: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 21:58:49 +00: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 21:58:52 +00:00
|
|
|
break;
|
2017-07-03 21:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count) {
|
|
|
|
if (n < 0)
|
|
|
|
weprintf("read %s:", fname);
|
|
|
|
if (n <= 0)
|
|
|
|
return n;
|
|
|
|
}
|
2015-02-09 14:06:17 +00:00
|
|
|
|
2017-07-03 21:58:49 +00: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 17:38:41 +00:00
|
|
|
}
|
2015-02-09 14:06:17 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 21:58:49 +00:00
|
|
|
static int
|
|
|
|
taketail(int fd, const char *fname, size_t count)
|
2015-02-09 14:06:17 +00:00
|
|
|
{
|
2017-07-03 21:58:49 +00: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 09:20:40 +00:00
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
n = read(fd, buf + len, size - len);
|
|
|
|
if (n < 0) {
|
|
|
|
weprintf("read %s:", fname);
|
|
|
|
return -1;
|
2015-02-09 17:38:41 +00:00
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
len += n;
|
2017-07-03 21:58:52 +00:00
|
|
|
switch (mode) {
|
|
|
|
case 'n': /* lines */
|
2017-07-03 21:58:49 +00: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 21:58:52 +00:00
|
|
|
break;
|
|
|
|
case 'c': /* bytes */
|
|
|
|
p = count < len ? buf + len - count : buf;
|
|
|
|
break;
|
|
|
|
case 'm': /* runes */
|
2017-07-03 21:58:49 +00: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 21:58:52 +00:00
|
|
|
break;
|
2017-07-03 21:58:49 +00:00
|
|
|
}
|
|
|
|
if (p > buf) {
|
|
|
|
len -= p - buf;
|
|
|
|
memmove(buf, p, len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (writeall(1, buf, len) < 0)
|
|
|
|
eprintf("write:");
|
|
|
|
return 0;
|
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)
|
|
|
|
{
|
2017-07-03 21:58:52 +00:00
|
|
|
eprintf("usage: %s [-f] [-c num | -m 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;
|
2017-07-03 21:58:49 +00:00
|
|
|
int fd;
|
|
|
|
size_t n = 10;
|
2015-05-20 15:49:56 +00:00
|
|
|
int fflag = 0, ret = 0, newline = 0, many = 0;
|
2017-07-03 21:58:49 +00:00
|
|
|
char *numstr;
|
|
|
|
int (*tail)(int, const char *, size_t) = 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':
|
2017-07-03 21:58:52 +00:00
|
|
|
case 'm':
|
2013-06-14 18:20:47 +00:00
|
|
|
case 'n':
|
2015-02-09 17:38:41 +00:00
|
|
|
mode = ARGC();
|
|
|
|
numstr = EARGF(usage());
|
2016-03-07 09:20:40 +00:00
|
|
|
n = MIN(llabs(estrtonum(numstr, LLONG_MIN + 1,
|
|
|
|
MIN(LLONG_MAX, SIZE_MAX))), SIZE_MAX);
|
2015-02-09 17:38:41 +00:00
|
|
|
if (strchr(numstr, '+'))
|
2013-06-14 18:20:47 +00:00
|
|
|
tail = dropinit;
|
|
|
|
break;
|
2013-11-11 19:53:01 +00:00
|
|
|
ARGNUM:
|
2015-05-20 15:49:56 +00:00
|
|
|
n = ARGNUMF();
|
2013-11-11 19:53:01 +00:00
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2015-02-09 14:06:17 +00:00
|
|
|
|
2017-07-03 21:58:49 +00:00
|
|
|
if (!argc) {
|
|
|
|
if (tail(0, "<stdin>", n) < 0)
|
|
|
|
ret = 1;
|
|
|
|
} else {
|
2015-02-09 14:06:17 +00:00
|
|
|
if ((many = argc > 1) && fflag)
|
|
|
|
usage();
|
2015-03-17 22:24:43 +00:00
|
|
|
for (newline = 0; *argv; argc--, argv++) {
|
2015-05-19 15:44:15 +00:00
|
|
|
if (!strcmp(*argv, "-")) {
|
2015-05-15 11:28:39 +00:00
|
|
|
*argv = "<stdin>";
|
2017-07-03 21:58:49 +00:00
|
|
|
fd = 0;
|
|
|
|
} else if ((fd = open(*argv, O_RDONLY)) < 0) {
|
|
|
|
weprintf("open %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);
|
2017-07-03 21:58:51 +00:00
|
|
|
if (fstat(fd, &st1) < 0)
|
|
|
|
eprintf("fstat %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;
|
2017-07-03 21:58:49 +00:00
|
|
|
if (tail(fd, *argv, n) < 0) {
|
|
|
|
ret = 1;
|
|
|
|
fflag = 0;
|
|
|
|
}
|
2015-03-17 22:24:43 +00:00
|
|
|
|
|
|
|
if (!fflag) {
|
2017-07-03 21:58:49 +00:00
|
|
|
if (fd != 0)
|
|
|
|
close(fd);
|
2015-03-17 22:24:43 +00:00
|
|
|
continue;
|
|
|
|
}
|
2017-07-03 21:58:49 +00:00
|
|
|
for (;;) {
|
|
|
|
if (concat(fd, *argv, 1, "<stdout>") < 0)
|
|
|
|
exit(1);
|
2017-07-03 21:58:51 +00: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 14:06:17 +00:00
|
|
|
}
|
2017-07-03 21:58:51 +00:00
|
|
|
st1 = st2;
|
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
|
|
|
|
2015-05-24 23:33:19 +00:00
|
|
|
return ret;
|
2011-05-26 17:18:42 +00:00
|
|
|
}
|