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)
108 /* Thread-safe, exported version of that. */
110 CONCAT(_nss_files_end,ENTNAME) (void)
112 __libc_lock_lock (lock);
116 /* Reset STAYOPEN flag. */
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 /* Be prepared that the set*ent function was not called before. */
137 enum nss_status status;
139 status = internal_setent (0);
140 if (status != NSS_STATUS_SUCCESS)
144 if (buflen < (int) sizeof *data + 1)
147 return NSS_STATUS_NOTFOUND;
152 p = fgets (data->linebuffer, linebuflen, stream);
155 /* End of file or read error. */
156 H_ERRNO_SET (HOST_NOT_FOUND);
157 return NSS_STATUS_NOTFOUND;
160 /* Terminate the line for any case. */
161 data->linebuffer[linebuflen - 1] = '\0';
163 /* Skip leading blanks. */
166 } while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
167 /* Parse the line. If it is invalid, loop to
168 get the next line of the file to parse. */
169 ! parse_line (p, result, data, buflen));
171 /* Filled in RESULT with the next entry from the database file. */
172 return NSS_STATUS_SUCCESS;
176 /* Return the next entry from the database file, doing locking. */
178 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
179 char *buffer, int buflen H_ERRNO_PROTO)
181 /* Return next entry in host file. */
184 __libc_lock_lock (lock);
186 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
188 __libc_lock_unlock (lock);
193 /* Macro for defining lookup functions for this file-based database.
195 NAME is the name of the lookup; e.g. `hostbyname'.
197 PROTO describes the arguments for the lookup key;
198 e.g. `const char *hostname'.
200 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
201 to the lookup key arguments and does `break;' if they match. */
203 #define DB_LOOKUP(name, break_if_match, proto...) \
205 _nss_files_get##name##_r (proto, \
206 struct STRUCTURE *result, \
207 char *buffer, int buflen H_ERRNO_PROTO) \
209 enum nss_status status; \
211 __libc_lock_lock (lock); \
213 /* Reset file pointer to beginning or open file. */ \
214 internal_setent (keep_stream); \
216 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
217 == NSS_STATUS_SUCCESS) \
221 internal_endent (); \
223 __libc_lock_unlock (lock); \