Update.
[glibc.git] / nss / getnssent_r.c
blob0c10ece562911a23c311a4198705e0c881e5ca45
1 /* Copyright (C) 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <errno.h>
20 #include <netdb.h>
21 #include "nsswitch.h"
23 /* Set up NIP to run through the services. If ALL is zero, use NIP's
24 current location if it's not nil. Return nonzero if there are no
25 services (left). */
26 static int
27 setup (const char *func_name, db_lookup_function lookup_fct,
28 void **fctp, service_user **nip, service_user **startp, int all)
30 int no_more;
31 if (*startp == NULL)
33 no_more = lookup_fct (nip, func_name, fctp);
34 *startp = no_more ? (service_user *) -1l : *nip;
36 else if (*startp == (service_user *) -1l)
37 /* No services at all. */
38 return 1;
39 else
41 if (all || !*nip)
42 /* Reset to the beginning of the service list. */
43 *nip = *startp;
44 /* Look up the first function. */
45 no_more = __nss_lookup (nip, func_name, fctp);
47 return no_more;
50 void
51 __nss_setent (const char *func_name, db_lookup_function lookup_fct,
52 service_user **nip, service_user **startp,
53 service_user **last_nip, int stayopen, int *stayopen_tmp,
54 int res)
56 setent_function fct;
57 int no_more;
59 if (res && (_res.options & RES_INIT) == 0
60 && __res_ninit (&_res) == -1)
62 __set_h_errno (NETDB_INTERNAL);
63 return;
66 /* Cycle through the services and run their `setXXent' functions until
67 we find an available service. */
68 no_more = setup (func_name, lookup_fct, (void **) &fct, nip,
69 startp, 1);
70 while (! no_more)
72 int is_last_nip = *nip == *last_nip;
73 enum nss_status status;
75 if (stayopen_tmp)
76 status = DL_CALL_FCT (fct, (*stayopen_tmp));
77 else
78 status = DL_CALL_FCT (fct, (0));
80 no_more = __nss_next (nip, func_name, (void **) &fct,
81 status, 0);
82 if (is_last_nip)
83 *last_nip = *nip;
86 if (stayopen_tmp)
87 *stayopen_tmp = stayopen;
91 void
92 __nss_endent (const char *func_name, db_lookup_function lookup_fct,
93 service_user **nip, service_user **startp,
94 service_user **last_nip, int res)
96 endent_function fct;
97 int no_more;
99 if (res && (_res.options & RES_INIT) == 0
100 && __res_ninit (&_res) == -1)
102 __set_h_errno (NETDB_INTERNAL);
103 return;
106 /* Cycle through all the services and run their endXXent functions. */
107 no_more = setup (func_name, lookup_fct, (void **) &fct, nip, startp, 1);
108 while (! no_more)
110 /* Ignore status, we force check in __NSS_NEXT. */
111 DL_CALL_FCT (fct, ());
113 if (*nip == *last_nip)
114 /* We have processed all services which were used. */
115 break;
117 no_more = __nss_next (nip, func_name, (void **) &fct, 0, 1);
119 *last_nip = *nip = NULL;
124 __nss_getent_r (const char *getent_func_name,
125 const char *setent_func_name,
126 db_lookup_function lookup_fct,
127 service_user **nip, service_user **startp,
128 service_user **last_nip, int *stayopen_tmp, int res,
129 void *resbuf, char *buffer, size_t buflen,
130 void **result, int *h_errnop)
132 getent_function fct;
133 int no_more;
134 enum nss_status status;
136 if (res && (_res.options & RES_INIT) == 0
137 && __res_ninit (&_res) == -1)
139 __set_h_errno (NETDB_INTERNAL);
140 *result = NULL;
141 return errno;
144 /* Initialize status to return if no more functions are found. */
145 status = NSS_STATUS_NOTFOUND;
147 /* Run through available functions, starting with the same function last
148 run. We will repeat each function as long as it succeeds, and then go
149 on to the next service action. */
150 no_more = setup (getent_func_name, lookup_fct, (void **) &fct, nip,
151 startp, 0);
152 while (! no_more)
154 int is_last_nip = *nip == *last_nip;
156 status = DL_CALL_FCT (fct,
157 (resbuf, buffer, buflen, &errno, &h_errno));
159 /* The the status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
160 provided buffer is too small. In this case we should give
161 the user the possibility to enlarge the buffer and we should
162 not simply go on with the next service (even if the TRYAGAIN
163 action tells us so). */
164 if (status == NSS_STATUS_TRYAGAIN
165 && (h_errnop == NULL || *h_errnop == NETDB_INTERNAL)
166 && errno == ERANGE)
167 break;
171 no_more = __nss_next (nip, getent_func_name, (void **) &fct,
172 status, 0);
174 if (is_last_nip)
175 *last_nip = *nip;
177 if (! no_more)
179 /* Call the `setXXent' function. This wasn't done before. */
180 setent_function sfct;
182 no_more = __nss_lookup (nip, setent_func_name,
183 (void **) &sfct);
185 if (! no_more)
187 if (stayopen_tmp)
188 status = DL_CALL_FCT (sfct, (*stayopen_tmp));
189 else
190 status = DL_CALL_FCT (sfct, (0));
192 else
193 status = NSS_STATUS_NOTFOUND;
196 while (! no_more && status != NSS_STATUS_SUCCESS);
199 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
200 return status == NSS_STATUS_SUCCESS ? 0 : errno;