tail: fix fallout from tail -c optimization
authorDenis Vlasenko <vda.linux@googlemail.com>
Sun, 23 Mar 2008 03:28:40 +0000 (03:28 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Sun, 23 Mar 2008 03:28:40 +0000 (03:28 -0000)
coreutils/tail.c
testsuite/tail/tail-n-works
testsuite/tail/tail-works

index 35b25a416bd25b408638b2b5b041063ed297f715..2f997a9f6c61e788a72ecf072ccd457fdc3810b9 100644 (file)
@@ -163,8 +163,6 @@ int tail_main(int argc, char **argv)
        fmt = header_fmt + 1;   /* Skip header leading newline on first output. */
        i = 0;
        do {
-               off_t current;
-
                if (nfiles > header_threshhold) {
                        tail_xprint_header(fmt, argv[i]);
                        fmt = header_fmt;
@@ -173,19 +171,17 @@ int tail_main(int argc, char **argv)
                /* Optimizing count-bytes case if the file is seekable.
                 * Beware of backing up too far.
                 * Also we exclude files with size 0 (because of /proc/xxx) */
-               current = lseek(fds[i], 0, SEEK_END);
-               if (current > 0) {
-                       if (!from_top) {
+               if (COUNT_BYTES && !from_top) {
+                       off_t current = lseek(fds[i], 0, SEEK_END);
+                       if (current > 0) {
                                if (count == 0)
                                        continue; /* showing zero lines is easy :) */
-                               if (COUNT_BYTES) {
-                                       current -= count;
-                                       if (current < 0)
-                                               current = 0;
-                                       xlseek(fds[i], current, SEEK_SET);
-                                       bb_copyfd_size(fds[i], STDOUT_FILENO, count);
-                                       continue;
-                               }
+                               current -= count;
+                               if (current < 0)
+                                       current = 0;
+                               xlseek(fds[i], current, SEEK_SET);
+                               bb_copyfd_size(fds[i], STDOUT_FILENO, count);
+                               continue;
                        }
                }
 
index 27a905f88e63abc62f65691bb345d487622e9943..e5b260caf57773d17ad14a191deeb52823c755be 100644 (file)
@@ -1,4 +1,4 @@
-[ -n "$d" ] || d=..
-tail -n 2 "$d/README" > logfile.gnu
-busybox tail -n 2 "$d/README" > logfile.bb
-cmp logfile.gnu logfile.bb
+echo -ne "abc\ndef\n123\n" >input
+echo -ne "def\n123\n" >logfile.ok
+busybox tail -n 2 input > logfile.bb
+cmp logfile.ok logfile.bb
index 27a905f88e63abc62f65691bb345d487622e9943..64e6d88abfb91a134c145cb1c28867f049b699f5 100644 (file)
@@ -1,4 +1,4 @@
-[ -n "$d" ] || d=..
-tail -n 2 "$d/README" > logfile.gnu
-busybox tail -n 2 "$d/README" > logfile.bb
-cmp logfile.gnu logfile.bb
+echo -ne "abc\ndef\n123\n" >input
+echo -ne "def\n123\n" >logfile.ok
+busybox tail -2 input > logfile.bb
+cmp logfile.ok logfile.bb