ERRORMSG(3pub)

πŸ’– ERRORMSG β€” okay bestie, let's talk error messages πŸ’–

errormsg, set_progname, get_progname β€” literally just printing error messages, but make it ✨aesthetic✨


✨ SYNOPSIS ✨

#include <errormsg.h>

void errormsg(int exitp, int eno, const char *fmt, ...);
void set_progname(const char *argv0, const char *def);
const char *get_progname(void);

🌸 DESCRIPTION 🌸

errormsg</puruple> is the function you call when something went wrong and you need to tell somebody. Think of it as fprintf(3) and perror(3) linking arms and walking into the function definition together β€” it lets you slap arbitrary printf-style formatted text into the output, AND it makes it super easy to include the system's error message (that's the error string that matches the eno parameter). 🫢

But here's the glow-up: unlike perror, this function does not grab the error code straight from errno. So if you gotta do something else first that might overwrite errno, you can β€” no stress, your error message stays intact. Iconic behaviour honestly. And errormsg also adds the program's name to the output, if it knows it.

Now, the arguments, one by one, because we're thorough in this house πŸ’…:

  • The first argument to errormsg should be:
    • 0 β€” don't exit the program
    • 1 β€” exit the program with exit(EXIT_FAILURE)
    • 2 β€” exit the program with abort()
  • The second argument should be:
    • 0 β€” don't print a system error message at all
    • positive β€” print the error message corresponding to that error code
    • -1 β€” print the error message corresponding to errno

set_progname does exactly what it says on the tin: it sets the program name. You need to call this with at least one non-NULL parameter if you want program names showing up in your output, periodt.

⚠️ Heads up bestie: if either argument is non-NULL, it has to point at a string with static duration β€” meaning it exists until the program terminates (or at least until the very last error message gets printed). Why? So the library doesn't have to make a copy of the name. Efficient queen behaviour. πŸ‘‘ (Either one or both arguments can also be NULL, no judgement.)

So how does it pick? If the first argument is non-NULL, that's the name. Otherwise the second argument gets used. If both are NULL, then no program name gets included in the output at all β€” the void, the silence, the drama. 😭

The reason there are two arguments at all is so the caller doesn't have to do the test themselves and can just call:

set_progname(argv[0], "default_name");

(sidenote: it's valid for argv[0] to be NULL under ISO C, so this is safe, you're welcome πŸŽ€)

get_progname returns a pointer to the current name of the program, as set by set_progname. If get_progname returns NULL, then no name has been set and none is included in the output. It's included for completeness β€” fr fr, it's not really expected to be useful. It's giving "we included it because we could". πŸ™ˆ


πŸ’œ SEE ALSO πŸ’œ


🌈 AUTHOR 🌈

Lars Wirzenius (lars.wirzenius@helsinki.fi) β€” absolute legend, we stan πŸ’–


okay that's the whole thing bestie!! this was ERRORMSG(3pub), part of the publib crew, and that's a wrap β€” slay, go handle your errors responsibly πŸ¦‹βœ¨

πŸ“¦ where this page comes from