xref: /openbsd-src/lib/libc/gen/err.3 (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1.\"	$OpenBSD: err.3,v 1.12 2001/02/17 17:01:03 pjanzen Exp $
2.\"
3.\" Copyright (c) 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\"    must display the following acknowledgement:
16.\"	This product includes software developed by the University of
17.\"	California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\"    may be used to endorse or promote products derived from this software
20.\"    without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.Dd August 8, 1997
35.Dt ERR 3
36.Os
37.Sh NAME
38.Nm err ,
39.Nm verr ,
40.Nm errx ,
41.Nm verrx ,
42.Nm warn ,
43.Nm vwarn ,
44.Nm warnx ,
45.Nm vwarnx
46.Nd formatted error messages
47.Sh SYNOPSIS
48.Fd #include <err.h>
49.Ft void
50.Fn err "int eval" "const char *fmt" "..."
51.Ft void
52.Fn verr "int eval" "const char *fmt" "va_list args"
53.Ft void
54.Fn errx "int eval" "const char *fmt" "..."
55.Ft void
56.Fn verrx "int eval" "const char *fmt" "va_list args"
57.Ft void
58.Fn warn "const char *fmt" "..."
59.Ft void
60.Fn vwarn "const char *fmt" "va_list args"
61.Ft void
62.Fn warnx "const char *fmt" "..."
63.Ft void
64.Fn vwarnx "const char *fmt" "va_list args"
65.Sh DESCRIPTION
66The
67.Fn err
68and
69.Fn warn
70family of functions display a formatted error message on the standard
71error output.
72In all cases, the last component of the program name, followed by
73a colon
74.Pq Sq \&:
75character and a space, are output.
76The text that follows depends on the function being called.
77The
78.Fa fmt
79specification (and associated arguments) may be any format allowed by
80.Xr printf 3 ,
81a simple string, or
82.Dv NULL .
83If the
84.Fa fmt
85argument is not
86.Dv NULL ,
87the formatted error message is output.
88.Pp
89In the case of the
90.Fn err ,
91.Fn verr ,
92.Fn warn ,
93and
94.Fn vwarn
95functions only, the error message string affiliated with the current value of
96the global variable
97.Va errno
98is output (see
99.Xr strerror 3 ) ,
100preceded by a colon character and a space if
101.Fa fmt
102is not
103.Dv NULL .
104That is, the output is as follows:
105.Bd -literal -offset indent
106progname: fmt: error message string
107.Ed
108.Pp
109if
110.Fa fmt
111is not
112.Dv NULL ,
113or:
114.Bd -literal -offset indent
115progname: error message string
116.Ed
117.Pp
118if it is.
119.Pp
120The counterpart functions,
121.Fn errx ,
122.Fn verrx ,
123.Fn warnx ,
124and
125.Fn vwarnx ,
126do not output the error message string, so the output looks like the following:
127.Bd -literal -offset indent
128progname: fmt
129.Ed
130.Pp
131In all cases, the output is followed by a newline character.
132.Pp
133The
134.Fn err ,
135.Fn verr ,
136.Fn errx ,
137and
138.Fn verrx
139functions do not return, but exit with the value of the argument
140.Fa eval .
141.Sh EXAMPLES
142Display the current
143.Va errno
144information string and exit:
145.Bd -literal -offset indent
146if ((p = malloc(size)) == NULL)
147	err(1, NULL);
148if ((fd = open(file_name, O_RDONLY, 0)) == -1)
149	err(1, "%s", file_name);
150.Ed
151.Pp
152Display an error message and exit:
153.Bd -literal -offset indent
154if (tm.tm_hour < START_TIME)
155	errx(1, "too early, wait until %s", start_time_string);
156.Ed
157.Pp
158Warn of an error:
159.Bd -literal -offset indent
160if ((fd = open(raw_device, O_RDONLY, 0)) == -1)
161	warnx("%s: %s: trying the block device",
162	    raw_device, strerror(errno));
163if ((fd = open(block_device, O_RDONLY, 0)) == -1)
164	err(1, "%s", block_device);
165.Ed
166.Sh SEE ALSO
167.Xr exit 3 ,
168.Xr perror 3 ,
169.Xr printf 3 ,
170.Xr strerror 3
171.Sh HISTORY
172The
173.Fn err
174and
175.Fn warn
176functions first appeared in
177.Bx 4.4 .
178.Sh CAVEATS
179It is important never to pass a string with user-supplied data as a
180format without using
181.Ql %s .
182An attacker can put format specifiers in the string to mangle your stack,
183leading to a possible security hole.
184This holds true even if you have built the string
185.Dq by hand
186using a function like
187.Fn snprintf ,
188as the resulting string may still contain user-supplied conversion specifiers
189for later interpolation by the
190.Fn err
191and
192.Fn warn
193functions.
194.Pp
195Always be sure to use the proper secure idiom:
196.Bd -literal -offset indent
197err(1, "%s", string);
198.Ed
199