xref: /onnv-gate/usr/src/lib/lvm/libsvm/common/getdrvname.c (revision 2805:a4be0ff24d1f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2805Seota  * Common Development and Distribution License (the "License").
6*2805Seota  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2805Seota  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
30*2805Seota #include <ctype.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <limits.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <svm.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  *	Macros to produce a quoted string containing the value of a
380Sstevel@tonic-gate  *	preprocessor macro. For example, if SIZE is defined to be 256,
390Sstevel@tonic-gate  *	VAL2STR(SIZE) is "256". This is used to construct format
400Sstevel@tonic-gate  *	strings for scanf-family functions below.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate #define	QUOTE(x)	#x
430Sstevel@tonic-gate #define	VAL2STR(x)	QUOTE(x)
440Sstevel@tonic-gate 
45*2805Seota static int is_blank(char *);
46*2805Seota 
47*2805Seota /*
48*2805Seota  * is_blank() returns 1 (true) if a line specified is composed of
49*2805Seota  * whitespace characters only. otherwise, it returns 0 (false).
50*2805Seota  *
51*2805Seota  * Note. the argument (line) must be null-terminated.
52*2805Seota  */
53*2805Seota static int
is_blank(char * line)54*2805Seota is_blank(char *line)
55*2805Seota {
56*2805Seota 	for (/* nothing */; *line != '\0'; line++)
57*2805Seota 		if (!isspace(*line))
58*2805Seota 			return (0);
59*2805Seota 	return (1);
60*2805Seota }
61*2805Seota 
620Sstevel@tonic-gate /*
630Sstevel@tonic-gate  * FUNCTION:
640Sstevel@tonic-gate  *	Return the driver name for a major number
650Sstevel@tonic-gate  *
660Sstevel@tonic-gate  * INPUT: major number, mount point for name_to_major file, pointer
670Sstevel@tonic-gate  * to a valid buffer.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * RETURN VALUES:
700Sstevel@tonic-gate  *	0 - SUCCESS - buf contain the driver name.
710Sstevel@tonic-gate  *	-1 - FAIL
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate 
750Sstevel@tonic-gate int
get_drv_name(major_t major,char * mnt,char * buf)760Sstevel@tonic-gate get_drv_name(major_t major, char *mnt, char *buf)
770Sstevel@tonic-gate {
780Sstevel@tonic-gate 	FILE *fp;
790Sstevel@tonic-gate 	char drv[FILENAME_MAX + 1];
800Sstevel@tonic-gate 	char entry[FILENAME_MAX + 1];
81*2805Seota 	char line[MAX_N2M_ALIAS_LINE], *cp;
820Sstevel@tonic-gate 	char fname[PATH_MAX];
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	int status = RET_NOERROR;
850Sstevel@tonic-gate 	(void) snprintf(fname, sizeof (fname), "%s%s", mnt, NAME_TO_MAJOR);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if ((fp = fopen(fname, "r")) == NULL) {
880Sstevel@tonic-gate 		return (RET_ERROR);
890Sstevel@tonic-gate 	}
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	while ((fgets(line, sizeof (line), fp) != NULL) &&
920Sstevel@tonic-gate 						status == RET_NOERROR) {
93*2805Seota 		/* cut off comments starting with '#' */
94*2805Seota 		if ((cp = strchr(line, '#')) != NULL)
95*2805Seota 			*cp = '\0';
96*2805Seota 		/* ignore comment or blank lines */
97*2805Seota 		if (is_blank(line))
98*2805Seota 			continue;
99*2805Seota 		/* sanity-check */
1000Sstevel@tonic-gate 		if (sscanf(line,
1010Sstevel@tonic-gate 		    "%" VAL2STR(FILENAME_MAX) "s %" VAL2STR(FILENAME_MAX) "s",
1020Sstevel@tonic-gate 		    drv, entry) != 2) {
1030Sstevel@tonic-gate 			status = RET_ERROR;
1040Sstevel@tonic-gate 		}
1050Sstevel@tonic-gate 		if (atoi(entry) == major)
1060Sstevel@tonic-gate 			break;
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (status == RET_NOERROR)
1100Sstevel@tonic-gate 		(void) strcpy(buf, drv);
1110Sstevel@tonic-gate 	(void) fclose(fp);
1120Sstevel@tonic-gate 	return (status);
1130Sstevel@tonic-gate }
114