tags:

views:

717

answers:

4

Is there any function in UNIX to the convert errno to its corresponding string for e.g. EIDRM to "EIDRM". Its very annoying to debug to check for errors with these integer errnos.

+5  A: 

strerror() should do it. http://linux.die.net/man/3/strerror

FYI, so that you can find these things more easily, yourself: If you type man errno (or whatever function you're investigating), and look at the very bottom of the man page, you'll see a list of related functions. If you man each of those (taking a guess about which one(s) to do first based on their names) you'll often find the answer to similar questions.

atk
strerror will actually turn them into much more useful strings than just "EIDRM" as the op asked, but that's the answer I would have given too.
Paul Tomblin
+1  A: 

I'm not sure about such enum-style names, but for debugging and error reporting purposes you may use perror(3) or strerror(3) C functions that return a human-readable representation of the error code. Please refer to the man pages for more details.

Andrey Vlasovskikh
I thought perror() just prints to STDERR rather than converting the number to a value. Do I misremember (and misread the man page)?
atk
Yes, `perror` is a debugging facility for `errno`-related issues and it writes its messages to `STDERR`.
Andrey Vlasovskikh
+1 I forgot about perror.
Kinopiko
+2  A: 

Just another solution that solves exactly the problem you have, but in Python instead of C:

>>> import errno
>>> errno.errorcode[errno.EIDRM]
'EIDRM'
Andrey Vlasovskikh
+2  A: 

If you do indeed want EIDRM and not its error string: no. However, on OpenBSD,

man errno|egrep ' [0-9]+ E[A-Z]+'|sed 's/^ *//'|cut -d' ' -f1,2

prints out a nice table of "...\n89 EIDM\n..." that you can convert further into a data structure for the programming language that you'd like to have this function in.

ayrnieu