libc: add errc, verrc, warnc, wvarnc
[unleashed.git] / usr / src / lib / libc / port / gen / err.c
blob18450780a23996a64516330c5f6f5f0c7ab42416
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include "lint.h"
27 #include "file64.h"
28 #include "mtlib.h"
29 #include "thr_uberdata.h"
30 #include <sys/types.h>
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <dlfcn.h>
38 #include "stdiom.h"
40 /* Function exit/warning functions and global variables. */
42 const char *__progname; /* GNU/Linux/BSD compatibility */
44 #define PROGNAMESIZE 128 /* buffer size for __progname */
46 const char *
47 getprogname(void)
49 return (__progname);
52 void
53 setprogname(const char *argv0)
55 uberdata_t *udp = curthread->ul_uberdata;
56 const char *progname;
58 if ((progname = strrchr(argv0, '/')) == NULL)
59 progname = argv0;
60 else
61 progname++;
63 if (udp->progname == NULL)
64 udp->progname = lmalloc(PROGNAMESIZE);
65 (void) strlcpy(udp->progname, progname, PROGNAMESIZE);
66 __progname = udp->progname;
69 /* called only from libc_init() */
70 void
71 init_progname(void)
73 Dl_argsinfo_t args;
74 const char *argv0;
76 if (dlinfo(RTLD_SELF, RTLD_DI_ARGSINFO, &args) < 0 ||
77 args.dla_argc <= 0 ||
78 (argv0 = args.dla_argv[0]) == NULL)
79 argv0 = "UNKNOWN";
81 setprogname(argv0);
85 * warncore() is the workhorse of these functions. Everything else has
86 * a warncore() component in it.
88 static rmutex_t *
89 warncore(FILE *fp, const char *fmt, va_list args)
91 rmutex_t *lk;
93 FLOCKFILE(lk, fp);
95 if (__progname != NULL)
96 (void) fprintf(fp, "%s: ", __progname);
98 if (fmt != NULL) {
99 (void) vfprintf(fp, fmt, args);
102 return (lk);
105 /* Finish a warning with a newline and a flush of stderr. */
106 static void
107 warnfinish(FILE *fp, rmutex_t *lk)
109 (void) fputc('\n', fp);
110 (void) fflush(fp);
111 FUNLOCKFILE(lk);
114 void
115 _vwarnxfp(FILE *fp, const char *fmt, va_list args)
117 rmutex_t *lk;
119 lk = warncore(fp, fmt, args);
120 warnfinish(fp, lk);
123 void
124 vwarnx(const char *fmt, va_list args)
126 _vwarnxfp(stderr, fmt, args);
129 void
130 _vwarnfp(FILE *fp, int code, const char *fmt, va_list args)
132 rmutex_t *lk;
134 lk = warncore(fp, fmt, args);
135 if (fmt != NULL) {
136 (void) fputc(':', fp);
137 (void) fputc(' ', fp);
139 (void) fputs(strerror(code), fp);
140 warnfinish(fp, lk);
143 void
144 vwarn(const char *fmt, va_list args)
146 _vwarnfp(stderr, errno, fmt, args);
149 /* PRINTFLIKE1 */
150 void
151 warnx(const char *fmt, ...)
153 va_list args;
155 va_start(args, fmt);
156 vwarnx(fmt, args);
157 va_end(args);
160 void
161 _warnfp(FILE *fp, const char *fmt, ...)
163 va_list args;
165 va_start(args, fmt);
166 _vwarnfp(fp, errno, fmt, args);
167 va_end(args);
170 void
171 _warnxfp(FILE *fp, const char *fmt, ...)
173 va_list args;
175 va_start(args, fmt);
176 _vwarnxfp(fp, fmt, args);
177 va_end(args);
180 /* PRINTFLIKE1 */
181 void
182 warn(const char *fmt, ...)
184 va_list args;
186 va_start(args, fmt);
187 vwarn(fmt, args);
188 va_end(args);
191 void
192 warnc(int code, const char *fmt, ...)
194 va_list args;
195 va_start(args, fmt);
196 vwarnc(code, fmt, args);
197 va_end(args);
200 void
201 vwarnc(int code, const char *fmt, va_list args)
203 _vwarnfp(stderr, code, fmt, args);
206 /* PRINTFLIKE2 */
207 void
208 err(int status, const char *fmt, ...)
210 va_list args;
212 va_start(args, fmt);
213 vwarn(fmt, args);
214 va_end(args);
215 exit(status);
218 void
219 errc(int status, int code, const char *fmt, ...)
221 va_list args;
222 va_start(args, fmt);
223 vwarnc(code, fmt, args);
224 va_end(args);
225 exit(status);
228 void
229 verrc(int status, int code, const char *fmt, va_list args)
231 vwarnc(code, fmt, args);
232 exit(status);
235 void
236 _errfp(FILE *fp, int status, const char *fmt, ...)
238 va_list args;
240 va_start(args, fmt);
241 _vwarnfp(fp, errno, fmt, args);
242 va_end(args);
243 exit(status);
246 void
247 verr(int status, const char *fmt, va_list args)
249 vwarn(fmt, args);
250 exit(status);
254 void
255 _verrfp(FILE *fp, int status, const char *fmt, va_list args)
257 _vwarnfp(fp, errno, fmt, args);
258 exit(status);
261 /* PRINTFLIKE2 */
262 void
263 errx(int status, const char *fmt, ...)
265 va_list args;
267 va_start(args, fmt);
268 vwarnx(fmt, args);
269 va_end(args);
270 exit(status);
273 void
274 _errxfp(FILE *fp, int status, const char *fmt, ...)
276 va_list args;
278 va_start(args, fmt);
279 _vwarnxfp(fp, fmt, args);
280 va_end(args);
281 exit(status);
284 void
285 verrx(int status, const char *fmt, va_list args)
287 vwarnx(fmt, args);
288 exit(status);
291 void
292 _verrxfp(FILE *fp, int status, const char *fmt, va_list args)
294 _vwarnxfp(fp, fmt, args);
295 exit(status);