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 DATABASE -- string of the database file's name ("hosts", "passwd").
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)
38 #define DATAFILE "/etc/" DATABASE
41 #define H_ERRNO_PROTO , int *herrnop
42 #define H_ERRNO_ARG , herrnop
43 #define H_ERRNO_SET(val) (*herrnop = (val))
47 #define H_ERRNO_SET(val) ((void) 0)
50 /* Locks the static variables in this file. */
51 __libc_lock_define_initialized (static, lock);
53 /* Maintenance of the shared stream open on the database file. */
56 static int keep_stream;
58 /* Open database file if not already opened. */
60 internal_setent (int stayopen)
62 int status = NSS_STATUS_SUCCESS;
66 stream = fopen (DATAFILE, "r");
69 status = NSS_STATUS_UNAVAIL;
74 /* Remember STAYOPEN flag. */
76 keep_stream |= stayopen;
82 /* Thread-safe, exported version of that. */
84 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
88 __libc_lock_lock (lock);
90 status = internal_setent (stayopen);
92 __libc_lock_unlock (lock);
98 /* Close the database file. */
100 internal_endent (void)
110 /* Thread-safe, exported version of that. */
112 CONCAT(_nss_files_end,ENTNAME) (void)
114 __libc_lock_lock (lock);
118 /* Reset STAYOPEN flag. */
121 __libc_lock_unlock (lock);
123 return NSS_STATUS_SUCCESS;
126 /* Parsing the database file into `struct STRUCTURE' data structures. */
128 static enum nss_status
129 internal_getent (struct STRUCTURE *result,
130 char *buffer, int buflen H_ERRNO_PROTO)
133 struct parser_data *data = (void *) buffer;
134 int linebuflen = buffer + buflen - data->linebuffer;
136 /* Be prepared that the set*ent function was not called before. */
139 enum nss_status status;
141 status = internal_setent (0);
142 if (status != NSS_STATUS_SUCCESS)
146 if (buflen < (int) sizeof *data + 1)
149 return NSS_STATUS_TRYAGAIN;
154 p = fgets (data->linebuffer, linebuflen, stream);
157 /* End of file or read error. */
158 H_ERRNO_SET (HOST_NOT_FOUND);
159 return NSS_STATUS_NOTFOUND;
162 /* Terminate the line for any case. */
163 data->linebuffer[linebuflen - 1] = '\0';
165 /* Skip leading blanks. */
168 } while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
169 /* Parse the line. If it is invalid, loop to
170 get the next line of the file to parse. */
171 ! parse_line (p, result, data, buflen));
173 /* Filled in RESULT with the next entry from the database file. */
174 return NSS_STATUS_SUCCESS;
178 /* Return the next entry from the database file, doing locking. */
180 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
181 char *buffer, int buflen H_ERRNO_PROTO)
183 /* Return next entry in host file. */
186 __libc_lock_lock (lock);
188 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
190 __libc_lock_unlock (lock);
195 /* Macro for defining lookup functions for this file-based database.
197 NAME is the name of the lookup; e.g. `hostbyname'.
199 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
201 PROTO describes the arguments for the lookup key;
202 e.g. `const char *hostname'.
204 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
205 to the lookup key arguments and does `break;' if they match. */
207 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
209 _nss_files_get##name##_r (proto, \
210 struct STRUCTURE *result, \
211 char *buffer, int buflen H_ERRNO_PROTO) \
213 enum nss_status status; \
215 __libc_lock_lock (lock); \
217 /* Reset file pointer to beginning or open file. */ \
218 internal_setent (keep_stream); \
220 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
221 == NSS_STATUS_SUCCESS) \
225 internal_endent (); \
227 __libc_lock_unlock (lock); \