xref: /titanic_41/usr/src/lib/lvm/libsvm/common/getdrvname.c (revision 1816cb7076d3ec8a78ef9ac9f895574e13c43645)
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 #include <stdio.h>
29 #include <stdlib.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include <limits.h>
33 #include <sys/types.h>
34 #include <svm.h>
35 
36 /*
37  *	Macros to produce a quoted string containing the value of a
38  *	preprocessor macro. For example, if SIZE is defined to be 256,
39  *	VAL2STR(SIZE) is "256". This is used to construct format
40  *	strings for scanf-family functions below.
41  */
42 #define	QUOTE(x)	#x
43 #define	VAL2STR(x)	QUOTE(x)
44 
45 static int is_blank(char *);
46 
47 /*
48  * is_blank() returns 1 (true) if a line specified is composed of
49  * whitespace characters only. otherwise, it returns 0 (false).
50  *
51  * Note. the argument (line) must be null-terminated.
52  */
53 static int
54 is_blank(char *line)
55 {
56 	for (/* nothing */; *line != '\0'; line++)
57 		if (!isspace(*line))
58 			return (0);
59 	return (1);
60 }
61 
62 /*
63  * FUNCTION:
64  *	Return the driver name for a major number
65  *
66  * INPUT: major number, mount point for name_to_major file, pointer
67  * to a valid buffer.
68  *
69  * RETURN VALUES:
70  *	0 - SUCCESS - buf contain the driver name.
71  *	-1 - FAIL
72  *
73  */
74 
75 int
76 get_drv_name(major_t major, char *mnt, char *buf)
77 {
78 	FILE *fp;
79 	char drv[FILENAME_MAX + 1];
80 	char entry[FILENAME_MAX + 1];
81 	char line[MAX_N2M_ALIAS_LINE], *cp;
82 	char fname[PATH_MAX];
83 
84 	int status = RET_NOERROR;
85 	(void) snprintf(fname, sizeof (fname), "%s%s", mnt, NAME_TO_MAJOR);
86 
87 	if ((fp = fopen(fname, "r")) == NULL) {
88 		return (RET_ERROR);
89 	}
90 
91 	while ((fgets(line, sizeof (line), fp) != NULL) &&
92 						status == RET_NOERROR) {
93 		/* cut off comments starting with '#' */
94 		if ((cp = strchr(line, '#')) != NULL)
95 			*cp = '\0';
96 		/* ignore comment or blank lines */
97 		if (is_blank(line))
98 			continue;
99 		/* sanity-check */
100 		if (sscanf(line,
101 		    "%" VAL2STR(FILENAME_MAX) "s %" VAL2STR(FILENAME_MAX) "s",
102 		    drv, entry) != 2) {
103 			status = RET_ERROR;
104 		}
105 		if (atoi(entry) == major)
106 			break;
107 	}
108 
109 	if (status == RET_NOERROR)
110 		(void) strcpy(buf, drv);
111 	(void) fclose(fp);
112 	return (status);
113 }
114