xref: /illumos-gate/usr/src/tools/ctf/common/utils.c (revision 86d949f9497332fe19be6b5d711d265eb957439f)
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 (c) 1998-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <string.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <errno.h>
32 
33 #include "utils.h"
34 
35 /*LINTLIBRARY*/
36 
37 static const char *pname;
38 
39 #pragma init(getpname)
40 const char *
41 getpname(void)
42 {
43 	const char *p, *q;
44 
45 	if (pname != NULL)
46 		return (pname);
47 
48 	if ((p = getexecname()) != NULL)
49 		q = strrchr(p, '/');
50 	else
51 		q = NULL;
52 
53 	if (q == NULL)
54 		pname = p;
55 	else
56 		pname = q + 1;
57 
58 	return (pname);
59 }
60 
61 void
62 vwarn(const char *format, va_list alist)
63 {
64 	int err = errno;
65 
66 	if (pname != NULL)
67 		(void) fprintf(stderr, "%s: ", pname);
68 
69 	(void) vfprintf(stderr, format, alist);
70 
71 	if (strchr(format, '\n') == NULL)
72 		(void) fprintf(stderr, ": %s\n", strerror(err));
73 }
74 
75 /*PRINTFLIKE1*/
76 void
77 warn(const char *format, ...)
78 {
79 	va_list alist;
80 
81 	va_start(alist, format);
82 	vwarn(format, alist);
83 	va_end(alist);
84 }
85 
86 void
87 vdie(const char *format, va_list alist)
88 {
89 	vwarn(format, alist);
90 	exit(E_ERROR);
91 }
92 
93 /*PRINTFLIKE1*/
94 void
95 die(const char *format, ...)
96 {
97 	va_list alist;
98 
99 	va_start(alist, format);
100 	vdie(format, alist);
101 	va_end(alist);
102 }
103