xref: /freebsd/lib/libc/gen/err.3 (revision e2afbc45258f2fa4bdcf126e959ac660e76fc802)
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 March 29, 2012
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.
131The format of the printed string is not contractual and might be changed.
132.Pp
133In the case of the
134.Fn errc ,
135.Fn verrc ,
136.Fn warnc ,
137and
138.Fn vwarnc
139functions,
140the
141.Fa code
142argument is used to look up the error message.
143.Pp
144The
145.Fn err ,
146.Fn verr ,
147.Fn warn ,
148and
149.Fn vwarn
150functions use the global variable
151.Va errno
152to look up the error message.
153.Pp
154The
155.Fn errx
156and
157.Fn warnx
158functions do not append an error message.
159.Pp
160The
161.Fn err ,
162.Fn verr ,
163.Fn errc ,
164.Fn verrc ,
165.Fn errx ,
166and
167.Fn verrx
168functions do not return, but exit with the value of the argument
169.Fa eval .
170It is recommended that the standard values defined in
171.Xr sysexits 3
172be used for the value of
173.Fa eval .
174The
175.Fn err_set_exit
176function can be used to specify a function which is called before
177.Xr exit 3
178to perform any necessary cleanup; passing a null function pointer for
179.Va exitf
180resets the hook to do nothing.
181The
182.Fn err_set_file
183function sets the output stream used by the other functions.
184Its
185.Fa vfp
186argument must be either a pointer to an open stream
187(possibly already converted to void *)
188or a null pointer
189(in which case the output stream is set to standard error).
190.Sh EXAMPLES
191Display the current errno information string and exit:
192.Bd -literal -offset indent
193if ((p = malloc(size)) == NULL)
194	err(EX_OSERR, NULL);
195if ((fd = open(file_name, O_RDONLY, 0)) == -1)
196	err(EX_NOINPUT, "%s", file_name);
197.Ed
198.Pp
199Display an error message and exit:
200.Bd -literal -offset indent
201if (tm.tm_hour < START_TIME)
202	errx(EX_DATAERR, "too early, wait until %s",
203	    start_time_string);
204.Ed
205.Pp
206Warn of an error:
207.Bd -literal -offset indent
208if ((fd = open(raw_device, O_RDONLY, 0)) == -1)
209	warnx("%s: %s: trying the block device",
210	    raw_device, strerror(errno));
211if ((fd = open(block_device, O_RDONLY, 0)) == -1)
212	err(EX_OSFILE, "%s", block_device);
213.Ed
214.Pp
215Warn of an error without using the global variable
216.Va errno :
217.Bd -literal -offset indent
218error = my_function();	/* returns a value from <errno.h> */
219if (error != 0)
220	warnc(error, "my_function");
221.Ed
222.Sh SEE ALSO
223.Xr exit 3 ,
224.Xr fmtmsg 3 ,
225.Xr printf 3 ,
226.Xr strerror 3 ,
227.Xr sysexits 3
228.Sh STANDARDS
229The
230.Fn err
231and
232.Fn warn
233families of functions are
234.Bx
235extensions.
236As such they should not be used in truly portable code.
237Use
238.Fn strerror
239or similar functions instead.
240.Sh HISTORY
241The
242.Fn err
243and
244.Fn warn
245functions first appeared in
246.Bx 4.4 .
247The
248.Fn err_set_exit
249and
250.Fn err_set_file
251functions first appeared in
252.Fx 2.1 .
253The
254.Fn errc
255and
256.Fn warnc
257functions first appeared in
258.Fx 3.0 .
259