O'Reilly logo

Advanced UNIX Programming by Warren W. Gay

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ) { /* ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required