. , , , , .
errno.
If the symbol is not known in the implementation below, a general macro is written, for example ERROR_161. If you know the script that generated the code, review its documentation and manually enable possible error codes. This way, you will eventually create a code library. (I agree that this is not very elegant.)
Codes are created by macros and stored as a static array of strings with designated initializers. The size of the array is determined by the largest index.
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#define ERRNAME(X) [X] = #X
static const char *errname[] = {
ERRNAME(E2BIG),
ERRNAME(EACCES),
ERRNAME(EADDRINUSE),
ERRNAME(EADDRNOTAVAIL),
ERRNAME(EAFNOSUPPORT),
ERRNAME(EAGAIN),
};
const char *errsym(int e)
{
static char buf[20];
if (e >= 0 && e < (sizeof(errname) / sizeof(*errname))) {
if (errname[e]) return errname[e];
}
snprintf(buf, sizeof(buf), "ERROR_%u", (unsigned int) e);
return buf;
}
source
share