1 /* Copyright (c) 1997-2013 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/>. */
21 #include <rpcsvc/nis.h>
24 nis_leaf_of (const_nis_name name
)
26 static char result
[NIS_MAXNAMELEN
+ 1];
28 return nis_leaf_of_r (name
, result
, NIS_MAXNAMELEN
);
32 nis_leaf_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
38 while (name
[i
] != '.' && name
[i
] != '\0')
41 if (__builtin_expect (i
>= buflen
, 0))
47 *((char *) __mempcpy (buffer
, name
, i
)) = '\0';
51 libnsl_hidden_def (nis_leaf_of_r
)
54 nis_name_of (const_nis_name name
)
56 static char result
[NIS_MAXNAMELEN
+ 1];
58 return nis_name_of_r (name
, result
, NIS_MAXNAMELEN
);
62 nis_name_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
67 local_domain
= nis_local_directory ();
69 diff
= strlen (name
) - strlen (local_domain
);
73 if (strcmp (&name
[diff
], local_domain
) != 0)
76 if ((size_t) diff
>= buflen
)
82 *((char *) __mempcpy (buffer
, name
, diff
- 1)) = '\0';
89 libnsl_hidden_def (nis_name_of_r
)
91 static int __always_inline
92 count_dots (const_nis_name str
)
96 for (size_t i
= 0; str
[i
] != '\0'; ++i
)
103 /* If we run out of memory, we don't give already allocated memory
104 free. The overhead for bringing getnames back in a safe state to
105 free it is to big. */
107 nis_getnames (const_nis_name name
)
109 const char *local_domain
= nis_local_directory ();
110 size_t local_domain_len
= strlen (local_domain
);
111 size_t name_len
= strlen (name
);
114 char *saveptr
= NULL
;
120 nis_name
*getnames
= malloc ((count
+ 1) * sizeof (char *));
121 if (__builtin_expect (getnames
== NULL
, 0))
124 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
125 if (name
[name_len
- 1] == '.')
127 if ((getnames
[0] = strdup (name
)) == NULL
)
131 free (getnames
[pos
]);
141 /* If the passed NAME is shared a suffix (the latter of course with
142 a final dot) with each other we pass back NAME with a final
144 if (local_domain_len
> 2)
147 cp
= &local_domain
[local_domain_len
- 2];
148 cp2
= &name
[name_len
- 1];
156 if (cp
< local_domain
)
158 have_point
= cp2
< name
|| *cp2
== '.';
163 have_point
= *cp
== '.';
170 getnames
[0] = malloc (name_len
+ 2);
171 if (getnames
[0] == NULL
)
174 strcpy (stpcpy (getnames
[0], name
), ".");
179 /* Get the search path, where we have to search "name" */
180 path
= getenv ("NIS_PATH");
182 path
= strdupa ("$");
184 path
= strdupa (path
);
186 have_point
= strchr (name
, '.') != NULL
;
188 cp
= __strtok_r (path
, ":", &saveptr
);
191 if (strcmp (cp
, "$") == 0)
193 const char *cptr
= local_domain
;
196 while (*cptr
!= '\0' && count_dots (cptr
) >= 2)
201 nis_name
*newp
= realloc (getnames
,
202 (count
+ 1) * sizeof (char *));
203 if (__builtin_expect (newp
== NULL
, 0))
207 tmp
= malloc (strlen (cptr
) + local_domain_len
+ name_len
+ 2);
208 if (__builtin_expect (tmp
== NULL
, 0))
212 tmp
= stpcpy (tmp
, name
);
221 while (*cptr
!= '.' && *cptr
!= '\0')
223 if (cptr
[0] != '\0' && cptr
[1] != '\0')
224 /* If we have only ".", don't remove the "." */
231 size_t cplen
= strlen (cp
);
233 if (cp
[cplen
- 1] == '$')
237 tmp
= malloc (cplen
+ local_domain_len
+ name_len
+ 2);
238 if (__builtin_expect (tmp
== NULL
, 0))
241 p
= __stpcpy (tmp
, name
);
243 p
= __mempcpy (p
, cp
, cplen
);
247 __stpcpy (p
, local_domain
);
253 tmp
= malloc (cplen
+ name_len
+ 3);
254 if (__builtin_expect (tmp
== NULL
, 0))
257 p
= __mempcpy (tmp
, name
, name_len
);
259 p
= __mempcpy (p
, cp
, cplen
);
268 nis_name
*newp
= realloc (getnames
,
269 (count
+ 1) * sizeof (char *));
270 if (__builtin_expect (newp
== NULL
, 0))
277 cp
= __strtok_r (NULL
, ":", &saveptr
);
281 && __asprintf (&getnames
[pos
++], "%s%s%s%s",
282 name
, name
[name_len
- 1] == '.' ? "" : ".",
284 local_domain
[local_domain_len
- 1] == '.' ? "" : ".") < 0)
287 getnames
[pos
] = NULL
;
291 libnsl_hidden_def (nis_getnames
)
294 nis_freenames (nis_name
*names
)
298 while (names
[i
] != NULL
)
306 libnsl_hidden_def (nis_freenames
)
309 nis_dir_cmp (const_nis_name n1
, const_nis_name n2
)
318 if (strcmp (n1
, n2
) == 0)
321 return NOT_SEQUENTIAL
;
326 if (n2
[len2
- len1
- 1] != '.')
327 return NOT_SEQUENTIAL
;
328 else if (strcmp (&n2
[len2
- len1
], n1
) == 0)
331 return NOT_SEQUENTIAL
;
335 if (n1
[len1
- len2
- 1] != '.')
336 return NOT_SEQUENTIAL
;
337 else if (strcmp (&n1
[len1
- len2
], n2
) == 0)
340 return NOT_SEQUENTIAL
;
344 libnsl_hidden_def (nis_dir_cmp
)
347 nis_destroy_object (nis_object
*obj
)
349 nis_free_object (obj
);
351 libnsl_hidden_def (nis_destroy_object
)