Implement suggestion from Adam Slattery, (don't default to killing closing bug #1190.
[oweals/busybox.git] / tail.c
diff --git a/tail.c b/tail.c
index 40511aa7bbf3663dff4a90b4864cee8181470f32..90cc2a6efad9e5d9e09ce90296953768eba99544 100644 (file)
--- a/tail.c
+++ b/tail.c
@@ -21,7 +21,6 @@
  *
  */
 
-#include "busybox.h"
 
 #include <fcntl.h>
 #include <getopt.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include "busybox.h"
 
-static struct suffix_mult tail_suffixes[] = {
+static const struct suffix_mult tail_suffixes[] = {
        { "b", 512 },
        { "k", 1024 },
        { "m", 1048576 },
        { NULL, 0 }
 };
 
-#ifndef BB_FEATURE_SIMPLE_TAIL
-static struct suffix_mult null_suffixes[] = {
-       { NULL, 0 }
-};
-#endif
-
 static const int BYTES = 0;
 static const int LINES = 1;
 
@@ -50,14 +44,14 @@ static char *tailbuf;
 static int taillen;
 static int newline;
 
-void tailbuf_append(char *buf, int len)
+static void tailbuf_append(char *buf, int len)
 {
        tailbuf = xrealloc(tailbuf, taillen + len);
        memcpy(tailbuf + taillen, buf, len);
        taillen += len;
 }
 
-void tailbuf_trunc()
+static void tailbuf_trunc()
 {
        char *s;
        s = memchr(tailbuf, '\n', taillen);
@@ -79,7 +73,7 @@ int tail_main(int argc, char **argv)
                        case 'f':
                                follow = 1;
                                break;
-#ifndef BB_FEATURE_SIMPLE_TAIL
+#ifdef BB_FEATURE_FANCY_TAIL
                        case 'c':
                                units = BYTES;
                                /* FALLS THROUGH */
@@ -91,19 +85,19 @@ int tail_main(int argc, char **argv)
                                if (optarg[0] == '+')
                                        from_top = 1;
                                break;
-#ifndef BB_FEATURE_SIMPLE_TAIL
+#ifdef BB_FEATURE_FANCY_TAIL
                        case 'q':
                                hide_headers = 1;
                                break;
                        case 's':
-                               sleep_period = parse_number(optarg, null_suffixes);
+                               sleep_period = parse_number(optarg, 0);
                                break;
                        case 'v':
                                show_headers = 1;
                                break;
 #endif
                        default:
-                               usage(tail_usage);
+                               show_usage();
                }
        }
 
@@ -124,7 +118,7 @@ int tail_main(int argc, char **argv)
                }
        }
        
-#ifndef BB_FEATURE_SIMPLE_TAIL
+#ifdef BB_FEATURE_FANCY_TAIL
        /* tail the files */
        if (!from_top && units == BYTES)
                tailbuf = xmalloc(count);
@@ -133,12 +127,16 @@ int tail_main(int argc, char **argv)
        for (i = 0; i < nfiles; i++) {
                if (fds[i] == -1)
                        continue;
+               if (!count) {
+                       lseek(fds[i], 0, SEEK_END);
+                       continue;
+               }
                seen = 0;
                if (show_headers || (!hide_headers && nfiles > 1))
                        printf("%s==> %s <==\n", i == 0 ? "" : "\n", argv[optind + i]);
                while ((nread = safe_read(fds[i], buf, sizeof(buf))) > 0) {
                        if (from_top) {
-#ifndef BB_FEATURE_SIMPLE_TAIL
+#ifdef BB_FEATURE_FANCY_TAIL
                                if (units == BYTES) {
                                        if (count - 1 <= seen)
                                                nwrite = nread;
@@ -171,7 +169,7 @@ int tail_main(int argc, char **argv)
                                        break;
                                }
                        } else {
-#ifndef BB_FEATURE_SIMPLE_TAIL
+#ifdef BB_FEATURE_FANCY_TAIL
                                if (units == BYTES) {
                                        if (nread < count) {
                                                memmove(tailbuf, tailbuf + nread, count - nread);
@@ -205,7 +203,7 @@ int tail_main(int argc, char **argv)
                        status = EXIT_FAILURE;
                }
 
-#ifndef BB_FEATURE_SIMPLE_TAIL
+#ifdef BB_FEATURE_FANCY_TAIL
                if (!from_top && units == BYTES) {
                        if (count < seen)
                                seen = count;