Address post-commit patch comments.
[glibc.git] / nis / nis_getservlist.c
blobf180bad0dd1d0662d6148e4e5ed055757cc78d33
1 /* Copyright (c) 1997-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <string.h>
20 #include <rpcsvc/nis.h>
22 #include "nis_xdr.h"
23 #include "nis_intern.h"
25 nis_server **
26 nis_getservlist (const_nis_name dir)
28 nis_result *res;
29 nis_server **serv;
31 res = nis_lookup (dir, FOLLOW_LINKS);
33 if (res != NULL && NIS_RES_STATUS (res) == NIS_SUCCESS)
35 unsigned long i;
36 nis_server *server;
38 serv =
39 malloc (sizeof (nis_server *) *
40 (NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len + 1));
41 if (__glibc_unlikely (serv == NULL))
43 nis_freeresult (res);
44 return NULL;
47 for (i = 0; i < NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len;
48 ++i)
50 server =
51 &NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_val[i];
52 serv[i] = calloc (1, sizeof (nis_server));
53 if (__glibc_unlikely (serv[i] == NULL))
55 free_all:
56 while (i-- > 0)
58 free (serv[i]->pkey.n_bytes);
59 if (serv[i]->ep.ep_val != NULL)
61 unsigned long int j;
62 for (j = 0; j < serv[i]->ep.ep_len; ++j)
64 free (serv[i]->ep.ep_val[j].proto);
65 free (serv[i]->ep.ep_val[j].family);
66 free (serv[i]->ep.ep_val[j].uaddr);
68 free (serv[i]->ep.ep_val);
70 free (serv[i]->name);
71 free (serv[i]);
74 free (serv);
76 nis_freeresult (res);
78 return NULL;
81 if (server->name != NULL)
83 serv[i]->name = strdup (server->name);
84 if (__glibc_unlikely (serv[i]->name == NULL))
86 ++i;
87 goto free_all;
91 serv[i]->ep.ep_len = server->ep.ep_len;
92 if (serv[i]->ep.ep_len > 0)
94 unsigned long int j;
96 serv[i]->ep.ep_val =
97 malloc (server->ep.ep_len * sizeof (endpoint));
98 if (__glibc_unlikely (serv[i]->ep.ep_val == NULL))
100 ++i;
101 goto free_all;
104 for (j = 0; j < serv[i]->ep.ep_len; ++j)
106 if (server->ep.ep_val[j].uaddr)
107 serv[i]->ep.ep_val[j].uaddr =
108 strdup (server->ep.ep_val[j].uaddr);
109 else
110 serv[i]->ep.ep_val[j].uaddr = NULL;
111 if (server->ep.ep_val[j].family)
112 serv[i]->ep.ep_val[j].family =
113 strdup (server->ep.ep_val[j].family);
114 else
115 serv[i]->ep.ep_val[j].family = NULL;
116 if (server->ep.ep_val[j].proto)
117 serv[i]->ep.ep_val[j].proto =
118 strdup (server->ep.ep_val[j].proto);
119 else
120 serv[i]->ep.ep_val[j].proto = NULL;
124 serv[i]->key_type = server->key_type;
125 serv[i]->pkey.n_len = server->pkey.n_len;
126 if (server->pkey.n_len > 0)
128 serv[i]->pkey.n_bytes = malloc (server->pkey.n_len);
129 if (__glibc_unlikely (serv[i]->pkey.n_bytes == NULL))
131 ++i;
132 goto free_all;
134 memcpy (serv[i]->pkey.n_bytes, server->pkey.n_bytes,
135 server->pkey.n_len);
138 serv[i] = NULL;
140 else
142 serv = malloc (sizeof (nis_server *));
143 if (__glibc_unlikely (serv != NULL))
144 serv[0] = NULL;
147 nis_freeresult (res);
149 return serv;
152 void
153 nis_freeservlist (nis_server **serv)
155 int i;
157 if (serv == NULL)
158 return;
160 i = 0;
161 while (serv[i] != NULL)
163 xdr_free ((xdrproc_t)_xdr_nis_server, (char *)serv[i]);
164 free (serv[i]);
165 ++i;
167 free (serv);