xref: /freebsd/lib/libc/gen/err.c (revision ae477ca7da55f76d28859e1bd01cd1051e36f28f)
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 "namespace.h"
33 #include <err.h>
34 #include <errno.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "un-namespace.h"
40 
41 #include "libc_private.h"
42 
43 static FILE *err_file; /* file to use for error output */
44 static void (*err_exit)(int);
45 
46 /*
47  * This is declared to take a `void *' so that the caller is not required
48  * to include <stdio.h> first.  However, it is really a `FILE *', and the
49  * manual page documents it as such.
50  */
51 void
52 err_set_file(void *fp)
53 {
54 	if (fp)
55 		err_file = fp;
56 	else
57 		err_file = stderr;
58 }
59 
60 void
61 err_set_exit(void (*ef)(int))
62 {
63 	err_exit = ef;
64 }
65 
66 __weak_reference(_err, err);
67 
68 void
69 _err(int eval, const char *fmt, ...)
70 {
71 	va_list ap;
72 	va_start(ap, fmt);
73 	verrc(eval, errno, fmt, ap);
74 	va_end(ap);
75 }
76 
77 void
78 verr(int eval, const char *fmt, va_list ap)
79 {
80 	verrc(eval, errno, fmt, ap);
81 }
82 
83 void
84 errc(int eval, int code, const char *fmt, ...)
85 {
86 	va_list ap;
87 	va_start(ap, fmt);
88 	verrc(eval, code, fmt, ap);
89 	va_end(ap);
90 }
91 
92 void
93 verrc(int eval, int code, const char *fmt, va_list ap)
94 {
95 	if (err_file == NULL)
96 		err_set_file(NULL);
97 	fprintf(err_file, "%s: ", _getprogname());
98 	if (fmt != NULL) {
99 		vfprintf(err_file, fmt, ap);
100 		fprintf(err_file, ": ");
101 	}
102 	fprintf(err_file, "%s\n", strerror(code));
103 	if (err_exit)
104 		err_exit(eval);
105 	exit(eval);
106 }
107 
108 void
109 errx(int eval, const char *fmt, ...)
110 {
111 	va_list ap;
112 	va_start(ap, fmt);
113 	verrx(eval, fmt, ap);
114 	va_end(ap);
115 }
116 
117 void
118 verrx(int eval, const char *fmt, va_list ap)
119 {
120 	if (err_file == NULL)
121 		err_set_file(NULL);
122 	fprintf(err_file, "%s: ", _getprogname());
123 	if (fmt != NULL)
124 		vfprintf(err_file, fmt, ap);
125 	fprintf(err_file, "\n");
126 	if (err_exit)
127 		err_exit(eval);
128 	exit(eval);
129 }
130 
131 __weak_reference(_warn, warn);
132 
133 void
134 _warn(const char *fmt, ...)
135 {
136 	va_list ap;
137 	va_start(ap, fmt);
138 	vwarnc(errno, fmt, ap);
139 	va_end(ap);
140 }
141 
142 void
143 vwarn(const char *fmt, va_list ap)
144 {
145 	vwarnc(errno, fmt, ap);
146 }
147 
148 void
149 warnc(int code, const char *fmt, ...)
150 {
151 	va_list ap;
152 	va_start(ap, fmt);
153 	vwarnc(code, fmt, ap);
154 	va_end(ap);
155 }
156 
157 void
158 vwarnc(int code, const char *fmt, va_list ap)
159 {
160 	int saved_errno;
161 
162 	saved_errno = errno;
163 	if (err_file == NULL)
164 		err_set_file(NULL);
165 	fprintf(err_file, "%s: ", _getprogname());
166 	if (fmt != NULL) {
167 		vfprintf(err_file, fmt, ap);
168 		fprintf(err_file, ": ");
169 	}
170 	fprintf(err_file, "%s\n", strerror(code));
171 	errno = saved_errno;
172 }
173 
174 void
175 warnx(const char *fmt, ...)
176 {
177 	va_list ap;
178 	va_start(ap, fmt);
179 	vwarnx(fmt, ap);
180 	va_end(ap);
181 }
182 
183 void
184 vwarnx(const char *fmt, va_list ap)
185 {
186 	int saved_errno;
187 
188 	saved_errno = errno;
189 	if (err_file == NULL)
190 		err_set_file(NULL);
191 	fprintf(err_file, "%s: ", _getprogname());
192 	if (fmt != NULL)
193 		vfprintf(err_file, fmt, ap);
194 	fprintf(err_file, "\n");
195 	errno = saved_errno;
196 }
197