diff options
author | Jim Meyering <jim@meyering.net> | 2003-05-01 11:49:12 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 2003-05-01 11:49:12 +0000 |
commit | 882edccbd6fbdb3ff3e56419722a701e3d78ffeb (patch) | |
tree | 1b1ed23a815e52137086e95ab45ac3f3f3bcf027 /src | |
parent | 67fa85dc34d162351bd7b77687b47902cb6f3237 (diff) | |
download | coreutils-882edccbd6fbdb3ff3e56419722a701e3d78ffeb.tar.xz |
(start_lines): Rewrite to use memchr. Clean up.
Diffstat (limited to 'src')
-rw-r--r-- | src/tail.c | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/src/tail.c b/src/tail.c index bc244a45e..3dac34bfa 100644 --- a/src/tail.c +++ b/src/tail.c @@ -748,17 +748,15 @@ start_bytes (const char *pretty_filename, int fd, off_t n_bytes) static int start_lines (const char *pretty_filename, int fd, long int n_lines) { - char buffer[BUFSIZ]; - size_t bytes_read = 0; - size_t bytes_to_skip = 0; - if (n_lines == 0) return 0; - while (n_lines) + while (1) { - bytes_to_skip = 0; - bytes_read = safe_read (fd, buffer, BUFSIZ); + char buffer[BUFSIZ]; + char *p = buffer; + size_t bytes_read = safe_read (fd, buffer, BUFSIZ); + char *buffer_end = buffer + bytes_read; if (bytes_read == 0) /* EOF */ return -1; if (bytes_read == SAFE_READ_ERROR) /* error */ @@ -767,18 +765,17 @@ start_lines (const char *pretty_filename, int fd, long int n_lines) return 1; } - while (bytes_to_skip < bytes_read) - if (buffer[bytes_to_skip++] == '\n' && --n_lines == 0) - break; - } - - if (bytes_to_skip < bytes_read) - { - xwrite (STDOUT_FILENO, &buffer[bytes_to_skip], - bytes_read - bytes_to_skip); + while ((p = memchr (p, '\n', buffer_end - p))) + { + ++p; + if (--n_lines == 0) + { + if (p < buffer_end) + xwrite (STDOUT_FILENO, p, buffer_end - p); + return 0; + } + } } - - return 0; } /* FIXME: describe */ |