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