also move stat() into the fileno check
