aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/wc.c
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2016-02-12 08:13:06 -0800
committerRob Landley <rob@landley.net>2016-02-14 12:36:28 -0600
commit09d95477765d3941aacb61c97f76ee94301b8faa (patch)
tree902eff9ad737270bb435d5f799972dd29a1ebdc9 /toys/posix/wc.c
parent363659c859b1e6800e5568582af6b2a61f74405d (diff)
downloadtoybox-09d95477765d3941aacb61c97f76ee94301b8faa.tar.gz
Fix wc -c optimization.
Check the fstat(2) return value rather than read uninitialized memory if it failed, and add a special case for files that claim to be zero-length but aren't (as is common in /proc on Linux).
Diffstat (limited to 'toys/posix/wc.c')
-rw-r--r--toys/posix/wc.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/toys/posix/wc.c b/toys/posix/wc.c
index 62d1f7a0..e7afc813 100644
--- a/toys/posix/wc.c
+++ b/toys/posix/wc.c
@@ -53,8 +53,8 @@ static void do_wc(int fd, char *name)
if (toys.optflags == FLAG_c) {
struct stat st;
- fstat(fd, &st);
- if (S_ISREG(st.st_mode)) {
+ // On Linux, files in /proc often report their size as 0.
+ if (!fstat(fd, &st) && S_ISREG(st.st_mode) && st.st_size > 0) {
lengths[2] = st.st_size;
goto show;
}