xref: /illumos-gate/usr/src/lib/libc/port/gen/err.c (revision d362b749)
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 	flockfile(fp);
58 	if (progname == NULL) {
59 		progname = strrchr(getexecname(), '/');
60 		if (progname == NULL)
61 			progname = getexecname();
62 		else
63 			progname++;
64 	}
65 
66 	(void) fprintf(fp, "%s: ", progname);
67 
68 	if (fmt != NULL) {
69 		(void) vfprintf(fp, fmt, args);
70 	}
71 }
72 
73 /* Finish a warning with a newline and a flush of stderr. */
74 static void
75 warnfinish(FILE *fp)
76 {
77 	(void) fputc('\n', fp);
78 	(void) fflush(fp);
79 	funlockfile(fp);
80 }
81 
82 void
83 _vwarnxfp(FILE *fp, const char *fmt, va_list args)
84 {
85 	warncore(fp, fmt, args);
86 	warnfinish(fp);
87 }
88 
89 void
90 vwarnx(const char *fmt, va_list args)
91 {
92 	_vwarnxfp(stderr, fmt, args);
93 }
94 
95 void
96 _vwarnfp(FILE *fp, const char *fmt, va_list args)
97 {
98 	int tmperr = errno;	/* Capture errno now. */
99 
100 	warncore(fp, fmt, args);
101 	if (fmt != NULL) {
102 		(void) fputc(':', fp);
103 		(void) fputc(' ', fp);
104 	}
105 	(void) fputs(strerror(tmperr), fp);
106 	warnfinish(fp);
107 }
108 
109 void
110 vwarn(const char *fmt, va_list args)
111 {
112 	_vwarnfp(stderr, fmt, args);
113 }
114 
115 /* PRINTFLIKE1 */
116 void
117 warnx(const char *fmt, ...)
118 {
119 	va_list args;
120 
121 	va_start(args, fmt);
122 	vwarnx(fmt, args);
123 	va_end(args);
124 }
125 
126 void
127 _warnfp(FILE *fp, const char *fmt, ...)
128 {
129 	va_list args;
130 
131 	va_start(args, fmt);
132 	_vwarnfp(fp, fmt, args);
133 	va_end(args);
134 }
135 
136 void
137 _warnxfp(FILE *fp, const char *fmt, ...)
138 {
139 	va_list args;
140 
141 	va_start(args, fmt);
142 	_vwarnxfp(fp, fmt, args);
143 	va_end(args);
144 }
145 
146 /* PRINTFLIKE1 */
147 void
148 warn(const char *fmt, ...)
149 {
150 	va_list args;
151 
152 	va_start(args, fmt);
153 	vwarn(fmt, args);
154 	va_end(args);
155 }
156 
157 /* PRINTFLIKE2 */
158 void
159 err(int status, const char *fmt, ...)
160 {
161 	va_list args;
162 
163 	va_start(args, fmt);
164 	vwarn(fmt, args);
165 	va_end(args);
166 	exit(status);
167 }
168 
169 void
170 _errfp(FILE *fp, int status, const char *fmt, ...)
171 {
172 	va_list args;
173 
174 	va_start(args, fmt);
175 	_vwarnfp(fp, fmt, args);
176 	va_end(args);
177 	exit(status);
178 }
179 
180 void
181 verr(int status, const char *fmt, va_list args)
182 {
183 	vwarn(fmt, args);
184 	exit(status);
185 }
186 
187 void
188 _verrfp(FILE *fp, int status, const char *fmt, va_list args)
189 {
190 	_vwarnfp(fp, fmt, args);
191 	exit(status);
192 }
193 
194 /* PRINTFLIKE2 */
195 void
196 errx(int status, const char *fmt, ...)
197 {
198 	va_list args;
199 
200 	va_start(args, fmt);
201 	vwarnx(fmt, args);
202 	va_end(args);
203 	exit(status);
204 }
205 
206 void
207 _errxfp(FILE *fp, int status, const char *fmt, ...)
208 {
209 	va_list args;
210 
211 	va_start(args, fmt);
212 	_vwarnxfp(fp, fmt, args);
213 	va_end(args);
214 	exit(status);
215 }
216 
217 void
218 verrx(int status, const char *fmt, va_list args)
219 {
220 	vwarnx(fmt, args);
221 	exit(status);
222 }
223 
224 void
225 _verrxfp(FILE *fp, int status, const char *fmt, va_list args)
226 {
227 	_vwarnxfp(fp, fmt, args);
228 	exit(status);
229 }
230