xref: /illumos-gate/usr/src/lib/libc/port/gen/err.c (revision 942c5e3c2dd127463517e5cc1694ee94ca45e021)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #pragma weak err = _err
29 #pragma weak errx = _errx
30 #pragma weak verr = _verr
31 #pragma weak verrx = _verrx
32 #pragma weak warn = _warn
33 #pragma weak warnx = _warnx
34 #pragma weak vwarn = _vwarn
35 #pragma weak vwarnx = _vwarnx
36 
37 #include "synonyms.h"
38 #include <sys/types.h>
39 #include <err.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <stdarg.h>
43 #include <string.h>
44 #include <errno.h>
45 
46 /* Function exit/warning functions and global variables. */
47 
48 static const char *progname;
49 
50 /*
51  * warncore() is the workhorse of these functions.  Everything else has
52  * a warncore() component in it.
53  */
54 static void
55 warncore(FILE *fp, const char *fmt, va_list args)
56 {
57 	const char *execname;
58 
59 	flockfile(fp);
60 	if (progname == NULL) {
61 		execname = getexecname();
62 		if ((execname != NULL) &&
63 		    ((progname = strrchr(execname, '/')) != NULL))
64 			progname++;
65 		else
66 			progname = execname;
67 	}
68 
69 	if (progname != NULL)
70 		(void) fprintf(fp, "%s: ", progname);
71 
72 	if (fmt != NULL) {
73 		(void) vfprintf(fp, fmt, args);
74 	}
75 }
76 
77 /* Finish a warning with a newline and a flush of stderr. */
78 static void
79 warnfinish(FILE *fp)
80 {
81 	(void) fputc('\n', fp);
82 	(void) fflush(fp);
83 	funlockfile(fp);
84 }
85 
86 void
87 _vwarnxfp(FILE *fp, const char *fmt, va_list args)
88 {
89 	warncore(fp, fmt, args);
90 	warnfinish(fp);
91 }
92 
93 void
94 vwarnx(const char *fmt, va_list args)
95 {
96 	_vwarnxfp(stderr, fmt, args);
97 }
98 
99 void
100 _vwarnfp(FILE *fp, const char *fmt, va_list args)
101 {
102 	int tmperr = errno;	/* Capture errno now. */
103 
104 	warncore(fp, fmt, args);
105 	if (fmt != NULL) {
106 		(void) fputc(':', fp);
107 		(void) fputc(' ', fp);
108 	}
109 	(void) fputs(strerror(tmperr), fp);
110 	warnfinish(fp);
111 }
112 
113 void
114 vwarn(const char *fmt, va_list args)
115 {
116 	_vwarnfp(stderr, fmt, args);
117 }
118 
119 /* PRINTFLIKE1 */
120 void
121 warnx(const char *fmt, ...)
122 {
123 	va_list args;
124 
125 	va_start(args, fmt);
126 	vwarnx(fmt, args);
127 	va_end(args);
128 }
129 
130 void
131 _warnfp(FILE *fp, const char *fmt, ...)
132 {
133 	va_list args;
134 
135 	va_start(args, fmt);
136 	_vwarnfp(fp, fmt, args);
137 	va_end(args);
138 }
139 
140 void
141 _warnxfp(FILE *fp, const char *fmt, ...)
142 {
143 	va_list args;
144 
145 	va_start(args, fmt);
146 	_vwarnxfp(fp, fmt, args);
147 	va_end(args);
148 }
149 
150 /* PRINTFLIKE1 */
151 void
152 warn(const char *fmt, ...)
153 {
154 	va_list args;
155 
156 	va_start(args, fmt);
157 	vwarn(fmt, args);
158 	va_end(args);
159 }
160 
161 /* PRINTFLIKE2 */
162 void
163 err(int status, const char *fmt, ...)
164 {
165 	va_list args;
166 
167 	va_start(args, fmt);
168 	vwarn(fmt, args);
169 	va_end(args);
170 	exit(status);
171 }
172 
173 void
174 _errfp(FILE *fp, int status, const char *fmt, ...)
175 {
176 	va_list args;
177 
178 	va_start(args, fmt);
179 	_vwarnfp(fp, fmt, args);
180 	va_end(args);
181 	exit(status);
182 }
183 
184 void
185 verr(int status, const char *fmt, va_list args)
186 {
187 	vwarn(fmt, args);
188 	exit(status);
189 }
190 
191 void
192 _verrfp(FILE *fp, int status, const char *fmt, va_list args)
193 {
194 	_vwarnfp(fp, fmt, args);
195 	exit(status);
196 }
197 
198 /* PRINTFLIKE2 */
199 void
200 errx(int status, const char *fmt, ...)
201 {
202 	va_list args;
203 
204 	va_start(args, fmt);
205 	vwarnx(fmt, args);
206 	va_end(args);
207 	exit(status);
208 }
209 
210 void
211 _errxfp(FILE *fp, int status, const char *fmt, ...)
212 {
213 	va_list args;
214 
215 	va_start(args, fmt);
216 	_vwarnxfp(fp, fmt, args);
217 	va_end(args);
218 	exit(status);
219 }
220 
221 void
222 verrx(int status, const char *fmt, va_list args)
223 {
224 	vwarnx(fmt, args);
225 	exit(status);
226 }
227 
228 void
229 _verrxfp(FILE *fp, int status, const char *fmt, va_list args)
230 {
231 	_vwarnxfp(fp, fmt, args);
232 	exit(status);
233 }
234