xref: /illumos-gate/usr/src/lib/libc/port/gen/err.c (revision 9622934a862fa39a8e90c816c4136e293d75629d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #pragma weak err = _err
30 #pragma weak errx = _errx
31 #pragma weak verr = _verr
32 #pragma weak verrx = _verrx
33 #pragma weak warn = _warn
34 #pragma weak warnx = _warnx
35 #pragma weak vwarn = _vwarn
36 #pragma weak vwarnx = _vwarnx
37 
38 #include "synonyms.h"
39 #include "file64.h"
40 #include "mtlib.h"
41 #include <sys/types.h>
42 #include <err.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <stdarg.h>
46 #include <string.h>
47 #include <errno.h>
48 #include "stdiom.h"
49 
50 /* Function exit/warning functions and global variables. */
51 
52 static const char *progname;
53 
54 /*
55  * warncore() is the workhorse of these functions.  Everything else has
56  * a warncore() component in it.
57  */
58 static rmutex_t *
59 warncore(FILE *fp, const char *fmt, va_list args)
60 {
61 	const char *execname;
62 	rmutex_t *lk;
63 
64 	FLOCKFILE(lk, fp);
65 
66 	if (progname == NULL) {
67 		execname = getexecname();
68 		if ((execname != NULL) &&
69 		    ((progname = strrchr(execname, '/')) != NULL))
70 			progname++;
71 		else
72 			progname = execname;
73 	}
74 
75 	if (progname != NULL)
76 		(void) fprintf(fp, "%s: ", progname);
77 
78 	if (fmt != NULL) {
79 		(void) vfprintf(fp, fmt, args);
80 	}
81 
82 	return (lk);
83 }
84 
85 /* Finish a warning with a newline and a flush of stderr. */
86 static void
87 warnfinish(FILE *fp, rmutex_t *lk)
88 {
89 	(void) fputc('\n', fp);
90 	(void) fflush(fp);
91 	FUNLOCKFILE(lk);
92 }
93 
94 void
95 _vwarnxfp(FILE *fp, const char *fmt, va_list args)
96 {
97 	rmutex_t *lk;
98 
99 	lk = warncore(fp, fmt, args);
100 	warnfinish(fp, lk);
101 }
102 
103 void
104 vwarnx(const char *fmt, va_list args)
105 {
106 	_vwarnxfp(stderr, fmt, args);
107 }
108 
109 void
110 _vwarnfp(FILE *fp, const char *fmt, va_list args)
111 {
112 	int tmperr = errno;	/* Capture errno now. */
113 	rmutex_t *lk;
114 
115 	lk = warncore(fp, fmt, args);
116 	if (fmt != NULL) {
117 		(void) fputc(':', fp);
118 		(void) fputc(' ', fp);
119 	}
120 	(void) fputs(strerror(tmperr), fp);
121 	warnfinish(fp, lk);
122 }
123 
124 void
125 vwarn(const char *fmt, va_list args)
126 {
127 	_vwarnfp(stderr, fmt, args);
128 }
129 
130 /* PRINTFLIKE1 */
131 void
132 warnx(const char *fmt, ...)
133 {
134 	va_list args;
135 
136 	va_start(args, fmt);
137 	vwarnx(fmt, args);
138 	va_end(args);
139 }
140 
141 void
142 _warnfp(FILE *fp, const char *fmt, ...)
143 {
144 	va_list args;
145 
146 	va_start(args, fmt);
147 	_vwarnfp(fp, fmt, args);
148 	va_end(args);
149 }
150 
151 void
152 _warnxfp(FILE *fp, const char *fmt, ...)
153 {
154 	va_list args;
155 
156 	va_start(args, fmt);
157 	_vwarnxfp(fp, fmt, args);
158 	va_end(args);
159 }
160 
161 /* PRINTFLIKE1 */
162 void
163 warn(const char *fmt, ...)
164 {
165 	va_list args;
166 
167 	va_start(args, fmt);
168 	vwarn(fmt, args);
169 	va_end(args);
170 }
171 
172 /* PRINTFLIKE2 */
173 void
174 err(int status, const char *fmt, ...)
175 {
176 	va_list args;
177 
178 	va_start(args, fmt);
179 	vwarn(fmt, args);
180 	va_end(args);
181 	exit(status);
182 }
183 
184 void
185 _errfp(FILE *fp, int status, const char *fmt, ...)
186 {
187 	va_list args;
188 
189 	va_start(args, fmt);
190 	_vwarnfp(fp, fmt, args);
191 	va_end(args);
192 	exit(status);
193 }
194 
195 void
196 verr(int status, const char *fmt, va_list args)
197 {
198 	vwarn(fmt, args);
199 	exit(status);
200 }
201 
202 void
203 _verrfp(FILE *fp, int status, const char *fmt, va_list args)
204 {
205 	_vwarnfp(fp, fmt, args);
206 	exit(status);
207 }
208 
209 /* PRINTFLIKE2 */
210 void
211 errx(int status, const char *fmt, ...)
212 {
213 	va_list args;
214 
215 	va_start(args, fmt);
216 	vwarnx(fmt, args);
217 	va_end(args);
218 	exit(status);
219 }
220 
221 void
222 _errxfp(FILE *fp, int status, const char *fmt, ...)
223 {
224 	va_list args;
225 
226 	va_start(args, fmt);
227 	_vwarnxfp(fp, fmt, args);
228 	va_end(args);
229 	exit(status);
230 }
231 
232 void
233 verrx(int status, const char *fmt, va_list args)
234 {
235 	vwarnx(fmt, args);
236 	exit(status);
237 }
238 
239 void
240 _verrxfp(FILE *fp, int status, const char *fmt, va_list args)
241 {
242 	_vwarnxfp(fp, fmt, args);
243 	exit(status);
244 }
245