sbase/tail.c

164 lines
3.2 KiB
C
Raw Normal View History

2011-05-27 10:13:38 +00:00
/* See LICENSE file for copyright and license details. */
#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>
2011-05-26 17:18:42 +00:00
#include "text.h"
#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';
static void
2015-02-09 17:46:47 +00:00
dropinit(FILE *fp, const char *str)
{
Rune r;
char *buf = NULL;
2015-02-09 14:47:08 +00:00
size_t size = 0, i = 0;
ssize_t len;
if (mode == 'n') {
while (i < num && (len = getline(&buf, &size, fp)) > 0)
if (len > 0 && buf[len - 1] == '\n')
i++;
} else {
while (i < num && (len = efgetrune(&r, fp, str)))
i++;
}
free(buf);
concat(fp, str, stdout, "<stdout>");
}
static void
2015-02-09 17:46:47 +00:00
taketail(FILE *fp, const char *str)
{
Rune *r = NULL;
char **ring = NULL;
2015-02-09 14:47:08 +00:00
size_t i, j, *size = NULL;
if (mode == 'n') {
2015-02-09 17:46:47 +00:00
ring = ecalloc(num, sizeof *ring);
size = ecalloc(num, sizeof *size);
for (i = j = 0; getline(&ring[i], &size[i], fp) > 0; )
2015-02-09 17:46:47 +00:00
i = j = (i + 1) % num;
} else {
2015-02-09 17:46:47 +00:00
r = ecalloc(num, sizeof *r);
for (i = j = 0; efgetrune(&r[i], fp, str); )
2015-02-09 17:46:47 +00:00
i = j = (i + 1) % num;
}
if (ferror(fp))
eprintf("%s: read error:", str);
do {
if (ring && ring[j]) {
fputs(ring[j], stdout);
free(ring[j]);
2015-02-09 17:46:47 +00:00
} else if (r) {
efputrune(&r[j], stdout, "<stdout>");
}
2015-02-09 17:46:47 +00:00
} while ((j = (j + 1) % num) != i);
free(ring);
free(size);
2015-02-09 17:46:47 +00:00
free(r);
}
2011-05-26 17:18:42 +00:00
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
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[])
{
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;
int ret = 0, newline = 0, many = 0;
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 {
case 'f':
fflag = 1;
break;
case 'c':
2013-06-14 18:20:47 +00:00
case 'n':
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;
ARGNUM:
num = ARGNUMF();
break;
2013-06-14 18:20:47 +00:00
default:
usage();
} ARGEND;
if (!num)
return 0;
if (!argc)
2015-02-09 17:46:47 +00:00
tail(stdin, "<stdin>");
else {
if ((many = argc > 1) && fflag)
usage();
for (newline = 0; *argv; argc--, argv++) {
if ((*argv)[0] == '-' && !(*argv)[1]) {
*argv = "<stdin>";
fp = stdin;
} else if (!(fp = fopen(*argv, "r"))) {
weprintf("fopen %s:", *argv);
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);
if (stat(*argv, &st1) < 0)
eprintf("stat %s:", *argv);
if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
fflag = 0;
2014-11-20 23:09:14 +00:00
newline = 1;
tail(fp, *argv);
if (!fflag) {
if (fp != stdin && fshut(fp, *argv))
Add *fshut() functions to properly flush file streams This has been a known issue for a long time. Example: printf "word" > /dev/full wouldn't report there's not enough space on the device. This is due to the fact that every libc has internal buffers for stdout which store fragments of written data until they reach a certain size or on some callback to flush them all at once to the kernel. You can force the libc to flush them with fflush(). In case flushing fails, you can check the return value of fflush() and report an error. However, previously, sbase didn't have such checks and without fflush(), the libc silently flushes the buffers on exit without checking the errors. No offense, but there's no way for the libc to report errors in the exit- condition. GNU coreutils solve this by having onexit-callbacks to handle the flushing and report issues, but they have obvious deficiencies. After long discussions on IRC, we came to the conclusion that checking the return value of every io-function would be a bit too much, and having a general-purpose fclose-wrapper would be the best way to go. It turned out that fclose() alone is not enough to detect errors. The right way to do it is to fflush() + check ferror on the fp and then to a fclose(). This is what fshut does and that's how it's done before each return. The return value is obviously affected, reporting an error in case a flush or close failed, but also when reading failed for some reason, the error- state is caught. the !!( ... + ...) construction is used to call all functions inside the brackets and not "terminating" on the first. We want errors to be reported, but there's no reason to stop flushing buffers when one other file buffer has issues. Obviously, functionales come before the flush and ret-logic comes after to prevent early exits as well without reporting warnings if there are any. One more advantage of fshut() is that it is even able to report errors on obscure NFS-setups which the other coreutils are unable to detect, because they only check the return-value of fflush() and fclose(), not ferror() as well.
2015-04-04 19:25:17 +00:00
ret = 1;
continue;
}
for (tmp = NULL, tmpsize = 0;;) {
while (getline(&tmp, &tmpsize, fp) > 0) {
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);
}
st1 = st2;
}
sleep(1);
}
2013-11-12 10:45:18 +00:00
}
}
Add *fshut() functions to properly flush file streams This has been a known issue for a long time. Example: printf "word" > /dev/full wouldn't report there's not enough space on the device. This is due to the fact that every libc has internal buffers for stdout which store fragments of written data until they reach a certain size or on some callback to flush them all at once to the kernel. You can force the libc to flush them with fflush(). In case flushing fails, you can check the return value of fflush() and report an error. However, previously, sbase didn't have such checks and without fflush(), the libc silently flushes the buffers on exit without checking the errors. No offense, but there's no way for the libc to report errors in the exit- condition. GNU coreutils solve this by having onexit-callbacks to handle the flushing and report issues, but they have obvious deficiencies. After long discussions on IRC, we came to the conclusion that checking the return value of every io-function would be a bit too much, and having a general-purpose fclose-wrapper would be the best way to go. It turned out that fclose() alone is not enough to detect errors. The right way to do it is to fflush() + check ferror on the fp and then to a fclose(). This is what fshut does and that's how it's done before each return. The return value is obviously affected, reporting an error in case a flush or close failed, but also when reading failed for some reason, the error- state is caught. the !!( ... + ...) construction is used to call all functions inside the brackets and not "terminating" on the first. We want errors to be reported, but there's no reason to stop flushing buffers when one other file buffer has issues. Obviously, functionales come before the flush and ret-logic comes after to prevent early exits as well without reporting warnings if there are any. One more advantage of fshut() is that it is even able to report errors on obscure NFS-setups which the other coreutils are unable to detect, because they only check the return-value of fflush() and fclose(), not ferror() as well.
2015-04-04 19:25:17 +00:00
return !!(fshut(stdin, "<stdin>") + fshut(stdout, "<stdout>")) || ret;
2011-05-26 17:18:42 +00:00
}