How to know what the 'errno' means?

Barth picture Barth · Feb 2, 2009 · Viewed 343.9k times · Source

When calling execl(...), I get an errno=2. What does it mean? How can I know the meaning of this errno?

Answer

Commodore Jaeger picture Commodore Jaeger · Feb 2, 2009

You can use strerror() to get a human-readable string for the error number. This is the same string printed by perror() but it's useful if you're formatting the error message for something other than standard error output.

For example:

#include <errno.h>
#include <string.h>

/* ... */

if(read(fd, buf, 1)==-1) {
    printf("Oh dear, something went wrong with read()! %s\n", strerror(errno));
}

Linux also supports the explicitly-threadsafe variant strerror_r().