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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * Error-handling routines
29  */
30 
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <errno.h>
36 
37 #include <inj_err.h>
38 
39 /*LINTLIBRARY*/
40 
41 static const char *pname;
42 
43 #pragma init(getpname)
44 const char *
getpname(void)45 getpname(void)
46 {
47 	const char *p, *q;
48 
49 	if (pname != NULL)
50 		return (pname);
51 
52 	if ((p = getexecname()) != NULL)
53 		q = strrchr(p, '/');
54 	else
55 		q = NULL;
56 
57 	if (q == NULL)
58 		pname = p;
59 	else
60 		pname = q + 1;
61 
62 	return (pname);
63 }
64 
65 void
vwarn(const char * format,va_list alist)66 vwarn(const char *format, va_list alist)
67 {
68 	int err = errno;
69 
70 	if (pname != NULL)
71 		(void) fprintf(stderr, "%s: ", pname);
72 
73 	(void) vfprintf(stderr, format, alist);
74 
75 	if (strchr(format, '\n') == NULL)
76 		(void) fprintf(stderr, ": %s\n", strerror(err));
77 }
78 
79 /*PRINTFLIKE1*/
80 void
warn(const char * format,...)81 warn(const char *format, ...)
82 {
83 	va_list alist;
84 
85 	va_start(alist, format);
86 	vwarn(format, alist);
87 	va_end(alist);
88 }
89 
90 void
vdie(const char * format,va_list alist)91 vdie(const char *format, va_list alist)
92 {
93 	vwarn(format, alist);
94 	exit(E_ERROR);
95 }
96 
97 /*PRINTFLIKE1*/
98 void
die(const char * format,...)99 die(const char *format, ...)
100 {
101 	va_list alist;
102 
103 	va_start(alist, format);
104 	vdie(format, alist);
105 	va_end(alist);
106 }
107 
108 int
inj_set_errno(int err)109 inj_set_errno(int err)
110 {
111 	errno = err;
112 	return (-1);
113 }
114