]>
Commit | Line | Data |
---|---|---|
344c67b6 | 1 | /* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc. |
26761c28 | 2 | This file is part of the GNU C Library. |
5f0e6fc7 | 3 | |
26761c28 UD |
4 | The GNU C Library is free software; you can redistribute it and/or |
5 | modify it under the terms of the GNU Library General Public License as | |
6 | published by the Free Software Foundation; either version 2 of the | |
7 | License, or (at your option) any later version. | |
5f0e6fc7 | 8 | |
26761c28 UD |
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 | Library General Public License for more details. | |
5f0e6fc7 | 13 | |
26761c28 UD |
14 | You should have received a copy of the GNU Library General Public |
15 | License along with the GNU C Library; see the file COPYING.LIB. If not, | |
16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
17 | Boston, MA 02111-1307, USA. */ | |
5f0e6fc7 | 18 | |
344c67b6 | 19 | #include <assert.h> |
26761c28 | 20 | #include <errno.h> |
5107cf1d | 21 | #include <bits/libc-lock.h> |
26761c28 UD |
22 | #include <stdlib.h> |
23 | ||
24 | #include "nsswitch.h" | |
5f0e6fc7 RM |
25 | |
26 | /*******************************************************************\ | |
27 | |* Here we assume several symbols to be defined: *| | |
28 | |* *| | |
29 | |* LOOKUP_TYPE - the return type of the function *| | |
30 | |* *| | |
31 | |* FUNCTION_NAME - name of the non-reentrant function *| | |
32 | |* *| | |
33 | |* DATABASE_NAME - name of the database the function accesses *| | |
34 | |* (e.g., host, services, ...) *| | |
35 | |* *| | |
36 | |* ADD_PARAMS - additional parameter, can vary in number *| | |
37 | |* *| | |
38 | |* ADD_VARIABLES - names of additional parameter *| | |
39 | |* *| | |
40 | |* BUFLEN - length of buffer allocated for the non *| | |
41 | |* reentrant version *| | |
42 | |* *| | |
43 | |* Optionally the following vars can be defined: *| | |
44 | |* *| | |
45 | |* NEED_H_ERRNO - an extra parameter will be passed to point to *| | |
46 | |* the global `h_errno' variable. *| | |
47 | |* *| | |
48 | \*******************************************************************/ | |
49 | ||
50 | /* To make the real sources a bit prettier. */ | |
51 | #define REENTRANT_NAME APPEND_R (FUNCTION_NAME) | |
52 | #define APPEND_R(name) APPEND_R1 (name) | |
53 | #define APPEND_R1(name) name##_r | |
23396375 UD |
54 | #define INTERNAL(name) INTERNAL1 (name) |
55 | #define INTERNAL1(name) __##name | |
5f0e6fc7 RM |
56 | |
57 | /* Sometimes we need to store error codes in the `h_errno' variable. */ | |
58 | #ifdef NEED_H_ERRNO | |
59 | # define H_ERRNO_PARM , int *h_errnop | |
54d79e99 | 60 | # define H_ERRNO_VAR , &h_errno_tmp |
5f0e6fc7 RM |
61 | #else |
62 | # define H_ERRNO_PARM | |
63 | # define H_ERRNO_VAR | |
64 | #endif | |
65 | ||
66 | ||
67 | /* Prototype for reentrant version we use here. */ | |
ba1ffaa1 UD |
68 | extern int INTERNAL (REENTRANT_NAME) (ADD_PARAMS, LOOKUP_TYPE *resbuf, |
69 | char *buffer, size_t buflen, | |
70 | LOOKUP_TYPE **result H_ERRNO_PARM); | |
5f0e6fc7 | 71 | |
26761c28 UD |
72 | /* We need to protect the dynamic buffer handling. */ |
73 | __libc_lock_define_initialized (static, lock); | |
74 | ||
75 | ||
5f0e6fc7 RM |
76 | LOOKUP_TYPE * |
77 | FUNCTION_NAME (ADD_PARAMS) | |
78 | { | |
26761c28 UD |
79 | static char *buffer; |
80 | static size_t buffer_size; | |
ba1ffaa1 UD |
81 | static LOOKUP_TYPE resbuf; |
82 | LOOKUP_TYPE *result; | |
26761c28 | 83 | int save; |
54d79e99 UD |
84 | #ifdef NEED_H_ERRNO |
85 | int h_errno_tmp = 0; | |
86 | #endif | |
26761c28 UD |
87 | |
88 | /* Get lock. */ | |
89 | __libc_lock_lock (lock); | |
90 | ||
91 | if (buffer == NULL) | |
92 | { | |
93 | buffer_size = BUFLEN; | |
94 | buffer = malloc (buffer_size); | |
95 | } | |
96 | ||
61c162b5 UD |
97 | if (buffer != NULL) |
98 | { | |
99 | #ifdef HANDLE_DIGITS_DOTS | |
d17a729b UD |
100 | /* We have to test for the use of IPv6 which can only be done by |
101 | examining `_res'. */ | |
102 | if ((_res.options & RES_INIT) == 0 && res_init () == -1) | |
103 | { | |
104 | # ifdef NEED_H_ERRNO | |
105 | h_errno_tmp = NETDB_INTERNAL; | |
106 | # endif | |
107 | result = NULL; | |
108 | goto done; | |
109 | } | |
61c162b5 UD |
110 | # include "digits_dots.c" |
111 | #endif | |
112 | } | |
113 | ||
26761c28 UD |
114 | while (buffer != NULL |
115 | && INTERNAL (REENTRANT_NAME) (ADD_VARIABLES, &resbuf, buffer, | |
116 | buffer_size, &result H_ERRNO_VAR) != 0 | |
e4cf5070 UD |
117 | #ifdef NEED_H_ERRNO |
118 | && h_errno_tmp == NETDB_INTERNAL | |
119 | #endif | |
26761c28 UD |
120 | && errno == ERANGE) |
121 | { | |
122 | char *new_buf; | |
123 | buffer_size += BUFLEN; | |
124 | new_buf = realloc (buffer, buffer_size); | |
125 | if (new_buf == NULL) | |
e4cf5070 UD |
126 | { |
127 | /* We are out of memory. Free the current buffer so that the | |
128 | process gets a chance for a normal termination. */ | |
129 | save = errno; | |
130 | free (buffer); | |
131 | __set_errno (save); | |
132 | } | |
26761c28 UD |
133 | buffer = new_buf; |
134 | } | |
5f0e6fc7 | 135 | |
22d57dd3 UD |
136 | if (buffer == NULL) |
137 | result = NULL; | |
138 | ||
61c162b5 UD |
139 | #ifdef HANDLE_DIGITS_DOTS |
140 | done: | |
141 | #endif | |
26761c28 UD |
142 | /* Release lock. Preserve error value. */ |
143 | save = errno; | |
144 | __libc_lock_unlock (lock); | |
145 | __set_errno (save); | |
ba1ffaa1 | 146 | |
54d79e99 UD |
147 | #ifdef NEED_H_ERRNO |
148 | if (h_errno_tmp != 0) | |
149 | __set_h_errno (h_errno_tmp); | |
150 | #endif | |
151 | ||
ba1ffaa1 | 152 | return result; |
5f0e6fc7 | 153 | } |