xref: /freebsd/lib/libc/gen/err.3 (revision df7f5d4de4592a8948a25ce01e5bddfbb7ce39dc)
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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	From: @(#)err.3	8.1 (Berkeley) 6/9/93
33.\"	$Id: err.3,v 1.6 1997/02/22 14:58:02 peter Exp $
34.\"
35.Dd April 13, 1995
36.Dt ERR 3
37.Os BSD 4
38.Sh NAME
39.Nm err ,
40.Nm verr ,
41.Nm errx ,
42.Nm verrx ,
43.Nm warn ,
44.Nm vwarn ,
45.Nm warnx ,
46.Nm vwarnx ,
47.Nm err_set_file ,
48.Nm err_set_exit
49.Nd formatted error messages
50.Sh SYNOPSIS
51.Fd #include <err.h>
52.Ft void
53.Fn err "int eval" "const char *fmt" "..."
54.Ft void
55.Fn errx "int eval" "const char *fmt" "..."
56.Ft void
57.Fn warn "const char *fmt" "..."
58.Ft void
59.Fn warnx "const char *fmt" "..."
60.Ft void
61.Fn err_set_file "void *fp"
62.Ft void
63.Fn err_set_exit "void (*exitf)(int)"
64.Fd #include <stdarg.h>
65.Ft void
66.Fn verr "int eval" "const char *fmt" "va_list args"
67.Ft void
68.Fn verrx "int eval" "const char *fmt" "va_list args"
69.Ft void
70.Fn vwarn "const char *fmt" "va_list args"
71.Ft void
72.Fn vwarnx "const char *fmt" "va_list args"
73.Sh DESCRIPTION
74The
75.Fn err
76and
77.Fn warn
78family of functions display a formatted error message on the standard
79error output, or on another file specified using the
80.Fn err_set_file
81function.
82In all cases, the last component of the program name, a colon character,
83and a space are output.
84If the
85.Va fmt
86argument is not NULL, the formatted error message, a colon character,
87and a space are output.
88In the case of the
89.Fn err ,
90.Fn verr ,
91.Fn warn ,
92and
93.Fn vwarn
94functions, the error message string affiliated with the current value of
95the global variable
96.Va errno
97is output.
98In all cases, the output is followed by a newline character.
99.Pp
100The
101.Fn err ,
102.Fn verr ,
103.Fn errx ,
104and
105.Fn verrx
106functions do not return, but exit with the value of the argument
107.Fa eval .
108The
109.Fn err_set_exit
110function can be used to specify a function which is called before
111.Xr exit 3
112to perform any necessary cleanup; passing a null function pointer for
113.Va exitf
114resets the hook to do nothing.
115.Sh EXAMPLES
116Display the current errno information string and exit:
117.Bd -literal -offset indent
118if ((p = malloc(size)) == NULL)
119	err(1, NULL);
120if ((fd = open(file_name, O_RDONLY, 0)) == -1)
121	err(1, "%s", file_name);
122.Ed
123.Pp
124Display an error message and exit:
125.Bd -literal -offset indent
126if (tm.tm_hour < START_TIME)
127	errx(1, "too early, wait until %s", start_time_string);
128.Ed
129.Pp
130Warn of an error:
131.Bd -literal -offset indent
132if ((fd = open(raw_device, O_RDONLY, 0)) == -1)
133	warnx("%s: %s: trying the block device",
134	    raw_device, strerror(errno));
135if ((fd = open(block_device, O_RDONLY, 0)) == -1)
136	err(1, "%s", block_device);
137.Ed
138.Sh SEE ALSO
139.Xr exit 3 ,
140.Xr strerror 3
141.Sh HISTORY
142The
143.Fn err
144and
145.Fn warn
146functions first appeared in
147.Bx 4.4 .
148