* elf/rtld.c (_dl_start): Set bootstrap_map.l_relocated even
[glibc.git] / nis / nis_subr.c
blob4c4ef8b5b41ef91d1f0fca8269613aee26a1d0d3
1 /* Copyright (c) 1997, 1999, 2000, 2004 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 <errno.h>
21 #include <string.h>
22 #include <rpcsvc/nis.h>
24 nis_name
25 nis_leaf_of (const_nis_name name)
27 static char result[NIS_MAXNAMELEN + 1];
29 return nis_leaf_of_r (name, result, NIS_MAXNAMELEN);
32 nis_name
33 nis_leaf_of_r (const_nis_name name, char *buffer, size_t buflen)
35 size_t i = 0;
37 buffer[0] = '\0';
39 while (name[i] != '.' && name[i] != '\0')
40 i++;
42 if (i > buflen - 1)
44 __set_errno (ERANGE);
45 return NULL;
48 if (i > 0)
50 if ((size_t)i >= buflen)
52 __set_errno (ERANGE);
53 return NULL;
56 *((char *) __mempcpy (buffer, name, i)) = '\0';
59 return buffer;
61 libnsl_hidden_def (nis_leaf_of_r)
63 nis_name
64 nis_name_of (const_nis_name name)
66 static char result[NIS_MAXNAMELEN + 1];
68 return nis_name_of_r (name, result, NIS_MAXNAMELEN);
71 nis_name
72 nis_name_of_r (const_nis_name name, char *buffer, size_t buflen)
74 char *local_domain;
75 int diff;
77 local_domain = nis_local_directory ();
79 diff = strlen (name) - strlen (local_domain);
80 if (diff <= 0)
81 return NULL;
83 if (strcmp (&name[diff], local_domain) != 0)
84 return NULL;
86 if ((size_t) diff >= buflen)
88 __set_errno (ERANGE);
89 return NULL;
92 *((char *) __mempcpy (buffer, name, diff - 1)) = '\0';
94 if (diff - 1 == 0)
95 return NULL;
97 return buffer;
99 libnsl_hidden_def (nis_name_of_r)
101 static int
102 count_dots (const_nis_name str)
104 int count = 0;
105 size_t l = strlen (str);
107 for (size_t i = 0; i < l; ++i)
108 if (str[i] == '.')
109 ++count;
111 return count;
114 /* If we run out of memory, we don't give already allocated memory
115 free. The overhead for bringing getnames back in a safe state to
116 free it is to big. */
117 nis_name *
118 nis_getnames (const_nis_name name)
120 nis_name *getnames = NULL;
121 char local_domain[NIS_MAXNAMELEN + 1];
122 char *path;
123 char *cp;
124 int count;
125 int pos = 0;
126 int have_point;
127 char *saveptr;
129 strncpy (local_domain, nis_local_directory (), NIS_MAXNAMELEN);
130 local_domain[NIS_MAXNAMELEN] = '\0';
132 count = 1;
133 getnames = malloc ((count + 1) * sizeof (char *));
134 if (__builtin_expect (getnames == NULL, 0))
135 return NULL;
137 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
138 if (name[strlen (name) - 1] == '.')
140 if ((getnames[0] = strdup (name)) == NULL)
142 free_null:
143 while (pos-- > 0)
144 free (getnames[pos]);
145 free (getnames);
146 return NULL;
149 getnames[1] = NULL;
151 return getnames;
154 /* Get the search path, where we have to search "name" */
155 path = getenv ("NIS_PATH");
156 if (path == NULL)
157 path = strdupa ("$");
158 else
159 path = strdupa (path);
161 have_point = (strchr (name, '.') != NULL);
163 cp = __strtok_r (path, ":", &saveptr);
164 while (cp)
166 if (strcmp (cp, "$") == 0)
168 char *cptr = local_domain;
169 char *tmp;
171 while ((have_point && *cptr != '\0') || (count_dots (cptr) >= 2))
173 if (pos >= count)
175 count += 5;
176 nis_name *newp = realloc (getnames,
177 (count + 1) * sizeof (char *));
178 if (__builtin_expect (newp == NULL, 0))
179 goto free_null;
180 getnames = newp;
182 tmp = malloc (strlen (cptr) + strlen (local_domain) +
183 strlen (name) + 2);
184 if (__builtin_expect (tmp == NULL, 0))
185 goto free_null;
187 getnames[pos] = tmp;
188 tmp = stpcpy (tmp, name);
189 *tmp++ = '.';
190 if (cptr[1] != '\0')
191 stpcpy (tmp, cptr);
192 else
193 ++cptr;
195 ++pos;
197 while (*cptr != '.' && *cptr != '\0')
198 ++cptr;
199 if (cptr[0] != '\0' && cptr[1] != '\0')
200 /* If we have only ".", don't remove the "." */
201 ++cptr;
204 else
206 char *tmp;
207 size_t cplen = strlen (cp);
209 if (cp[cplen - 1] == '$')
211 char *p;
213 tmp = malloc (cplen + strlen (local_domain) + strlen (name) + 2);
214 if (__builtin_expect (tmp == NULL, 0))
215 goto free_null;
217 p = __stpcpy (tmp, name);
218 *p++ = '.';
219 p = __mempcpy (p, cp, cplen);
220 --p;
221 if (p[-1] != '.')
222 *p++ = '.';
223 __stpcpy (p, local_domain);
225 else
227 char *p;
229 tmp = malloc (cplen + strlen (name) + 2);
230 if (__builtin_expect (tmp == NULL, 0))
231 goto free_null;
233 p = __stpcpy (tmp, name);
234 *p++ = '.';
235 memcpy (p, cp, cplen + 1);
238 if (pos >= count)
240 count += 5;
241 nis_name *newp = realloc (getnames,
242 (count + 1) * sizeof (char *));
243 if (__builtin_expect (newp == NULL, 0))
244 goto free_null;
245 getnames = newp;
247 getnames[pos] = tmp;
248 ++pos;
250 cp = __strtok_r (NULL, ":", &saveptr);
253 getnames[pos] = NULL;
255 return getnames;
257 libnsl_hidden_def (nis_getnames)
259 void
260 nis_freenames (nis_name *names)
262 int i = 0;
264 while (names[i] != NULL)
266 free (names[i]);
267 ++i;
270 free (names);
272 libnsl_hidden_def (nis_freenames)
274 name_pos
275 nis_dir_cmp (const_nis_name n1, const_nis_name n2)
277 int len1, len2;
279 len1 = strlen (n1);
280 len2 = strlen (n2);
282 if (len1 == len2)
284 if (strcmp (n1, n2) == 0)
285 return SAME_NAME;
286 else
287 return NOT_SEQUENTIAL;
290 if (len1 < len2)
292 if (n2[len2 - len1 - 1] != '.')
293 return NOT_SEQUENTIAL;
294 else if (strcmp (&n2[len2 - len1], n1) == 0)
295 return HIGHER_NAME;
296 else
297 return NOT_SEQUENTIAL;
299 else
301 if (n1[len1 - len2 - 1] != '.')
302 return NOT_SEQUENTIAL;
303 else if (strcmp (&n1[len1 - len2], n2) == 0)
304 return LOWER_NAME;
305 else
306 return NOT_SEQUENTIAL;
310 libnsl_hidden_def (nis_dir_cmp)
312 void
313 nis_destroy_object (nis_object *obj)
315 nis_free_object (obj);
317 libnsl_hidden_def (nis_destroy_object)