17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
518c2affartem * Common Development and Distribution License (the "License").
618c2affartem * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217c478bdstevel@tonic-gate/*
2218c2affartem * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
2318c2affartem * Use is subject to license terms.
247c478bdstevel@tonic-gate */
257c478bdstevel@tonic-gate
267c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bdstevel@tonic-gate
287c478bdstevel@tonic-gate/*
297c478bdstevel@tonic-gate * routines in this module are meant to be called by other libvolmgt
307c478bdstevel@tonic-gate * routines only
317c478bdstevel@tonic-gate */
327c478bdstevel@tonic-gate
337c478bdstevel@tonic-gate#include	<stdio.h>
347c478bdstevel@tonic-gate#include	<string.h>
357c478bdstevel@tonic-gate#include	<dirent.h>
367c478bdstevel@tonic-gate#include	<string.h>
377c478bdstevel@tonic-gate#include	<libintl.h>
387c478bdstevel@tonic-gate#include	<limits.h>
397c478bdstevel@tonic-gate#include	<unistd.h>
407c478bdstevel@tonic-gate#include	<stdlib.h>
417c478bdstevel@tonic-gate#include	<sys/types.h>
427c478bdstevel@tonic-gate#include	<sys/stat.h>
437c478bdstevel@tonic-gate#include	<sys/param.h>
447c478bdstevel@tonic-gate#include	<sys/varargs.h>
457c478bdstevel@tonic-gate#include	"volmgt_private.h"
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate
487c478bdstevel@tonic-gate/*
497c478bdstevel@tonic-gate * fix the getfull{raw,blk}name problem for the fd and diskette case
507c478bdstevel@tonic-gate *
517c478bdstevel@tonic-gate * return value is malloc'ed, and must be free'd
527c478bdstevel@tonic-gate *
537c478bdstevel@tonic-gate * no match gets a malloc'ed null string
547c478bdstevel@tonic-gate */
557c478bdstevel@tonic-gate
567c478bdstevel@tonic-gatechar *
577c478bdstevel@tonic-gatevolmgt_getfullblkname(char *n)
587c478bdstevel@tonic-gate{
597c478bdstevel@tonic-gate	extern char	*getfullblkname(char *);
607c478bdstevel@tonic-gate	char		*rval;
617c478bdstevel@tonic-gate	char		namebuf[MAXPATHLEN+1];
627c478bdstevel@tonic-gate	char		*s;
637c478bdstevel@tonic-gate	char		c;
647c478bdstevel@tonic-gate	char		*res;
657c478bdstevel@tonic-gate
667c478bdstevel@tonic-gate
677c478bdstevel@tonic-gate
687c478bdstevel@tonic-gate	/* try to get full block-spcl device name */
697c478bdstevel@tonic-gate	rval = getfullblkname(n);
707c478bdstevel@tonic-gate	if ((rval != NULL) && (*rval != NULLC)) {
717c478bdstevel@tonic-gate		/* found it */
727c478bdstevel@tonic-gate		res = rval;
737c478bdstevel@tonic-gate		goto dun;
747c478bdstevel@tonic-gate	}
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate	/* we have a null-string result */
777c478bdstevel@tonic-gate	if (rval != NULL) {
787c478bdstevel@tonic-gate		/* free null string */
797c478bdstevel@tonic-gate		free(rval);
807c478bdstevel@tonic-gate	}
817c478bdstevel@tonic-gate
827c478bdstevel@tonic-gate	/* ok, so we either have a bad device or a floppy */
837c478bdstevel@tonic-gate
847c478bdstevel@tonic-gate	/* try the rfd# or rdiskette forms */
857c478bdstevel@tonic-gate	if (((s = strstr(n, "/rfd")) != NULL) ||
867c478bdstevel@tonic-gate	    ((s = strstr(n, "/rdiskette")) != NULL) ||
877c478bdstevel@tonic-gate	    ((s = strstr(n, "/rdsk/")) != NULL)) {
887c478bdstevel@tonic-gate		/*
897c478bdstevel@tonic-gate		 * we do not have to check for room here, since we will
907c478bdstevel@tonic-gate		 * be making the string one shorter
917c478bdstevel@tonic-gate		 */
927c478bdstevel@tonic-gate		c = *++s;			/* save the first char */
937c478bdstevel@tonic-gate		*s = NULLC;			/* replace it with a null */
947c478bdstevel@tonic-gate		(void) strcpy(namebuf, n);	/* save first part of it */
957c478bdstevel@tonic-gate		*s++ = c;			/* give the first char back */
967c478bdstevel@tonic-gate		(void) strcat(namebuf, s);	/* copy the rest */
977c478bdstevel@tonic-gate		res = strdup(namebuf);
987c478bdstevel@tonic-gate		goto dun;
997c478bdstevel@tonic-gate	}
1007c478bdstevel@tonic-gate
1017c478bdstevel@tonic-gate	/* no match found */
1027c478bdstevel@tonic-gate	res = strdup("");
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gatedun:
1057c478bdstevel@tonic-gate	return (res);
1067c478bdstevel@tonic-gate}
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate
1097c478bdstevel@tonic-gatechar *
1107c478bdstevel@tonic-gatevolmgt_getfullrawname(char *n)
1117c478bdstevel@tonic-gate{
1127c478bdstevel@tonic-gate	extern char	*getfullrawname(char *);
1137c478bdstevel@tonic-gate	char		*rval;
1147c478bdstevel@tonic-gate	char		namebuf[MAXPATHLEN+1];
1157c478bdstevel@tonic-gate	char		*s;
1167c478bdstevel@tonic-gate	char		c;
1177c478bdstevel@tonic-gate	char		*res;
1187c478bdstevel@tonic-gate
1197c478bdstevel@tonic-gate
1207c478bdstevel@tonic-gate#ifdef	DEBUG
1217c478bdstevel@tonic-gate	denter("volmgt_getfullrawname(%s): entering\n", n);
1227c478bdstevel@tonic-gate#endif
1237c478bdstevel@tonic-gate	/* try to get full char-spcl device name */
1247c478bdstevel@tonic-gate	rval = getfullrawname(n);
1257c478bdstevel@tonic-gate	if ((rval != NULL) && (*rval != NULLC)) {
1267c478bdstevel@tonic-gate		/* found it */
1277c478bdstevel@tonic-gate		res = rval;
1287c478bdstevel@tonic-gate		goto dun;
1297c478bdstevel@tonic-gate	}
1307c478bdstevel@tonic-gate
1317c478bdstevel@tonic-gate	/* we have a null-string result */
1327c478bdstevel@tonic-gate	if (rval != NULL) {
1337c478bdstevel@tonic-gate		/* free null string */
1347c478bdstevel@tonic-gate		free(rval);
1357c478bdstevel@tonic-gate	}
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate	/* ok, so we either have a bad device or a floppy */
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gate	/* try the "fd", "diskette", and the "dsk" form */
1407c478bdstevel@tonic-gate	if (((s = strstr(n, "/fd")) != NULL) ||
1417c478bdstevel@tonic-gate	    ((s = strstr(n, "/diskette")) != NULL) ||
1427c478bdstevel@tonic-gate	    ((s = strstr(n, "/dsk/")) != NULL)) {
1437c478bdstevel@tonic-gate		/*
1447c478bdstevel@tonic-gate		 * ensure we have room to add one more char
1457c478bdstevel@tonic-gate		 */
1467c478bdstevel@tonic-gate		if (strlen(n) < (MAXPATHLEN - 1)) {
1477c478bdstevel@tonic-gate			c = *++s;		/* save the first char */
1487c478bdstevel@tonic-gate			*s = NULLC;		/* replace it with a null */
1497c478bdstevel@tonic-gate			(void) strcpy(namebuf, n); /* save first part of str */
1507c478bdstevel@tonic-gate			*s = c;			/* put first charback */
1517c478bdstevel@tonic-gate			(void) strcat(namebuf, "r"); /* insert an 'r' */
1527c478bdstevel@tonic-gate			(void) strcat(namebuf, s); /* copy rest of str */
1537c478bdstevel@tonic-gate			res = strdup(namebuf);
1547c478bdstevel@tonic-gate			goto dun;
1557c478bdstevel@tonic-gate		}
1567c478bdstevel@tonic-gate	}
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gate	/* no match found */
1597c478bdstevel@tonic-gate	res = strdup("");
1607c478bdstevel@tonic-gatedun:
1617c478bdstevel@tonic-gate#ifdef	DEBUG
1627c478bdstevel@tonic-gate	dexit("volmgt_getfullrawname: returning %s\n",
1637c478bdstevel@tonic-gate	    res ? res : "<null ptr>");
1647c478bdstevel@tonic-gate#endif
1657c478bdstevel@tonic-gate	return (res);
1667c478bdstevel@tonic-gate}
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate
1697c478bdstevel@tonic-gate#ifdef	DEBUG
1707c478bdstevel@tonic-gate
1717c478bdstevel@tonic-gate/*
1727c478bdstevel@tonic-gate * debug print routines -- private to libvolmgt
1737c478bdstevel@tonic-gate */
1747c478bdstevel@tonic-gate
1757c478bdstevel@tonic-gate#define	DEBUG_INDENT_SPACES	"  "
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gateint	debug_level = 0;
1787c478bdstevel@tonic-gate
1797c478bdstevel@tonic-gate
1807c478bdstevel@tonic-gatestatic void
1817c478bdstevel@tonic-gatederrprint(char *fmt, va_list ap)
1827c478bdstevel@tonic-gate{
1837c478bdstevel@tonic-gate	int		i;
1847c478bdstevel@tonic-gate	int		j;
1857c478bdstevel@tonic-gate	char		date_buf[256];
1867c478bdstevel@tonic-gate	time_t		t;
1877c478bdstevel@tonic-gate	struct tm	*tm;
1887c478bdstevel@tonic-gate
1897c478bdstevel@tonic-gate
1907c478bdstevel@tonic-gate	(void) time(&t);
1917c478bdstevel@tonic-gate	tm = localtime(&t);
1927c478bdstevel@tonic-gate	(void) fprintf(stderr, "%02d/%02d/%02d %02d:%02d:%02d ",
1937c478bdstevel@tonic-gate	    tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100,
1947c478bdstevel@tonic-gate	    tm->tm_hour, tm->tm_min, tm->tm_sec);
1957c478bdstevel@tonic-gate	for (i = 0; i < debug_level; i++) {
1967c478bdstevel@tonic-gate		(void) fprintf(stderr, DEBUG_INDENT_SPACES);
1977c478bdstevel@tonic-gate	}
1987c478bdstevel@tonic-gate	(void) vfprintf(stderr, fmt, ap);
1997c478bdstevel@tonic-gate}
2007c478bdstevel@tonic-gate
2017c478bdstevel@tonic-gate/*
2027c478bdstevel@tonic-gate * denter -- do a derrprint(), then increment debug level
2037c478bdstevel@tonic-gate */
2047c478bdstevel@tonic-gatevoid
2057c478bdstevel@tonic-gatedenter(char *fmt, ...)
2067c478bdstevel@tonic-gate{
2077c478bdstevel@tonic-gate	va_list		ap;
2087c478bdstevel@tonic-gate
2097c478bdstevel@tonic-gate	va_start(ap, fmt);
2107c478bdstevel@tonic-gate	derrprint(fmt, ap);
2117c478bdstevel@tonic-gate	va_end(ap);
2127c478bdstevel@tonic-gate	debug_level++;
2137c478bdstevel@tonic-gate}
2147c478bdstevel@tonic-gate
2157c478bdstevel@tonic-gate/*
2167c478bdstevel@tonic-gate * dexit -- decrement debug level then do a derrprint()
2177c478bdstevel@tonic-gate */
2187c478bdstevel@tonic-gatevoid
2197c478bdstevel@tonic-gatedexit(char *fmt, ...)
2207c478bdstevel@tonic-gate{
2217c478bdstevel@tonic-gate	va_list		ap;
2227c478bdstevel@tonic-gate
2237c478bdstevel@tonic-gate	if (--debug_level < 0) {
2247c478bdstevel@tonic-gate		debug_level = 0;
2257c478bdstevel@tonic-gate	}
2267c478bdstevel@tonic-gate	va_start(ap, fmt);
2277c478bdstevel@tonic-gate	derrprint(fmt, ap);
2287c478bdstevel@tonic-gate	va_end(ap);
2297c478bdstevel@tonic-gate}
2307c478bdstevel@tonic-gate
2317c478bdstevel@tonic-gate/*
2327c478bdstevel@tonic-gate * dprintf -- print debug info, indenting based on debug level
2337c478bdstevel@tonic-gate */
2347c478bdstevel@tonic-gatevoid
2357c478bdstevel@tonic-gatedprintf(char *fmt, ...)
2367c478bdstevel@tonic-gate{
2377c478bdstevel@tonic-gate	va_list		ap;
2387c478bdstevel@tonic-gate
2397c478bdstevel@tonic-gate	va_start(ap, fmt);
2407c478bdstevel@tonic-gate	derrprint(fmt, ap);
2417c478bdstevel@tonic-gate	va_end(ap);
2427c478bdstevel@tonic-gate}
2437c478bdstevel@tonic-gate
2447c478bdstevel@tonic-gate#endif	/* DEBUG */
245