The Old errno Value

The original method that the programmer used to gain access to the error code was to declare an external reference to the int value errno:

extern int errno;

When an attempt to open a file fails, a program can simply query the external variable errno to determine the reason for the failure. The following example shows how the make(1) command could be written using the old errno method:

 #include <errno.h> /* Defines ENOENT */ extern int errno; /* Error code */ int fd; /* File descriptor */ /* Attempt to open makefile */ if ( (fd = open("makefile",O_RDONLY)) == -1 ) { /* Fail to open? */ if ( errno == ENOENT ) /* File does not exist? */ fd = open("Makefile",O_RDONLY); /* No, so try Makefile instead */ } if ( fd == -1 ) { /* ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.