Tom Seddon
Tom Seddon

Reputation: 2748

Is there a function that will return a symbol name, such as `EIO`, for an `errno` value?

On Linux and OS X, strerror returns a human-readable name. For example, here's what it returned on Linux just now for error number 5.

Input/output error

That's fine, but the man pages use a symbolic name, such as EIO, and don't list the corresponding number. Is there a function anywhere that I can use to get the symbolic name?

Upvotes: 2

Views: 135

Answers (1)

Dummy00001
Dummy00001

Reputation: 17430

I very much doubt that pure POSIX provides facilities to accomplish that in a portable fashion. In most cases I personally just open the /usr/include/errno.h file in editor and browse it from there. (On Linux that eventually leads to /usr/include/asm-generic/errno-base.h and /usr/include/asm-generic/errno.h files where the codes are actually specified.)

Also, for systems with GCC (or clang), I could come up with the scriptlet like this:

gcc -dM -E - < /usr/include/errno.h  |
    grep 'define E\w\+ [0-9]\+$'  |
    sort -k3 -n

The GNU preprocessor has an option (-dM) to print all defines it encounters to the output. That can be used to help parse the /usr/include/errno.h file to extract the error codes.

Upvotes: 2

Related Questions