diff options
author | Denys Vlasenko | 2013-07-08 02:39:51 +0200 |
---|---|---|
committer | Denys Vlasenko | 2013-07-08 02:39:51 +0200 |
commit | d87fcd48886f26373ca12bcfcff9cea44ce9fae4 (patch) | |
tree | 23cac9ff61e5ae154e7b8b37af5208bc5128da3d /coreutils | |
parent | 40ab27a225f81b8d1d9b642f293c366f5cc4108f (diff) | |
download | busybox-d87fcd48886f26373ca12bcfcff9cea44ce9fae4.zip busybox-d87fcd48886f26373ca12bcfcff9cea44ce9fae4.tar.gz |
tail: code shrink
function old new delta
tail_main 1548 1613 +65
tail_read 136 34 -102
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 65/-102) Total: -37 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils')
-rw-r--r-- | coreutils/tail.c | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/coreutils/tail.c b/coreutils/tail.c index fb07ca2..c9f9d00 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c @@ -66,23 +66,10 @@ static void tail_xprint_header(const char *fmt, const char *filename) bb_perror_nomsg_and_die(); } -static ssize_t tail_read(int fd, char *buf, size_t count, int follow) +static ssize_t tail_read(int fd, char *buf, size_t count) { ssize_t r; - if (follow) { - /* tail -f keeps following files even if they are truncated */ - off_t current; - struct stat sbuf; - - /* /proc files report zero st_size, don't lseek them */ - if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) { - current = lseek(fd, 0, SEEK_CUR); - if (sbuf.st_size < current) - xlseek(fd, 0, SEEK_SET); - } - } - r = full_read(fd, buf, count); if (r < 0) { bb_perror_msg(bb_msg_read_error); @@ -255,7 +242,7 @@ int tail_main(int argc, char **argv) * Used only by +N code ("start from Nth", 1-based): */ seen = 1; newlines_seen = 0; - while ((nread = tail_read(fd, buf, tailbufsize - taillen, /*follow:*/ 0)) > 0) { + while ((nread = tail_read(fd, buf, tailbufsize - taillen)) > 0) { if (G.from_top) { int nwrite = nread; if (seen < count) { @@ -367,7 +354,19 @@ int tail_main(int argc, char **argv) if (nfiles > header_threshhold) { fmt = header_fmt_str; } - while ((nread = tail_read(fd, tailbuf, BUFSIZ, /*follow:*/ 1)) > 0) { + for (;;) { + /* tail -f keeps following files even if they are truncated */ + struct stat sbuf; + /* /proc files report zero st_size, don't lseek them */ + if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) { + off_t current = lseek(fd, 0, SEEK_CUR); + if (sbuf.st_size < current) + xlseek(fd, 0, SEEK_SET); + } + + nread = tail_read(fd, tailbuf, BUFSIZ); + if (nread <= 0) + break; if (fmt) { tail_xprint_header(fmt, filename); fmt = NULL; |