From: Paul Eggert Date: Mon, 14 Jul 2025 20:32:27 +0000 (-0700) Subject: wc: don’t assume opening a file puts you at start X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dfe4df56948294c4889b7201f7b6636b8ff01ea5;p=thirdparty%2Fcoreutils.git wc: don’t assume opening a file puts you at start This is not true on Solaris when opening /dev/stdin; it could be at a nonzero file offset. Arguably Linux should do likewise. * src/wc.c (wc): Omit last arg, and act as if it is always -1. All uses changed. --- diff --git a/src/wc.c b/src/wc.c index ed4ad62c41..24dc4b3a98 100644 --- a/src/wc.c +++ b/src/wc.c @@ -296,10 +296,9 @@ wc_lines (int fd) /* Count words. FILE_X is the name of the file (or null for standard input) that is open on descriptor FD. *FSTATUS is its status. - CURRENT_POS is the current file offset if known, negative if unknown. Return true if successful. */ static bool -wc (int fd, char const *file_x, struct fstatus *fstatus, off_t current_pos) +wc (int fd, char const *file_x, struct fstatus *fstatus) { int err = 0; char buf[IO_BUFSIZE + 1]; @@ -351,10 +350,10 @@ wc (int fd, char const *file_x, struct fstatus *fstatus, off_t current_pos) && 0 <= fstatus->st.st_size) { off_t end_pos = fstatus->st.st_size; + off_t current_pos = lseek (fd, 0, SEEK_CUR); if (current_pos < 0) - current_pos = lseek (fd, 0, SEEK_CUR); - - if (end_pos % page_size) + ; + else if (end_pos % page_size) { /* We only need special handling of /proc and /sys files etc. when they're a multiple of PAGE_SIZE. In the common case @@ -629,7 +628,7 @@ wc_file (char const *file, struct fstatus *fstatus) { have_read_stdin = true; xset_binary_mode (STDIN_FILENO, O_BINARY); - return wc (STDIN_FILENO, file, fstatus, -1); + return wc (STDIN_FILENO, file, fstatus); } else { @@ -641,7 +640,7 @@ wc_file (char const *file, struct fstatus *fstatus) } else { - bool ok = wc (fd, file, fstatus, 0); + bool ok = wc (fd, file, fstatus); if (close (fd) != 0) { error (0, errno, "%s", quotef (file));