From: Jim Meyering Date: Fri, 10 Feb 1995 05:53:54 +0000 (+0000) Subject: (wc): Eliminate fstat call -- using lseek is sufficient. X-Git-Tag: textutils-1_12_1~300 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=a3d2e589edcca9f8b511436027d2fd7f62c7849c;p=thirdparty%2Fcoreutils.git (wc): Eliminate fstat call -- using lseek is sufficient. Detect/handle case in which CURR position > EOF. --- diff --git a/src/wc.c b/src/wc.c index 34b3e0872e..4d307cd18e 100644 --- a/src/wc.c +++ b/src/wc.c @@ -216,14 +216,15 @@ wc (fd, file) if (print_chars && !print_words && !print_lines) { - struct stat stats; off_t current_pos, end_pos; - if (fstat (fd, &stats) == 0 && S_ISREG (stats.st_mode) - && (current_pos = lseek (fd, (off_t)0, SEEK_CUR)) != -1 - && (end_pos = lseek (fd, (off_t)0, SEEK_END)) != -1) + if ((current_pos = lseek (fd, (off_t) 0, SEEK_CUR)) != -1 + && (end_pos = lseek (fd, (off_t) 0, SEEK_END)) != -1) { - chars = end_pos - current_pos; + off_t diff; + /* Be careful here. The current position may actually be + beyond the end of the file. As in the example above. */ + chars = (diff = end_pos - current_pos) < 0 ? 0 : diff; } else {