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