xref: /illumos-gate/usr/src/lib/libvolmgt/common/volprivate.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * routines in this module are meant to be called by other libvolmgt
28  * routines only
29  */
30 
31 #include	<stdio.h>
32 #include	<string.h>
33 #include	<dirent.h>
34 #include	<string.h>
35 #include	<libintl.h>
36 #include	<limits.h>
37 #include	<unistd.h>
38 #include	<stdlib.h>
39 #include	<sys/types.h>
40 #include	<sys/stat.h>
41 #include	<sys/param.h>
42 #include	<sys/varargs.h>
43 #include	"volmgt_private.h"
44 
45 
46 /*
47  * fix the getfull{raw,blk}name problem for the fd and diskette case
48  *
49  * return value is malloc'ed, and must be free'd
50  *
51  * no match gets a malloc'ed null string
52  */
53 
54 char *
55 volmgt_getfullblkname(char *n)
56 {
57 	extern char	*getfullblkname(char *);
58 	char		*rval;
59 	char		namebuf[MAXPATHLEN+1];
60 	char		*s;
61 	char		c;
62 	char		*res;
63 
64 
65 
66 	/* try to get full block-spcl device name */
67 	rval = getfullblkname(n);
68 	if ((rval != NULL) && (*rval != NULLC)) {
69 		/* found it */
70 		res = rval;
71 		goto dun;
72 	}
73 
74 	/* we have a null-string result */
75 	if (rval != NULL) {
76 		/* free null string */
77 		free(rval);
78 	}
79 
80 	/* ok, so we either have a bad device or a floppy */
81 
82 	/* try the rfd# or rdiskette forms */
83 	if (((s = strstr(n, "/rfd")) != NULL) ||
84 	    ((s = strstr(n, "/rdiskette")) != NULL) ||
85 	    ((s = strstr(n, "/rdsk/")) != NULL)) {
86 		/*
87 		 * we do not have to check for room here, since we will
88 		 * be making the string one shorter
89 		 */
90 		c = *++s;			/* save the first char */
91 		*s = NULLC;			/* replace it with a null */
92 		(void) strcpy(namebuf, n);	/* save first part of it */
93 		*s++ = c;			/* give the first char back */
94 		(void) strcat(namebuf, s);	/* copy the rest */
95 		res = strdup(namebuf);
96 		goto dun;
97 	}
98 
99 	/* no match found */
100 	res = strdup("");
101 
102 dun:
103 	return (res);
104 }
105 
106 
107 char *
108 volmgt_getfullrawname(char *n)
109 {
110 	extern char	*getfullrawname(char *);
111 	char		*rval;
112 	char		namebuf[MAXPATHLEN+1];
113 	char		*s;
114 	char		c;
115 	char		*res;
116 
117 
118 #ifdef	DEBUG
119 	denter("volmgt_getfullrawname(%s): entering\n", n);
120 #endif
121 	/* try to get full char-spcl device name */
122 	rval = getfullrawname(n);
123 	if ((rval != NULL) && (*rval != NULLC)) {
124 		/* found it */
125 		res = rval;
126 		goto dun;
127 	}
128 
129 	/* we have a null-string result */
130 	if (rval != NULL) {
131 		/* free null string */
132 		free(rval);
133 	}
134 
135 	/* ok, so we either have a bad device or a floppy */
136 
137 	/* try the "fd", "diskette", and the "dsk" form */
138 	if (((s = strstr(n, "/fd")) != NULL) ||
139 	    ((s = strstr(n, "/diskette")) != NULL) ||
140 	    ((s = strstr(n, "/dsk/")) != NULL)) {
141 		/*
142 		 * ensure we have room to add one more char
143 		 */
144 		if (strlen(n) < (MAXPATHLEN - 1)) {
145 			c = *++s;		/* save the first char */
146 			*s = NULLC;		/* replace it with a null */
147 			(void) strcpy(namebuf, n); /* save first part of str */
148 			*s = c;			/* put first charback */
149 			(void) strcat(namebuf, "r"); /* insert an 'r' */
150 			(void) strcat(namebuf, s); /* copy rest of str */
151 			res = strdup(namebuf);
152 			goto dun;
153 		}
154 	}
155 
156 	/* no match found */
157 	res = strdup("");
158 dun:
159 #ifdef	DEBUG
160 	dexit("volmgt_getfullrawname: returning %s\n",
161 	    res ? res : "<null ptr>");
162 #endif
163 	return (res);
164 }
165 
166 
167 #ifdef	DEBUG
168 
169 /*
170  * debug print routines -- private to libvolmgt
171  */
172 
173 #define	DEBUG_INDENT_SPACES	"  "
174 
175 int	debug_level = 0;
176 
177 
178 static void
179 derrprint(char *fmt, va_list ap)
180 {
181 	int		i;
182 	int		j;
183 	char		date_buf[256];
184 	time_t		t;
185 	struct tm	*tm;
186 
187 
188 	(void) time(&t);
189 	tm = localtime(&t);
190 	(void) fprintf(stderr, "%02d/%02d/%02d %02d:%02d:%02d ",
191 	    tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100,
192 	    tm->tm_hour, tm->tm_min, tm->tm_sec);
193 	for (i = 0; i < debug_level; i++) {
194 		(void) fprintf(stderr, DEBUG_INDENT_SPACES);
195 	}
196 	(void) vfprintf(stderr, fmt, ap);
197 }
198 
199 /*
200  * denter -- do a derrprint(), then increment debug level
201  */
202 void
203 denter(char *fmt, ...)
204 {
205 	va_list		ap;
206 
207 	va_start(ap, fmt);
208 	derrprint(fmt, ap);
209 	va_end(ap);
210 	debug_level++;
211 }
212 
213 /*
214  * dexit -- decrement debug level then do a derrprint()
215  */
216 void
217 dexit(char *fmt, ...)
218 {
219 	va_list		ap;
220 
221 	if (--debug_level < 0) {
222 		debug_level = 0;
223 	}
224 	va_start(ap, fmt);
225 	derrprint(fmt, ap);
226 	va_end(ap);
227 }
228 
229 /*
230  * dprintf -- print debug info, indenting based on debug level
231  */
232 void
233 dprintf(char *fmt, ...)
234 {
235 	va_list		ap;
236 
237 	va_start(ap, fmt);
238 	derrprint(fmt, ap);
239 	va_end(ap);
240 }
241 
242 #endif	/* DEBUG */
243