1ERRC(3bsd) LOCAL ERRC(3bsd)
2
4 errc, verrc, warnc, vwarnc — formatted error messages
5
7 Utility functions from BSD systems (libbsd, -lbsd)
8
10 #include <err.h>
11 (See libbsd(7) for include usage.)
12
13 void
14 errc(int status, int code, const char *fmt, ...);
15
16 void
17 verrc(int status, int code, const char *fmt, va_list args);
18
19 void
20 warnc(int code, const char *fmt, ...);
21
22 void
23 vwarnc(int code, const char *fmt, va_list args);
24
26 The err() and warn() family of functions display a formatted error mes‐
27 sage on the standard error output. In all cases, the last component of
28 the program name, followed by a colon (‘:’) character and a space, are
29 output. The text that follows depends on the function being called. The
30 fmt specification (and associated arguments) may be any format allowed by
31 printf(3) or NULL. If the fmt argument is not NULL, the formatted error
32 message is output.
33
34 The functions all output an error message string affiliated with an error
35 value (see strerror(3)), preceded by a colon character and a space if fmt
36 is not NULL. That is, the output is as follows:
37
38 progname: fmt: error message string
39
40 if fmt is not NULL, or:
41
42 progname: error message string
43
44 if it is.
45
46 The argument code is used as the error value instead of the current value
47 of the global variable errno.
48
49 In all cases, the output is followed by a newline character.
50
51 The errc(), and verrc() functions do not return, but exit with the value
52 of the argument status.
53
55 Display the current errno information string and exit:
56
57 if ((p = malloc(size)) == NULL)
58 err(1, NULL);
59 if ((fd = open(file_name, O_RDONLY, 0)) == -1)
60 err(1, "%s", file_name);
61
62 Display an error message and exit:
63
64 if (tm.tm_hour < START_TIME)
65 errx(1, "too early, wait until %s", start_time_string);
66
67 Warn of an error:
68
69 if ((fd = open(raw_device, O_RDONLY, 0)) == -1)
70 warnx("%s: %s: trying the block device",
71 raw_device, strerror(errno));
72 if ((fd = open(block_device, O_RDONLY, 0)) == -1)
73 err(1, "%s", block_device);
74
76 err(3) exit(3), perror(3), printf(3), strerror(3)
77
79 The functions errc(), verrc(), warnc(), and vwarnc() first appeared in
80 FreeBSD 3.0, NetBSD 7.0 and OpenBSD 5.6.
81
83 It is important never to pass a string with user-supplied data as a for‐
84 mat without using ‘%s’. An attacker can put format specifiers in the
85 string to mangle the stack, leading to a possible security hole. This
86 holds true even if the string has been built “by hand” using a function
87 like snprintf(), as the resulting string may still contain user-supplied
88 conversion specifiers for later interpolation by the err() and warn()
89 family of functions.
90
91 Always be sure to use the proper secure idiom:
92
93 errc(1, 0, "%s", string);
94
95BSD June 20, 2019 BSD