xref: /freebsd/lib/libc/gen/err.3 (revision aeecffebe50e876dd229e3af1a19fa226c23b8d3)
158f0484fSRodney W. Grimes.\" Copyright (c) 1993
258f0484fSRodney W. Grimes.\"	The Regents of the University of California.  All rights reserved.
358f0484fSRodney W. Grimes.\"
458f0484fSRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without
558f0484fSRodney W. Grimes.\" modification, are permitted provided that the following conditions
658f0484fSRodney W. Grimes.\" are met:
758f0484fSRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright
858f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer.
958f0484fSRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright
1058f0484fSRodney W. Grimes.\"    notice, this list of conditions and the following disclaimer in the
1158f0484fSRodney W. Grimes.\"    documentation and/or other materials provided with the distribution.
1258f0484fSRodney W. Grimes.\" 3. All advertising materials mentioning features or use of this software
1358f0484fSRodney W. Grimes.\"    must display the following acknowledgement:
1458f0484fSRodney W. Grimes.\"	This product includes software developed by the University of
1558f0484fSRodney W. Grimes.\"	California, Berkeley and its contributors.
1658f0484fSRodney W. Grimes.\" 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes.\"    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes.\"    without specific prior written permission.
1958f0484fSRodney W. Grimes.\"
2058f0484fSRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes.\" SUCH DAMAGE.
3158f0484fSRodney W. Grimes.\"
323688be0eSGarrett Wollman.\"	From: @(#)err.3	8.1 (Berkeley) 6/9/93
337f3dea24SPeter Wemm.\" $FreeBSD$
3458f0484fSRodney W. Grimes.\"
35a2fd3702SRuslan Ermilov.Dd March 6, 1999
3658f0484fSRodney W. Grimes.Dt ERR 3
37a307d598SRuslan Ermilov.Os
3858f0484fSRodney W. Grimes.Sh NAME
3958f0484fSRodney W. Grimes.Nm err ,
4058f0484fSRodney W. Grimes.Nm verr ,
41b4b4fb87SGarrett Wollman.Nm errc ,
42b4b4fb87SGarrett Wollman.Nm verrc ,
4358f0484fSRodney W. Grimes.Nm errx ,
4458f0484fSRodney W. Grimes.Nm verrx ,
4558f0484fSRodney W. Grimes.Nm warn ,
4658f0484fSRodney W. Grimes.Nm vwarn ,
47b4b4fb87SGarrett Wollman.Nm warnc ,
48b4b4fb87SGarrett Wollman.Nm vwarnc ,
4958f0484fSRodney W. Grimes.Nm warnx ,
503688be0eSGarrett Wollman.Nm vwarnx ,
516da7e7c6SBruce Evans.Nm err_set_exit ,
526da7e7c6SBruce Evans.Nm err_set_file
5358f0484fSRodney W. Grimes.Nd formatted error messages
5425bb73e0SAlexey Zelkin.Sh LIBRARY
5525bb73e0SAlexey Zelkin.Lb libc
5658f0484fSRodney W. Grimes.Sh SYNOPSIS
5732eef9aeSRuslan Ermilov.In err.h
5858f0484fSRodney W. Grimes.Ft void
5958f0484fSRodney W. Grimes.Fn err "int eval" "const char *fmt" "..."
6058f0484fSRodney W. Grimes.Ft void
616da7e7c6SBruce Evans.Fn err_set_exit "void (*exitf)(int)"
626da7e7c6SBruce Evans.Ft void
636da7e7c6SBruce Evans.Fn err_set_file "void *vfp"
646da7e7c6SBruce Evans.Ft void
65b4b4fb87SGarrett Wollman.Fn errc "int eval" "int code" "const char *fmt" "..."
66b4b4fb87SGarrett Wollman.Ft void
6758f0484fSRodney W. Grimes.Fn errx "int eval" "const char *fmt" "..."
6858f0484fSRodney W. Grimes.Ft void
6958f0484fSRodney W. Grimes.Fn warn "const char *fmt" "..."
7058f0484fSRodney W. Grimes.Ft void
71b4b4fb87SGarrett Wollman.Fn warnc "int code" "const char *fmt" "..."
7258f0484fSRodney W. Grimes.Ft void
73b4b4fb87SGarrett Wollman.Fn warnx "const char *fmt" "..."
7432eef9aeSRuslan Ermilov.In stdarg.h
757a30f183SBruce Evans.Ft void
767a30f183SBruce Evans.Fn verr "int eval" "const char *fmt" "va_list args"
777a30f183SBruce Evans.Ft void
78b4b4fb87SGarrett Wollman.Fn verrc "int eval" "int code" "const char *fmt" "va_list args"
79b4b4fb87SGarrett Wollman.Ft void
807a30f183SBruce Evans.Fn verrx "int eval" "const char *fmt" "va_list args"
817a30f183SBruce Evans.Ft void
827a30f183SBruce Evans.Fn vwarn "const char *fmt" "va_list args"
837a30f183SBruce Evans.Ft void
84b4b4fb87SGarrett Wollman.Fn vwarnc "int code" "const char *fmt" "va_list args"
85b4b4fb87SGarrett Wollman.Ft void
867a30f183SBruce Evans.Fn vwarnx "const char *fmt" "va_list args"
8758f0484fSRodney W. Grimes.Sh DESCRIPTION
8858f0484fSRodney W. GrimesThe
8958f0484fSRodney W. Grimes.Fn err
9058f0484fSRodney W. Grimesand
9158f0484fSRodney W. Grimes.Fn warn
9258f0484fSRodney W. Grimesfamily of functions display a formatted error message on the standard
933688be0eSGarrett Wollmanerror output, or on another file specified using the
943688be0eSGarrett Wollman.Fn err_set_file
953688be0eSGarrett Wollmanfunction.
9658f0484fSRodney W. GrimesIn all cases, the last component of the program name, a colon character,
9758f0484fSRodney W. Grimesand a space are output.
9858f0484fSRodney W. GrimesIf the
99b4183771SGuy Helmer.Fa fmt
10007a9238fSDavid E. O'Brienargument is not NULL, the
10164437d39SRuslan Ermilov.Xr printf 3 Ns
10207a9238fSDavid E. O'Brien-like formatted error message is output.
103b4183771SGuy HelmerThe output is terminated by a newline character.
104b4183771SGuy Helmer.Pp
105b4183771SGuy HelmerThe
106b4183771SGuy Helmer.Fn err ,
107b4183771SGuy Helmer.Fn errc ,
108b4183771SGuy Helmer.Fn verr ,
109b4183771SGuy Helmer.Fn verrc ,
110b4183771SGuy Helmer.Fn warn ,
111b4183771SGuy Helmer.Fn warnc ,
112b4183771SGuy Helmer.Fn vwarn ,
113b4183771SGuy Helmerand
114b4183771SGuy Helmer.Fn vwarnc
115b4183771SGuy Helmerfunctions append an error message obtained from
116b4183771SGuy Helmer.Xr strerror 3
117aeecffebSDiomidis Spinellisbased on a supplied error code value or the global variable
118b4183771SGuy Helmer.Va errno ,
1197c637968SJeroen Ruigrok van der Wervenpreceded by another colon and space unless the
120b4183771SGuy Helmer.Fa fmt
121b4183771SGuy Helmerargument is
122b4183771SGuy Helmer.Dv NULL .
123b4183771SGuy Helmer.Pp
12458f0484fSRodney W. GrimesIn the case of the
125b4b4fb87SGarrett Wollman.Fn errc ,
126b4b4fb87SGarrett Wollman.Fn verrc ,
127b4b4fb87SGarrett Wollman.Fn warnc ,
12858f0484fSRodney W. Grimesand
129b4b4fb87SGarrett Wollman.Fn vwarnc
130ac51e282SRobert Nordierfunctions,
131b4183771SGuy Helmerthe
132b4183771SGuy Helmer.Fa code
133b4183771SGuy Helmerargument is used to look up the error message.
13458f0484fSRodney W. Grimes.Pp
13558f0484fSRodney W. GrimesThe
13658f0484fSRodney W. Grimes.Fn err ,
13758f0484fSRodney W. Grimes.Fn verr ,
138b4b4fb87SGarrett Wollman.Fn warn ,
139b4b4fb87SGarrett Wollmanand
140b4b4fb87SGarrett Wollman.Fn vwarn
141b4b4fb87SGarrett Wollmanfunctions use the global variable
142b4b4fb87SGarrett Wollman.Va errno
143b4183771SGuy Helmerto look up the error message.
144b4183771SGuy Helmer.Pp
145b4183771SGuy HelmerThe
146b4183771SGuy Helmer.Fn errx
147b4183771SGuy Helmerand
148b4183771SGuy Helmer.Fn warnx
149b4183771SGuy Helmerfunctions do not append an error message.
150b4b4fb87SGarrett Wollman.Pp
151b4b4fb87SGarrett WollmanThe
152b4b4fb87SGarrett Wollman.Fn err ,
153b4b4fb87SGarrett Wollman.Fn verr ,
154b4b4fb87SGarrett Wollman.Fn errc ,
155b4b4fb87SGarrett Wollman.Fn verrc ,
15658f0484fSRodney W. Grimes.Fn errx ,
15758f0484fSRodney W. Grimesand
15858f0484fSRodney W. Grimes.Fn verrx
15958f0484fSRodney W. Grimesfunctions do not return, but exit with the value of the argument
16058f0484fSRodney W. Grimes.Fa eval .
1616febaa3aSJoseph KoshyIt is recommended that the standard values defined in
1626febaa3aSJoseph Koshy.Xr sysexits 3
1636febaa3aSJoseph Koshybe used for the value of
1646febaa3aSJoseph Koshy.Fa eval .
1653688be0eSGarrett WollmanThe
1663688be0eSGarrett Wollman.Fn err_set_exit
1673688be0eSGarrett Wollmanfunction can be used to specify a function which is called before
16878b0b234SMike Pritchard.Xr exit 3
1693688be0eSGarrett Wollmanto perform any necessary cleanup; passing a null function pointer for
1703688be0eSGarrett Wollman.Va exitf
1713688be0eSGarrett Wollmanresets the hook to do nothing.
1726da7e7c6SBruce EvansThe
1736da7e7c6SBruce Evans.Fn err_set_file
1746da7e7c6SBruce Evansfunction sets the output stream used by the other functions.
1756da7e7c6SBruce EvansIts
176b4183771SGuy Helmer.Fa vfp
1776da7e7c6SBruce Evansargument must be either a pointer to an open stream
1786da7e7c6SBruce Evans(possibly already converted to void *)
1796da7e7c6SBruce Evansor a null pointer
1806da7e7c6SBruce Evans(in which case the output stream is set to standard error).
18158f0484fSRodney W. Grimes.Sh EXAMPLES
18258f0484fSRodney W. GrimesDisplay the current errno information string and exit:
18358f0484fSRodney W. Grimes.Bd -literal -offset indent
18458f0484fSRodney W. Grimesif ((p = malloc(size)) == NULL)
18558f0484fSRodney W. Grimes	err(1, NULL);
18658f0484fSRodney W. Grimesif ((fd = open(file_name, O_RDONLY, 0)) == -1)
18758f0484fSRodney W. Grimes	err(1, "%s", file_name);
18858f0484fSRodney W. Grimes.Ed
18958f0484fSRodney W. Grimes.Pp
19058f0484fSRodney W. GrimesDisplay an error message and exit:
19158f0484fSRodney W. Grimes.Bd -literal -offset indent
19258f0484fSRodney W. Grimesif (tm.tm_hour < START_TIME)
19358f0484fSRodney W. Grimes	errx(1, "too early, wait until %s", start_time_string);
19458f0484fSRodney W. Grimes.Ed
19558f0484fSRodney W. Grimes.Pp
19658f0484fSRodney W. GrimesWarn of an error:
19758f0484fSRodney W. Grimes.Bd -literal -offset indent
19858f0484fSRodney W. Grimesif ((fd = open(raw_device, O_RDONLY, 0)) == -1)
19958f0484fSRodney W. Grimes	warnx("%s: %s: trying the block device",
20058f0484fSRodney W. Grimes	    raw_device, strerror(errno));
20158f0484fSRodney W. Grimesif ((fd = open(block_device, O_RDONLY, 0)) == -1)
20258f0484fSRodney W. Grimes	err(1, "%s", block_device);
20358f0484fSRodney W. Grimes.Ed
204b4b4fb87SGarrett Wollman.Pp
205b4b4fb87SGarrett WollmanWarn of an error without using the global variable
206b4b4fb87SGarrett Wollman.Va errno :
207b4b4fb87SGarrett Wollman.Bd -literal -offset indent
208b4b4fb87SGarrett Wollmanerror = my_function();	/* returns a value from <errno.h> */
209b4b4fb87SGarrett Wollmanif (error != 0)
210b4b4fb87SGarrett Wollman	warnc(error, "my_function");
211b4b4fb87SGarrett Wollman.Ed
21258f0484fSRodney W. Grimes.Sh SEE ALSO
21378b0b234SMike Pritchard.Xr exit 3 ,
214823f68a2SMike Barcroft.Xr fmtmsg 3 ,
21507a9238fSDavid E. O'Brien.Xr printf 3 ,
2166febaa3aSJoseph Koshy.Xr strerror 3 ,
2176febaa3aSJoseph Koshy.Xr sysexits 3
21858f0484fSRodney W. Grimes.Sh HISTORY
21958f0484fSRodney W. GrimesThe
22058f0484fSRodney W. Grimes.Fn err
22158f0484fSRodney W. Grimesand
22258f0484fSRodney W. Grimes.Fn warn
2237bdf80e5SMike Pritchardfunctions first appeared in
2247bdf80e5SMike Pritchard.Bx 4.4 .
225b4b4fb87SGarrett WollmanThe
226b4b4fb87SGarrett Wollman.Fn err_set_exit
2276da7e7c6SBruce Evansand
2286da7e7c6SBruce Evans.Fn err_set_file
229b4b4fb87SGarrett Wollmanfunctions first appeared in
230b4b4fb87SGarrett Wollman.Fx 2.1 .
231b4b4fb87SGarrett WollmanThe
232b4b4fb87SGarrett Wollman.Fn errc
233b4b4fb87SGarrett Wollmanand
234b4b4fb87SGarrett Wollman.Fn warnc
235b4b4fb87SGarrett Wollmanfunctions first appeared in
236b4b4fb87SGarrett Wollman.Fx 3.0 .
237