diff options
author | Jim Meyering <jim@meyering.net> | 1995-02-10 05:53:54 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1995-02-10 05:53:54 +0000 |
commit | a3d2e589edcca9f8b511436027d2fd7f62c7849c (patch) | |
tree | 70d06870aeb7b2446e8ba6fae8a0a692a02098b1 | |
parent | 15d9f70d1a5af7d27ac66de79be0514e8d09e5f6 (diff) | |
download | coreutils-a3d2e589edcca9f8b511436027d2fd7f62c7849c.tar.xz |
(wc): Eliminate fstat call -- using lseek is sufficient.
Detect/handle case in which CURR position > EOF.
-rw-r--r-- | src/wc.c | 11 |
1 files changed, 6 insertions, 5 deletions
@@ -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 { |