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