(spec-nvr): only get the first one
[glibc.git] / nis / nis_getservlist.c
blob315960c8e8b1b71b4885a8f284f0f5d3d48e1c1a
1 /* Copyright (c) 1997, 1998, 1999, 2000 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))
43 return NULL;
45 for (i = 0; i < NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_len;
46 ++i)
48 server =
49 &NIS_RES_OBJECT (res)->DI_data.do_servers.do_servers_val[i];
50 serv[i] = calloc (1, sizeof (nis_server));
51 if (__builtin_expect (serv[i] == NULL, 0))
52 return NULL;
54 if (server->name != NULL)
56 serv[i]->name = strdup (server->name);
57 if (__builtin_expect (serv[i]->name == NULL, 0))
58 return NULL;
61 serv[i]->ep.ep_len = server->ep.ep_len;
62 if (serv[i]->ep.ep_len > 0)
64 unsigned long int j;
66 serv[i]->ep.ep_val =
67 malloc (server->ep.ep_len * sizeof (endpoint));
68 if (__builtin_expect (serv[i]->ep.ep_val == NULL, 0))
69 return NULL;
71 for (j = 0; j < serv[i]->ep.ep_len; ++j)
73 if (server->ep.ep_val[j].uaddr)
74 serv[i]->ep.ep_val[j].uaddr =
75 strdup (server->ep.ep_val[j].uaddr);
76 else
77 serv[i]->ep.ep_val[j].uaddr = NULL;
78 if (server->ep.ep_val[j].family)
79 serv[i]->ep.ep_val[j].family =
80 strdup (server->ep.ep_val[j].family);
81 else
82 serv[i]->ep.ep_val[j].family = NULL;
83 if (server->ep.ep_val[j].proto)
84 serv[i]->ep.ep_val[j].proto =
85 strdup (server->ep.ep_val[j].proto);
86 else
87 serv[i]->ep.ep_val[j].proto = NULL;
90 else
91 serv[i]->ep.ep_val = NULL;
92 serv[i]->key_type = server->key_type;
93 serv[i]->pkey.n_len = server->pkey.n_len;
94 if (server->pkey.n_len > 0)
96 serv[i]->pkey.n_bytes = malloc (server->pkey.n_len);
97 if (__builtin_expect (serv[i]->pkey.n_bytes == NULL, 0))
98 return NULL;
99 memcpy (serv[i]->pkey.n_bytes, server->pkey.n_bytes,
100 server->pkey.n_len);
102 else
103 serv[i]->pkey.n_bytes = NULL;
105 serv[i] = NULL;
107 else
109 serv = malloc (sizeof (nis_server *));
110 if (__builtin_expect (serv != NULL, 0))
111 serv[0] = NULL;
114 if (res != NULL)
115 nis_freeresult (res);
117 return serv;
120 void
121 nis_freeservlist (nis_server **serv)
123 int i;
125 if (serv == NULL)
126 return;
128 i = 0;
129 while (serv[i] != NULL)
131 xdr_free ((xdrproc_t)_xdr_nis_server, (char *)serv[i]);
132 free (serv[i]);
133 ++i;
135 free (serv);