From 882edccbd6fbdb3ff3e56419722a701e3d78ffeb Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Thu, 1 May 2003 11:49:12 +0000 Subject: (start_lines): Rewrite to use memchr. Clean up. --- src/tail.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'src') 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 */ -- cgit v1.2.3-70-g09d2