2004, 2005 by Marc J. Rochkind. All rights reserved. Portions marked "Open Source" may be copied under license.

 

Translating Error-Code Macros to Strings (macrostr.c)

The mechanism described here replaces the code shown on p. 27 of the book. Replacing the errcodes array, as shown in the book, is a new array of structures called macrostr_db, which is defined in the file macrostr.c like this:

static struct {
    char *ms_cat;
    intptr_t ms_code;
    char *ms_macro;
    char *ms_desc;
} macrostr_db[] = {
    #include "macrostr1.incl"
    { NULL, 0, NULL, NULL}
};

The ms_code member's values may not be constant, so they're initialized by this function:

void macrostr_init(void)
{
    if (macrostr_db[0].ms_code == 0) {
        #include "macrostr2.incl"
    }
}

The file macrostr1.incl looks like this:

{"errno", 0, "EPERM", ""},
{"errno", 0, "ENOENT", ""},
{"errno", 0, "ESRCH", ""},
{"errno", 0, "EINTR", ""},
{"errno", 0, "EIO", ""},

... [rest not shown]

and macrostr2.incl looks like this:

macrostr_db[0].ms_code = (intptr_t)EPERM;
macrostr_db[1].ms_code = (intptr_t)ENOENT;
macrostr_db[2].ms_code = (intptr_t)ESRCH;
macrostr_db[3].ms_code = (intptr_t)EINTR;
macrostr_db[4].ms_code = (intptr_t)EIO;

... [rest not shown]

The two "incl" files were produced by the program mkmacrostr.c which takes the text file macrostr.txt as input. Older versions of these files are in the example source archive; the new ones are in a separate archive that you can download from here.