How can I print the symbolic name of an errno in C?

Will picture Will · Nov 10, 2013 · Viewed 7.2k times · Source

I can use perror() or strerror() to print the "human readable" error message belonging to an errno, but what if I also want to print the symbolic name (e.g., "EAGAIN") of the errno?

Any convenient function or macro to do that?

update: attaching the code I ended up writing, based on the idea of the accepted answer below and its comments:

#include <ctype.h>
#include <errno.h>
#include <stdio.h>

int get_errno_name(char *buf, int buf_size) {
    // Using the linux-only gawk instead of awk, because of the convenient
    // match() functionality. For Posix portability, use a different recipe...
    char cmd[] = "e=       && " // errno to be inserted here (max digits = 6)
                 "echo '#include <errno.h>' | "
                 "gcc -dM -E - | " // optionally, use $CC inead of gcc
                 "gawk \"match(\\$0, /^#[[:space:]]*define[[:space:]]+"
                     "(E[[:alnum:]]+)[[:space:]]+$e($|[^[:alnum:]])/, m) "
                     "{ print m[1] }\"";
    {
        // Insert the errno as the "e" shell variable in the command above.
        int errno_digit_c = snprintf(cmd + 2, 6, "%d", errno);
        if (errno_digit_c < 1) {
            fprintf(stderr, "Failed to stringify an errno "
                            "in get_errno_name().\n");
            return -1;
        }
        // Replace the inserted terminating '\0' with whitespace
        cmd[errno_digit_c + 2] = ' ';
    }
    FILE *f = popen(cmd, "r");
    if (f == NULL) {
        perror("Failed to popen() in get_errno_name()");
        return -1;
    }
    int read_size = 0, c;
    while ((c = getc(f)) != EOF) {
        if (isalnum(c)) {
            buf[read_size++] = c;
            if (read_size + 1 > buf_size) {
                fprintf(stderr, "Read errno name is larger than the character "
                                "buffer supplied to get_errno_name().\n");
                return -1;
            }
        }
    }
    buf[read_size++] = '\0';
    if (pclose(f) == -1) {
        perror("Failed to pclose() in get_errno_name()");
        return -1;
    }
    return read_size;
}

Answer

Jonathan Leffler picture Jonathan Leffler · Nov 10, 2013

There isn't a simple way to do that.

You can create a program — and I have created one, which could be repackaged as a library function — that converts from number to name. But generating the table is moderately hard. I use a Perl script that runs the compiler (GCC or equivalent) with options (-H) to list the headers that are included by including /usr/include/errno.h, and then scans those files, looking for names (#define plus E followed by an upper-case letter or digit), numbers, and comments. This works on Linux, Mac OS X, Solaris, HP-UX, AIX. It isn't particularly trivial.

Beware, the errno.h on Mac OS X includes a name ELAST (a name reserved for the implementation to use) that is a duplicate of the highest number (but the mapping changes from release to release; it was 105 in Mountain Lion, I believe, but it is 106 in Mavericks).