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
errormsgshould be:0β don't exit the program1β exit the program withexit(EXIT_FAILURE)2β exit the program withabort()
- 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 toerrno
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 beNULL, 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 π
publib(3)β go read it, iykyk
π 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
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms