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