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