1err(3C) Standard C Library Functions err(3C)
2
3
4
6 err, verr, errx, verrx, warn, vwarn, warnx, vwarnx - formatted error
7 messages
8
10 #include <err.h>
11
12 void err(int eval, const char *fmt, ...);
13
14
15 void verr(int eval, const char *fmt, va_list args);
16
17
18 void errx(int eval, const char *fmt, ...);
19
20
21 void verrx(int eval, const char *fmt, va_list args);
22
23
24 void warn(const char *fmt, ...);
25
26
27 void vwarn(const char *fmt, va_list args);
28
29
30 void warnx(const char *fmt, ...);
31
32
33 void vwarnx(const char *fmt, va_list args);
34
35
37 The err() and warn() family of functions display a formatted error mes‐
38 sage on the standard error output. In all cases, the last component of
39 the program name, followed by a colon character and a space, are out‐
40 put. If the fmt argument is not NULL, the formatted error message is
41 output. In the case of the err(), verr(), warn(), and vwarn() func‐
42 tions, the error message string affiliated with the current value of
43 the global variable errno is output next, preceded by a colon character
44 and a space if fmt is not NULL. In all cases, the output is followed by
45 a newline character. The errx(), verrx(), warnx(), and vwarnx() func‐
46 tions will not output this error message string.
47
48
49 The err(), verr(), errx(), and verrx() functions do not return, but
50 instead cause the program to terminate with the status value given by
51 the argument status.
52
54 Example 1 Display the current errno information string and terminate
55 with status indicating failure.
56
57 if ((p = malloc(size)) == NULL)
58 err(EXIT_FAILURE, NULL);
59 if ((fd = open(file_name, O_RDONLY, 0)) == -1)
60 err(EXIT_FAILURE, "%s", file_name);
61
62
63 Example 2 Display an error message and terminate with status indicating
64 failure.
65
66 if (tm.tm_hour < START_TIME)
67 errx(EXIT_FAILURE, "too early, wait until %s", start_time_string);
68
69
70 Example 3 Warn of an error.
71
72 if ((fd = open(raw_device, O_RDONLY, 0)) == -1)
73 warnx("%s: %s: trying the block device",
74 raw_device, strerror(errno));
75 if ((fd = open(block_device, O_RDONLY, 0)) == -1)
76 warn("%s", block_device);
77
78
80 It is important never to pass a string with user-supplied data as a
81 format without using `%s'. An attacker can put format specifiers in the
82 string to mangle the stack, leading to a possible security hole. This
83 holds true even if the string has been built ``by hand'' using a func‐
84 tion like snprintf(3C), as the resulting string can still contain user-
85 supplied conversion specifiers for later interpolation by the err() and
86 warn() functions.
87
88
89 Always be sure to use the proper secure idiom:
90
91 err(1, "%s", string);
92
93
95 See attributes(5) for descriptions of the following attributes:
96
97
98
99
100 ┌─────────────────────────────┬─────────────────────────────┐
101 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
102 ├─────────────────────────────┼─────────────────────────────┤
103 │Interface Stability │Committed │
104 ├─────────────────────────────┼─────────────────────────────┤
105 │MT-Level │Safe with Exceptions │
106 └─────────────────────────────┴─────────────────────────────┘
107
108
109 These functions are safe to use in multithreaded applications as long
110 as setlocale(3C) is not being called to change the locale.
111
113 exit(3C), getexecname(3C), setlocale(3C), strerror(3C), attributes(5)
114
115
116
117SunOS 5.11 20 Aug 2007 err(3C)