]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
head: omit unnecessary lseek
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 14 Jul 2025 18:07:21 +0000 (11:07 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 4 Aug 2025 02:48:04 +0000 (19:48 -0700)
* src/head.c (elseek_diagnostic): New function.
(elseek): Use it.
(head_lines): Use it to avoid an unnecessary lseek.

src/head.c

index 1620f17bc7c17162998ad71547cbe885d4636b8e..c7b48321c4bd2cd89e150aebf658f88805f6351e 100644 (file)
@@ -214,6 +214,18 @@ copy_fd (int src_fd, uintmax_t n_bytes)
   return COPY_FD_OK;
 }
 
+/* Report an lseek failure at OFFSET compared to WHENCE, for FILENAME.  */
+static void
+elseek_diagnostic (off_t offset, int whence, char const *filename)
+{
+  intmax_t off = offset;
+  error (0, errno,
+         _(whence == SEEK_SET
+           ? N_("%s: cannot seek to offset %jd")
+           : N_("%s: cannot seek to relative offset %jd")),
+         quotef (filename), off);
+}
+
 /* Call lseek (FD, OFFSET, WHENCE), where file descriptor FD
    corresponds to the file FILENAME.  WHENCE must be SEEK_SET or
    SEEK_CUR.  Return the resulting offset.  Give a diagnostic and
@@ -225,12 +237,7 @@ elseek (int fd, off_t offset, int whence, char const *filename)
   off_t new_offset = lseek (fd, offset, whence);
 
   if (new_offset < 0)
-    error (0, errno,
-           _(whence == SEEK_SET
-             ? N_("%s: cannot seek to offset %jd")
-             : N_("%s: cannot seek to relative offset %jd")),
-           quotef (filename),
-           (intmax_t) offset);
+    elseek_diagnostic (offset, whence, filename);
 
   return new_offset;
 }
@@ -808,7 +815,7 @@ head_lines (char const *filename, int fd, uintmax_t lines_to_write)
               {
                 struct stat st;
                 if (fstat (fd, &st) != 0 || S_ISREG (st.st_mode))
-                  elseek (fd, -n_bytes_past_EOL, SEEK_CUR, filename);
+                  elseek_diagnostic (-n_bytes_past_EOL, SEEK_CUR, filename);
               }
             break;
           }