Speedup first memmem match
[glibc.git] / nis / nis_getservlist.c
blob4fbb5da43ed5e153e2d7946955f39cfba2a4119c
1 /* Copyright (c) 1997-2018 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>
21 #include <shlib-compat.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 (__glibc_unlikely (serv == NULL))
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 (__glibc_unlikely (serv[i] == NULL))
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 (__glibc_unlikely (serv[i]->name == NULL))
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 (__glibc_unlikely (serv[i]->ep.ep_val == NULL))
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 (__glibc_unlikely (serv[i]->pkey.n_bytes == NULL))
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 (__glibc_unlikely (serv != NULL))
145 serv[0] = NULL;
148 nis_freeresult (res);
150 return serv;
152 libnsl_hidden_nolink_def (nis_getservlist, GLIBC_2_1)
154 void
155 nis_freeservlist (nis_server **serv)
157 int i;
159 if (serv == NULL)
160 return;
162 i = 0;
163 while (serv[i] != NULL)
165 xdr_free ((xdrproc_t)_xdr_nis_server, (char *)serv[i]);
166 free (serv[i]);
167 ++i;
169 free (serv);
171 libnsl_hidden_nolink_def (nis_freeservlist, GLIBC_2_1)