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