_DL_HWCAP_FIRST): Decrease.
[glibc/history.git] / nis / nis_subr.c
blob40c92705012fb226b76e81a65b9e4bad5b783a3d
1 /* Copyright (c) 1997,1999,2000,2004,2005,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 <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 (__builtin_expect (i >= buflen, 0))
44 __set_errno (ERANGE);
45 return NULL;
48 *((char *) __mempcpy (buffer, name, i)) = '\0';
50 return buffer;
52 libnsl_hidden_def (nis_leaf_of_r)
54 nis_name
55 nis_name_of (const_nis_name name)
57 static char result[NIS_MAXNAMELEN + 1];
59 return nis_name_of_r (name, result, NIS_MAXNAMELEN);
62 nis_name
63 nis_name_of_r (const_nis_name name, char *buffer, size_t buflen)
65 char *local_domain;
66 int diff;
68 local_domain = nis_local_directory ();
70 diff = strlen (name) - strlen (local_domain);
71 if (diff <= 0)
72 return NULL;
74 if (strcmp (&name[diff], local_domain) != 0)
75 return NULL;
77 if ((size_t) diff >= buflen)
79 __set_errno (ERANGE);
80 return NULL;
83 *((char *) __mempcpy (buffer, name, diff - 1)) = '\0';
85 if (diff - 1 == 0)
86 return NULL;
88 return buffer;
90 libnsl_hidden_def (nis_name_of_r)
92 static int __always_inline
93 count_dots (const_nis_name str)
95 int count = 0;
97 for (size_t i = 0; str[i] != '\0'; ++i)
98 if (str[i] == '.')
99 ++count;
101 return count;
104 /* If we run out of memory, we don't give already allocated memory
105 free. The overhead for bringing getnames back in a safe state to
106 free it is to big. */
107 nis_name *
108 nis_getnames (const_nis_name name)
110 const char *local_domain = nis_local_directory ();
111 size_t local_domain_len = strlen (local_domain);
112 size_t name_len = strlen (name);
113 char *path;
114 int pos = 0;
115 char *saveptr;
116 int have_point;
117 const char *cp;
118 const char *cp2;
120 int count = 2;
121 nis_name *getnames = malloc ((count + 1) * sizeof (char *));
122 if (__builtin_expect (getnames == NULL, 0))
123 return NULL;
125 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
126 if (name[name_len - 1] == '.')
128 if ((getnames[0] = strdup (name)) == NULL)
130 free_null:
131 while (pos-- > 0)
132 free (getnames[pos]);
133 free (getnames);
134 return NULL;
137 getnames[1] = NULL;
139 return getnames;
142 /* If the passed NAME is shared a suffix (the latter of course with
143 a final dot) with each other we pass back NAME with a final
144 dot. */
145 if (local_domain_len > 2)
147 have_point = 0;
148 cp = &local_domain[local_domain_len - 2];
149 cp2 = &name[name_len - 1];
151 while (*cp == *cp2)
153 if (*cp == '.')
154 have_point = 1;
155 --cp;
156 --cp2;
157 if (cp < local_domain)
159 have_point = cp2 < name || *cp2 == '.';
160 break;
162 if (cp2 < name)
164 have_point = *cp == '.';
165 break;
169 if (have_point)
171 getnames[0] = malloc (name_len + 2);
172 if (getnames[0] == NULL)
173 goto free_null;
175 strcpy (stpcpy (getnames[0], name), ".");
176 ++pos;
180 /* Get the search path, where we have to search "name" */
181 path = getenv ("NIS_PATH");
182 if (path == NULL)
183 path = strdupa ("$");
184 else
185 path = strdupa (path);
187 have_point = strchr (name, '.') != NULL;
189 cp = __strtok_r (path, ":", &saveptr);
190 while (cp)
192 if (strcmp (cp, "$") == 0)
194 const char *cptr = local_domain;
195 char *tmp;
197 while (*cptr != '\0' && count_dots (cptr) >= 2)
199 if (pos >= count)
201 count += 5;
202 nis_name *newp = realloc (getnames,
203 (count + 1) * sizeof (char *));
204 if (__builtin_expect (newp == NULL, 0))
205 goto free_null;
206 getnames = newp;
208 tmp = malloc (strlen (cptr) + local_domain_len + name_len + 2);
209 if (__builtin_expect (tmp == NULL, 0))
210 goto free_null;
212 getnames[pos] = tmp;
213 tmp = stpcpy (tmp, name);
214 *tmp++ = '.';
215 if (cptr[1] != '\0')
216 stpcpy (tmp, cptr);
217 else
218 ++cptr;
220 ++pos;
222 while (*cptr != '.' && *cptr != '\0')
223 ++cptr;
224 if (cptr[0] != '\0' && cptr[1] != '\0')
225 /* If we have only ".", don't remove the "." */
226 ++cptr;
229 else
231 char *tmp;
232 size_t cplen = strlen (cp);
234 if (cp[cplen - 1] == '$')
236 char *p;
238 tmp = malloc (cplen + local_domain_len + name_len + 2);
239 if (__builtin_expect (tmp == NULL, 0))
240 goto free_null;
242 p = __stpcpy (tmp, name);
243 *p++ = '.';
244 p = __mempcpy (p, cp, cplen);
245 --p;
246 if (p[-1] != '.')
247 *p++ = '.';
248 __stpcpy (p, local_domain);
250 else
252 char *p;
254 tmp = malloc (cplen + name_len + 2);
255 if (__builtin_expect (tmp == NULL, 0))
256 goto free_null;
258 p = __stpcpy (tmp, name);
259 *p++ = '.';
260 memcpy (p, cp, cplen + 1);
263 if (pos >= count)
265 count += 5;
266 nis_name *newp = realloc (getnames,
267 (count + 1) * sizeof (char *));
268 if (__builtin_expect (newp == NULL, 0))
269 goto free_null;
270 getnames = newp;
272 getnames[pos] = tmp;
273 ++pos;
275 cp = __strtok_r (NULL, ":", &saveptr);
278 getnames[pos] = NULL;
280 return getnames;
282 libnsl_hidden_def (nis_getnames)
284 void
285 nis_freenames (nis_name *names)
287 int i = 0;
289 while (names[i] != NULL)
291 free (names[i]);
292 ++i;
295 free (names);
297 libnsl_hidden_def (nis_freenames)
299 name_pos
300 nis_dir_cmp (const_nis_name n1, const_nis_name n2)
302 int len1, len2;
304 len1 = strlen (n1);
305 len2 = strlen (n2);
307 if (len1 == len2)
309 if (strcmp (n1, n2) == 0)
310 return SAME_NAME;
311 else
312 return NOT_SEQUENTIAL;
315 if (len1 < len2)
317 if (n2[len2 - len1 - 1] != '.')
318 return NOT_SEQUENTIAL;
319 else if (strcmp (&n2[len2 - len1], n1) == 0)
320 return HIGHER_NAME;
321 else
322 return NOT_SEQUENTIAL;
324 else
326 if (n1[len1 - len2 - 1] != '.')
327 return NOT_SEQUENTIAL;
328 else if (strcmp (&n1[len1 - len2], n2) == 0)
329 return LOWER_NAME;
330 else
331 return NOT_SEQUENTIAL;
335 libnsl_hidden_def (nis_dir_cmp)
337 void
338 nis_destroy_object (nis_object *obj)
340 nis_free_object (obj);
342 libnsl_hidden_def (nis_destroy_object)