]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nis_subr.c
Define hidden attribute macros for libnsl.
[thirdparty/glibc.git] / nis / nis_subr.c
CommitLineData
9be31a51 1/* Copyright (c) 1997, 1999, 2000, 2004 Free Software Foundation, Inc.
e61abf83
UD
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
4
5 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
e61abf83
UD
9
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
41bdb6e2 13 Lesser General Public License for more details.
e61abf83 14
41bdb6e2
AJ
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. */
e61abf83
UD
19
20#include <errno.h>
21#include <string.h>
22#include <rpcsvc/nis.h>
e61abf83
UD
23
24nis_name
c131718c 25nis_leaf_of (const_nis_name name)
e61abf83
UD
26{
27 static char result[NIS_MAXNAMELEN + 1];
28
29 return nis_leaf_of_r (name, result, NIS_MAXNAMELEN);
30}
31
32nis_name
c131718c 33nis_leaf_of_r (const_nis_name name, char *buffer, size_t buflen)
e61abf83
UD
34{
35 size_t i = 0;
36
37 buffer[0] = '\0';
38
39 while (name[i] != '.' && name[i] != '\0')
40 i++;
41
42 if (i > buflen - 1)
43 {
44 errno = ERANGE;
45 return NULL;
46 }
47
51702635
UD
48 if (i > 0)
49 {
50 if ((size_t)i >= buflen)
51 {
52 errno = ERANGE;
53 return NULL;
54 }
32abdb71
UD
55
56 *((char *) __mempcpy (buffer, name, i)) = '\0';
51702635 57 }
e61abf83
UD
58
59 return buffer;
60}
61
62nis_name
c131718c 63nis_name_of (const_nis_name name)
e61abf83
UD
64{
65 static char result[NIS_MAXNAMELEN + 1];
66
67 return nis_name_of_r (name, result, NIS_MAXNAMELEN);
68}
69
70nis_name
c131718c 71nis_name_of_r (const_nis_name name, char *buffer, size_t buflen)
e61abf83
UD
72{
73 char *local_domain;
74 int diff;
75
76 local_domain = nis_local_directory ();
77
78 diff = strlen (name) - strlen (local_domain);
79 if (diff <= 0)
80 return NULL;
81
82 if (strcmp (&name[diff], local_domain) != 0)
83 return NULL;
84
85 if ((size_t) diff >= buflen)
86 {
87 errno = ERANGE;
88 return NULL;
89 }
32abdb71
UD
90
91 *((char *) __mempcpy (buffer, name, diff - 1)) = '\0';
e61abf83
UD
92
93 if (diff - 1 == 0)
94 return NULL;
95
96 return buffer;
97}
98
e61abf83 99static int
c131718c 100count_dots (const_nis_name str)
e61abf83
UD
101{
102 int count = 0;
103 size_t i;
104
105 for (i = 0; i < strlen (str); ++i)
106 if (str[i] == '.')
107 ++count;
108
109 return count;
110}
111
32abdb71
UD
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. */
e61abf83 115nis_name *
c131718c 116nis_getnames (const_nis_name name)
e61abf83
UD
117{
118 nis_name *getnames = NULL;
119 char local_domain[NIS_MAXNAMELEN + 1];
9be31a51
UD
120 char *path;
121 char *cp;
122 int count;
123 int pos = 0;
124 int have_point;
32abdb71 125 char *saveptr;
e61abf83
UD
126
127 strncpy (local_domain, nis_local_directory (), NIS_MAXNAMELEN);
128 local_domain[NIS_MAXNAMELEN] = '\0';
129
130 count = 1;
32abdb71 131 getnames = malloc ((count + 1) * sizeof (char *));
54eb84d0 132 if (__builtin_expect (getnames == NULL, 0))
e61abf83
UD
133 return NULL;
134
135 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
136 if (name[strlen (name) - 1] == '.')
137 {
138 if ((getnames[0] = strdup (name)) == NULL)
9be31a51
UD
139 {
140 free_null:
141 while (pos-- > 0)
142 free (getnames[pos]);
143 free (getnames);
144 return NULL;
145 }
32abdb71 146
e61abf83
UD
147 getnames[1] = NULL;
148
149 return getnames;
150 }
151
152 /* Get the search path, where we have to search "name" */
153 path = getenv ("NIS_PATH");
154 if (path == NULL)
155 path = strdupa ("$");
156 else
157 path = strdupa (path);
158
2d7da676
UD
159 have_point = (strchr (name, '.') != NULL);
160
32abdb71 161 cp = __strtok_r (path, ":", &saveptr);
e61abf83
UD
162 while (cp)
163 {
164 if (strcmp (cp, "$") == 0)
165 {
166 char *cptr = local_domain;
167 char *tmp;
168
2d7da676 169 while ((have_point && *cptr != '\0') || (count_dots (cptr) >= 2))
e61abf83
UD
170 {
171 if (pos >= count)
172 {
173 count += 5;
9be31a51
UD
174 nis_name *newp = realloc (getnames,
175 (count + 1) * sizeof (char *));
176 if (__builtin_expect (newp == NULL, 0))
177 goto free_null;
178 getnames = newp;
e61abf83
UD
179 }
180 tmp = malloc (strlen (cptr) + strlen (local_domain) +
181 strlen (name) + 2);
54eb84d0 182 if (__builtin_expect (tmp == NULL, 0))
9be31a51 183 goto free_null;
e61abf83
UD
184
185 getnames[pos] = tmp;
186 tmp = stpcpy (tmp, name);
187 *tmp++ = '.';
2d7da676
UD
188 if (cptr[1] != '\0')
189 stpcpy (tmp, cptr);
190 else
191 ++cptr;
e61abf83
UD
192
193 ++pos;
194
2d7da676
UD
195 while (*cptr != '.' && *cptr != '\0')
196 ++cptr;
197 if (cptr[0] != '\0' && cptr[1] != '\0')
198 /* If we have only ".", don't remove the "." */
e61abf83 199 ++cptr;
e61abf83
UD
200 }
201 }
202 else
203 {
204 char *tmp;
32abdb71 205 size_t cplen = strlen (cp);
e61abf83 206
32abdb71 207 if (cp[cplen - 1] == '$')
e61abf83 208 {
2d7da676
UD
209 char *p;
210
32abdb71 211 tmp = malloc (cplen + strlen (local_domain) + strlen (name) + 2);
54eb84d0 212 if (__builtin_expect (tmp == NULL, 0))
9be31a51 213 goto free_null;
e61abf83 214
32abdb71 215 p = __stpcpy (tmp, name);
2d7da676 216 *p++ = '.';
32abdb71 217 p = __mempcpy (p, cp, cplen);
2d7da676
UD
218 --p;
219 if (p[-1] != '.')
220 *p++ = '.';
32abdb71 221 __stpcpy (p, local_domain);
e61abf83
UD
222 }
223 else
224 {
2d7da676
UD
225 char *p;
226
32abdb71 227 tmp = malloc (cplen + strlen (name) + 2);
54eb84d0 228 if (__builtin_expect (tmp == NULL, 0))
9be31a51 229 goto free_null;
e61abf83 230
32abdb71 231 p = __stpcpy (tmp, name);
2d7da676 232 *p++ = '.';
32abdb71 233 memcpy (p, cp, cplen + 1);
e61abf83
UD
234 }
235
2d7da676 236 if (pos >= count)
e61abf83
UD
237 {
238 count += 5;
9be31a51
UD
239 nis_name *newp = realloc (getnames,
240 (count + 1) * sizeof (char *));
241 if (__builtin_expect (newp == NULL, 0))
242 goto free_null;
243 getnames = newp;
e61abf83
UD
244 }
245 getnames[pos] = tmp;
246 ++pos;
247 }
32abdb71 248 cp = __strtok_r (NULL, ":", &saveptr);
e61abf83
UD
249 }
250
251 getnames[pos] = NULL;
252
253 return getnames;
254}
255
256void
257nis_freenames (nis_name *names)
258{
259 int i = 0;
260
261 while (names[i] != NULL)
262 {
263 free (names[i]);
264 ++i;
265 }
266
267 free (names);
268}
269
270name_pos
c131718c 271nis_dir_cmp (const_nis_name n1, const_nis_name n2)
e61abf83
UD
272{
273 int len1, len2;
274
275 len1 = strlen (n1);
276 len2 = strlen (n2);
277
278 if (len1 == len2)
279 {
280 if (strcmp (n1, n2) == 0)
281 return SAME_NAME;
282 else
283 return NOT_SEQUENTIAL;
284 }
285
286 if (len1 < len2)
287 {
288 if (n2[len2 - len1 - 1] != '.')
289 return NOT_SEQUENTIAL;
290 else if (strcmp (&n2[len2 - len1], n1) == 0)
291 return HIGHER_NAME;
292 else
293 return NOT_SEQUENTIAL;
294 }
295 else
296 {
297 if (n1[len1 - len2 - 1] != '.')
298 return NOT_SEQUENTIAL;
299 else if (strcmp (&n1[len1 - len2], n2) == 0)
300 return LOWER_NAME;
301 else
302 return NOT_SEQUENTIAL;
303
304 }
305}
306
307void
308nis_destroy_object (nis_object *obj)
309{
310 nis_free_object (obj);
311}