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