err(), errx()

Updated: April 19, 2023

Display a formatted error message, and then exit

Synopsis:

#include <err.h>

void err( int eval, 
          const char *fmt, ...);

void errx( int eval,
           const char *fmt, ...);

Arguments:

eval
The value to use as the exit code of the process.
fmt
NULL, or a printf()-style string used to format the message.
Additional arguments
As required by the format string.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The err() and warn() family of functions display a formatted error message on stderr:

Function errno string? Varargs? Exits?
err() Yes No Yes
errx() No No Yes
verr() Yes Yes Yes
verrx() No Yes Yes
vwarn() Yes Yes No
vwarnx() No Yes No
warn() Yes No No
warnx() No No No

The err() function produces a message that consists of:

The errx() function produces a similar message, except that it doesn't include the string associated with errno. The message consists of:

The err() and errx() functions don't return, but exit with the value of the argument eval.

Examples:

Display the current errno information string, and then exit:

if ((p = malloc(size)) == NULL)
   err(1, NULL);
if ((fd = open(file_name, O_RDONLY, 0)) == -1)
   err(1, "%s", file_name);

Display an error message, and then exit:

if (tm.tm_hour < START_TIME)
    errx(1, "too early, wait until %s", start_time_string);

Classification:

Unix

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler Yes
Thread Yes