]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(start_lines): Rewrite to use memchr. Clean up.
authorJim Meyering <jim@meyering.net>
Thu, 1 May 2003 11:49:12 +0000 (11:49 +0000)
committerJim Meyering <jim@meyering.net>
Thu, 1 May 2003 11:49:12 +0000 (11:49 +0000)
src/tail.c

index bc244a45e21c3abd2b6700c84a4bdc032873843c..3dac34bfa85d0541d3a7048928af58dec141fba7 100644 (file)
@@ -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 */