Return -1 on error and set errno

All functions returns the value 0 or a positive value on success;
otherwise the value -1 is returned and the global variable errno
is set to indicate the error.