xref: /titanic_51/usr/src/lib/libfsmgt/common/nfs_netcfg.c (revision bbaa8b60dd95d714741fc474adad3cf710ef4efd)
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 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <errno.h>
30 #include <netconfig.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "libfsmgt.h"
34 
35 /*
36  * Public methods
37  */
38 
39 void
40 netcfg_free_networkid_list(char **netlist, int num_elements)
41 {
42 	fileutil_free_string_array(netlist, num_elements);
43 } /* netcfg_free_networkid_list */
44 
45 char **
46 netcfg_get_networkid_list(int *num_elements, int *errp)
47 {
48 	struct netconfig	*nconf;
49 	NCONF_HANDLE		*nc;
50 	char			**return_list = NULL;
51 
52 	/*
53 	 * setnetconfig must be called before getnetconfig in order to get the
54 	 * netconfig handle.
55 	 */
56 	if ((nc = setnetconfig()) == (NCONF_HANDLE *)NULL) {
57 		*errp = errno;
58 		return (NULL);
59 	}
60 
61 	*num_elements = 0;
62 	while (nconf = getnetconfig(nc)) {
63 		char	**tmp_list;
64 
65 		/*
66 		 * Put the elements in the array.
67 		 */
68 		tmp_list = realloc(return_list,
69 			(size_t)(((*num_elements)+1) * sizeof (char *)));
70 		if (tmp_list == NULL) {
71 			*errp = errno;
72 			netcfg_free_networkid_list(return_list, *num_elements);
73 			*num_elements = 0;
74 			(void) endnetconfig(nc);
75 			return (NULL);
76 		}
77 
78 		return_list = tmp_list;
79 
80 		return_list[(*num_elements)] = strdup(nconf->nc_netid);
81 		if (return_list[(*num_elements)] == NULL) {
82 			*errp = ENOMEM;
83 			netcfg_free_networkid_list(return_list, *num_elements);
84 			*num_elements = 0;
85 			(void) endnetconfig(nc);
86 			return (NULL);
87 		}
88 
89 		*num_elements = *num_elements + 1;
90 	}
91 
92 	(void) endnetconfig(nc);
93 	/*
94 	 * Caller must free the space allocated to return_list by calling
95 	 * netcfg_free_networkid_list.
96 	 */
97 	return (return_list);
98 } /* netcfg_get_networkid_list */
99