aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/lib.c8
-rw-r--r--lib/lib.h3
-rwxr-xr-xtests/tail.test6
-rw-r--r--toys/posix/tail.c138
4 files changed, 75 insertions, 80 deletions
diff --git a/lib/lib.c b/lib/lib.c
index 9f9b8136..49fd5dd2 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -713,9 +713,9 @@ void poke(void *ptr, long long val, unsigned size)
void loopfiles_rw(char **argv, int flags, int permissions,
void (*function)(int fd, char *name))
{
- int fd, failok = !(flags&WARN_ONLY);
+ int fd, failok = !(flags&WARN_ONLY), anyway = flags & LOOPFILES_ANYWAY;
- flags &= ~WARN_ONLY;
+ flags &= ~(WARN_ONLY|LOOPFILES_ANYWAY);
// If no arguments, read from stdin.
if (!*argv) function((flags & O_ACCMODE) != O_RDONLY ? 1 : 0, "-");
@@ -726,10 +726,10 @@ void loopfiles_rw(char **argv, int flags, int permissions,
if (!strcmp(*argv, "-")) fd = 0;
else if (0>(fd = notstdio(open(*argv, flags, permissions))) && !failok) {
perror_msg_raw(*argv);
- continue;
+ if (!anyway) continue;
}
function(fd, *argv);
- if ((flags & O_CLOEXEC) && fd) close(fd);
+ if ((flags & O_CLOEXEC) && fd>0) close(fd);
} while (*++argv);
}
diff --git a/lib/lib.h b/lib/lib.h
index cf1920f9..7484eb8f 100644
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -112,7 +112,8 @@ void show_help(FILE *out, int full);
// Tell xopen and friends to print warnings but return -1 as necessary
// The largest O_BLAH flag so far is arch/alpha's O_PATH at 0x800000 so
// plenty of headroom.
-#define WARN_ONLY (1<<31)
+#define WARN_ONLY (1<<31) // don't exit, just warn
+#define LOOPFILES_ANYWAY (1<<30) // call function with fd -1
// xwrap.c
void xstrncpy(char *dest, char *src, size_t size);
diff --git a/tests/tail.test b/tests/tail.test
index 036f433c..94644c8f 100755
--- a/tests/tail.test
+++ b/tests/tail.test
@@ -73,3 +73,9 @@ testing "-f one two three" \
'tail -f one two three & sleep .25 ; echo more >> three ; echo also >> one; sleep .25; kill $! >/dev/null' \
"==> one <==\nuno\n\n==> two <==\ndos\n\n==> three <==\ntres\nmore\n\n==> one <==\nalso\n" "" ""
rm one two three
+
+testing "-F" "tail -s .1 -F walrus 2>/dev/null & sleep .2; echo hello > walrus;
+sleep .2; truncate -s 0 walrus; sleep .2; echo potato >> walrus; sleep .2;
+echo hello >> walrus; sleep .2; rm walrus; sleep .2; echo done > walrus;
+ sleep .5; kill %1" "hello\npotato\nhello\ndone\n" "" ""
+rm -f walrus
diff --git a/toys/posix/tail.c b/toys/posix/tail.c
index 842814c0..98309c0f 100644
--- a/toys/posix/tail.c
+++ b/toys/posix/tail.c
@@ -6,7 +6,7 @@
*
* Deviations from posix: -f waits for pipe/fifo on stdin (nonblock?).
-USE_TAIL(NEWTOY(tail, "?fFs#=1c-n-[-cn][-fF]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_TAIL(NEWTOY(tail, "?fFs:c-n-[-cn][-fF]", TOYFLAG_USR|TOYFLAG_BIN))
config TAIL
bool "tail"
@@ -27,18 +27,18 @@ config TAIL
#define FOR_tail
#include "toys.h"
-struct follow_file {
- char *path;
- int fd;
- dev_t dev;
- ino_t ino;
-};
-
GLOBALS(
- long n, c, s;
- int file_no, last_fd;
+ long n, c;
+ char *s;
+
+ int file_no, last_fd, ss;
struct xnotify *not;
- struct follow_file *F;
+ struct {
+ char *path;
+ int fd;
+ dev_t dev;
+ ino_t ino;
+ } *F;
)
struct line_list {
@@ -132,66 +132,53 @@ static int try_lseek(int fd, long bytes, long lines)
return 1;
}
-static void show_new(int fd, char *path)
-{
- int len;
-
- while ((len = read(fd, toybuf, sizeof(toybuf)))>0) {
- if (TT.last_fd != fd) {
- TT.last_fd = fd;
- xprintf("\n==> %s <==\n", path);
- }
- xwrite(1, toybuf, len);
- }
-}
-
-static void tail_F()
+// For -f and -F
+static void tail_continue()
{
+ long long pos;
+ char *path;
struct stat sb;
- int i, old_fd;
-
- for (;;) {
- for (i = 0; i<TT.file_no; ++i) {
- old_fd = TT.F[i].fd;
- if (stat(TT.F[i].path, &sb) != 0) {
- if (old_fd >= 0) {
- xprintf("tail: file inaccessible: %s\n", TT.F[i].path);
- close(old_fd);
+ int i = 0, fd, len;
+
+ for (i = 0; ; i++) {
+ if (FLAG(f)) fd = xnotify_wait(TT.not, &path);
+ else {
+ if (i == TT.file_no) {
+ i = 0;
+ msleep(TT.ss);
+ }
+ fd = TT.F[i].fd;
+ path = TT.F[i].path;
+
+ if (stat(TT.F[i].path, &sb)) {
+ if (fd >= 0) {
+ close(fd);
TT.F[i].fd = -1;
+ error_msg("file inaccessible: %s\n", TT.F[i].path);
}
continue;
}
- if (old_fd < 0 || sb.st_dev != TT.F[i].dev || sb.st_ino != TT.F[i].ino) {
- if (old_fd >= 0) close(old_fd);
- TT.F[i].fd = open(TT.F[i].path, O_RDONLY);
- if (TT.F[i].fd == -1) continue;
- else {
- xprintf("tail: following new file: %s\n", TT.F[i].path);
- TT.F[i].dev = sb.st_dev;
- TT.F[i].ino = sb.st_ino;
- }
- } else if (sb.st_size && sb.st_size < lseek(TT.F[i].fd, 0, SEEK_CUR)) {
- // If file was truncated, move to start.
- xprintf("tail: file truncated: %s\n", TT.F[i].path);
- xlseek(TT.F[i].fd, 0, SEEK_SET);
+ if (fd<0 || sb.st_dev!=TT.F[i].dev || sb.st_ino!=TT.F[i].ino) {
+ if (fd>=0) close(fd);
+ if (-1 == (TT.F[i].fd = fd = open(path, O_RDONLY))) continue;
+ error_msg("following new file: %s\n", path);
+ TT.F[i].dev = sb.st_dev;
+ TT.F[i].ino = sb.st_ino;
+ } else if (sb.st_size <= (pos = lseek(fd, 0, SEEK_CUR))) {
+ if (pos == sb.st_size) continue;
+ error_msg("file truncated: %s\n", path);
+ lseek(fd, 0, SEEK_SET);
}
-
- show_new(TT.F[i].fd, TT.F[i].path);
}
- sleep(TT.s);
- }
-}
-
-static void tail_f()
-{
- char *path;
- int fd;
-
- for (;;) {
- fd = xnotify_wait(TT.not, &path);
- show_new(fd, path);
+ while ((len = read(fd, toybuf, sizeof(toybuf)))>0) {
+ if (TT.file_no>1 && TT.last_fd != fd) {
+ TT.last_fd = fd;
+ xprintf("\n==> %s <==\n", path);
+ }
+ xwrite(1, toybuf, len);
+ }
}
}
@@ -201,6 +188,7 @@ static void do_tail(int fd, char *name)
long bytes = TT.c, lines = TT.n;
int linepop = 1;
+ if (fd == -1 && !FLAG(F)) return;
if (FLAG(f) || FLAG(F)) {
char *s = name;
struct stat sb;
@@ -209,11 +197,13 @@ static void do_tail(int fd, char *name)
if (FLAG(f)) xnotify_add(TT.not, fd, s);
if (FLAG(F)) {
- if (fstat(fd, &sb) != 0) perror_exit("%s", name);
+ if (fd != -1) {
+ if (fstat(fd, &sb)) perror_exit("%s", name);
+ TT.F[TT.file_no].dev = sb.st_dev;
+ TT.F[TT.file_no].ino = sb.st_ino;
+ }
TT.F[TT.file_no].fd = fd;
TT.F[TT.file_no].path = s;
- TT.F[TT.file_no].dev = sb.st_dev;
- TT.F[TT.file_no].ino = sb.st_ino;
}
}
@@ -298,23 +288,21 @@ void tail_main(void)
if (!FLAG(n) && !FLAG(c)) {
char *arg = *args;
- // handle old "-42" style arguments
+ // handle old "-42" style arguments, else default to last 10 lines
if (arg && *arg == '-' && arg[1]) {
TT.n = atolx(*(args++));
toys.optc--;
- } else {
- // if nothing specified, default -n to -10
- TT.n = -10;
- }
+ } else TT.n = -10;
}
- if (FLAG(F)) TT.F = xmalloc(toys.optc * sizeof(struct follow_file));
+ if (FLAG(F)) TT.F = xzalloc(toys.optc*sizeof(*TT.F));
else if (FLAG(f)) TT.not = xnotify_init(toys.optc);
+ TT.ss = TT.s ? xparsemillitime(TT.s) : 1000;
- loopfiles_rw(args, O_RDONLY|WARN_ONLY|(O_CLOEXEC*!(FLAG(f) || FLAG(F))), 0, do_tail);
+ loopfiles_rw(args,
+ O_RDONLY|WARN_ONLY|LOOPFILES_ANYWAY|(O_CLOEXEC*!(FLAG(f) || FLAG(F))),
+ 0, do_tail);
- if (TT.file_no) {
- if (FLAG(F)) tail_F();
- else if (FLAG(f)) tail_f();
- }
+ // Wait for more data when following files
+ if (TT.file_no && (FLAG(F) || FLAG(f))) tail_continue();
}