[BZ #3306]
[glibc.git] / nis / nis_getservlist.c
blobe57649d5260a6ea4a0627c50f82e4bb604faa7b4
1 /* Copyright (c) 1997, 1998, 1999, 2000, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <string.h>
21 #include <rpcsvc/nis.h>
23 #include "nis_xdr.h"
24 #include "nis_intern.h"
26 nis_server **
27 nis_getservlist (const_nis_name dir)
29 nis_result *res;
30 nis_server **serv;
32 res = nis_lookup (dir, FOLLOW_LINKS);
34 if (res != NULL && NIS_RES_STATUS (res) == NIS_SUCCESS)
36 unsigned long i;
37 nis_server *server;
39 serv =
40 malloc (sizeof (nis_server *) *
41 (NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len + 1));
42 if (__builtin_expect (serv == NULL, 0))
44 nis_freeresult (res);
45 return NULL;
48 for (i = 0; i < NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len;
49 ++i)
51 server =
52 &NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_val[i];
53 serv[i] = calloc (1, sizeof (nis_server));
54 if (__builtin_expect (serv[i] == NULL, 0))
56 free_all:
57 while (i-- > 0)
59 free (serv[i]->pkey.n_bytes);
60 if (serv[i]->ep.ep_val != NULL)
62 unsigned long int j;
63 for (j = 0; j < serv[i]->ep.ep_len; ++j)
65 free (serv[i]->ep.ep_val[j].proto);
66 free (serv[i]->ep.ep_val[j].family);
67 free (serv[i]->ep.ep_val[j].uaddr);
69 free (serv[i]->ep.ep_val);
71 free (serv[i]->name);
72 free (serv[i]);
75 free (serv);
77 nis_freeresult (res);
79 return NULL;
82 if (server->name != NULL)
84 serv[i]->name = strdup (server->name);
85 if (__builtin_expect (serv[i]->name == NULL, 0))
87 ++i;
88 goto free_all;
92 serv[i]->ep.ep_len = server->ep.ep_len;
93 if (serv[i]->ep.ep_len > 0)
95 unsigned long int j;
97 serv[i]->ep.ep_val =
98 malloc (server->ep.ep_len * sizeof (endpoint));
99 if (__builtin_expect (serv[i]->ep.ep_val == NULL, 0))
101 ++i;
102 goto free_all;
105 for (j = 0; j < serv[i]->ep.ep_len; ++j)
107 if (server->ep.ep_val[j].uaddr)
108 serv[i]->ep.ep_val[j].uaddr =
109 strdup (server->ep.ep_val[j].uaddr);
110 else
111 serv[i]->ep.ep_val[j].uaddr = NULL;
112 if (server->ep.ep_val[j].family)
113 serv[i]->ep.ep_val[j].family =
114 strdup (server->ep.ep_val[j].family);
115 else
116 serv[i]->ep.ep_val[j].family = NULL;
117 if (server->ep.ep_val[j].proto)
118 serv[i]->ep.ep_val[j].proto =
119 strdup (server->ep.ep_val[j].proto);
120 else
121 serv[i]->ep.ep_val[j].proto = NULL;
125 serv[i]->key_type = server->key_type;
126 serv[i]->pkey.n_len = server->pkey.n_len;
127 if (server->pkey.n_len > 0)
129 serv[i]->pkey.n_bytes = malloc (server->pkey.n_len);
130 if (__builtin_expect (serv[i]->pkey.n_bytes == NULL, 0))
132 ++i;
133 goto free_all;
135 memcpy (serv[i]->pkey.n_bytes, server->pkey.n_bytes,
136 server->pkey.n_len);
139 serv[i] = NULL;
141 else
143 serv = malloc (sizeof (nis_server *));
144 if (__builtin_expect (serv != NULL, 0))
145 serv[0] = NULL;
148 nis_freeresult (res);
150 return serv;
153 void
154 nis_freeservlist (nis_server **serv)
156 int i;
158 if (serv == NULL)
159 return;
161 i = 0;
162 while (serv[i] != NULL)
164 xdr_free ((xdrproc_t)_xdr_nis_server, (char *)serv[i]);
165 free (serv[i]);
166 ++i;
168 free (serv);