tail: Use fstat in case file is removed

This commit is contained in:
Michael Forney 2017-07-03 14:58:51 -07:00 committed by Anselm R Garbe
parent 51e432cc44
commit 1ab4ac60ff

20
tail.c
View File

@ -175,8 +175,8 @@ main(int argc, char *argv[])
} }
if (many) if (many)
printf("%s==> %s <==\n", newline ? "\n" : "", *argv); printf("%s==> %s <==\n", newline ? "\n" : "", *argv);
if (stat(*argv, &st1) < 0) if (fstat(fd, &st1) < 0)
eprintf("stat %s:", *argv); eprintf("fstat %s:", *argv);
if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode))) if (!(S_ISFIFO(st1.st_mode) || S_ISREG(st1.st_mode)))
fflag = 0; fflag = 0;
newline = 1; newline = 1;
@ -193,16 +193,14 @@ main(int argc, char *argv[])
for (;;) { for (;;) {
if (concat(fd, *argv, 1, "<stdout>") < 0) if (concat(fd, *argv, 1, "<stdout>") < 0)
exit(1); exit(1);
/* ignore error in case file was removed, we continue if (fstat(fd, &st2) < 0)
* tracking the existing open file descriptor */ eprintf("fstat %s:", *argv);
if (!stat(*argv, &st2)) { if (st2.st_size < st1.st_size) {
if (st2.st_size < st1.st_size) { fprintf(stderr, "%s: file truncated\n", *argv);
fprintf(stderr, "%s: file truncated\n", *argv); if (lseek(fd, SEEK_SET, 0) < 0)
if (lseek(fd, SEEK_SET, 0) < 0) eprintf("lseek:");
eprintf("lseek:");
}
st1 = st2;
} }
st1 = st2;
sleep(1); sleep(1);
} }
} }