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