From: Paul Eggert Date: Mon, 14 Jul 2025 18:07:21 +0000 (-0700) Subject: head: omit unnecessary lseek X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca30c06e4cd0dbd74d9c95ccc2b05cb26bd053a3;p=thirdparty%2Fcoreutils.git head: omit unnecessary lseek * src/head.c (elseek_diagnostic): New function. (elseek): Use it. (head_lines): Use it to avoid an unnecessary lseek. --- diff --git a/src/head.c b/src/head.c index 1620f17bc7..c7b48321c4 100644 --- a/src/head.c +++ b/src/head.c @@ -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; }