1 /* Common code for file-based databases in nss_files module.
2 Copyright (C) 1996 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 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., 675 Mass Ave,
18 Cambridge, MA 02139, USA. */
23 #include <libc-lock.h>
26 /* These symbols are defined by the including source file:
28 ENTNAME -- database name of the structure and functions (hostent, pwent).
29 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
30 DATAFILE -- string of the database file's name.
32 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
33 MIDLINE_COMMENTS - defined iff # before \n terminates a database line.
36 #define ENTNAME_r CONCAT(ENTNAME,_r)
39 #define H_ERRNO_PROTO , int *herrnop
40 #define H_ERRNO_ARG , herrnop
41 #define H_ERRNO_SET(val) (*herrnop = (val))
45 #define H_ERRNO_SET(val) ((void) 0)
48 /* Locks the static variables in this file. */
49 __libc_lock_define_initialized (static, lock);
51 /* Maintenance of the shared stream open on the database file. */
54 static int keep_stream;
56 /* Open database file if not already opened. */
58 internal_setent (int stayopen)
60 int status = NSS_STATUS_SUCCESS;
64 stream = fopen (DATAFILE, "r");
67 status = NSS_STATUS_UNAVAIL;
72 /* Remember STAYOPEN flag. */
74 keep_stream |= stayopen;
80 /* Thread-safe, exported version of that. */
82 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
86 __libc_lock_lock (lock);
88 status = internal_setent (stayopen);
90 __libc_lock_unlock (lock);
96 /* Close the database file. */
98 internal_endent (void)
106 /* Reset STAYOPEN flag. */
111 /* Thread-safe, exported version of that. */
113 CONCAT(_nss_files_end,ENTNAME) (void)
115 __libc_lock_lock (lock);
119 __libc_lock_unlock (lock);
121 return NSS_STATUS_SUCCESS;
124 /* Parsing the database file into `struct STRUCTURE' data structures. */
126 static enum nss_status
127 internal_getent (struct STRUCTURE *result,
128 char *buffer, int buflen H_ERRNO_PROTO)
131 struct parser_data *data = (void *) buffer;
132 int linebuflen = buffer + buflen - data->linebuffer;
134 /* Someone called internal_setent before calling us, so if the
135 stream is not open now the file could not be opened. */
138 H_ERRNO_SET (NETDB_INTERNAL);
139 return NSS_STATUS_UNAVAIL;
142 if (buflen < sizeof *data + 1)
145 return NSS_STATUS_NOTFOUND;
150 p = fgets (data->linebuffer, linebuflen, stream);
153 /* End of file or read error. */
154 H_ERRNO_SET (HOST_NOT_FOUND);
155 return NSS_STATUS_NOTFOUND;
158 /* Skip leading blanks. */
161 } while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
162 /* Parse the line. If it is invalid, loop to
163 get the next line of the file to parse. */
164 ! parse_line (p, result, data, buflen));
166 /* Filled in RESULT with the next entry from the database file. */
167 return NSS_STATUS_SUCCESS;
171 /* Return the next entry from the database file, doing locking. */
173 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
174 char *buffer, int buflen H_ERRNO_PROTO)
176 /* Return next entry in host file. */
179 __libc_lock_lock (lock);
181 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
183 __libc_lock_unlock (lock);
188 /* Macro for defining lookup functions for this file-based database.
190 NAME is the name of the lookup; e.g. `hostbyname'.
192 PROTO describes the arguments for the lookup key;
193 e.g. `const char *hostname'.
195 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
196 to the lookup key arguments and does `break;' if they match. */
198 #define DB_LOOKUP(name, break_if_match, proto...) \
200 _nss_files_get##name##_r (proto, \
201 struct STRUCTURE *result, \
202 char *buffer, int buflen H_ERRNO_PROTO) \
204 enum nss_status status; \
206 __libc_lock_lock (lock); \
208 /* Reset file pointer to beginning or open file. */ \
209 internal_setent (keep_stream); \
211 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
212 == NSS_STATUS_SUCCESS) \
216 internal_endent (); \
218 __libc_lock_unlock (lock); \