Update.
[glibc.git] / nscd / nscd_conf.c
blob6ac1677a8bfcdd5ec50297f99fd86b77baabe83b
1 /* Copyright (c) 1998 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.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 <sys/param.h>
26 #include <sys/types.h>
28 #include "dbg_log.h"
29 #include "nscd.h"
31 /* Names of the databases. */
32 const char *dbnames[lastdb] =
34 [pwddb] = "passwd",
35 [grpdb] = "group",
36 [hstdb] = "hosts"
39 int
40 nscd_parse_file (const char *fname, struct database dbs[lastdb])
42 FILE *fp;
43 char *line, *cp, *entry, *arg1, *arg2;
44 size_t len;
45 int cnt;
47 /* Open the configuration file. */
48 fp = fopen (fname, "r");
49 if (fp == NULL)
50 return -1;
52 line = NULL;
53 len = 0;
57 ssize_t n = getline (&line, &len, fp);
58 if (n < 0)
59 break;
60 if (line[n - 1] == '\n')
61 line[n - 1] = '\0';
63 /* Because the file format does not know any form of quoting we
64 can search forward for the next '#' character and if found
65 make it terminating the line. */
66 cp = strchr (line, '#');
67 if (cp != NULL)
68 *cp = '\0';
70 /* If the line is blank it is ignored. */
71 if (line[0] == '\0')
72 continue;
74 entry = line;
75 while (isspace (*entry) && *entry != '\0')
76 ++entry;
77 cp = entry;
78 while (!isspace (*cp) && *cp != '\0')
79 ++cp;
80 arg1 = cp;
81 ++arg1;
82 *cp = '\0';
83 if (strlen (entry) == 0)
84 dbg_log (_("Parse error: %s"), line);
85 while (isspace (*arg1) && *arg1 != '\0')
86 ++arg1;
87 cp = arg1;
88 while (!isspace (*cp) && *cp != '\0')
89 ++cp;
90 arg2 = cp;
91 ++arg2;
92 *cp = '\0';
93 if (strlen (arg2) > 0)
95 while (isspace (*arg2) && *arg2 != '\0')
96 ++arg2;
97 cp = arg2;
98 while (!isspace (*cp) && *cp != '\0')
99 ++cp;
100 *cp = '\0';
103 if (strcmp (entry, "positive-time-to-live") == 0)
105 for (cnt = 0; cnt < lastdb; ++cnt)
106 if (strcmp (arg1, dbnames[cnt]) == 0)
108 dbs[cnt].postimeout = atol (arg2);
109 break;
111 if (cnt == lastdb)
112 dbg_log ("server %s is not supported\n", arg1);
114 else if (strcmp (entry, "negative-time-to-live") == 0)
116 for (cnt = 0; cnt < lastdb; ++cnt)
117 if (strcmp (arg1, dbnames[cnt]) == 0)
119 dbs[cnt].negtimeout = atol (arg2);
120 break;
122 if (cnt == lastdb)
123 dbg_log ("server %s is not supported\n", arg1);
125 else if (strcmp (entry, "suggested-size") == 0)
127 for (cnt = 0; cnt < lastdb; ++cnt)
128 if (strcmp (arg1, dbnames[cnt]) == 0)
130 dbs[cnt].module = atol (arg2);
131 break;
133 if (cnt == lastdb)
134 dbg_log ("server %s is not supported\n", arg1);
136 else if (strcmp (entry, "enable-cache") == 0)
138 for (cnt = 0; cnt < lastdb; ++cnt)
139 if (strcmp (arg1, dbnames[cnt]) == 0)
141 if (strcmp (arg2, "no") == 0)
142 dbs[cnt].enabled = 0;
143 else if (strcmp (arg2, "yes") == 0)
144 dbs[cnt].enabled = 1;
145 break;
147 if (cnt == lastdb)
148 dbg_log ("server %s is not supported\n", arg1);
150 else if (strcmp (entry, "check-files") == 0)
152 for (cnt = 0; cnt < lastdb; ++cnt)
153 if (strcmp (arg1, dbnames[cnt]) == 0)
155 if (strcmp (arg2, "no") == 0)
156 dbs[cnt].check_file = 0;
157 else if (strcmp (arg2, "yes") == 0)
158 dbs[cnt].check_file = 1;
159 break;
161 if (cnt == lastdb)
162 dbg_log ("server %s is not supported\n", arg1);
164 else if (strcmp (entry, "logfile") == 0)
166 if (!set_logfile (arg1))
167 dbg_log (_("Could not create log file \"%s\""), arg1);
169 else if (strcmp (entry, "debug-level") == 0)
171 int level = atoi (arg1);
172 if (level > 0)
173 debug_level = level;
175 else if (strcmp (entry, "threads") == 0)
177 if (nthreads == -1)
178 nthreads = MAX (atol (arg1), lastdb);
180 else
181 dbg_log (_("Unknown option: %s %s %s"), entry, arg1, arg2);
183 while (!feof (fp));
185 /* Free the buffer. */
186 free (line);
187 /* Close configuration file. */
188 fclose (fp);
190 return 0;