diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2013-07-08 02:39:51 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2013-07-08 02:39:51 +0200 |
commit | d87fcd48886f26373ca12bcfcff9cea44ce9fae4 (patch) | |
tree | 23cac9ff61e5ae154e7b8b37af5208bc5128da3d | |
parent | 40ab27a225f81b8d1d9b642f293c366f5cc4108f (diff) | |
download | busybox-w32-d87fcd48886f26373ca12bcfcff9cea44ce9fae4.tar.gz busybox-w32-d87fcd48886f26373ca12bcfcff9cea44ce9fae4.tar.bz2 busybox-w32-d87fcd48886f26373ca12bcfcff9cea44ce9fae4.zip |
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>
-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 fb07ca27b..c9f9d00f6 100644 --- a/coreutils/tail.c +++ b/coreutils/tail.c | |||
@@ -66,23 +66,10 @@ static void tail_xprint_header(const char *fmt, const char *filename) | |||
66 | bb_perror_nomsg_and_die(); | 66 | bb_perror_nomsg_and_die(); |
67 | } | 67 | } |
68 | 68 | ||
69 | static ssize_t tail_read(int fd, char *buf, size_t count, int follow) | 69 | static ssize_t tail_read(int fd, char *buf, size_t count) |
70 | { | 70 | { |
71 | ssize_t r; | 71 | ssize_t r; |
72 | 72 | ||
73 | if (follow) { | ||
74 | /* tail -f keeps following files even if they are truncated */ | ||
75 | off_t current; | ||
76 | struct stat sbuf; | ||
77 | |||
78 | /* /proc files report zero st_size, don't lseek them */ | ||
79 | if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) { | ||
80 | current = lseek(fd, 0, SEEK_CUR); | ||
81 | if (sbuf.st_size < current) | ||
82 | xlseek(fd, 0, SEEK_SET); | ||
83 | } | ||
84 | } | ||
85 | |||
86 | r = full_read(fd, buf, count); | 73 | r = full_read(fd, buf, count); |
87 | if (r < 0) { | 74 | if (r < 0) { |
88 | bb_perror_msg(bb_msg_read_error); | 75 | bb_perror_msg(bb_msg_read_error); |
@@ -255,7 +242,7 @@ int tail_main(int argc, char **argv) | |||
255 | * Used only by +N code ("start from Nth", 1-based): */ | 242 | * Used only by +N code ("start from Nth", 1-based): */ |
256 | seen = 1; | 243 | seen = 1; |
257 | newlines_seen = 0; | 244 | newlines_seen = 0; |
258 | while ((nread = tail_read(fd, buf, tailbufsize - taillen, /*follow:*/ 0)) > 0) { | 245 | while ((nread = tail_read(fd, buf, tailbufsize - taillen)) > 0) { |
259 | if (G.from_top) { | 246 | if (G.from_top) { |
260 | int nwrite = nread; | 247 | int nwrite = nread; |
261 | if (seen < count) { | 248 | if (seen < count) { |
@@ -367,7 +354,19 @@ int tail_main(int argc, char **argv) | |||
367 | if (nfiles > header_threshhold) { | 354 | if (nfiles > header_threshhold) { |
368 | fmt = header_fmt_str; | 355 | fmt = header_fmt_str; |
369 | } | 356 | } |
370 | while ((nread = tail_read(fd, tailbuf, BUFSIZ, /*follow:*/ 1)) > 0) { | 357 | for (;;) { |
358 | /* tail -f keeps following files even if they are truncated */ | ||
359 | struct stat sbuf; | ||
360 | /* /proc files report zero st_size, don't lseek them */ | ||
361 | if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) { | ||
362 | off_t current = lseek(fd, 0, SEEK_CUR); | ||
363 | if (sbuf.st_size < current) | ||
364 | xlseek(fd, 0, SEEK_SET); | ||
365 | } | ||
366 | |||
367 | nread = tail_read(fd, tailbuf, BUFSIZ); | ||
368 | if (nread <= 0) | ||
369 | break; | ||
371 | if (fmt) { | 370 | if (fmt) { |
372 | tail_xprint_header(fmt, filename); | 371 | tail_xprint_header(fmt, filename); |
373 | fmt = NULL; | 372 | fmt = NULL; |