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