xref: /titanic_41/usr/src/lib/libadm/common/devattr.c (revision b509e89b2befbaa42939abad9da1d7f5a8c6aaae)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1997, by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2 */
32 
33 /*LINTLIBRARY*/
34 
35 /*
36  *  devattr.c
37  *
38  *  Contents:
39  *	devattr()	Get the value of a attribute for a specific device
40  */
41 
42 /*
43  *  Header files needed
44  *	<sys/types.h>		System Data Types
45  *	<stdio.h>		Standard I/O Definitions
46  *	<errno.h>		Error-value definitions
47  *	<string.h>		String function and constant definitions
48  *	<devmgmt.h>		Device table definitions available to the world
49  *	"devtab.h"		Local device table definitions
50  */
51 
52 #include	<sys/types.h>
53 #include	<stdio.h>
54 #include	<errno.h>
55 #include	<string.h>
56 #include	<stdlib.h>
57 #include	<devmgmt.h>
58 #include	"devtab.h"
59 
60 /*
61  *  Local constant definitions
62  */
63 
64 
65 /*
66  *  Local static data
67  */
68 
69 /*
70  *  char *devattr(device, attr)
71  *
72  *	This function searches the device table, looking for the device
73  *	specified by <device>.  If it finds a record corresponding to that
74  *	device (see below for a definition of that correspondence), it
75  *	extracts the value of the field <attr> from that record, if any.
76  *	It returns a pointer to that value, or (char *) NULL if none.
77  *
78  *  Arguments:
79  *	device		Pointer to the character-string that describes the
80  *			device whose record is to be looked for
81  *	attr		The device's attribute to be looked for
82  *
83  *  Returns:  char *
84  *	A pointer to the character-string containing the value of the
85  *	attribute <attr> for the device <device>, or (char *) NULL if none
86  *	was found.  If the function returns (char *) NULL and the error was
87  *	detected by this function, it sets "errno" to indicate the problem.
88  *
89  *  "errno" Values:
90  *	EPERM		Permissions deny reading access of the device-table
91  *			file
92  *	ENOENT		The specified device-table file could not be found
93  *	ENODEV		Device not found in the device table
94  *	EINVAL		The device does not have that attribute defined
95  *	ENOMEM		No memory available
96  */
97 
98 char *
99 devattr(
100 	char   *device,		/* The device ) we're to look for */
101 	char   *attribute)	/* The attribute to extract */
102 {
103 	/* Automatic data */
104 	struct devtabent	*record;	/* Retrieved record */
105 	struct attrval		*p;		/* attr/val records */
106 	char			*val;		/* Extracted value */
107 	char			*rtnval;	/* Value to return */
108 	int			found;		/* TRUE if attribute found */
109 
110 
111 	/* Get the record for the specified device */
112 	if (!(record = _getdevrec(device))) {
113 		_enddevtab();
114 		return (NULL);
115 	}
116 
117 	/* Search the record for the specified attribute */
118 	found = FALSE;
119 
120 	/* Did they ask for the device alias? */
121 	if (strcmp(attribute, DTAB_ALIAS) == 0) {
122 	    val = (record->alias != NULL) ? record->alias : "";
123 	    found = TRUE;
124 	}
125 
126 	/* Did they ask for the character-special device? */
127 	else if (strcmp(attribute, DTAB_CDEVICE) == 0) {
128 	    val = (record->cdevice != NULL) ? record->cdevice : "";
129 	    found = TRUE;
130 	}
131 
132 	/* Did they ask for the block-special device? */
133 	else if (strcmp(attribute, DTAB_BDEVICE) == 0) {
134 	    val = (record->bdevice != NULL) ? record->bdevice : "";
135 	    found = TRUE;
136 	}
137 
138 	/* Did they ask for the pathname? */
139 	else if (strcmp(attribute, DTAB_PATHNAME) == 0) {
140 	    val = (record->pathname != NULL) ? record->pathname : "";
141 	    found = TRUE;
142 	}
143 
144 	else {
145 
146 	/*
147 	 * Didn't ask for one of the easy ones, search the attr/val
148 	 * structure
149 	 */
150 
151 	    p = record->attrlist;
152 	    while (!found && (p)) {
153 		if (strcmp(p->attr, attribute) == 0) {
154 		    val = p->val;
155 		    found = TRUE;
156 		} else p = p->next;
157 	    }
158 	}
159 
160 	/*
161 	 * If the attribute was found, copy it into malloc()ed space.
162 	 * If not, set errno appropriately; we'll return NULL
163 	 */
164 
165 	if (found) {
166 	    if (rtnval = malloc(strlen(val)+1))
167 		(void) strcpy(rtnval, val);
168 	    else errno = ENOMEM;
169 	} else {
170 	    rtnval = NULL;
171 	    errno = EINVAL;
172 	}
173 
174 	/* Free the space allocated to the struct devtabent structure */
175 	_freedevtabent(record);
176 
177 	_enddevtab();
178 
179 	/* Fini */
180 	return (rtnval);
181 }
182