xref: /titanic_41/usr/src/lib/lvm/libsvm/common/getdrvname.c (revision fa9e4066f08beec538e775443c5be79dd423fcab)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.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 /*
46  * FUNCTION:
47  *	Return the driver name for a major number
48  *
49  * INPUT: major number, mount point for name_to_major file, pointer
50  * to a valid buffer.
51  *
52  * RETURN VALUES:
53  *	0 - SUCCESS - buf contain the driver name.
54  *	-1 - FAIL
55  *
56  */
57 
58 int
59 get_drv_name(major_t major, char *mnt, char *buf)
60 {
61 	FILE *fp;
62 	char drv[FILENAME_MAX + 1];
63 	char entry[FILENAME_MAX + 1];
64 	char line[MAX_N2M_ALIAS_LINE];
65 	char fname[PATH_MAX];
66 
67 	int status = RET_NOERROR;
68 	(void) snprintf(fname, sizeof (fname), "%s%s", mnt, NAME_TO_MAJOR);
69 
70 	if ((fp = fopen(fname, "r")) == NULL) {
71 		return (RET_ERROR);
72 	}
73 
74 	while ((fgets(line, sizeof (line), fp) != NULL) &&
75 						status == RET_NOERROR) {
76 		if (sscanf(line,
77 		    "%" VAL2STR(FILENAME_MAX) "s %" VAL2STR(FILENAME_MAX) "s",
78 		    drv, entry) != 2) {
79 			status = RET_ERROR;
80 		}
81 		if (atoi(entry) == major)
82 			break;
83 
84 	}
85 
86 	if (status == RET_NOERROR)
87 		(void) strcpy(buf, drv);
88 	(void) fclose(fp);
89 	return (status);
90 }
91