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
22 #include <rpcsvc/nis.h>
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
);
33 nis_leaf_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
39 while (name
[i
] != '.' && name
[i
] != '\0')
50 if ((size_t)i
>= buflen
)
56 *((char *) __mempcpy (buffer
, name
, i
)) = '\0';
61 libnsl_hidden_def (nis_leaf_of_r
)
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
);
72 nis_name_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
77 local_domain
= nis_local_directory ();
79 diff
= strlen (name
) - strlen (local_domain
);
83 if (strcmp (&name
[diff
], local_domain
) != 0)
86 if ((size_t) diff
>= buflen
)
92 *((char *) __mempcpy (buffer
, name
, diff
- 1)) = '\0';
99 libnsl_hidden_def (nis_name_of_r
)
102 count_dots (const_nis_name str
)
105 size_t l
= strlen (str
);
107 for (size_t i
= 0; i
< l
; ++i
)
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. */
118 nis_getnames (const_nis_name name
)
120 nis_name
*getnames
= NULL
;
121 char local_domain
[NIS_MAXNAMELEN
+ 1];
129 strncpy (local_domain
, nis_local_directory (), NIS_MAXNAMELEN
);
130 local_domain
[NIS_MAXNAMELEN
] = '\0';
133 getnames
= malloc ((count
+ 1) * sizeof (char *));
134 if (__builtin_expect (getnames
== NULL
, 0))
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
)
144 free (getnames
[pos
]);
154 /* Get the search path, where we have to search "name" */
155 path
= getenv ("NIS_PATH");
157 path
= strdupa ("$");
159 path
= strdupa (path
);
161 have_point
= (strchr (name
, '.') != NULL
);
163 cp
= __strtok_r (path
, ":", &saveptr
);
166 if (strcmp (cp
, "$") == 0)
168 char *cptr
= local_domain
;
171 while ((have_point
&& *cptr
!= '\0') || (count_dots (cptr
) >= 2))
176 nis_name
*newp
= realloc (getnames
,
177 (count
+ 1) * sizeof (char *));
178 if (__builtin_expect (newp
== NULL
, 0))
182 tmp
= malloc (strlen (cptr
) + strlen (local_domain
) +
184 if (__builtin_expect (tmp
== NULL
, 0))
188 tmp
= stpcpy (tmp
, name
);
197 while (*cptr
!= '.' && *cptr
!= '\0')
199 if (cptr
[0] != '\0' && cptr
[1] != '\0')
200 /* If we have only ".", don't remove the "." */
207 size_t cplen
= strlen (cp
);
209 if (cp
[cplen
- 1] == '$')
213 tmp
= malloc (cplen
+ strlen (local_domain
) + strlen (name
) + 2);
214 if (__builtin_expect (tmp
== NULL
, 0))
217 p
= __stpcpy (tmp
, name
);
219 p
= __mempcpy (p
, cp
, cplen
);
223 __stpcpy (p
, local_domain
);
229 tmp
= malloc (cplen
+ strlen (name
) + 2);
230 if (__builtin_expect (tmp
== NULL
, 0))
233 p
= __stpcpy (tmp
, name
);
235 memcpy (p
, cp
, cplen
+ 1);
241 nis_name
*newp
= realloc (getnames
,
242 (count
+ 1) * sizeof (char *));
243 if (__builtin_expect (newp
== NULL
, 0))
250 cp
= __strtok_r (NULL
, ":", &saveptr
);
253 getnames
[pos
] = NULL
;
257 libnsl_hidden_def (nis_getnames
)
260 nis_freenames (nis_name
*names
)
264 while (names
[i
] != NULL
)
272 libnsl_hidden_def (nis_freenames
)
275 nis_dir_cmp (const_nis_name n1
, const_nis_name n2
)
284 if (strcmp (n1
, n2
) == 0)
287 return NOT_SEQUENTIAL
;
292 if (n2
[len2
- len1
- 1] != '.')
293 return NOT_SEQUENTIAL
;
294 else if (strcmp (&n2
[len2
- len1
], n1
) == 0)
297 return NOT_SEQUENTIAL
;
301 if (n1
[len1
- len2
- 1] != '.')
302 return NOT_SEQUENTIAL
;
303 else if (strcmp (&n1
[len1
- len2
], n2
) == 0)
306 return NOT_SEQUENTIAL
;
310 libnsl_hidden_def (nis_dir_cmp
)
313 nis_destroy_object (nis_object
*obj
)
315 nis_free_object (obj
);
317 libnsl_hidden_def (nis_destroy_object
)