Home

shell: errno memo

The errno manpage gives a list of defined error codes and their meaning.
Unfortunately it does not give the associated value.

When writing a program, calling standard function is possible to reverse this mapping. However when debugging a program, it can become tedious. Grepping the value in /usr/include/errno.h gets old fast.

A C compiler preprocessor can be used. The command is a little longish so putting it in a shell function helps:

# $1: (nil) or errno value
# output: full errno list or errno value name
errno () {(
    err=${1}

    [ "$err" ] && filter="| grep \"${err}\""
    filter="$filter |column -t"
    cmdline="cc -E -dM /usr/include/errno.h \
            |grep 'define E' \
            |sort -n -k 3 \
            |cut -d' ' -f2- \
            $filter"
    eval $cmdline
)}

$ errno 26
ETXTBSY  26
ENOKEY   126
$ errno ENOBUFS
ENOBUFS  105

So many seconds saved, now let's do something less productive.