]> git.ipfire.org Git - thirdparty/glibc.git/blame - nss/nss_files/files-XXX.c
Thu Jun 27 23:43:22 1996 Richard Henderson <rth@tamu.edu>
[thirdparty/glibc.git] / nss / nss_files / files-XXX.c
CommitLineData
5f0e6fc7
RM
1/* Common code for file-based databases in nss_files module.
2Copyright (C) 1996 Free Software Foundation, Inc.
3This file is part of the GNU C Library.
4
5The GNU C Library is free software; you can redistribute it and/or
6modify it under the terms of the GNU Library General Public License as
7published by the Free Software Foundation; either version 2 of the
8License, or (at your option) any later version.
9
10The GNU C Library is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13Library General Public License for more details.
14
15You should have received a copy of the GNU Library General Public
16License along with the GNU C Library; see the file COPYING.LIB. If
17not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18Cambridge, MA 02139, USA. */
19
20#include <stdio.h>
21#include <ctype.h>
22#include <assert.h>
23#include <libc-lock.h>
24#include "nsswitch.h"
25
26/* These symbols are defined by the including source file:
27
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.
31
32 NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
33 MIDLINE_COMMENTS - defined iff # before \n terminates a database line.
34*/
35
36#define ENTNAME_r CONCAT(ENTNAME,_r)
37
38#ifdef NEED_H_ERRNO
39#define H_ERRNO_PROTO , int *herrnop
40#define H_ERRNO_ARG , herrnop
41#define H_ERRNO_SET(val) (*herrnop = (val))
42#else
43#define H_ERRNO_PROTO
44#define H_ERRNO_ARG
45#define H_ERRNO_SET(val) ((void) 0)
46#endif
47
48/* Locks the static variables in this file. */
49__libc_lock_define_initialized (static, lock);
50\f
51/* Maintenance of the shared stream open on the database file. */
52
53static FILE *stream;
54static int keep_stream;
55
56/* Open database file if not already opened. */
57static int
58internal_setent (int stayopen)
59{
60 int status = NSS_STATUS_SUCCESS;
61
62 if (stream == NULL)
63 {
64 stream = fopen (DATAFILE, "r");
65
66 if (stream == NULL)
67 status = NSS_STATUS_UNAVAIL;
68 }
69 else
70 rewind (stream);
71
72 /* Remember STAYOPEN flag. */
73 if (stream != NULL)
74 keep_stream |= stayopen;
75
76 return status;
77}
78
79
80/* Thread-safe, exported version of that. */
81int
82CONCAT(_nss_files_set,ENTNAME) (int stayopen)
83{
84 int status;
85
86 __libc_lock_lock (lock);
87
88 status = internal_setent (stayopen);
89
90 __libc_lock_unlock (lock);
91
92 return status;
93}
94
95
96/* Close the database file. */
97static void
98internal_endent (void)
99{
100 if (stream != NULL)
101 {
102 fclose (stream);
103 stream = NULL;
104 }
105
106 /* Reset STAYOPEN flag. */
107 keep_stream = 0;
108}
109
110
111/* Thread-safe, exported version of that. */
112int
113CONCAT(_nss_files_end,ENTNAME) (void)
114{
115 __libc_lock_lock (lock);
116
117 internal_endent ();
118
119 __libc_lock_unlock (lock);
120
121 return NSS_STATUS_SUCCESS;
122}
123\f
124/* Parsing the database file into `struct STRUCTURE' data structures. */
125
126static enum nss_status
127internal_getent (struct STRUCTURE *result,
128 char *buffer, int buflen H_ERRNO_PROTO)
129{
130 char *p;
131 struct parser_data *data = (void *) buffer;
132 int linebuflen = buffer + buflen - data->linebuffer;
133
134 /* Someone called internal_setent before calling us, so if the
135 stream is not open now the file could not be opened. */
136 if (stream == NULL)
137 {
138 H_ERRNO_SET (NETDB_INTERNAL);
139 return NSS_STATUS_UNAVAIL;
140 }
141
142 if (buflen < sizeof *data + 1)
143 {
144 errno = ERANGE;
145 return NSS_STATUS_NOTFOUND;
146 }
147
148 do
149 {
150 p = fgets (data->linebuffer, linebuflen, stream);
151 if (p == NULL)
152 {
153 /* End of file or read error. */
154 H_ERRNO_SET (HOST_NOT_FOUND);
155 return NSS_STATUS_NOTFOUND;
156 }
157
158 /* Skip leading blanks. */
159 while (isspace (*p))
160 ++p;
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));
165
166 /* Filled in RESULT with the next entry from the database file. */
167 return NSS_STATUS_SUCCESS;
168}
169
170
171/* Return the next entry from the database file, doing locking. */
172int
173CONCAT(_nss_files_get,ENTNAME_r) (struct STRUCTURE *result,
174 char *buffer, int buflen H_ERRNO_PROTO)
175{
176 /* Return next entry in host file. */
177 int status;
178
179 __libc_lock_lock (lock);
180
181 status = internal_getent (result, buffer, buflen H_ERRNO_ARG);
182
183 __libc_lock_unlock (lock);
184
185 return status;
186}
187\f
188/* Macro for defining lookup functions for this file-based database.
189
190 NAME is the name of the lookup; e.g. `hostbyname'.
191
192 PROTO describes the arguments for the lookup key;
193 e.g. `const char *hostname'.
194
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. */
197
198#define DB_LOOKUP(name, break_if_match, proto...) \
199enum nss_status \
200_nss_files_get##name##_r (proto, \
201 struct STRUCTURE *result, \
202 char *buffer, int buflen H_ERRNO_PROTO) \
203{ \
204 enum nss_status status; \
205 \
206 __libc_lock_lock (lock); \
207 \
208 /* Reset file pointer to beginning or open file. */ \
209 internal_setent (keep_stream); \
210 \
211 while ((status = internal_getent (result, buffer, buflen H_ERRNO_ARG)) \
212 == NSS_STATUS_SUCCESS) \
213 { break_if_match } \
214 \
215 if (! keep_stream) \
216 internal_endent (); \
217 \
218 __libc_lock_unlock (lock); \
219 \
220 return status; \
221}