xref: /titanic_52/usr/src/lib/libadm/common/listdev.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2 */
32*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * listdev.c
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  *  Contains:
38*7c478bd9Sstevel@tonic-gate  *	listdev()	List attributes defined for a device
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate /*
42*7c478bd9Sstevel@tonic-gate  *  Header files needed:
43*7c478bd9Sstevel@tonic-gate  *	<sys/types.h>	System Data Types
44*7c478bd9Sstevel@tonic-gate  *	<string.h>	Standard string definitions
45*7c478bd9Sstevel@tonic-gate  *	<devmgmt.h>	Device management definitions
46*7c478bd9Sstevel@tonic-gate  *	"devtab.h"	Local device table definitions
47*7c478bd9Sstevel@tonic-gate  */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate #include	<sys/types.h>
50*7c478bd9Sstevel@tonic-gate #include	<string.h>
51*7c478bd9Sstevel@tonic-gate #include	<devmgmt.h>
52*7c478bd9Sstevel@tonic-gate #include	"devtab.h"
53*7c478bd9Sstevel@tonic-gate #include	<stdlib.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate  *  Local Definitions:
57*7c478bd9Sstevel@tonic-gate  */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate /*
61*7c478bd9Sstevel@tonic-gate  *  Local, Static data:
62*7c478bd9Sstevel@tonic-gate  */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * void sortlist(list)
66*7c478bd9Sstevel@tonic-gate  *	char  **list
67*7c478bd9Sstevel@tonic-gate  *
68*7c478bd9Sstevel@tonic-gate  *	This function sorts a list of character strings
69*7c478bd9Sstevel@tonic-gate  *	so that the list is ordered alphabetically.
70*7c478bd9Sstevel@tonic-gate  *
71*7c478bd9Sstevel@tonic-gate  *  Arguments:
72*7c478bd9Sstevel@tonic-gate  *	list	The list to be sorted
73*7c478bd9Sstevel@tonic-gate  *
74*7c478bd9Sstevel@tonic-gate  *  Returns:  void
75*7c478bd9Sstevel@tonic-gate  */
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate static	void
78*7c478bd9Sstevel@tonic-gate sortlist(char **list)		/* List to be sorted */
79*7c478bd9Sstevel@tonic-gate {
80*7c478bd9Sstevel@tonic-gate 	char  **pp;		/* Pointer to item being sorted */
81*7c478bd9Sstevel@tonic-gate 	char  **qq;
82*7c478bd9Sstevel@tonic-gate 	char  **rr;
83*7c478bd9Sstevel@tonic-gate 	char   *t;		/* Temp for swapping pointers */
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	/* If the list isn't empty ... */
86*7c478bd9Sstevel@tonic-gate 	if (*list) {
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	    /* Find the last item in the list */
89*7c478bd9Sstevel@tonic-gate 	    for (pp = list; *pp; pp++)
90*7c478bd9Sstevel@tonic-gate 		;
91*7c478bd9Sstevel@tonic-gate 	    --pp;
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate 	/*
94*7c478bd9Sstevel@tonic-gate 	 * Sort 'em by sorting larger and larger portions
95*7c478bd9Sstevel@tonic-gate 	 * of the list (my CSC101 fails me, I forget what
96*7c478bd9Sstevel@tonic-gate 	 * this sort is called!) [Where I come from, CS
97*7c478bd9Sstevel@tonic-gate 	 * is Crop Science...]
98*7c478bd9Sstevel@tonic-gate 	 */
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	    while (pp != list) {
101*7c478bd9Sstevel@tonic-gate 		qq = pp;
102*7c478bd9Sstevel@tonic-gate 		rr = --pp;
103*7c478bd9Sstevel@tonic-gate 		while (*qq && (strcmp(*rr, *qq) > 0)) {
104*7c478bd9Sstevel@tonic-gate 		    t = *rr;
105*7c478bd9Sstevel@tonic-gate 		    *rr++ = *qq;
106*7c478bd9Sstevel@tonic-gate 		    *qq++ = t;
107*7c478bd9Sstevel@tonic-gate 		}
108*7c478bd9Sstevel@tonic-gate 	    }
109*7c478bd9Sstevel@tonic-gate 	}
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate /*
113*7c478bd9Sstevel@tonic-gate  * char **listdev(device)
114*7c478bd9Sstevel@tonic-gate  *	char   *device;
115*7c478bd9Sstevel@tonic-gate  *
116*7c478bd9Sstevel@tonic-gate  *	Generate an alphabetized list of attribute names of the
117*7c478bd9Sstevel@tonic-gate  *	attributes defined for the device <device>.
118*7c478bd9Sstevel@tonic-gate  *
119*7c478bd9Sstevel@tonic-gate  *  Arguments:
120*7c478bd9Sstevel@tonic-gate  *	device		Device who's attributes are to be listed
121*7c478bd9Sstevel@tonic-gate  *
122*7c478bd9Sstevel@tonic-gate  *  Returns: char **
123*7c478bd9Sstevel@tonic-gate  *	List of attribute names of the attributes defined for this
124*7c478bd9Sstevel@tonic-gate  *	device.  (Never empty since all devices have the "alias"
125*7c478bd9Sstevel@tonic-gate  *	attribute defined.)
126*7c478bd9Sstevel@tonic-gate  */
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate char **
129*7c478bd9Sstevel@tonic-gate listdev(char *device)		/* Device to describe */
130*7c478bd9Sstevel@tonic-gate {
131*7c478bd9Sstevel@tonic-gate 	/*  Automatic data  */
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	struct devtabent	*devtabent;	/* Ptr to devtab entry */
134*7c478bd9Sstevel@tonic-gate 	struct attrval		*attrval;	/* Ptr to attr val pair */
135*7c478bd9Sstevel@tonic-gate 	char			**list;		/* Ptr to alloc'd list */
136*7c478bd9Sstevel@tonic-gate 	char			**rtnval;	/* Value to return */
137*7c478bd9Sstevel@tonic-gate 	char			**pp;		/* Ptr to current val in list */
138*7c478bd9Sstevel@tonic-gate 	int			noerror;	/* FLAG, TRUE if :-) */
139*7c478bd9Sstevel@tonic-gate 	int			n;		/* Temp counter */
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 
142*7c478bd9Sstevel@tonic-gate 	/*  If the device <device> is defined ...  */
143*7c478bd9Sstevel@tonic-gate 	if (devtabent = _getdevrec(device)) {
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	/*
146*7c478bd9Sstevel@tonic-gate 	 *  Count the number of attributes defined for the device
147*7c478bd9Sstevel@tonic-gate 	 *  being sure to count the (char *) NULL that terminates
148*7c478bd9Sstevel@tonic-gate 	 *  the list
149*7c478bd9Sstevel@tonic-gate 	 */
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	    n = 1;
152*7c478bd9Sstevel@tonic-gate 	    if (devtabent->alias) n++;		/*  Alias, if defined  */
153*7c478bd9Sstevel@tonic-gate 	    if (devtabent->cdevice) n++;	/*  Char spcl, if defined  */
154*7c478bd9Sstevel@tonic-gate 	    if (devtabent->bdevice) n++;	/*  Blk spcl, if defined  */
155*7c478bd9Sstevel@tonic-gate 	    if (devtabent->pathname) n++;	/*  Pathname, if defined  */
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	    /*  Other attributes, if any  */
158*7c478bd9Sstevel@tonic-gate 	    if ((attrval = devtabent->attrlist) != NULL) {
159*7c478bd9Sstevel@tonic-gate 		do
160*7c478bd9Sstevel@tonic-gate 		    n++;
161*7c478bd9Sstevel@tonic-gate 		while ((attrval = attrval->next) != NULL);
162*7c478bd9Sstevel@tonic-gate 	    }
163*7c478bd9Sstevel@tonic-gate 	    noerror = TRUE;
164*7c478bd9Sstevel@tonic-gate 	    if (list = malloc(n*sizeof (char *))) {
165*7c478bd9Sstevel@tonic-gate 		pp = list;
166*7c478bd9Sstevel@tonic-gate 		if (devtabent->alias) {
167*7c478bd9Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_ALIAS)+1))
168*7c478bd9Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_ALIAS);
169*7c478bd9Sstevel@tonic-gate 		    else noerror = FALSE;
170*7c478bd9Sstevel@tonic-gate 		}
171*7c478bd9Sstevel@tonic-gate 		if (noerror && devtabent->bdevice) {
172*7c478bd9Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_BDEVICE)+1))
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_BDEVICE);
175*7c478bd9Sstevel@tonic-gate 		    else noerror = FALSE;
176*7c478bd9Sstevel@tonic-gate 		}
177*7c478bd9Sstevel@tonic-gate 		if (noerror && devtabent->cdevice) {
178*7c478bd9Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_CDEVICE)+1))
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_CDEVICE);
181*7c478bd9Sstevel@tonic-gate 		    else noerror = FALSE;
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 		if (noerror && devtabent->pathname) {
184*7c478bd9Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_PATHNAME)+1))
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_PATHNAME);
187*7c478bd9Sstevel@tonic-gate 		    else noerror = FALSE;
188*7c478bd9Sstevel@tonic-gate 		}
189*7c478bd9Sstevel@tonic-gate 		if (noerror && (attrval = devtabent->attrlist)) {
190*7c478bd9Sstevel@tonic-gate 		    do {
191*7c478bd9Sstevel@tonic-gate 			if (*pp = malloc(strlen(attrval->attr)+1))
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 			    (void) strcpy(*pp++, attrval->attr);
194*7c478bd9Sstevel@tonic-gate 			else noerror = FALSE;
195*7c478bd9Sstevel@tonic-gate 		    } while (noerror && (attrval = attrval->next));
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 		if (noerror) {
198*7c478bd9Sstevel@tonic-gate 		    *pp = NULL;
199*7c478bd9Sstevel@tonic-gate 		    sortlist(list);
200*7c478bd9Sstevel@tonic-gate 		    rtnval = list;
201*7c478bd9Sstevel@tonic-gate 		} else {
202*7c478bd9Sstevel@tonic-gate 		    for (pp = list; *pp; pp++) free(*pp);
203*7c478bd9Sstevel@tonic-gate 		    free(list);
204*7c478bd9Sstevel@tonic-gate 		    rtnval = NULL;
205*7c478bd9Sstevel@tonic-gate 		}
206*7c478bd9Sstevel@tonic-gate 	    } else rtnval = NULL;
207*7c478bd9Sstevel@tonic-gate 	} else rtnval = NULL;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	_enddevtab();
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	/* Fini */
212*7c478bd9Sstevel@tonic-gate 	return (rtnval);
213*7c478bd9Sstevel@tonic-gate }
214