update from main archive 961113
[glibc.git] / nss / nss_files / files-XXX.c
blobc741ab6f48fbad041a1c219de73809acd1da97fe
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 not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <stdio.h>
21 #include <ctype.h>
22 #include <assert.h>
23 #include <errno.h>
24 #include <libc-lock.h>
25 #include "nsswitch.h"
27 /* These symbols are defined by the including source file:
29 ENTNAME -- database name of the structure and functions (hostent, pwent).
30 STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
31 DATABASE -- string of the database file's name ("hosts", "passwd").
33 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
35 Also see files-parse.c.
38 #define ENTNAME_r CONCAT(ENTNAME,_r)
40 #define DATAFILE "/etc/" DATABASE
42 #ifdef NEED_H_ERRNO
43 # define H_ERRNO_PROTO , int *herrnop
44 # define H_ERRNO_ARG , herrnop
45 # define H_ERRNO_SET(val) (*herrnop = (val))
46 #else
47 # define H_ERRNO_PROTO
48 # define H_ERRNO_ARG
49 # define H_ERRNO_SET(val) ((void) 0)
50 #endif
52 /* Locks the static variables in this file. */
53 __libc_lock_define_initialized (static, lock)
55 /* Maintenance of the shared stream open on the database file. */
57 static FILE *stream;
58 static fpos_t position;
59 static enum { none, getent, getby } last_use;
60 static int keep_stream;
62 /* Open database file if not already opened. */
63 static enum nss_status
64 internal_setent (int stayopen)
66 enum nss_status status = NSS_STATUS_SUCCESS;
68 if (stream == NULL)
70 stream = fopen (DATAFILE, "r");
72 if (stream == NULL)
73 status = NSS_STATUS_UNAVAIL;
75 else
76 rewind (stream);
78 /* Remember STAYOPEN flag. */
79 if (stream != NULL)
80 keep_stream |= stayopen;
82 return status;
86 /* Thread-safe, exported version of that. */
87 enum nss_status
88 CONCAT(_nss_files_set,ENTNAME) (int stayopen)
90 enum nss_status status;
92 __libc_lock_lock (lock);
94 status = internal_setent (stayopen);
96 if (status == NSS_STATUS_SUCCESS && fgetpos (stream, &position) < 0)
98 fclose (stream);
99 stream = NULL;
100 status = NSS_STATUS_UNAVAIL;
103 last_use = getent;
105 __libc_lock_unlock (lock);
107 return status;
111 /* Close the database file. */
112 static void
113 internal_endent (void)
115 if (stream != NULL)
117 fclose (stream);
118 stream = NULL;
123 /* Thread-safe, exported version of that. */
124 enum nss_status
125 CONCAT(_nss_files_end,ENTNAME) (void)
127 __libc_lock_lock (lock);
129 internal_endent ();
131 /* Reset STAYOPEN flag. */
132 keep_stream = 0;
134 __libc_lock_unlock (lock);
136 return NSS_STATUS_SUCCESS;
139 /* Parsing the database file into `struct STRUCTURE' data structures. */
141 static enum nss_status
142 internal_getent (struct STRUCTURE *result,
143 char *buffer, int buflen H_ERRNO_PROTO)
145 char *p;
146 struct parser_data *data = (void *) buffer;
147 int linebuflen = buffer + buflen - data->linebuffer;
149 if (buflen < (int) sizeof *data + 1)
151 __set_errno (ERANGE);
152 return NSS_STATUS_TRYAGAIN;
157 /* Terminate the line so that we can test for overflow. */
158 data->linebuffer[linebuflen - 1] = '\0';
160 p = fgets (data->linebuffer, linebuflen, stream);
161 if (p == NULL)
163 /* End of file or read error. */
164 H_ERRNO_SET (HOST_NOT_FOUND);
165 return NSS_STATUS_NOTFOUND;
167 else if (data->linebuffer[linebuflen - 1] != '\0')
169 /* The line is too long. Give the user the opportunity to
170 enlarge the buffer. */
171 __set_errno (ERANGE);
172 H_ERRNO_SET (NETDB_INTERNAL);
173 return NSS_STATUS_TRYAGAIN;
176 /* Skip leading blanks. */
177 while (isspace (*p))
178 ++p;
180 while (*p == '\0' || *p == '#' /* Ignore empty and comment lines. */
181 /* Parse the line. If it is invalid, loop to get the next
182 line of the file to parse. */
183 || ! parse_line (p, result, data, buflen));
185 /* Filled in RESULT with the next entry from the database file. */
186 return NSS_STATUS_SUCCESS;
190 /* Return the next entry from the database file, doing locking. */
191 enum nss_status
192 CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
193 char *buffer, size_t buflen H_ERRNO_PROTO)
195 /* Return next entry in host file. */
196 enum nss_status status = NSS_STATUS_SUCCESS;
198 __libc_lock_lock (lock);
200 /* Be prepared that the set*ent function was not called before. */
201 if (stream == NULL)
202 status = internal_setent (0);
204 if (status != NSS_STATUS_SUCCESS)
206 /* If the last use was not by the getent function we need the
207 position the stream. */
208 if (last_use != getent)
209 if (fsetpos (stream, &position) < 0)
210 status = NSS_STATUS_UNAVAIL;
211 else
212 last_use = getent;
214 if (status == NSS_STATUS_SUCCESS)
216 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
218 /* Remember this position if we were successful. If the
219 operation failed we give the user a chance to repeat the
220 operation (perhaps the buffer was too small). */
221 if (status == NSS_STATUS_SUCCESS)
222 fgetpos (stream, &position);
223 else
224 /* We must make sure we reposition the stream the next call. */
225 last_use = none;
229 __libc_lock_unlock (lock);
231 return status;
234 /* Macro for defining lookup functions for this file-based database.
236 NAME is the name of the lookup; e.g. `hostbyname'.
238 KEYSIZE and KEYPATTERN are ignored here but used by ../nss_db/db-XXX.c.
240 PROTO describes the arguments for the lookup key;
241 e.g. `const char *hostname'.
243 BREAK_IF_MATCH is a block of code which compares `struct STRUCTURE *result'
244 to the lookup key arguments and does `break;' if they match. */
246 #define DB_LOOKUP(name, keysize, keypattern, break_if_match, proto...) \
247 enum nss_status \
248 _nss_files_get##name##_r (proto, \
249 struct STRUCTURE *result, \
250 char *buffer, size_t buflen H_ERRNO_PROTO) \
252 enum nss_status status; \
254 __libc_lock_lock (lock); \
256 /* Reset file pointer to beginning or open file. */ \
257 status = internal_setent (keep_stream); \
259 if (status == NSS_STATUS_SUCCESS) \
261 /* Tell getent function that we have repositioned the file pointer. */ \
262 last_use = getby; \
264 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
265 == NSS_STATUS_SUCCESS) \
266 { break_if_match } \
268 if (! keep_stream) \
269 internal_endent (); \
272 __libc_lock_unlock (lock); \
274 return status; \