2.4 Library Function CallsWe introduce most library functions by a condensed version of its specification, and you should always refer to the man pages for more complete information.The summary starts with a brief description of the function and its parameters, followed by a SYNOPSIS box giving the required header files and the function prototype. (Unfortunately, some compilers do not give warning messages if the header files are missing, so be sure to use lint as described in Appendix A to detect these problems.) The SYNOPSIS box also names the POSIX standard that specifies the function. A description of the function return values and a discussion of how the function reports errors follows the SYNOPSIS box. Here is a typical summary.The close function deallocates the file descriptor specified by fildes.
If successful, close returns 0. If unsuccessful, close returns 1 and sets errno. The following table lists the mandatory errors for close.
Example 2.4The following code segment demonstrates how to call the close function.
The code assumes that the unistd.h header file has been included in the source. In general, we do not show the header files for code segments.The perror function outputs to standard error a message corresponding to the current value of errno. If s is not NULL, perror outputs the string (an array of characters terminated by a null character) pointed to by s and followed by a colon and a space. Then, perror outputs an error message corresponding to the current value of errno followed by a newline.
No return values and no errors are defined for perror. Example 2.5The output produced by Example 2.4 might be as follows.
The strerror function returns a pointer to the system error message corresponding to the error code errnum.
If successful, strerror returns a pointer to the error string. No values are reserved for failure.Use strerror to produce informative messages, or use it with functions that return error codes directly without setting errno. Example 2.6The following code segment uses strerror to output a more informative error message when close fails.
The strerror function may change errno. You should save and restore errno if you need to use it again. Example 2.7The following code segment illustrates how to use strerror and still preserve the value of errno.
Correctly handing errno is a tricky business. Because its implementation may call other functions that set errno, a library function may change errno, even though the man page doesn't explicitly state that it does. Also, applications cannot change the string returned from strerror, but subsequent calls to either strerror or perror may overwrite this string.Another common problem is that many library calls abort if the process is interrupted by a signal. Functions generally report this type of return with an error code of EINTR. For example, the close function may be interrupted by a signal. In this case, the error was not due to a problem with its execution but was a result of some external factor. Usually the program should not treat this interruption as an error but should restart the call. Example 2.8The following code segment restarts the close function if a signal occurs.
The while loop of Example 2.8 has an empty statement clause. It simply calls close until it either executes successfully or encounters a real error. The problem of restarting library calls is so common that we provide a library of restarted calls with prototypes defined in restart.h. The functions are designated by a leading r_ prepended to the regular library name. For example, the restart library designates a restarted version of close by the name r_close. Example 2.9The following code segment illustrates how to use a version of close from the restart library.
|