xref: /illumos-gate/usr/src/lib/libadm/common/listdgrp.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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  *  listdgrp.c
35  *
36  *  Contents:
37  *	listdgrp()	List devices that belong to a device group.
38  */
39 
40 /*
41  * Header files referenced:
42  *	<sys/types.h>	System Data Types
43  *	<errno.h>	UNIX and C error definitions
44  *	<string.h>	String handling definitions
45  *	<devmgmt.h>	Device management definitions
46  *	"devtab.h"	Local device table definitions
47  */
48 
49 #include	<sys/types.h>
50 #include	<errno.h>
51 #include	<string.h>
52 #include	<stdlib.h>
53 #include	<devmgmt.h>
54 #include	"devtab.h"
55 
56 /*
57  * Local definitions
58  */
59 
60 
61 /*
62  *  Structure definitions:
63  */
64 
65 /*
66  * Local functions referenced
67  */
68 
69 /*
70  * Global Data
71  */
72 
73 /*
74  * Static Data
75  */
76 
77 /*
78  * char **listdgrp(dgroup)
79  *	char   *dgroup
80  *
81  *	List the members of a device group.
82  *
83  *  Arguments:
84  *	char *dgroup	The device group needed
85  *
86  *  Returns:  char **
87  *	A pointer to a list of pointers to char-strings containing
88  *	the members of the device group.
89  *
90  *  Notes:
91  *    -	malloc()ed space containing addresses
92  */
93 
94 char  **
listdgrp(char * dgroup)95 listdgrp(char *dgroup)	/* The device group to list */
96 {
97 	/* Automatic data */
98 	struct dgrptabent	*dgrpent;	/* Device group description */
99 	struct member		*member;	/* Device group member */
100 	char			**listbuf;	/* Buffer allocated for addrs */
101 	char			**rtnval;	/* Value to return */
102 	char			**pp;		/* Running ptr through addrs */
103 	int			noerror;	/* Flag, TRUE if all's well */
104 	int			n;		/* Counter */
105 
106 
107 	/*
108 	 *  Initializations
109 	 */
110 
111 	/*
112 	 *  Get the record for this device group
113 	 */
114 
115 	if (dgrpent = _getdgrprec(dgroup)) {
116 
117 	    /*  Count the number of members in the device group  */
118 	    n = 1;
119 	    for (member = dgrpent->membership; member; member = member->next)
120 		n++;
121 
122 	    /*  Get space for the list to return  */
123 	    if (listbuf = malloc(n*sizeof (char **))) {
124 
125 		/*
126 		 *  For each member in the device group, add that device
127 		 *  name to the list of devices we're building
128 		 */
129 
130 		pp = listbuf;
131 		noerror = TRUE;
132 		for (member = dgrpent->membership; noerror && member;
133 		    member = member->next) {
134 
135 		    if (*pp = malloc(strlen(member->name)+1))
136 
137 			(void) strcpy(*pp++, member->name);
138 		    else noerror = FALSE;
139 		}
140 
141 
142 		/*
143 		 *  If there's no error, terminate the list we've built.
144 		 *  Otherwise, free the space allocated to the stuff we've built
145 		 */
146 
147 		if (noerror) {
148 		    *pp = NULL;
149 		    rtnval = listbuf;
150 		} else {
151 		    /*  Some error occurred.  Clean up allocations  */
152 		    for (pp = listbuf; *pp; pp++) free(*pp);
153 		    free(listbuf);
154 		    rtnval = NULL;
155 		}
156 
157 	    }  /* if (malloc()) */
158 
159 	    /*  Free space alloced to the device group entry  */
160 	    _freedgrptabent(dgrpent);
161 
162 	}  /* if (_getdgrprec()) */
163 	else rtnval = NULL;
164 
165 
166 	/*  Finished -- wasn't that simple?  */
167 	return (rtnval);
168 }
169