xref: /freebsd/lib/libc/gen/err.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
1 /*-
2  * Copyright (c) 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)err.c	8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
37 
38 #include <err.h>
39 #include <errno.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #ifdef __STDC__
45 #include <stdarg.h>
46 #else
47 #include <varargs.h>
48 #endif
49 
50 extern char *__progname;		/* Program name, from crt0. */
51 
52 static FILE *err_file; /* file to use for error output */
53 static void (*err_exit)(int);
54 
55 void
56 err_set_file(void *fp)
57 {
58 	if (fp)
59 		err_file = fp;
60 	else
61 		err_file = stderr;
62 }
63 
64 void
65 err_set_exit(void (*ef)(int))
66 {
67 	err_exit = ef;
68 }
69 
70 __dead void
71 #ifdef __STDC__
72 err(int eval, const char *fmt, ...)
73 #else
74 err(eval, fmt, va_alist)
75 	int eval;
76 	const char *fmt;
77 	va_dcl
78 #endif
79 {
80 	va_list ap;
81 #if __STDC__
82 	va_start(ap, fmt);
83 #else
84 	va_start(ap);
85 #endif
86 	verr(eval, fmt, ap);
87 	va_end(ap);
88 }
89 
90 __dead void
91 verr(eval, fmt, ap)
92 	int eval;
93 	const char *fmt;
94 	va_list ap;
95 {
96 	int sverrno;
97 
98 	sverrno = errno;
99 	if (! err_file)
100 		err_set_file((FILE *)0);
101 	(void)fprintf(err_file, "%s: ", __progname);
102 	if (fmt != NULL) {
103 		(void)vfprintf(err_file, fmt, ap);
104 		(void)fprintf(err_file, ": ");
105 	}
106 	(void)fprintf(err_file, "%s\n", strerror(sverrno));
107 	if(err_exit)
108 		err_exit(eval);
109 	exit(eval);
110 }
111 
112 __dead void
113 #if __STDC__
114 errx(int eval, const char *fmt, ...)
115 #else
116 errx(eval, fmt, va_alist)
117 	int eval;
118 	const char *fmt;
119 	va_dcl
120 #endif
121 {
122 	va_list ap;
123 #if __STDC__
124 	va_start(ap, fmt);
125 #else
126 	va_start(ap);
127 #endif
128 	verrx(eval, fmt, ap);
129 	va_end(ap);
130 }
131 
132 __dead void
133 verrx(eval, fmt, ap)
134 	int eval;
135 	const char *fmt;
136 	va_list ap;
137 {
138 	if (! err_file)
139 		err_set_file((FILE *)0);
140 	(void)fprintf(err_file, "%s: ", __progname);
141 	if (fmt != NULL)
142 		(void)vfprintf(err_file, fmt, ap);
143 	(void)fprintf(err_file, "\n");
144 	if (err_exit)
145 		err_exit(eval);
146 	exit(eval);
147 }
148 
149 void
150 #if __STDC__
151 warn(const char *fmt, ...)
152 #else
153 warn(fmt, va_alist)
154 	const char *fmt;
155 	va_dcl
156 #endif
157 {
158 	va_list ap;
159 #if __STDC__
160 	va_start(ap, fmt);
161 #else
162 	va_start(ap);
163 #endif
164 	vwarn(fmt, ap);
165 	va_end(ap);
166 }
167 
168 void
169 vwarn(fmt, ap)
170 	const char *fmt;
171 	va_list ap;
172 {
173 	int sverrno;
174 
175 	sverrno = errno;
176 	if (! err_file)
177 		err_set_file((FILE *)0);
178 	(void)fprintf(err_file, "%s: ", __progname);
179 	if (fmt != NULL) {
180 		(void)vfprintf(err_file, fmt, ap);
181 		(void)fprintf(err_file, ": ");
182 	}
183 	(void)fprintf(err_file, "%s\n", strerror(sverrno));
184 }
185 
186 void
187 #ifdef __STDC__
188 warnx(const char *fmt, ...)
189 #else
190 warnx(fmt, va_alist)
191 	const char *fmt;
192 	va_dcl
193 #endif
194 {
195 	va_list ap;
196 #ifdef __STDC__
197 	va_start(ap, fmt);
198 #else
199 	va_start(ap);
200 #endif
201 	vwarnx(fmt, ap);
202 	va_end(ap);
203 }
204 
205 void
206 vwarnx(fmt, ap)
207 	const char *fmt;
208 	va_list ap;
209 {
210 	if (! err_file)
211 		err_set_file((FILE *)0);
212 	(void)fprintf(err_file, "%s: ", __progname);
213 	if (fmt != NULL)
214 		(void)vfprintf(err_file, fmt, ap);
215 	(void)fprintf(err_file, "\n");
216 }
217