From dfe4df56948294c4889b7201f7b6636b8ff01ea5 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Mon, 14 Jul 2025 13:32:27 -0700 Subject: [PATCH] =?utf8?q?wc:=20don=E2=80=99t=20assume=20opening=20a=20fil?= =?utf8?q?e=20puts=20you=20at=20start?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- src/wc.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) 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)); -- 2.47.2