xref: /freebsd/lib/libc/gen/err.c (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if defined(LIBC_SCCS) && !defined(lint)
31 static char sccsid[] = "@(#)err.c	8.1 (Berkeley) 6/4/93";
32 #endif /* LIBC_SCCS and not lint */
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "namespace.h"
37 #include <err.h>
38 #include <errno.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include "un-namespace.h"
44 
45 #include "libc_private.h"
46 
47 static FILE *err_file; /* file to use for error output */
48 static void (*err_exit)(int);
49 
50 /*
51  * This is declared to take a `void *' so that the caller is not required
52  * to include <stdio.h> first.  However, it is really a `FILE *', and the
53  * manual page documents it as such.
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 __weak_reference(_err, err);
71 
72 void
73 _err(int eval, const char *fmt, ...)
74 {
75 	va_list ap;
76 	va_start(ap, fmt);
77 	verrc(eval, errno, fmt, ap);
78 	va_end(ap);
79 }
80 
81 void
82 verr(eval, fmt, ap)
83 	int eval;
84 	const char *fmt;
85 	va_list ap;
86 {
87 	verrc(eval, errno, fmt, ap);
88 }
89 
90 void
91 errc(int eval, int code, const char *fmt, ...)
92 {
93 	va_list ap;
94 	va_start(ap, fmt);
95 	verrc(eval, code, fmt, ap);
96 	va_end(ap);
97 }
98 
99 void
100 verrc(int eval, int code, const char *fmt, va_list ap)
101 {
102 	if (err_file == 0)
103 		err_set_file((FILE *)0);
104 	fprintf(err_file, "%s: ", _getprogname());
105 	if (fmt != NULL) {
106 		vfprintf(err_file, fmt, ap);
107 		fprintf(err_file, ": ");
108 	}
109 	fprintf(err_file, "%s\n", strerror(code));
110 	if (err_exit)
111 		err_exit(eval);
112 	exit(eval);
113 }
114 
115 void
116 errx(int eval, const char *fmt, ...)
117 {
118 	va_list ap;
119 	va_start(ap, fmt);
120 	verrx(eval, fmt, ap);
121 	va_end(ap);
122 }
123 
124 void
125 verrx(int eval, const char *fmt, va_list ap)
126 {
127 	if (err_file == 0)
128 		err_set_file((FILE *)0);
129 	fprintf(err_file, "%s: ", _getprogname());
130 	if (fmt != NULL)
131 		vfprintf(err_file, fmt, ap);
132 	fprintf(err_file, "\n");
133 	if (err_exit)
134 		err_exit(eval);
135 	exit(eval);
136 }
137 
138 __weak_reference(_warn, warn);
139 
140 void
141 _warn(const char *fmt, ...)
142 {
143 	va_list ap;
144 	va_start(ap, fmt);
145 	vwarnc(errno, fmt, ap);
146 	va_end(ap);
147 }
148 
149 void
150 vwarn(const char *fmt, va_list ap)
151 {
152 	vwarnc(errno, fmt, ap);
153 }
154 
155 void
156 warnc(int code, const char *fmt, ...)
157 {
158 	va_list ap;
159 	va_start(ap, fmt);
160 	vwarnc(code, fmt, ap);
161 	va_end(ap);
162 }
163 
164 void
165 vwarnc(int code, const char *fmt, va_list ap)
166 {
167 	if (err_file == 0)
168 		err_set_file((FILE *)0);
169 	fprintf(err_file, "%s: ", _getprogname());
170 	if (fmt != NULL) {
171 		vfprintf(err_file, fmt, ap);
172 		fprintf(err_file, ": ");
173 	}
174 	fprintf(err_file, "%s\n", strerror(code));
175 }
176 
177 void
178 warnx(const char *fmt, ...)
179 {
180 	va_list ap;
181 	va_start(ap, fmt);
182 	vwarnx(fmt, ap);
183 	va_end(ap);
184 }
185 
186 void
187 vwarnx(const char *fmt, va_list ap)
188 {
189 	if (err_file == 0)
190 		err_set_file((FILE *)0);
191 	fprintf(err_file, "%s: ", _getprogname());
192 	if (fmt != NULL)
193 		vfprintf(err_file, fmt, ap);
194 	fprintf(err_file, "\n");
195 }
196