xref: /freebsd/lib/libc/gen/err.c (revision 1643f03d5ffcd546a5ab371be686995141365d93)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
32b4b4fb87SGarrett Wollman  *	From: @(#)err.c	8.1 (Berkeley) 6/4/93
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
35b4b4fb87SGarrett Wollman #if defined(LIBC_RCS) && !defined(lint)
36b4b4fb87SGarrett Wollman static const char rcsid[] =
377f3dea24SPeter Wemm   "$FreeBSD$";
38b4b4fb87SGarrett Wollman #endif /* LIBC_RCS and not lint */
3958f0484fSRodney W. Grimes 
401643f03dSBruce Evans #include "namespace.h"
4158f0484fSRodney W. Grimes #include <err.h>
421643f03dSBruce Evans #include "un-namespace.h"
4358f0484fSRodney W. Grimes #include <errno.h>
4458f0484fSRodney W. Grimes #include <stdio.h>
4558f0484fSRodney W. Grimes #include <stdlib.h>
4658f0484fSRodney W. Grimes #include <string.h>
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes #include <stdarg.h>
4958f0484fSRodney W. Grimes 
5058f0484fSRodney W. Grimes extern char *__progname;		/* Program name, from crt0. */
5158f0484fSRodney W. Grimes 
523688be0eSGarrett Wollman static FILE *err_file; /* file to use for error output */
533688be0eSGarrett Wollman static void (*err_exit)(int);
543688be0eSGarrett Wollman 
55b4b4fb87SGarrett Wollman /*
56b4b4fb87SGarrett Wollman  * This is declared to take a `void *' so that the caller is not required
57b4b4fb87SGarrett Wollman  * to include <stdio.h> first.  However, it is really a `FILE *', and the
58b4b4fb87SGarrett Wollman  * manual page documents it as such.
59b4b4fb87SGarrett Wollman  */
603688be0eSGarrett Wollman void
613688be0eSGarrett Wollman err_set_file(void *fp)
623688be0eSGarrett Wollman {
633688be0eSGarrett Wollman 	if (fp)
643688be0eSGarrett Wollman 		err_file = fp;
653688be0eSGarrett Wollman 	else
663688be0eSGarrett Wollman 		err_file = stderr;
673688be0eSGarrett Wollman }
683688be0eSGarrett Wollman 
693688be0eSGarrett Wollman void
703688be0eSGarrett Wollman err_set_exit(void (*ef)(int))
713688be0eSGarrett Wollman {
723688be0eSGarrett Wollman 	err_exit = ef;
733688be0eSGarrett Wollman }
743688be0eSGarrett Wollman 
75eaa86f9dSBruce Evans void
7658f0484fSRodney W. Grimes err(int eval, const char *fmt, ...)
7758f0484fSRodney W. Grimes {
7858f0484fSRodney W. Grimes 	va_list ap;
7958f0484fSRodney W. Grimes 	va_start(ap, fmt);
80b4b4fb87SGarrett Wollman 	verrc(eval, errno, fmt, ap);
8158f0484fSRodney W. Grimes 	va_end(ap);
8258f0484fSRodney W. Grimes }
8358f0484fSRodney W. Grimes 
84eaa86f9dSBruce Evans void
8558f0484fSRodney W. Grimes verr(eval, fmt, ap)
8658f0484fSRodney W. Grimes 	int eval;
8758f0484fSRodney W. Grimes 	const char *fmt;
8858f0484fSRodney W. Grimes 	va_list ap;
8958f0484fSRodney W. Grimes {
90b4b4fb87SGarrett Wollman 	verrc(eval, errno, fmt, ap);
9158f0484fSRodney W. Grimes }
92b4b4fb87SGarrett Wollman 
93b4b4fb87SGarrett Wollman void
94b4b4fb87SGarrett Wollman errc(int eval, int code, const char *fmt, ...)
95b4b4fb87SGarrett Wollman {
96b4b4fb87SGarrett Wollman 	va_list ap;
97b4b4fb87SGarrett Wollman 	va_start(ap, fmt);
98b4b4fb87SGarrett Wollman 	verrc(eval, code, fmt, ap);
99b4b4fb87SGarrett Wollman 	va_end(ap);
100b4b4fb87SGarrett Wollman }
101b4b4fb87SGarrett Wollman 
102b4b4fb87SGarrett Wollman void
103b4b4fb87SGarrett Wollman verrc(eval, code, fmt, ap)
104b4b4fb87SGarrett Wollman 	int eval;
105b4b4fb87SGarrett Wollman 	int code;
106b4b4fb87SGarrett Wollman 	const char *fmt;
107b4b4fb87SGarrett Wollman 	va_list ap;
108b4b4fb87SGarrett Wollman {
109b4b4fb87SGarrett Wollman 	if (err_file == 0)
110b4b4fb87SGarrett Wollman 		err_set_file((FILE *)0);
111b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
112b4b4fb87SGarrett Wollman 	if (fmt != NULL) {
113b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
114b4b4fb87SGarrett Wollman 		fprintf(err_file, ": ");
115b4b4fb87SGarrett Wollman 	}
116b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s\n", strerror(code));
1173688be0eSGarrett Wollman 	if (err_exit)
1183688be0eSGarrett Wollman 		err_exit(eval);
11958f0484fSRodney W. Grimes 	exit(eval);
12058f0484fSRodney W. Grimes }
12158f0484fSRodney W. Grimes 
122eaa86f9dSBruce Evans void
12358f0484fSRodney W. Grimes errx(int eval, const char *fmt, ...)
12458f0484fSRodney W. Grimes {
12558f0484fSRodney W. Grimes 	va_list ap;
12658f0484fSRodney W. Grimes 	va_start(ap, fmt);
12758f0484fSRodney W. Grimes 	verrx(eval, fmt, ap);
12858f0484fSRodney W. Grimes 	va_end(ap);
12958f0484fSRodney W. Grimes }
13058f0484fSRodney W. Grimes 
131eaa86f9dSBruce Evans void
13258f0484fSRodney W. Grimes verrx(eval, fmt, ap)
13358f0484fSRodney W. Grimes 	int eval;
13458f0484fSRodney W. Grimes 	const char *fmt;
13558f0484fSRodney W. Grimes 	va_list ap;
13658f0484fSRodney W. Grimes {
137b4b4fb87SGarrett Wollman 	if (err_file == 0)
1383688be0eSGarrett Wollman 		err_set_file((FILE *)0);
139b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
14058f0484fSRodney W. Grimes 	if (fmt != NULL)
141b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
142b4b4fb87SGarrett Wollman 	fprintf(err_file, "\n");
1433688be0eSGarrett Wollman 	if (err_exit)
1443688be0eSGarrett Wollman 		err_exit(eval);
14558f0484fSRodney W. Grimes 	exit(eval);
14658f0484fSRodney W. Grimes }
14758f0484fSRodney W. Grimes 
1481643f03dSBruce Evans __weak_reference(_warn, warn);
1491643f03dSBruce Evans 
15058f0484fSRodney W. Grimes void
1511643f03dSBruce Evans _warn(const char *fmt, ...)
15258f0484fSRodney W. Grimes {
15358f0484fSRodney W. Grimes 	va_list ap;
15458f0484fSRodney W. Grimes 	va_start(ap, fmt);
155b4b4fb87SGarrett Wollman 	vwarnc(errno, fmt, ap);
15658f0484fSRodney W. Grimes 	va_end(ap);
15758f0484fSRodney W. Grimes }
15858f0484fSRodney W. Grimes 
15958f0484fSRodney W. Grimes void
16058f0484fSRodney W. Grimes vwarn(fmt, ap)
16158f0484fSRodney W. Grimes 	const char *fmt;
16258f0484fSRodney W. Grimes 	va_list ap;
16358f0484fSRodney W. Grimes {
164b4b4fb87SGarrett Wollman 	vwarnc(errno, fmt, ap);
16558f0484fSRodney W. Grimes }
16658f0484fSRodney W. Grimes 
16758f0484fSRodney W. Grimes void
168b4b4fb87SGarrett Wollman warnc(int code, const char *fmt, ...)
16958f0484fSRodney W. Grimes {
17058f0484fSRodney W. Grimes 	va_list ap;
17158f0484fSRodney W. Grimes 	va_start(ap, fmt);
172b4b4fb87SGarrett Wollman 	vwarnc(code, fmt, ap);
173b4b4fb87SGarrett Wollman 	va_end(ap);
174b4b4fb87SGarrett Wollman }
175b4b4fb87SGarrett Wollman 
176b4b4fb87SGarrett Wollman void
177b4b4fb87SGarrett Wollman vwarnc(code, fmt, ap)
178b4b4fb87SGarrett Wollman 	int code;
179b4b4fb87SGarrett Wollman 	const char *fmt;
180b4b4fb87SGarrett Wollman 	va_list ap;
181b4b4fb87SGarrett Wollman {
182b4b4fb87SGarrett Wollman 	if (err_file == 0)
183b4b4fb87SGarrett Wollman 		err_set_file((FILE *)0);
184b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
185b4b4fb87SGarrett Wollman 	if (fmt != NULL) {
186b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
187b4b4fb87SGarrett Wollman 		fprintf(err_file, ": ");
188b4b4fb87SGarrett Wollman 	}
189b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s\n", strerror(code));
190b4b4fb87SGarrett Wollman }
191b4b4fb87SGarrett Wollman 
192b4b4fb87SGarrett Wollman void
193b4b4fb87SGarrett Wollman warnx(const char *fmt, ...)
194b4b4fb87SGarrett Wollman {
195b4b4fb87SGarrett Wollman 	va_list ap;
196b4b4fb87SGarrett Wollman 	va_start(ap, fmt);
19775a21a38SGarrett Wollman 	vwarnx(fmt, ap);
19858f0484fSRodney W. Grimes 	va_end(ap);
19958f0484fSRodney W. Grimes }
20058f0484fSRodney W. Grimes 
20158f0484fSRodney W. Grimes void
20258f0484fSRodney W. Grimes vwarnx(fmt, ap)
20358f0484fSRodney W. Grimes 	const char *fmt;
20458f0484fSRodney W. Grimes 	va_list ap;
20558f0484fSRodney W. Grimes {
206b4b4fb87SGarrett Wollman 	if (err_file == 0)
2073688be0eSGarrett Wollman 		err_set_file((FILE *)0);
208b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
20958f0484fSRodney W. Grimes 	if (fmt != NULL)
210b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
211b4b4fb87SGarrett Wollman 	fprintf(err_file, "\n");
21258f0484fSRodney W. Grimes }
213