From 908d9edb66e736f4004c91e148922cbb1d7b3090 Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Fri, 4 Dec 2015 13:30:02 -0800 Subject: Implement tail -f. --- toys/posix/tail.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 70 insertions(+), 4 deletions(-) (limited to 'toys/posix/tail.c') diff --git a/toys/posix/tail.c b/toys/posix/tail.c index 910b88f3..c2f71cbd 100644 --- a/toys/posix/tail.c +++ b/toys/posix/tail.c @@ -17,7 +17,7 @@ config TAIL -n output the last NUMBER lines (default 10), +X counts from start. -c output the last NUMBER bytes, +NUMBER counts from start - #-f follow FILE(s), waiting for more data to be appended [TODO] + -f follow FILE(s), waiting for more data to be appended config TAIL_SEEK bool "tail seek support" @@ -30,11 +30,20 @@ config TAIL_SEEK #define FOR_tail #include "toys.h" +#include + +struct file_info { + int fd; + int wd; + const char* path; +}; + GLOBALS( long lines; long bytes; int file_no; + struct file_info *files; ) struct line_list { @@ -135,10 +144,15 @@ static void do_tail(int fd, char *name) int linepop = 1; if (toys.optc > 1) { - if (TT.file_no++) xputc('\n'); + if (TT.file_no > 0) xputc('\n'); xprintf("==> %s <==\n", name); } + // -f support: cache name/descriptor + TT.files[TT.file_no].fd = dup(fd); + TT.files[TT.file_no].path = strdup(name); + ++TT.file_no; + // Are we measuring from the end of the file? if (bytes<0 || lines<0) { @@ -207,8 +221,6 @@ static void do_tail(int fd, char *name) } if (offset %s <==\n", TT.files[i].path); + } + + xwrite(1, toybuf, len); + } + break; + } + } + } } -- cgit v1.2.3