1 /* Copyright (c) 1997-2017 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
);
30 libnsl_hidden_nolink_def (nis_leaf_of
, GLIBC_2_1
)
33 nis_leaf_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
39 while (name
[i
] != '.' && name
[i
] != '\0')
42 if (__glibc_unlikely (i
>= buflen
))
48 *((char *) __mempcpy (buffer
, name
, i
)) = '\0';
52 libnsl_hidden_nolink_def (nis_leaf_of_r
, GLIBC_2_1
)
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
);
61 libnsl_hidden_nolink_def (nis_name_of
, GLIBC_2_1
)
64 nis_name_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
69 local_domain
= nis_local_directory ();
71 diff
= strlen (name
) - strlen (local_domain
);
75 if (strcmp (&name
[diff
], local_domain
) != 0)
78 if ((size_t) diff
>= buflen
)
84 *((char *) __mempcpy (buffer
, name
, diff
- 1)) = '\0';
91 libnsl_hidden_nolink_def (nis_name_of_r
, GLIBC_2_1
)
93 static int __always_inline
94 count_dots (const_nis_name str
)
98 for (size_t i
= 0; str
[i
] != '\0'; ++i
)
105 /* If we run out of memory, we don't give already allocated memory
106 free. The overhead for bringing getnames back in a safe state to
107 free it is to big. */
109 nis_getnames (const_nis_name name
)
111 const char *local_domain
= nis_local_directory ();
112 size_t local_domain_len
= strlen (local_domain
);
113 size_t name_len
= strlen (name
);
116 char *saveptr
= NULL
;
122 nis_name
*getnames
= malloc ((count
+ 1) * sizeof (char *));
123 if (__glibc_unlikely (getnames
== NULL
))
126 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
127 if (name
[name_len
- 1] == '.')
129 if ((getnames
[0] = strdup (name
)) == NULL
)
133 free (getnames
[pos
]);
143 /* If the passed NAME is shared a suffix (the latter of course with
144 a final dot) with each other we pass back NAME with a final
146 if (local_domain_len
> 2)
149 cp
= &local_domain
[local_domain_len
- 2];
150 cp2
= &name
[name_len
- 1];
158 if (cp
< local_domain
)
160 have_point
= cp2
< name
|| *cp2
== '.';
165 have_point
= *cp
== '.';
172 getnames
[0] = malloc (name_len
+ 2);
173 if (getnames
[0] == NULL
)
176 strcpy (stpcpy (getnames
[0], name
), ".");
181 /* Get the search path, where we have to search "name" */
182 path
= getenv ("NIS_PATH");
184 path
= strdupa ("$");
186 path
= strdupa (path
);
188 have_point
= strchr (name
, '.') != NULL
;
190 cp
= __strtok_r (path
, ":", &saveptr
);
193 if (strcmp (cp
, "$") == 0)
195 const char *cptr
= local_domain
;
198 while (*cptr
!= '\0' && count_dots (cptr
) >= 2)
203 nis_name
*newp
= realloc (getnames
,
204 (count
+ 1) * sizeof (char *));
205 if (__glibc_unlikely (newp
== NULL
))
209 tmp
= malloc (strlen (cptr
) + local_domain_len
+ name_len
+ 2);
210 if (__glibc_unlikely (tmp
== NULL
))
214 tmp
= stpcpy (tmp
, name
);
223 while (*cptr
!= '.' && *cptr
!= '\0')
225 if (cptr
[0] != '\0' && cptr
[1] != '\0')
226 /* If we have only ".", don't remove the "." */
233 size_t cplen
= strlen (cp
);
235 if (cp
[cplen
- 1] == '$')
239 tmp
= malloc (cplen
+ local_domain_len
+ name_len
+ 2);
240 if (__glibc_unlikely (tmp
== NULL
))
243 p
= __stpcpy (tmp
, name
);
245 p
= __mempcpy (p
, cp
, cplen
);
249 __stpcpy (p
, local_domain
);
255 tmp
= malloc (cplen
+ name_len
+ 3);
256 if (__glibc_unlikely (tmp
== NULL
))
259 p
= __mempcpy (tmp
, name
, name_len
);
261 p
= __mempcpy (p
, cp
, cplen
);
270 nis_name
*newp
= realloc (getnames
,
271 (count
+ 1) * sizeof (char *));
272 if (__glibc_unlikely (newp
== NULL
))
279 cp
= __strtok_r (NULL
, ":", &saveptr
);
283 && __asprintf (&getnames
[pos
++], "%s%s%s%s",
284 name
, name
[name_len
- 1] == '.' ? "" : ".",
286 local_domain
[local_domain_len
- 1] == '.' ? "" : ".") < 0)
289 getnames
[pos
] = NULL
;
293 libnsl_hidden_nolink_def (nis_getnames
, GLIBC_2_1
)
296 nis_freenames (nis_name
*names
)
300 while (names
[i
] != NULL
)
308 libnsl_hidden_nolink_def (nis_freenames
, GLIBC_2_1
)
311 nis_dir_cmp (const_nis_name n1
, const_nis_name n2
)
320 if (strcmp (n1
, n2
) == 0)
323 return NOT_SEQUENTIAL
;
328 if (n2
[len2
- len1
- 1] != '.')
329 return NOT_SEQUENTIAL
;
330 else if (strcmp (&n2
[len2
- len1
], n1
) == 0)
333 return NOT_SEQUENTIAL
;
337 if (n1
[len1
- len2
- 1] != '.')
338 return NOT_SEQUENTIAL
;
339 else if (strcmp (&n1
[len1
- len2
], n2
) == 0)
342 return NOT_SEQUENTIAL
;
346 libnsl_hidden_nolink_def (nis_dir_cmp
, GLIBC_2_1
)
349 nis_destroy_object (nis_object
*obj
)
351 nis_free_object (obj
);
353 libnsl_hidden_nolink_def (nis_destroy_object
, GLIBC_2_1
)