48 Chapter 3
There are two means by which an application program can determine the version
of the GNU C library present on the system: by testing constants or by calling a
library function. From version 2.0 onward, glibc defines two constants, __GLIBC__
and __GLIBC_MINOR__, that can be tested at compile time (in #ifdef statements). On a
system with glibc 2.12 installed, these constants would have the values 2 and 12.
However, these constants are of limited use in a program that is compiled on one
system but run on another system with a different glibc. To handle this possibility, a
program can call the gnu_get_libc_version() function to determine the version of
glibc available at run time.
The gnu_get_libc_version() function returns a pointer to a string, such as 2.12.
We can also obtain version information by using the confstr() function to
retrieve the value of the (glibc-specific) _CS_GNU_LIBC_VERSION configuration vari-
able. This call returns a string such as glibc 2.12.
3.4 Handling Errors from System Calls and Library Functions..................................................
Almost every system call and library function returns some type of status value indi-
cating whether the call succeeded or failed. This status value should always be
checked to see whether the call succeeded. If it did not, then appropriate action
should be taken—at the very least, the program should display an error message
warning that something unexpected occurred.
Although it is tempting to save typing time by excluding these checks (espe-
cially after seeing examples of UNIX and Linux programs where status values are
not checked), it is a false economy. Many hours of debugging time can be wasted
because a check was not made on the status return of a system call or library function
that “couldn’t possibly fail.”
A few system calls never fail. For example, getpid() always successfully returns
the ID of a process, and _exit() always terminates a process. It is not necessary
to check the return values from such system calls.
Handling system call errors
The manual page for each system call documents the possible return values of the
call, showing which value(s) indicate an error. Usually, an error is indicated by a
return of –1. Thus, a system call can be checked with code such as the following:
fd = open(pathname, flags, mode); /* system call to open a file */
if (fd == -1) {
/* Code to handle the error */
}
...
#include <gnu/libc-version.h>
const char *gnu_get_libc_version(void);
Returns pointer to null-terminated, statically allocated string
containing GNU C library version number