Como traduzir o número do erro para a constante 'errno'?

5

Suponha que eu tenha um aplicativo em execução em uma caixa UNIX que falhe com um status de erro do sistema igual a '13'. Agora, posso facilmente procurar esse valor em errno.h, para descobrir que é um problema de permissão negada.

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

Existe um comando mais simples para recuperar essas informações? Gostaria de poder executar algo assim:

> lookuperror 13
EACCES (Permission denied)

Em vez de grepping arquivos de cabeçalho do sistema. Existe tal comando / programa?

Atualização: Conforme apontado nas respostas abaixo, a chamada do sistema strerror() retorna essas informações. Existe algum sistema operacional UNIX que vem com um utilitário executável que faz essa chamada de sistema, ou eu preciso escrever meu próprio programa para fazer isso?

    
por An̲̳̳drew 09.07.2009 / 21:59

5 respostas

8

Eu costumava fazer

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

Você pode simplesmente colocar o código Perl em um arquivo e colocá-lo no caminho.
Claro que isso pode ser feito usando C também

    
por 09.07.2009 / 22:09
4
~% perror 13
OS error code  13:  Permission denied
~% rpm -qf =perror
mysql-server-5.0.45-7.el5
    
por 09.07.2009 / 22:22
2

Tente strerror (3).

Na página de manual:

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.
    
por 09.07.2009 / 22:11
1

Como solução alternativa, você poderia criar um alias ou uma função em seu shell:

por exemplo. .bashrc

function lookuperror
{
    grep -w "$@" /usr/include/errno.h
}
    
por 09.07.2009 / 22:06
1

cpp -dM pré-processa um arquivo de origem ou de cabeçalho e imprime cada #define que encontrar. É mais robusto do que percorrer /usr/include/errno.h , pois ele obterá todos os arquivos que /usr/include/errno.h inclui.

Combinando cpp -dM com as sugestões dos outros:

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

Insira em .bashrc ou coloque seu conteúdo como um script de shell independente.

    
por 09.07.2009 / 22:44