Scroll to navigation



errormsg, set_progname, get_progname - printing error messages


#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);


The errormsg function is used for printing error messages. It is a like a combination of fprintf(3) and perror(3), in that it makes it easy to add arbitrary, printf-like formatted text to the output, and makes it easy to include the system's error message (the error string corresponding to the eno parameter). Unlike perror, this function does not get the error code directly from errno, thus making it easier to do something else that might set it before printing out the error message. errormsg also adds the name of the program to the output, if known.

The first argument to errormsg should be 0 (don't exit program), 1 (exit program with exit(EXIT_FAILURE)) or 2 (with abort()). The second one should be 0 (don't print system error message), positive (print error message corresponding to the error code), or -1 (print the error message corresponding to errno).

The set_progname function sets the program name. You need to call this function with at least one non-NULL parameter to get the program names included in the output. If either argument is non-NULL, it should point at strings that have static duration, i.e. they exist until the program terminates (or at least until the last error message has been printed); this is so that it is not necessary to create a copy of the name. (Either or both arguments can also be NULL.)

If the first argument is non-NULL, that is used as the name, otherwise the seconds argument is used. If both are NULL, no program name is included in the output. The reason for having two arguments is so that the caller doesn't have to do the test, and can just call

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

(it is valid for argv[0] to be NULL, under ISO C).

The get_progname function 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. This function is included for completeness, it is not really expected to be useful.




Lars Wirzenius (