xref: /onnv-gate/usr/src/lib/libadm/common/listdev.c (revision 0:68f95e015346)
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 /*LINTLIBRARY*/
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate  * listdev.c
36*0Sstevel@tonic-gate  *
37*0Sstevel@tonic-gate  *  Contains:
38*0Sstevel@tonic-gate  *	listdev()	List attributes defined for a device
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate /*
42*0Sstevel@tonic-gate  *  Header files needed:
43*0Sstevel@tonic-gate  *	<sys/types.h>	System Data Types
44*0Sstevel@tonic-gate  *	<string.h>	Standard string definitions
45*0Sstevel@tonic-gate  *	<devmgmt.h>	Device management definitions
46*0Sstevel@tonic-gate  *	"devtab.h"	Local device table definitions
47*0Sstevel@tonic-gate  */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate #include	<sys/types.h>
50*0Sstevel@tonic-gate #include	<string.h>
51*0Sstevel@tonic-gate #include	<devmgmt.h>
52*0Sstevel@tonic-gate #include	"devtab.h"
53*0Sstevel@tonic-gate #include	<stdlib.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate /*
56*0Sstevel@tonic-gate  *  Local Definitions:
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  *  Local, Static data:
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate /*
65*0Sstevel@tonic-gate  * void sortlist(list)
66*0Sstevel@tonic-gate  *	char  **list
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  *	This function sorts a list of character strings
69*0Sstevel@tonic-gate  *	so that the list is ordered alphabetically.
70*0Sstevel@tonic-gate  *
71*0Sstevel@tonic-gate  *  Arguments:
72*0Sstevel@tonic-gate  *	list	The list to be sorted
73*0Sstevel@tonic-gate  *
74*0Sstevel@tonic-gate  *  Returns:  void
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate static	void
sortlist(char ** list)78*0Sstevel@tonic-gate sortlist(char **list)		/* List to be sorted */
79*0Sstevel@tonic-gate {
80*0Sstevel@tonic-gate 	char  **pp;		/* Pointer to item being sorted */
81*0Sstevel@tonic-gate 	char  **qq;
82*0Sstevel@tonic-gate 	char  **rr;
83*0Sstevel@tonic-gate 	char   *t;		/* Temp for swapping pointers */
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	/* If the list isn't empty ... */
86*0Sstevel@tonic-gate 	if (*list) {
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 	    /* Find the last item in the list */
89*0Sstevel@tonic-gate 	    for (pp = list; *pp; pp++)
90*0Sstevel@tonic-gate 		;
91*0Sstevel@tonic-gate 	    --pp;
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/*
94*0Sstevel@tonic-gate 	 * Sort 'em by sorting larger and larger portions
95*0Sstevel@tonic-gate 	 * of the list (my CSC101 fails me, I forget what
96*0Sstevel@tonic-gate 	 * this sort is called!) [Where I come from, CS
97*0Sstevel@tonic-gate 	 * is Crop Science...]
98*0Sstevel@tonic-gate 	 */
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	    while (pp != list) {
101*0Sstevel@tonic-gate 		qq = pp;
102*0Sstevel@tonic-gate 		rr = --pp;
103*0Sstevel@tonic-gate 		while (*qq && (strcmp(*rr, *qq) > 0)) {
104*0Sstevel@tonic-gate 		    t = *rr;
105*0Sstevel@tonic-gate 		    *rr++ = *qq;
106*0Sstevel@tonic-gate 		    *qq++ = t;
107*0Sstevel@tonic-gate 		}
108*0Sstevel@tonic-gate 	    }
109*0Sstevel@tonic-gate 	}
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate  * char **listdev(device)
114*0Sstevel@tonic-gate  *	char   *device;
115*0Sstevel@tonic-gate  *
116*0Sstevel@tonic-gate  *	Generate an alphabetized list of attribute names of the
117*0Sstevel@tonic-gate  *	attributes defined for the device <device>.
118*0Sstevel@tonic-gate  *
119*0Sstevel@tonic-gate  *  Arguments:
120*0Sstevel@tonic-gate  *	device		Device who's attributes are to be listed
121*0Sstevel@tonic-gate  *
122*0Sstevel@tonic-gate  *  Returns: char **
123*0Sstevel@tonic-gate  *	List of attribute names of the attributes defined for this
124*0Sstevel@tonic-gate  *	device.  (Never empty since all devices have the "alias"
125*0Sstevel@tonic-gate  *	attribute defined.)
126*0Sstevel@tonic-gate  */
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate char **
listdev(char * device)129*0Sstevel@tonic-gate listdev(char *device)		/* Device to describe */
130*0Sstevel@tonic-gate {
131*0Sstevel@tonic-gate 	/*  Automatic data  */
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate 	struct devtabent	*devtabent;	/* Ptr to devtab entry */
134*0Sstevel@tonic-gate 	struct attrval		*attrval;	/* Ptr to attr val pair */
135*0Sstevel@tonic-gate 	char			**list;		/* Ptr to alloc'd list */
136*0Sstevel@tonic-gate 	char			**rtnval;	/* Value to return */
137*0Sstevel@tonic-gate 	char			**pp;		/* Ptr to current val in list */
138*0Sstevel@tonic-gate 	int			noerror;	/* FLAG, TRUE if :-) */
139*0Sstevel@tonic-gate 	int			n;		/* Temp counter */
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate 	/*  If the device <device> is defined ...  */
143*0Sstevel@tonic-gate 	if (devtabent = _getdevrec(device)) {
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/*
146*0Sstevel@tonic-gate 	 *  Count the number of attributes defined for the device
147*0Sstevel@tonic-gate 	 *  being sure to count the (char *) NULL that terminates
148*0Sstevel@tonic-gate 	 *  the list
149*0Sstevel@tonic-gate 	 */
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	    n = 1;
152*0Sstevel@tonic-gate 	    if (devtabent->alias) n++;		/*  Alias, if defined  */
153*0Sstevel@tonic-gate 	    if (devtabent->cdevice) n++;	/*  Char spcl, if defined  */
154*0Sstevel@tonic-gate 	    if (devtabent->bdevice) n++;	/*  Blk spcl, if defined  */
155*0Sstevel@tonic-gate 	    if (devtabent->pathname) n++;	/*  Pathname, if defined  */
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 	    /*  Other attributes, if any  */
158*0Sstevel@tonic-gate 	    if ((attrval = devtabent->attrlist) != NULL) {
159*0Sstevel@tonic-gate 		do
160*0Sstevel@tonic-gate 		    n++;
161*0Sstevel@tonic-gate 		while ((attrval = attrval->next) != NULL);
162*0Sstevel@tonic-gate 	    }
163*0Sstevel@tonic-gate 	    noerror = TRUE;
164*0Sstevel@tonic-gate 	    if (list = malloc(n*sizeof (char *))) {
165*0Sstevel@tonic-gate 		pp = list;
166*0Sstevel@tonic-gate 		if (devtabent->alias) {
167*0Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_ALIAS)+1))
168*0Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_ALIAS);
169*0Sstevel@tonic-gate 		    else noerror = FALSE;
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate 		if (noerror && devtabent->bdevice) {
172*0Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_BDEVICE)+1))
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_BDEVICE);
175*0Sstevel@tonic-gate 		    else noerror = FALSE;
176*0Sstevel@tonic-gate 		}
177*0Sstevel@tonic-gate 		if (noerror && devtabent->cdevice) {
178*0Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_CDEVICE)+1))
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_CDEVICE);
181*0Sstevel@tonic-gate 		    else noerror = FALSE;
182*0Sstevel@tonic-gate 		}
183*0Sstevel@tonic-gate 		if (noerror && devtabent->pathname) {
184*0Sstevel@tonic-gate 		    if (*pp = malloc(strlen(DTAB_PATHNAME)+1))
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 			(void) strcpy(*pp++, DTAB_PATHNAME);
187*0Sstevel@tonic-gate 		    else noerror = FALSE;
188*0Sstevel@tonic-gate 		}
189*0Sstevel@tonic-gate 		if (noerror && (attrval = devtabent->attrlist)) {
190*0Sstevel@tonic-gate 		    do {
191*0Sstevel@tonic-gate 			if (*pp = malloc(strlen(attrval->attr)+1))
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 			    (void) strcpy(*pp++, attrval->attr);
194*0Sstevel@tonic-gate 			else noerror = FALSE;
195*0Sstevel@tonic-gate 		    } while (noerror && (attrval = attrval->next));
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 		if (noerror) {
198*0Sstevel@tonic-gate 		    *pp = NULL;
199*0Sstevel@tonic-gate 		    sortlist(list);
200*0Sstevel@tonic-gate 		    rtnval = list;
201*0Sstevel@tonic-gate 		} else {
202*0Sstevel@tonic-gate 		    for (pp = list; *pp; pp++) free(*pp);
203*0Sstevel@tonic-gate 		    free(list);
204*0Sstevel@tonic-gate 		    rtnval = NULL;
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 	    } else rtnval = NULL;
207*0Sstevel@tonic-gate 	} else rtnval = NULL;
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate 	_enddevtab();
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* Fini */
212*0Sstevel@tonic-gate 	return (rtnval);
213*0Sstevel@tonic-gate }
214