Is there a command-line tool that will take a symbolic errno such as EINVAL
and print the corresponding string, Invalid argument
?
I would like to avoid having to find that EINVAL is value 22 on my system and then using$ perror 22
.
Ideally I could write something like
$ errorcommand EINVAL
Invalid argument
$
Rob Wells is partially correct. Unfortunately
/usr/include/asm/errno.h
is nonstandard. You really need to grep/usr/include/errno.h
and/usr/include/*/errno.h
.To make this errorcommand, try adding this to your .bashrc file:
Which works like this:
Rob Wells is partially correct. Unfortunately
/usr/include/asm/errno.h
is nonstandard. You really need to grep/usr/include/errno.h
and/usr/include/*/errno.h
.To make this errorcommand, try adding this to your .bashrc file:
Which works like this:
A compact bash script that exactly does what you want:
Caution: As this script uses the location of ERROR MACROS,this might not be portable although it works on my system.
AFAIK, there isn't a standard tool that does the job. At one level, it wouldn't be particularly hard to write one - the messiest parts are finding the correct file to parse (is is often, but by no means always, /usr/include/sys/errno.h) and then taking the data from that to do the mapping of names to numbers. I have not found a system that uses enum values rather than #define values, but it is probably only a matter of time. It is also a moot point whether to generate a triple consisting of token number (EINTR, etc), token name ("EINTR", etc) and error message ("Interrupted system call", etc), or whether to use just the number and name and leave it to 'strerror()' to supply the message.
As I said, it isn't particularly hard. I already had a program called 'errno' that accepted pure numeric values and printed the corresponding error messages:
I've written a Perl script and hacked the program to handle symbolic error numbers too:
It does not handle ranges of symbolic error numbers (exercise for the reader).
generrno.pl
errno.c
The code needs some supporting files - stderr.h, range.h, range2.c and stderrmin.c (a simpler version of the stderr.c I normally use, which has extra bells and whistles for handling syslog and writing to file descriptors instead of file pointers.).
stderr.h
range.h
range2.c
stderrmin.c
This is about 400 lines, instead of about 700 lines. Yes, it is overkill for this program; I don't use it only in this program.
For people that want a quick, on-liner:
e.g.
At least for Ubuntu (12.04 and later, to my certain knowledge), there's an
errno
utility you can easily install viaapt-get install errno
.