xref: /illumos-gate/usr/src/cmd/pools/common/utils.c (revision 628e3cbed6489fa1db545d8524a06cd6535af456)
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 2003 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 <libintl.h>
30 #include <limits.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <errno.h>
36 
37 #include <pool.h>
38 #include "utils.h"
39 
40 static const char PNAME_FMT[] = "%s: ";
41 static const char ERRNO_FMT[] = ": %s\n";
42 
43 static const char *pname;
44 
45 /*LINTLIBRARY*/
46 const char *
47 get_errstr_err(int errnum, int syserr)
48 {
49 	const char *errstr;
50 
51 	if (errnum == POE_SYSTEM)
52 		errstr = strerror(syserr);
53 	else
54 		errstr = pool_strerror(errnum);
55 	return (errstr);
56 }
57 
58 const char *
59 get_errstr(void)
60 {
61 	return (get_errstr_err(pool_error(), errno));
62 }
63 
64 /*PRINTFLIKE1*/
65 void
66 warn(const char *format, ...)
67 {
68 	int err = errno;
69 	va_list alist;
70 
71 	if (pname != NULL)
72 		(void) fprintf(stderr, PNAME_FMT, pname);
73 
74 	va_start(alist, format);
75 	(void) vfprintf(stderr, format, alist);
76 	va_end(alist);
77 
78 	if (strrchr(format, '\n') == NULL)
79 		(void) fprintf(stderr, ERRNO_FMT, strerror(err));
80 }
81 
82 /*PRINTFLIKE1*/
83 void
84 die(const char *format, ...)
85 {
86 	int err = errno;
87 	va_list alist;
88 
89 	if (pname != NULL)
90 		(void) fprintf(stderr, PNAME_FMT, pname);
91 
92 	va_start(alist, format);
93 	(void) vfprintf(stderr, format, alist);
94 	va_end(alist);
95 
96 	if (strrchr(format, '\n') == NULL)
97 		(void) fprintf(stderr, ERRNO_FMT, strerror(err));
98 
99 	exit(E_ERROR);
100 }
101 
102 const char *
103 getpname(const char *arg0)
104 {
105 	const char *p = strrchr(arg0, '/');
106 
107 	if (p == NULL)
108 		p = arg0;
109 	else
110 		p++;
111 
112 	pname = p;
113 	return (p);
114 }
115