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