xref: /freebsd/lib/libc/gen/err.3 (revision 32cd3ee5901ea33d41ff550e5f40ce743c8d4165)
1.\" Copyright (c) 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.\"
28.Dd February 23, 2026
29.Dt ERR 3
30.Os
31.Sh NAME
32.Nm err ,
33.Nm verr ,
34.Nm errc ,
35.Nm verrc ,
36.Nm errx ,
37.Nm verrx ,
38.Nm warn ,
39.Nm vwarn ,
40.Nm warnc ,
41.Nm vwarnc ,
42.Nm warnx ,
43.Nm vwarnx ,
44.Nm err_set_exit ,
45.Nm err_set_file
46.Nd formatted error messages
47.Sh LIBRARY
48.Lb libc
49.Sh SYNOPSIS
50.In err.h
51.Ft void
52.Fn err "int eval" "const char *fmt" "..."
53.Ft void
54.Fn err_set_exit "void (*exitf)(int)"
55.Ft void
56.Fn err_set_file "void *vfp"
57.Ft void
58.Fn errc "int eval" "int code" "const char *fmt" "..."
59.Ft void
60.Fn errx "int eval" "const char *fmt" "..."
61.Ft void
62.Fn warn "const char *fmt" "..."
63.Ft void
64.Fn warnc "int code" "const char *fmt" "..."
65.Ft void
66.Fn warnx "const char *fmt" "..."
67.In stdarg.h
68.Ft void
69.Fn verr "int eval" "const char *fmt" "va_list args"
70.Ft void
71.Fn verrc "int eval" "int code" "const char *fmt" "va_list args"
72.Ft void
73.Fn verrx "int eval" "const char *fmt" "va_list args"
74.Ft void
75.Fn vwarn "const char *fmt" "va_list args"
76.Ft void
77.Fn vwarnc "int code" "const char *fmt" "va_list args"
78.Ft void
79.Fn vwarnx "const char *fmt" "va_list args"
80.Sh DESCRIPTION
81The
82.Fn err
83and
84.Fn warn
85family of functions display a formatted error message on the standard
86error output, or on another file specified using the
87.Fn err_set_file
88function.
89In all cases, the last component of the program name, a colon character,
90and a space are output.
91If the
92.Fa fmt
93argument is not NULL, the
94.Xr printf 3 Ns
95-like formatted error message is output.
96The output is terminated by a newline character.
97.Pp
98The
99.Fn err ,
100.Fn errc ,
101.Fn verr ,
102.Fn verrc ,
103.Fn warn ,
104.Fn warnc ,
105.Fn vwarn ,
106and
107.Fn vwarnc
108functions append an error message obtained from
109.Xr strerror 3
110based on a supplied error code value or the global variable
111.Va errno ,
112preceded by another colon and space unless the
113.Fa fmt
114argument is
115.Dv NULL .
116.Pp
117If the kernel returned an extended error string in addition to the
118.Va errno
119code, the
120.Fn err
121function prints the string with interpolated values for parameters,
122as provided to the corresponding invocation of
123.Xr EXTERROR 9 .
124If the extended error string was not provided, but extended error
125information was, or even if string was provided and the
126.Ev EXTERROR_VERBOSE
127environment variable is present, an additional report is printed.
128The report includes at least the category of the error, the name of
129the source file (if known by the used version of libc),
130the source line number, and parameters.
131If the
132.Ev EXTERROR_VERBOSE
133environment variable is present and set to "brief",
134the report adds only the name of
135the source file (if known by the used version of libc)
136and the source line number.
137The format of the printed string is not contractual and might be changed.
138.Pp
139In the case of the
140.Fn errc ,
141.Fn verrc ,
142.Fn warnc ,
143and
144.Fn vwarnc
145functions,
146the
147.Fa code
148argument is used to look up the error message.
149.Pp
150The
151.Fn err ,
152.Fn verr ,
153.Fn warn ,
154and
155.Fn vwarn
156functions use the global variable
157.Va errno
158to look up the error message.
159.Pp
160The
161.Fn errx
162and
163.Fn warnx
164functions do not append an error message.
165.Pp
166The
167.Fn err ,
168.Fn verr ,
169.Fn errc ,
170.Fn verrc ,
171.Fn errx ,
172and
173.Fn verrx
174functions do not return, but exit with the value of the argument
175.Fa eval .
176It is recommended that the standard values defined in
177.Xr sysexits 3
178be used for the value of
179.Fa eval .
180The
181.Fn err_set_exit
182function can be used to specify a function which is called before
183.Xr exit 3
184to perform any necessary cleanup; passing a null function pointer for
185.Va exitf
186resets the hook to do nothing.
187The
188.Fn err_set_file
189function sets the output stream used by the other functions.
190Its
191.Fa vfp
192argument must be either a pointer to an open stream
193(possibly already converted to void *)
194or a null pointer
195(in which case the output stream is set to standard error).
196.Sh EXAMPLES
197Display the current errno information string and exit:
198.Bd -literal -offset indent
199if ((p = malloc(size)) == NULL)
200	err(EX_OSERR, NULL);
201if ((fd = open(file_name, O_RDONLY, 0)) == -1)
202	err(EX_NOINPUT, "%s", file_name);
203.Ed
204.Pp
205Display an error message and exit:
206.Bd -literal -offset indent
207if (tm.tm_hour < START_TIME)
208	errx(EX_DATAERR, "too early, wait until %s",
209	    start_time_string);
210.Ed
211.Pp
212Warn of an error:
213.Bd -literal -offset indent
214if ((fd = open(raw_device, O_RDONLY, 0)) == -1)
215	warnx("%s: %s: trying the block device",
216	    raw_device, strerror(errno));
217if ((fd = open(block_device, O_RDONLY, 0)) == -1)
218	err(EX_OSFILE, "%s", block_device);
219.Ed
220.Pp
221Warn of an error without using the global variable
222.Va errno :
223.Bd -literal -offset indent
224error = my_function();	/* returns a value from <errno.h> */
225if (error != 0)
226	warnc(error, "my_function");
227.Ed
228.Sh SEE ALSO
229.Xr exit 3 ,
230.Xr fmtmsg 3 ,
231.Xr printf 3 ,
232.Xr strerror 3 ,
233.Xr sysexits 3
234.Sh STANDARDS
235The
236.Fn err
237and
238.Fn warn
239families of functions are
240.Bx
241extensions.
242As such they should not be used in truly portable code.
243Use
244.Fn strerror
245or similar functions instead.
246.Sh HISTORY
247The
248.Fn err
249and
250.Fn warn
251functions first appeared in
252.Bx 4.4 .
253The
254.Fn err_set_exit
255and
256.Fn err_set_file
257functions first appeared in
258.Fx 2.1 .
259The
260.Fn errc
261and
262.Fn warnc
263functions first appeared in
264.Fx 3.0 .
265