Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

5 Answers 5

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

share|improve this answer
2  
'Of course it can be done using C as well' -- less portable (needs recompile) –  LiraNuna Jul 9 '09 at 20:22
    
Yep ! That why I use perl ;) –  radius Jul 9 '09 at 20:25
1  
For what it's worth, $! also stringifies to its strerror() representation, so this works as well: perl -E 'say $!=shift' 13 –  pilcrow Aug 8 '12 at 15:36
add comment
~% perror 13
OS error code  13:  Permission denied
~% rpm -qf =perror
mysql-server-5.0.45-7.el5
share|improve this answer
    
What platform is this? Are you saying that perror is a utility installed along with MySQL Server? –  Andrew Jul 9 '09 at 20:27
1  
The program 'perror' can be found in the following packages: * mysql-server-5.0 * mysql-server-5.1 On debian. –  LiraNuna Jul 9 '09 at 20:55
1  
@Andrew: Yes, mysql-server installs this utility, "perror", probably because they noticed the lack of such utility, and how useful it would be. –  Juliano Jul 9 '09 at 21:06
add comment

Try strerror(3).

From the manpage:

DESCRIPTION

 The strerror(), strerror_r() and perror() functions look up the error
 message string corresponding to an error number.

 The strerror() function accepts an error number argument errnum and
 returns a pointer to the corresponding message string.

 The strerror_r() function renders the same result into strerrbuf for a
 maximum of buflen characters and returns 0 upon success.

 The perror() function finds the error message corresponding to the cur-
 rent value of the global variable errno (intro(2)) and writes it, fol-
 lowed by a newline, to the standard error file descriptor.  If the argu-
 ment string is non-NULL and does not point to the null character, this
 string is prepended to the message string and separated from it by a
 colon and space (``: ''); otherwise, only the error message string is
 printed.

 If the error number is not recognized, these functions return an error
 message string containing ``Unknown error: '' followed by the error num-
 ber in decimal.  The strerror() and strerror_r() functions return EINVAL
 as a warning.  Error numbers recognized by this implementation fall in
 the range 0 < errnum < sys_nerr.

 If insufficient storage is provided in strerrbuf (as specified in buflen)
 to contain the error string, strerror_r() returns ERANGE and strerrbuf
 will contain an error message that has been truncated and NUL terminated
 to fit the length specified by buflen.

 The message strings can be accessed directly using the external array
 sys_errlist.  The external value sys_nerr contains a count of the mes-
 sages in sys_errlist.  The use of these variables is deprecated;
 strerror() or strerror_r() should be used instead.
share|improve this answer
    
Do I have to compile a C program to make this system call? Or could I use something like awk? –  Andrew Jul 9 '09 at 20:18
add comment

As a workaround you could in your shell make an alias or function:

eg. .bashrc

function lookuperror
{
    grep -w "$@" /usr/include/errno.h
}
share|improve this answer
add comment

cpp -dM preprocesses a source or header file and prints every #define that it finds. It's more robust than grepping through /usr/include/errno.h, since it will get every file that /usr/include/errno.h includes.

Combining cpp -dM with others' suggestions:

function lookuperror
{
    cpp -dM /usr/include/errno.h | grep -w "$@"
    perl -MPOSIX -e 'print "Description:".strerror($ARGV[0])."\n";' $@
}

Insert to .bashrc, or place its contents as a standalone shell script.

share|improve this answer
    
Awesome, thanks! –  Andrew Jul 10 '09 at 1:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.