inetd: comment tweak. no code changes
[oweals/busybox.git] / coreutils / tail.c
index ef1326c831d9f0c512d7789e4c7ac0b333d1f109..05dadcd9bd1e34a5aecaca5ddd7386516f69ae91 100644 (file)
@@ -50,12 +50,12 @@ static ssize_t tail_read(int fd, char *buf, size_t count)
        off_t current;
        struct stat sbuf;
 
-       /* (A good comment is missing here) */
-       current = lseek(fd, 0, SEEK_CUR);
        /* /proc files report zero st_size, don't lseek them. */
-       if (fstat(fd, &sbuf) == 0 && sbuf.st_size)
+       if (fstat(fd, &sbuf) == 0 && sbuf.st_size > 0) {
+               current = lseek(fd, 0, SEEK_CUR);
                if (sbuf.st_size < current)
-                       lseek(fd, 0, SEEK_SET);
+                       xlseek(fd, 0, SEEK_SET);
+       }
 
        r = full_read(fd, buf, count);
        if (r < 0) {
@@ -119,10 +119,11 @@ int tail_main(int argc, char **argv)
 #if ENABLE_FEATURE_FANCY_TAIL
        /* q: make it impossible for nfiles to be > header_threshhold */
        if (opt & 0x8) header_threshhold = UINT_MAX; // -q
+       //if (opt & 0x10) // -s
        if (opt & 0x20) header_threshhold = 0; // -v
-#define FOLLOW_RETRY (opt & 0x40)
+# define FOLLOW_RETRY (opt & 0x40)
 #else
-#define FOLLOW_RETRY 0
+# define FOLLOW_RETRY 0
 #endif
        argc -= optind;
        argv += optind;
@@ -173,8 +174,9 @@ int tail_main(int argc, char **argv)
                int newlines_seen;
                unsigned seen;
                int nread;
+               int fd = fds[i];
 
-               if (ENABLE_FEATURE_FANCY_TAIL && fds[i] < 0)
+               if (ENABLE_FEATURE_FANCY_TAIL && fd < 0)
                        continue; /* may happen with -E */
 
                if (nfiles > header_threshhold) {
@@ -182,31 +184,50 @@ int tail_main(int argc, char **argv)
                        fmt = header_fmt;
                }
 
-               /* 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) */
-               if (COUNT_BYTES && !from_top) {
-                       off_t current = lseek(fds[i], 0, SEEK_END);
+               if (!from_top) {
+                       off_t current = lseek(fd, 0, SEEK_END);
                        if (current > 0) {
-                               if (count == 0)
-                                       continue; /* showing zero lines is easy :) */
-                               current -= count;
+                               unsigned off;
+                               if (COUNT_BYTES) {
+                               /* 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) */
+                                       if (count == 0)
+                                               continue; /* showing zero bytes is easy :) */
+                                       current -= count;
+                                       if (current < 0)
+                                               current = 0;
+                                       xlseek(fd, current, SEEK_SET);
+                                       bb_copyfd_size(fd, STDOUT_FILENO, count);
+                                       continue;
+                               }
+#if 1 /* This is technically incorrect for *LONG* strings, but very useful */
+                               /* Optimizing count-lines case if the file is seekable.
+                                * We assume the lines are <64k.
+                                * (Users complain that tail takes too long
+                                * on multi-gigabyte files) */
+                               off = (count | 0xf); /* for small counts, be more paranoid */
+                               if (off > (INT_MAX / (64*1024)))
+                                       off = (INT_MAX / (64*1024));
+                               current -= off * (64*1024);
                                if (current < 0)
                                        current = 0;
-                               xlseek(fds[i], current, SEEK_SET);
-                               bb_copyfd_size(fds[i], STDOUT_FILENO, count);
-                               continue;
+                               xlseek(fd, current, SEEK_SET);
+#endif
                        }
                }
 
                buf = tailbuf;
                taillen = 0;
+               /* "We saw 1st line/byte".
+                * Used only by +N code ("start from Nth", 1-based): */
                seen = 1;
                newlines_seen = 0;
-               while ((nread = tail_read(fds[i], buf, tailbufsize-taillen)) > 0) {
+               while ((nread = tail_read(fd, buf, tailbufsize-taillen)) > 0) {
                        if (from_top) {
                                int nwrite = nread;
                                if (seen < count) {
+                                       /* We need to skip a few more bytes/lines */
                                        if (COUNT_BYTES) {
                                                nwrite -= (count - seen);
                                                seen = count;