How to translate error number to ‘errno’ constant

troubleshootingunix

Suppose I have an application running on a UNIX box that is failing with a system error status of '13'. Now, I can easily look up this value in errno.h, to find out that it is a permission-denied problem.

> grep -w 13 /usr/include/errno.h
#define EACCES  13      /* Permission denied                    */

Is there a simpler command to retrieve this information? I'd like to be able to run something like this:

> lookuperror 13
EACCES (Permission denied)

Instead of grepping system header files. Does such a command/program exist?

Update: As pointed out in the answers below, the strerror() system call returns this information. Are there any UNIX operating systems that ship with an executable utility that makes this system call, or do I need to write my own program to do it?

Best Answer

I use to do

perl -MPOSIX -e 'print strerror($ARGV[0])."\n";' 13

You can just put the Perl code in a file and have it in the path.
Of course it can be done using C as well

Related Topic