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