Update.
[glibc.git] / nis / nis_subr.c
blobc7d58a60e9b03bca724548da92d9d596682b981d
1 /* Copyright (c) 1997, 1999 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 errno = ERANGE;
45 return NULL;
48 if (i > 0)
50 if ((size_t)i >= buflen)
52 errno = ERANGE;
53 return NULL;
56 *((char *) __mempcpy (buffer, name, i)) = '\0';
59 return buffer;
62 nis_name
63 nis_name_of (const_nis_name name)
65 static char result[NIS_MAXNAMELEN + 1];
67 return nis_name_of_r (name, result, NIS_MAXNAMELEN);
70 nis_name
71 nis_name_of_r (const_nis_name name, char *buffer, size_t buflen)
73 char *local_domain;
74 int diff;
76 local_domain = nis_local_directory ();
78 diff = strlen (name) - strlen (local_domain);
79 if (diff <= 0)
80 return NULL;
82 if (strcmp (&name[diff], local_domain) != 0)
83 return NULL;
85 if ((size_t) diff >= buflen)
87 errno = ERANGE;
88 return NULL;
91 *((char *) __mempcpy (buffer, name, diff - 1)) = '\0';
93 if (diff - 1 == 0)
94 return NULL;
96 return buffer;
99 static int
100 count_dots (const_nis_name str)
102 int count = 0;
103 size_t i;
105 for (i = 0; i < strlen (str); ++i)
106 if (str[i] == '.')
107 ++count;
109 return count;
112 /* If we run out of memory, we don't give already allocated memory
113 free. The overhead for bringing getnames back in a safe state to
114 free it is to big. */
115 nis_name *
116 nis_getnames (const_nis_name name)
118 nis_name *getnames = NULL;
119 char local_domain[NIS_MAXNAMELEN + 1];
120 char *path, *cp;
121 int count, pos, have_point;
122 char *saveptr;
124 strncpy (local_domain, nis_local_directory (), NIS_MAXNAMELEN);
125 local_domain[NIS_MAXNAMELEN] = '\0';
127 count = 1;
128 getnames = malloc ((count + 1) * sizeof (char *));
129 if (getnames == NULL)
130 return NULL;
132 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
133 if (name[strlen (name) - 1] == '.')
135 if ((getnames[0] = strdup (name)) == NULL)
136 return NULL;
138 getnames[1] = NULL;
140 return getnames;
143 /* Get the search path, where we have to search "name" */
144 path = getenv ("NIS_PATH");
145 if (path == NULL)
146 path = strdupa ("$");
147 else
148 path = strdupa (path);
150 have_point = (strchr (name, '.') != NULL);
152 pos = 0;
154 cp = __strtok_r (path, ":", &saveptr);
155 while (cp)
157 if (strcmp (cp, "$") == 0)
159 char *cptr = local_domain;
160 char *tmp;
162 while ((have_point && *cptr != '\0') || (count_dots (cptr) >= 2))
164 if (pos >= count)
166 count += 5;
167 getnames = realloc (getnames, (count + 1) * sizeof (char *));
168 if (getnames == NULL)
169 return NULL;
171 tmp = malloc (strlen (cptr) + strlen (local_domain) +
172 strlen (name) + 2);
173 if (tmp == NULL)
174 return NULL;
176 getnames[pos] = tmp;
177 tmp = stpcpy (tmp, name);
178 *tmp++ = '.';
179 if (cptr[1] != '\0')
180 stpcpy (tmp, cptr);
181 else
182 ++cptr;
184 ++pos;
186 while (*cptr != '.' && *cptr != '\0')
187 ++cptr;
188 if (cptr[0] != '\0' && cptr[1] != '\0')
189 /* If we have only ".", don't remove the "." */
190 ++cptr;
193 else
195 char *tmp;
196 size_t cplen = strlen (cp);
198 if (cp[cplen - 1] == '$')
200 char *p;
202 tmp = malloc (cplen + strlen (local_domain) + strlen (name) + 2);
203 if (tmp == NULL)
204 return NULL;
206 p = __stpcpy (tmp, name);
207 *p++ = '.';
208 p = __mempcpy (p, cp, cplen);
209 --p;
210 if (p[-1] != '.')
211 *p++ = '.';
212 __stpcpy (p, local_domain);
214 else
216 char *p;
218 tmp = malloc (cplen + strlen (name) + 2);
219 if (tmp == NULL)
220 return NULL;
222 p = __stpcpy (tmp, name);
223 *p++ = '.';
224 memcpy (p, cp, cplen + 1);
227 if (pos >= count)
229 count += 5;
230 getnames = realloc (getnames, (count + 1) * sizeof (char *));
231 if (getnames == NULL)
232 return NULL;
234 getnames[pos] = tmp;
235 ++pos;
237 cp = __strtok_r (NULL, ":", &saveptr);
240 getnames[pos] = NULL;
242 return getnames;
245 void
246 nis_freenames (nis_name *names)
248 int i = 0;
250 while (names[i] != NULL)
252 free (names[i]);
253 ++i;
256 free (names);
259 name_pos
260 nis_dir_cmp (const_nis_name n1, const_nis_name n2)
262 int len1, len2;
264 len1 = strlen (n1);
265 len2 = strlen (n2);
267 if (len1 == len2)
269 if (strcmp (n1, n2) == 0)
270 return SAME_NAME;
271 else
272 return NOT_SEQUENTIAL;
275 if (len1 < len2)
277 if (n2[len2 - len1 - 1] != '.')
278 return NOT_SEQUENTIAL;
279 else if (strcmp (&n2[len2 - len1], n1) == 0)
280 return HIGHER_NAME;
281 else
282 return NOT_SEQUENTIAL;
284 else
286 if (n1[len1 - len2 - 1] != '.')
287 return NOT_SEQUENTIAL;
288 else if (strcmp (&n1[len1 - len2], n2) == 0)
289 return LOWER_NAME;
290 else
291 return NOT_SEQUENTIAL;
296 void
297 nis_destroy_object (nis_object *obj)
299 nis_free_object (obj);