Obsolete pow10 functions.
[glibc.git] / nss / nss_files / files-XXX.c
blob265331ef21cd60ed301ab49eac4d6681d74bb388
1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <libc-lock.h>
24 #include "nsswitch.h"
26 #include <kernel-features.h>
28 /* These symbols are defined by the including source file:
30 ENTNAME -- database name of the structure and functions (hostent, pwent).
31 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
32 DATABASE -- string of the database file's name ("hosts", "passwd").
34 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
36 Also see files-parse.c.
39 #define ENTNAME_r CONCAT(ENTNAME,_r)
41 #define DATAFILE "/etc/" DATABASE
43 #ifdef NEED_H_ERRNO
44 # include <netdb.h>
45 # define H_ERRNO_PROTO , int *herrnop
46 # define H_ERRNO_ARG , herrnop
47 # define H_ERRNO_SET(val) (*herrnop = (val))
48 #else
49 # define H_ERRNO_PROTO
50 # define H_ERRNO_ARG
51 # define H_ERRNO_SET(val) ((void) 0)
52 #endif
54 #ifndef EXTRA_ARGS
55 # define EXTRA_ARGS
56 # define EXTRA_ARGS_DECL
57 # define EXTRA_ARGS_VALUE
58 #endif
60 /* Locks the static variables in this file. */
61 __libc_lock_define_initialized (static, lock)
63 /* Maintenance of the stream open on the database file. For getXXent
64 operations the stream needs to be held open across calls, the other
65 getXXbyYY operations all use their own stream. */
67 static FILE *stream;
69 /* Open database file if not already opened. */
70 static enum nss_status
71 internal_setent (FILE **stream)
73 enum nss_status status = NSS_STATUS_SUCCESS;
75 if (*stream == NULL)
77 *stream = fopen (DATAFILE, "rce");
79 if (*stream == NULL)
80 status = errno == EAGAIN ? NSS_STATUS_TRYAGAIN : NSS_STATUS_UNAVAIL;
82 else
83 rewind (*stream);
85 return status;
89 /* Thread-safe, exported version of that. */
90 enum nss_status
91 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
93 enum nss_status status;
95 __libc_lock_lock (lock);
97 status = internal_setent (&stream);
99 __libc_lock_unlock (lock);
101 return status;
105 /* Close the database file. */
106 static void
107 internal_endent (FILE **stream)
109 if (*stream != NULL)
111 fclose (*stream);
112 *stream = NULL;
117 /* Thread-safe, exported version of that. */
118 enum nss_status
119 CONCAT(_nss_files_end,ENTNAME) (void)
121 __libc_lock_lock (lock);
123 internal_endent (&stream);
125 __libc_lock_unlock (lock);
127 return NSS_STATUS_SUCCESS;
131 typedef enum
133 gcr_ok = 0,
134 gcr_error = -1,
135 gcr_overflow = -2
136 } get_contents_ret;
138 /* Hack around the fact that fgets only accepts int sizes. */
139 static get_contents_ret
140 get_contents (char *linebuf, size_t len, FILE *stream)
142 size_t remaining_len = len;
143 char *curbuf = linebuf;
147 int curlen = ((remaining_len > (size_t) INT_MAX) ? INT_MAX
148 : remaining_len);
150 /* Terminate the line so that we can test for overflow. */
151 ((unsigned char *) curbuf)[curlen - 1] = 0xff;
153 char *p = fgets_unlocked (curbuf, curlen, stream);
155 /* EOF or read error. */
156 if (p == NULL)
157 return gcr_error;
159 /* Done reading in the line. */
160 if (((unsigned char *) curbuf)[curlen - 1] == 0xff)
161 return gcr_ok;
163 /* Drop the terminating '\0'. */
164 remaining_len -= curlen - 1;
165 curbuf += curlen - 1;
167 /* fgets copies one less than the input length. Our last iteration is of
168 REMAINING_LEN and once that is done, REMAINING_LEN is decremented by
169 REMAINING_LEN - 1, leaving the result as 1. */
170 while (remaining_len > 1);
172 /* This means that the current buffer was not large enough. */
173 return gcr_overflow;
176 /* Parsing the database file into `struct STRUCTURE' data structures. */
177 static enum nss_status
178 internal_getent (FILE *stream, struct STRUCTURE *result,
179 char *buffer, size_t buflen, int *errnop H_ERRNO_PROTO
180 EXTRA_ARGS_DECL)
182 char *p;
183 struct parser_data *data = (void *) buffer;
184 size_t linebuflen = buffer + buflen - data->linebuffer;
185 int parse_result;
187 if (buflen < sizeof *data + 2)
189 *errnop = ERANGE;
190 H_ERRNO_SET (NETDB_INTERNAL);
191 return NSS_STATUS_TRYAGAIN;
196 get_contents_ret r = get_contents (data->linebuffer, linebuflen, stream);
198 if (r == gcr_error)
200 /* End of file or read error. */
201 H_ERRNO_SET (HOST_NOT_FOUND);
202 return NSS_STATUS_NOTFOUND;
205 if (r == gcr_overflow)
207 /* The line is too long. Give the user the opportunity to
208 enlarge the buffer. */
209 *errnop = ERANGE;
210 H_ERRNO_SET (NETDB_INTERNAL);
211 return NSS_STATUS_TRYAGAIN;
214 /* Everything OK. Now skip leading blanks. */
215 p = data->linebuffer;
216 while (isspace (*p))
217 ++p;
219 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
220 /* Parse the line. If it is invalid, loop to get the next
221 line of the file to parse. */
222 || ! (parse_result = parse_line (p, result, data, buflen, errnop
223 EXTRA_ARGS)));
225 if (__glibc_unlikely (parse_result == -1))
227 H_ERRNO_SET (NETDB_INTERNAL);
228 return NSS_STATUS_TRYAGAIN;
231 /* Filled in RESULT with the next entry from the database file. */
232 return NSS_STATUS_SUCCESS;
236 /* Return the next entry from the database file, doing locking. */
237 enum nss_status
238 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result, char *buffer,
239 size_t buflen, int *errnop H_ERRNO_PROTO)
241 /* Return next entry in host file. */
242 enum nss_status status = NSS_STATUS_SUCCESS;
244 __libc_lock_lock (lock);
246 /* Be prepared that the set*ent function was not called before. */
247 if (stream == NULL)
249 int save_errno = errno;
251 status = internal_setent (&stream);
253 __set_errno (save_errno);
256 if (status == NSS_STATUS_SUCCESS)
257 status = internal_getent (stream, result, buffer, buflen, errnop
258 H_ERRNO_ARG EXTRA_ARGS_VALUE);
260 __libc_lock_unlock (lock);
262 return status;
265 /* Macro for defining lookup functions for this file-based database.
267 NAME is the name of the lookup; e.g. `hostbyname'.
269 DB_CHAR, KEYPATTERN, KEYSIZE are ignored here but used by db-XXX.c
270 e.g. `1 + sizeof (id) * 4'.
272 PROTO is the potentially empty list of other parameters.
274 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
275 to the lookup key arguments and does `break;' if they match. */
277 #define DB_LOOKUP(name, db_char, keysize, keypattern, break_if_match, proto...)\
278 enum nss_status \
279 _nss_files_get##name##_r (proto, \
280 struct STRUCTURE *result, char *buffer, \
281 size_t buflen, int *errnop H_ERRNO_PROTO) \
283 enum nss_status status; \
284 FILE *stream = NULL; \
286 /* Open file. */ \
287 status = internal_setent (&stream); \
289 if (status == NSS_STATUS_SUCCESS) \
291 while ((status = internal_getent (stream, result, buffer, buflen, errnop \
292 H_ERRNO_ARG EXTRA_ARGS_VALUE)) \
293 == NSS_STATUS_SUCCESS) \
294 { break_if_match } \
296 internal_endent (&stream); \
299 return status; \