xref: /netbsd-src/lib/libc/gen/err.c (revision f95d2e2869b57f5a349548a4c6cc9bff73d6cb97)
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 /* from: static char sccsid[] = "@(#)err.c	8.1 (Berkeley) 6/4/93"; */
36 static char *rcsid = "$Id: err.c,v 1.7 1993/11/06 00:55:23 cgd Exp $";
37 #endif /* LIBC_SCCS and not lint */
38 
39 #include <err.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 
45 #ifdef __STDC__
46 #include <stdarg.h>
47 #else
48 #include <varargs.h>
49 #endif
50 
51 extern char *__progname;		/* Program name, from crt0. */
52 
53 __dead void
54 #ifdef __STDC__
55 err(int eval, const char *fmt, ...)
56 #else
57 err(eval, fmt, va_alist)
58 	int eval;
59 	const char *fmt;
60 	va_dcl
61 #endif
62 {
63 	va_list ap;
64 #if __STDC__
65 	va_start(ap, fmt);
66 #else
67 	va_start(ap);
68 #endif
69 	verr(eval, fmt, ap);
70 	va_end(ap);
71 }
72 
73 __dead void
74 verr(eval, fmt, ap)
75 	int eval;
76 	const char *fmt;
77 	va_list ap;
78 {
79 	int sverrno;
80 
81 	sverrno = errno;
82 	(void)fprintf(stderr, "%s: ", __progname);
83 	if (fmt != NULL) {
84 		(void)vfprintf(stderr, fmt, ap);
85 		(void)fprintf(stderr, ": ");
86 	}
87 	(void)fprintf(stderr, "%s\n", strerror(sverrno));
88 	exit(eval);
89 }
90 
91 __dead void
92 #if __STDC__
93 errx(int eval, const char *fmt, ...)
94 #else
95 errx(eval, fmt, va_alist)
96 	int eval;
97 	const char *fmt;
98 	va_dcl
99 #endif
100 {
101 	va_list ap;
102 #if __STDC__
103 	va_start(ap, fmt);
104 #else
105 	va_start(ap);
106 #endif
107 	verrx(eval, fmt, ap);
108 	va_end(ap);
109 }
110 
111 __dead void
112 verrx(eval, fmt, ap)
113 	int eval;
114 	const char *fmt;
115 	va_list ap;
116 {
117 	(void)fprintf(stderr, "%s: ", __progname);
118 	if (fmt != NULL)
119 		(void)vfprintf(stderr, fmt, ap);
120 	(void)fprintf(stderr, "\n");
121 	exit(eval);
122 }
123 
124 void
125 #if __STDC__
126 warn(const char *fmt, ...)
127 #else
128 warn(fmt, va_alist)
129 	const char *fmt;
130 	va_dcl
131 #endif
132 {
133 	va_list ap;
134 #if __STDC__
135 	va_start(ap, fmt);
136 #else
137 	va_start(ap);
138 #endif
139 	vwarn(fmt, ap);
140 	va_end(ap);
141 }
142 
143 void
144 vwarn(fmt, ap)
145 	const char *fmt;
146 	va_list ap;
147 {
148 	int sverrno;
149 
150 	sverrno = errno;
151 	(void)fprintf(stderr, "%s: ", __progname);
152 	if (fmt != NULL) {
153 		(void)vfprintf(stderr, fmt, ap);
154 		(void)fprintf(stderr, ": ");
155 	}
156 	(void)fprintf(stderr, "%s\n", strerror(sverrno));
157 }
158 
159 void
160 #ifdef __STDC__
161 warnx(const char *fmt, ...)
162 #else
163 warnx(fmt, va_alist)
164 	const char *fmt;
165 	va_dcl
166 #endif
167 {
168 	va_list ap;
169 #ifdef __STDC__
170 	va_start(ap, fmt);
171 #else
172 	va_start(ap);
173 #endif
174 	vwarnx(fmt, ap);
175 	va_end(ap);
176 }
177 
178 void
179 vwarnx(fmt, ap)
180 	const char *fmt;
181 	va_list ap;
182 {
183 	(void)fprintf(stderr, "%s: ", __progname);
184 	if (fmt != NULL)
185 		(void)vfprintf(stderr, fmt, ap);
186 	(void)fprintf(stderr, "\n");
187 }
188