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).
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;
}