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.
34 Also see files-parse.c.
37 #define ENTNAME_r CONCAT(ENTNAME,_r)
39 #define DATAFILE "/etc/" DATABASE
42 #define H_ERRNO_PROTO , int *herrnop
43 #define H_ERRNO_ARG , herrnop
44 #define H_ERRNO_SET(val) (*herrnop = (val))
48 #define H_ERRNO_SET(val) ((void) 0)
51 /* Locks the static variables in this file. */
52 __libc_lock_define_initialized (static, lock)
54 /* Maintenance of the shared stream open on the database file. */
57 static int keep_stream;
59 /* Open database file if not already opened. */
61 internal_setent (int stayopen)
63 int status = NSS_STATUS_SUCCESS;
67 stream = fopen (DATAFILE, "r");
70 status = NSS_STATUS_UNAVAIL;
75 /* Remember STAYOPEN flag. */
77 keep_stream |= stayopen;
83 /* Thread-safe, exported version of that. */
85 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
89 __libc_lock_lock (lock);
91 status = internal_setent (stayopen);
93 __libc_lock_unlock (lock);
99 /* Close the database file. */
101 internal_endent (void)
111 /* Thread-safe, exported version of that. */
113 CONCAT(_nss_files_end,ENTNAME) (void)
115 __libc_lock_lock (lock);
119 /* Reset STAYOPEN flag. */
122 __libc_lock_unlock (lock);
124 return NSS_STATUS_SUCCESS;
127 /* Parsing the database file into `struct STRUCTURE' data structures. */
129 static enum nss_status
130 internal_getent (struct STRUCTURE *result,
131 char *buffer, int buflen H_ERRNO_PROTO)
134 struct parser_data *data = (void *) buffer;
135 int linebuflen = buffer + buflen - data->linebuffer;
137 /* Be prepared that the set*ent function was not called before. */
140 enum nss_status status;
142 status = internal_setent (0);
143 if (status != NSS_STATUS_SUCCESS)
147 if (buflen < (int) sizeof *data + 1)
149 __set_errno (ERANGE);
150 return NSS_STATUS_TRYAGAIN;
155 p = fgets (data->linebuffer, linebuflen, stream);
158 /* End of file or read error. */
159 H_ERRNO_SET (HOST_NOT_FOUND);
160 return NSS_STATUS_NOTFOUND;
163 /* Terminate the line for any case. */
164 data->linebuffer[linebuflen - 1] = '\0';
166 /* Skip leading blanks. */
169 } while (*p == '\0' || *p == '#' || /* Ignore empty and comment lines. */
170 /* Parse the line. If it is invalid, loop to
171 get the next line of the file to parse. */
172 ! parse_line (p, result, data, buflen));
174 /* Filled in RESULT with the next entry from the database file. */
175 return NSS_STATUS_SUCCESS;
179 /* Return the next entry from the database file, doing locking. */
181 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
182 char *buffer, int buflen H_ERRNO_PROTO)
184 /* Return next entry in host file. */
187 __libc_lock_lock (lock);
189 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
191 __libc_lock_unlock (lock);
196 /* Macro for defining lookup functions for this file-based database.
198 NAME is the name of the lookup; e.g. `hostbyname'.
200 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
202 PROTO describes the arguments for the lookup key;
203 e.g. `const char *hostname'.
205 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
206 to the lookup key arguments and does `break;' if they match. */
208 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
210 _nss_files_get##name##_r (proto, \
211 struct STRUCTURE *result, \
212 char *buffer, int buflen H_ERRNO_PROTO) \
214 enum nss_status status; \
216 __libc_lock_lock (lock); \
218 /* Reset file pointer to beginning or open file. */ \
219 internal_setent (keep_stream); \
221 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
222 == NSS_STATUS_SUCCESS) \
226 internal_endent (); \
228 __libc_lock_unlock (lock); \