xref: /freebsd/lib/libc/gen/err.c (revision 75a21a3859bc3dcbae65858b3f9b36dc9fd29f11)
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[] =
3775a21a38SGarrett Wollman 	"$Id: err.c,v 1.4 1998/09/12 21:02:22 wollman Exp $";
38b4b4fb87SGarrett Wollman #endif /* LIBC_RCS and not lint */
3958f0484fSRodney W. Grimes 
4058f0484fSRodney W. Grimes #include <err.h>
4158f0484fSRodney W. Grimes #include <errno.h>
4258f0484fSRodney W. Grimes #include <stdio.h>
4358f0484fSRodney W. Grimes #include <stdlib.h>
4458f0484fSRodney W. Grimes #include <string.h>
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes #include <stdarg.h>
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes extern char *__progname;		/* Program name, from crt0. */
4958f0484fSRodney W. Grimes 
503688be0eSGarrett Wollman static FILE *err_file; /* file to use for error output */
513688be0eSGarrett Wollman static void (*err_exit)(int);
523688be0eSGarrett Wollman 
53b4b4fb87SGarrett Wollman /*
54b4b4fb87SGarrett Wollman  * This is declared to take a `void *' so that the caller is not required
55b4b4fb87SGarrett Wollman  * to include <stdio.h> first.  However, it is really a `FILE *', and the
56b4b4fb87SGarrett Wollman  * manual page documents it as such.
57b4b4fb87SGarrett Wollman  */
583688be0eSGarrett Wollman void
593688be0eSGarrett Wollman err_set_file(void *fp)
603688be0eSGarrett Wollman {
613688be0eSGarrett Wollman 	if (fp)
623688be0eSGarrett Wollman 		err_file = fp;
633688be0eSGarrett Wollman 	else
643688be0eSGarrett Wollman 		err_file = stderr;
653688be0eSGarrett Wollman }
663688be0eSGarrett Wollman 
673688be0eSGarrett Wollman void
683688be0eSGarrett Wollman err_set_exit(void (*ef)(int))
693688be0eSGarrett Wollman {
703688be0eSGarrett Wollman 	err_exit = ef;
713688be0eSGarrett Wollman }
723688be0eSGarrett Wollman 
73eaa86f9dSBruce Evans void
7458f0484fSRodney W. Grimes err(int eval, const char *fmt, ...)
7558f0484fSRodney W. Grimes {
7658f0484fSRodney W. Grimes 	va_list ap;
7758f0484fSRodney W. Grimes 	va_start(ap, fmt);
78b4b4fb87SGarrett Wollman 	verrc(eval, errno, fmt, ap);
7958f0484fSRodney W. Grimes 	va_end(ap);
8058f0484fSRodney W. Grimes }
8158f0484fSRodney W. Grimes 
82eaa86f9dSBruce Evans void
8358f0484fSRodney W. Grimes verr(eval, fmt, ap)
8458f0484fSRodney W. Grimes 	int eval;
8558f0484fSRodney W. Grimes 	const char *fmt;
8658f0484fSRodney W. Grimes 	va_list ap;
8758f0484fSRodney W. Grimes {
88b4b4fb87SGarrett Wollman 	verrc(eval, errno, fmt, ap);
8958f0484fSRodney W. Grimes }
90b4b4fb87SGarrett Wollman 
91b4b4fb87SGarrett Wollman void
92b4b4fb87SGarrett Wollman errc(int eval, int code, const char *fmt, ...)
93b4b4fb87SGarrett Wollman {
94b4b4fb87SGarrett Wollman 	va_list ap;
95b4b4fb87SGarrett Wollman 	va_start(ap, fmt);
96b4b4fb87SGarrett Wollman 	verrc(eval, code, fmt, ap);
97b4b4fb87SGarrett Wollman 	va_end(ap);
98b4b4fb87SGarrett Wollman }
99b4b4fb87SGarrett Wollman 
100b4b4fb87SGarrett Wollman void
101b4b4fb87SGarrett Wollman verrc(eval, code, fmt, ap)
102b4b4fb87SGarrett Wollman 	int eval;
103b4b4fb87SGarrett Wollman 	int code;
104b4b4fb87SGarrett Wollman 	const char *fmt;
105b4b4fb87SGarrett Wollman 	va_list ap;
106b4b4fb87SGarrett Wollman {
107b4b4fb87SGarrett Wollman 	if (err_file == 0)
108b4b4fb87SGarrett Wollman 		err_set_file((FILE *)0);
109b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
110b4b4fb87SGarrett Wollman 	if (fmt != NULL) {
111b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
112b4b4fb87SGarrett Wollman 		fprintf(err_file, ": ");
113b4b4fb87SGarrett Wollman 	}
114b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s\n", strerror(code));
1153688be0eSGarrett Wollman 	if (err_exit)
1163688be0eSGarrett Wollman 		err_exit(eval);
11758f0484fSRodney W. Grimes 	exit(eval);
11858f0484fSRodney W. Grimes }
11958f0484fSRodney W. Grimes 
120eaa86f9dSBruce Evans void
12158f0484fSRodney W. Grimes errx(int eval, const char *fmt, ...)
12258f0484fSRodney W. Grimes {
12358f0484fSRodney W. Grimes 	va_list ap;
12458f0484fSRodney W. Grimes 	va_start(ap, fmt);
12558f0484fSRodney W. Grimes 	verrx(eval, fmt, ap);
12658f0484fSRodney W. Grimes 	va_end(ap);
12758f0484fSRodney W. Grimes }
12858f0484fSRodney W. Grimes 
129eaa86f9dSBruce Evans void
13058f0484fSRodney W. Grimes verrx(eval, fmt, ap)
13158f0484fSRodney W. Grimes 	int eval;
13258f0484fSRodney W. Grimes 	const char *fmt;
13358f0484fSRodney W. Grimes 	va_list ap;
13458f0484fSRodney W. Grimes {
135b4b4fb87SGarrett Wollman 	if (err_file == 0)
1363688be0eSGarrett Wollman 		err_set_file((FILE *)0);
137b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
13858f0484fSRodney W. Grimes 	if (fmt != NULL)
139b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
140b4b4fb87SGarrett Wollman 	fprintf(err_file, "\n");
1413688be0eSGarrett Wollman 	if (err_exit)
1423688be0eSGarrett Wollman 		err_exit(eval);
14358f0484fSRodney W. Grimes 	exit(eval);
14458f0484fSRodney W. Grimes }
14558f0484fSRodney W. Grimes 
14658f0484fSRodney W. Grimes void
14758f0484fSRodney W. Grimes warn(const char *fmt, ...)
14858f0484fSRodney W. Grimes {
14958f0484fSRodney W. Grimes 	va_list ap;
15058f0484fSRodney W. Grimes 	va_start(ap, fmt);
151b4b4fb87SGarrett Wollman 	vwarnc(errno, fmt, ap);
15258f0484fSRodney W. Grimes 	va_end(ap);
15358f0484fSRodney W. Grimes }
15458f0484fSRodney W. Grimes 
15558f0484fSRodney W. Grimes void
15658f0484fSRodney W. Grimes vwarn(fmt, ap)
15758f0484fSRodney W. Grimes 	const char *fmt;
15858f0484fSRodney W. Grimes 	va_list ap;
15958f0484fSRodney W. Grimes {
160b4b4fb87SGarrett Wollman 	vwarnc(errno, fmt, ap);
16158f0484fSRodney W. Grimes }
16258f0484fSRodney W. Grimes 
16358f0484fSRodney W. Grimes void
164b4b4fb87SGarrett Wollman warnc(int code, const char *fmt, ...)
16558f0484fSRodney W. Grimes {
16658f0484fSRodney W. Grimes 	va_list ap;
16758f0484fSRodney W. Grimes 	va_start(ap, fmt);
168b4b4fb87SGarrett Wollman 	vwarnc(code, fmt, ap);
169b4b4fb87SGarrett Wollman 	va_end(ap);
170b4b4fb87SGarrett Wollman }
171b4b4fb87SGarrett Wollman 
172b4b4fb87SGarrett Wollman void
173b4b4fb87SGarrett Wollman vwarnc(code, fmt, ap)
174b4b4fb87SGarrett Wollman 	int code;
175b4b4fb87SGarrett Wollman 	const char *fmt;
176b4b4fb87SGarrett Wollman 	va_list ap;
177b4b4fb87SGarrett Wollman {
178b4b4fb87SGarrett Wollman 	if (err_file == 0)
179b4b4fb87SGarrett Wollman 		err_set_file((FILE *)0);
180b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
181b4b4fb87SGarrett Wollman 	if (fmt != NULL) {
182b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
183b4b4fb87SGarrett Wollman 		fprintf(err_file, ": ");
184b4b4fb87SGarrett Wollman 	}
185b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s\n", strerror(code));
186b4b4fb87SGarrett Wollman }
187b4b4fb87SGarrett Wollman 
188b4b4fb87SGarrett Wollman void
189b4b4fb87SGarrett Wollman warnx(const char *fmt, ...)
190b4b4fb87SGarrett Wollman {
191b4b4fb87SGarrett Wollman 	va_list ap;
192b4b4fb87SGarrett Wollman 	va_start(ap, fmt);
19375a21a38SGarrett Wollman 	vwarnx(fmt, ap);
19458f0484fSRodney W. Grimes 	va_end(ap);
19558f0484fSRodney W. Grimes }
19658f0484fSRodney W. Grimes 
19758f0484fSRodney W. Grimes void
19858f0484fSRodney W. Grimes vwarnx(fmt, ap)
19958f0484fSRodney W. Grimes 	const char *fmt;
20058f0484fSRodney W. Grimes 	va_list ap;
20158f0484fSRodney W. Grimes {
202b4b4fb87SGarrett Wollman 	if (err_file == 0)
2033688be0eSGarrett Wollman 		err_set_file((FILE *)0);
204b4b4fb87SGarrett Wollman 	fprintf(err_file, "%s: ", __progname);
20558f0484fSRodney W. Grimes 	if (fmt != NULL)
206b4b4fb87SGarrett Wollman 		vfprintf(err_file, fmt, ap);
207b4b4fb87SGarrett Wollman 	fprintf(err_file, "\n");
20858f0484fSRodney W. Grimes }
209