Sun Jul 14 01:51:39 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc/history.git] / nss / getXXbyYY_r.c
blob37ad22d04509ad767773c25a53ecacd5f7f794ae
1 /* Copyright (C) 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
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
17 not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "nsswitch.h"
22 /*******************************************************************\
23 |* Here we assume several symbols to be defined: *|
24 |* *|
25 |* LOOKUP_TYPE - the return type of the function *|
26 |* *|
27 |* FUNCTION_NAME - name of the non-reentrant function *|
28 |* *|
29 |* DATABASE_NAME - name of the database the function accesses *|
30 |* (e.g., host, services, ...) *|
31 |* *|
32 |* ADD_PARAMS - additional parameter, can vary in number *|
33 |* *|
34 |* ADD_VARIABLES - names of additional parameter *|
35 |* *|
36 |* Optionally the following vars can be defined: *|
37 |* *|
38 |* NEED_H_ERRNO - an extra parameter will be passed to point to *|
39 |* the global `h_errno' variable. *|
40 |* *|
41 |* NEED__RES - the global _res variable might be used so we *|
42 |* will have to initialize it if necessary *|
43 |* *|
44 \*******************************************************************/
46 /* To make the real sources a bit prettier. */
47 #define REENTRANT_NAME APPEND_R (FUNCTION_NAME)
48 #define APPEND_R(name) APPEND_R1 (name)
49 #define APPEND_R1(name) name##_r
51 #define FUNCTION_NAME_STRING STRINGIZE (FUNCTION_NAME)
52 #define REENTRANT_NAME_STRING STRINGIZE (REENTRANT_NAME)
53 #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME)
54 #define STRINGIZE(name) STRINGIZE1 (name)
55 #define STRINGIZE1(name) #name
57 #define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup)
58 #define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post)
59 #define CONCAT3_2(Pre, Name, Post) Pre##Name##Post
61 /* Sometimes we need to store error codes in the `h_errno' variable. */
62 #ifdef NEED_H_ERRNO
63 # define H_ERRNO_PARM , int *h_errnop
64 # define H_ERRNO_VAR , h_errnop
65 #else
66 # define H_ERRNO_PARM
67 # define H_ERRNO_VAR
68 #endif
71 /* Type of the lookup function we need here. */
72 typedef int (*lookup_function) (ADD_PARAMS, LOOKUP_TYPE *, char *, int
73 H_ERRNO_PARM);
75 /* Some usages of this file might use this variable. */
76 extern struct __res_state _res;
78 /* The lookup function for the first entry of this service. */
79 extern int DB_LOOKUP_FCT (service_user **nip, const char *name, void **fctp);
83 LOOKUP_TYPE *
84 REENTRANT_NAME (ADD_PARAMS, LOOKUP_TYPE *result, char *buffer, int buflen
85 H_ERRNO_PARM)
87 static service_user *startp = NULL;
88 static lookup_function start_fct;
89 service_user *nip;
90 lookup_function fct;
91 int no_more;
92 enum nss_status status = NSS_STATUS_UNAVAIL;
94 if (startp == NULL)
96 no_more = DB_LOOKUP_FCT (&nip, REENTRANT_NAME_STRING, (void **) &fct);
97 if (no_more)
98 startp = (service_user *) -1;
99 else
101 startp = nip;
102 start_fct = fct;
104 #ifdef NEED__RES
105 /* The resolver code will really be used so we have to
106 initialize it. */
107 if ((_res.options & RES_INIT) == 0 && res_init () == -1)
109 *h_errnop = NETDB_INTERNAL;
110 return NULL;
112 #endif /* need _res */
115 else
117 fct = start_fct;
118 no_more = (nip = startp) == (service_user *) -1;
121 while (no_more == 0)
123 status = (*fct) (ADD_VARIABLES, result, buffer, buflen H_ERRNO_VAR);
125 no_more = __nss_next (&nip, REENTRANT_NAME_STRING,
126 (void **) &fct, status, 0);
129 return status == NSS_STATUS_SUCCESS ? result : NULL;