xref: /titanic_52/usr/src/cmd/lofiadm/utils.c (revision b6c3f7863936abeae522e48a13887dddeb691a45)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/stat.h>
31 #include <sys/statvfs.h>
32 #include <sys/sysmacros.h>
33 #include <libintl.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <errno.h>
39 #include <dlfcn.h>
40 #include <link.h>
41 
42 #include "utils.h"
43 
44 static void *lib_hdl = NULL;
45 
46 static const char PNAME_FMT[] = "%s: ";
47 static const char ERRNO_FMT[] = ": %s\n";
48 
49 static const char *pname;
50 
51 /*PRINTFLIKE1*/
52 static void
53 warn(const char *format, ...)
54 {
55 	int err = errno;
56 	va_list alist;
57 
58 	if (pname != NULL)
59 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
60 
61 	va_start(alist, format);
62 	(void) vfprintf(stderr, format, alist);
63 	va_end(alist);
64 
65 	if (strchr(format, '\n') == NULL)
66 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
67 }
68 
69 /*PRINTFLIKE1*/
70 void
71 die(const char *format, ...)
72 {
73 	int err = errno;
74 	va_list alist;
75 
76 	if (pname != NULL)
77 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
78 
79 	va_start(alist, format);
80 	(void) vfprintf(stderr, format, alist);
81 	va_end(alist);
82 
83 	if (strchr(format, '\n') == NULL)
84 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
85 
86 	exit(E_ERROR);
87 }
88 
89 const char *
90 getpname(const char *arg0)
91 {
92 	const char *p = strrchr(arg0, '/');
93 
94 	if (p == NULL)
95 		p = arg0;
96 	else
97 		p++;
98 
99 	pname = p;
100 	return (p);
101 }
102 
103 int
104 valid_abspath(const char *p)
105 {
106 	if (p[0] != '/') {
107 		warn(gettext("pathname is not an absolute path -- %s\n"), p);
108 		return (0);
109 	}
110 
111 	if (strlen(p) > MAXPATHLEN) {
112 		warn(gettext("pathname is too long -- %s\n"), p);
113 		return (0);
114 	}
115 
116 	return (1);
117 }
118 
119 /*
120  * Wrapper for dlopen'ing a library.
121  * The caller must call closelib() once
122  * access to the library is no longer needed.
123  */
124 void *
125 openlib(const char *lib)
126 {
127 	lib_hdl = dlopen(lib, RTLD_LAZY);
128 	return (lib_hdl);
129 }
130 
131 void
132 closelib()
133 {
134 	if (lib_hdl != NULL)
135 		(void) dlclose(lib_hdl);
136 }
137