System Interface Guide

Error Handling

Functions that do not conclude successfully almost always return a value of -1 to your program. (For a few functions in man Pages(2): System Calls, there are calls for which no return value is defined, but these are the exceptions.) In addition to the -1 that is returned to the program, the unsuccessful function places an integer in an externally declared variable, errno. In a C program, you can determine the value in errno if your program contains the following statement:

#include <errno.h>

The value in errno is not cleared on successful calls, so check it only if the function returns -1. Since some functions return -1 but do not set errno refer to the man page for the function to be sure that errno contains a valid value. See error descriptions in Intro(2).

Use the C language function perror(3C) to print an error message on stderr based on the value of errno, or strerror(3C) to obtain the corresponding printable string.