Refactored to use EXIT_FAILURE or EXIT_SUCCESS from all functions, and to set errno to the approriate error condition where necessary.  If the error condition arises from antoher internal call, errno is left set to the value provided by the failing function cal.