[#2222] refactored to do error-checking and use off_t as appropriate