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