Update.
[glibc.git] / nscd / nscd_conf.c
blob9b8d75ce23f6bd796e467fa07c1604e8f855a015
1 /* Copyright (c) 1998, 2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1998.
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 <ctype.h>
21 #include <malloc.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <libintl.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
29 #include "dbg_log.h"
30 #include "nscd.h"
32 /* Names of the databases. */
33 const char *dbnames[lastdb] =
35 [pwddb] = "passwd",
36 [grpdb] = "group",
37 [hstdb] = "hosts"
40 int
41 nscd_parse_file (const char *fname, struct database dbs[lastdb])
43 FILE *fp;
44 char *line, *cp, *entry, *arg1, *arg2;
45 size_t len;
46 int cnt;
48 /* Open the configuration file. */
49 fp = fopen (fname, "r");
50 if (fp == NULL)
51 return -1;
53 line = NULL;
54 len = 0;
58 ssize_t n = getline (&line, &len, fp);
59 if (n < 0)
60 break;
61 if (line[n - 1] == '\n')
62 line[n - 1] = '\0';
64 /* Because the file format does not know any form of quoting we
65 can search forward for the next '#' character and if found
66 make it terminating the line. */
67 *strchrnul (line, '#') = '\0';
69 /* If the line is blank it is ignored. */
70 if (line[0] == '\0')
71 continue;
73 entry = line;
74 while (isspace (*entry) && *entry != '\0')
75 ++entry;
76 cp = entry;
77 while (!isspace (*cp) && *cp != '\0')
78 ++cp;
79 arg1 = cp;
80 ++arg1;
81 *cp = '\0';
82 if (strlen (entry) == 0)
83 dbg_log (_("Parse error: %s"), line);
84 while (isspace (*arg1) && *arg1 != '\0')
85 ++arg1;
86 cp = arg1;
87 while (!isspace (*cp) && *cp != '\0')
88 ++cp;
89 arg2 = cp;
90 ++arg2;
91 *cp = '\0';
92 if (strlen (arg2) > 0)
94 while (isspace (*arg2) && *arg2 != '\0')
95 ++arg2;
96 cp = arg2;
97 while (!isspace (*cp) && *cp != '\0')
98 ++cp;
99 *cp = '\0';
102 if (strcmp (entry, "positive-time-to-live") == 0)
104 for (cnt = 0; cnt < lastdb; ++cnt)
105 if (strcmp (arg1, dbnames[cnt]) == 0)
107 dbs[cnt].postimeout = atol (arg2);
108 break;
110 if (cnt == lastdb)
111 dbg_log ("server %s is not supported\n", arg1);
113 else if (strcmp (entry, "negative-time-to-live") == 0)
115 for (cnt = 0; cnt < lastdb; ++cnt)
116 if (strcmp (arg1, dbnames[cnt]) == 0)
118 dbs[cnt].negtimeout = atol (arg2);
119 break;
121 if (cnt == lastdb)
122 dbg_log ("server %s is not supported\n", arg1);
124 else if (strcmp (entry, "suggested-size") == 0)
126 for (cnt = 0; cnt < lastdb; ++cnt)
127 if (strcmp (arg1, dbnames[cnt]) == 0)
129 dbs[cnt].module = atol (arg2);
130 break;
132 if (cnt == lastdb)
133 dbg_log ("server %s is not supported\n", arg1);
135 else if (strcmp (entry, "enable-cache") == 0)
137 for (cnt = 0; cnt < lastdb; ++cnt)
138 if (strcmp (arg1, dbnames[cnt]) == 0)
140 if (strcmp (arg2, "no") == 0)
141 dbs[cnt].enabled = 0;
142 else if (strcmp (arg2, "yes") == 0)
143 dbs[cnt].enabled = 1;
144 break;
146 if (cnt == lastdb)
147 dbg_log ("server %s is not supported\n", arg1);
149 else if (strcmp (entry, "check-files") == 0)
151 for (cnt = 0; cnt < lastdb; ++cnt)
152 if (strcmp (arg1, dbnames[cnt]) == 0)
154 if (strcmp (arg2, "no") == 0)
155 dbs[cnt].check_file = 0;
156 else if (strcmp (arg2, "yes") == 0)
157 dbs[cnt].check_file = 1;
158 break;
160 if (cnt == lastdb)
161 dbg_log ("server %s is not supported\n", arg1);
163 else if (strcmp (entry, "logfile") == 0)
165 if (!set_logfile (arg1))
166 dbg_log (_("Could not create log file \"%s\""), arg1);
168 else if (strcmp (entry, "debug-level") == 0)
170 int level = atoi (arg1);
171 if (level > 0)
172 debug_level = level;
174 else if (strcmp (entry, "threads") == 0)
176 if (nthreads == -1)
177 nthreads = MAX (atol (arg1), lastdb);
179 else if (strcmp (entry, "server-user") == 0)
181 if (!arg1)
182 dbg_log (_("Must specify user name for server-user option"), arg1);
183 else
184 server_user = strdup (arg1);
186 else
187 dbg_log (_("Unknown option: %s %s %s"), entry, arg1, arg2);
189 while (!feof (fp));
191 /* Free the buffer. */
192 free (line);
193 /* Close configuration file. */
194 fclose (fp);
196 return 0;