xref: /freebsd/lib/libc/gen/err.c (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __SCCSID("@(#)err.c	8.1 (Berkeley) 6/4/93");
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(int eval, const char *fmt, va_list ap)
83 {
84 	verrc(eval, errno, fmt, ap);
85 }
86 
87 void
88 errc(int eval, int code, const char *fmt, ...)
89 {
90 	va_list ap;
91 	va_start(ap, fmt);
92 	verrc(eval, code, fmt, ap);
93 	va_end(ap);
94 }
95 
96 void
97 verrc(int eval, int code, const char *fmt, va_list ap)
98 {
99 	if (err_file == NULL)
100 		err_set_file(NULL);
101 	fprintf(err_file, "%s: ", _getprogname());
102 	if (fmt != NULL) {
103 		vfprintf(err_file, fmt, ap);
104 		fprintf(err_file, ": ");
105 	}
106 	fprintf(err_file, "%s\n", strerror(code));
107 	if (err_exit)
108 		err_exit(eval);
109 	exit(eval);
110 }
111 
112 void
113 errx(int eval, const char *fmt, ...)
114 {
115 	va_list ap;
116 	va_start(ap, fmt);
117 	verrx(eval, fmt, ap);
118 	va_end(ap);
119 }
120 
121 void
122 verrx(int eval, const char *fmt, va_list ap)
123 {
124 	if (err_file == NULL)
125 		err_set_file(NULL);
126 	fprintf(err_file, "%s: ", _getprogname());
127 	if (fmt != NULL)
128 		vfprintf(err_file, fmt, ap);
129 	fprintf(err_file, "\n");
130 	if (err_exit)
131 		err_exit(eval);
132 	exit(eval);
133 }
134 
135 __weak_reference(_warn, warn);
136 
137 void
138 _warn(const char *fmt, ...)
139 {
140 	va_list ap;
141 	va_start(ap, fmt);
142 	vwarnc(errno, fmt, ap);
143 	va_end(ap);
144 }
145 
146 void
147 vwarn(const char *fmt, va_list ap)
148 {
149 	vwarnc(errno, fmt, ap);
150 }
151 
152 void
153 warnc(int code, const char *fmt, ...)
154 {
155 	va_list ap;
156 	va_start(ap, fmt);
157 	vwarnc(code, fmt, ap);
158 	va_end(ap);
159 }
160 
161 void
162 vwarnc(int code, const char *fmt, va_list ap)
163 {
164 	int saved_errno;
165 
166 	saved_errno = errno;
167 	if (err_file == NULL)
168 		err_set_file(NULL);
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 	errno = saved_errno;
176 }
177 
178 void
179 warnx(const char *fmt, ...)
180 {
181 	va_list ap;
182 	va_start(ap, fmt);
183 	vwarnx(fmt, ap);
184 	va_end(ap);
185 }
186 
187 void
188 vwarnx(const char *fmt, va_list ap)
189 {
190 	int saved_errno;
191 
192 	saved_errno = errno;
193 	if (err_file == NULL)
194 		err_set_file(NULL);
195 	fprintf(err_file, "%s: ", _getprogname());
196 	if (fmt != NULL)
197 		vfprintf(err_file, fmt, ap);
198 	fprintf(err_file, "\n");
199 	errno = saved_errno;
200 }
201