Update.
[glibc.git] / nscd / nscd_conf.c
blobd31e2bde3a788f819a2c75967b63887b954e01ef
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 *strchrnul (line, '#') = '\0';
68 /* If the line is blank it is ignored. */
69 if (line[0] == '\0')
70 continue;
72 entry = line;
73 while (isspace (*entry) && *entry != '\0')
74 ++entry;
75 cp = entry;
76 while (!isspace (*cp) && *cp != '\0')
77 ++cp;
78 arg1 = cp;
79 ++arg1;
80 *cp = '\0';
81 if (strlen (entry) == 0)
82 dbg_log (_("Parse error: %s"), line);
83 while (isspace (*arg1) && *arg1 != '\0')
84 ++arg1;
85 cp = arg1;
86 while (!isspace (*cp) && *cp != '\0')
87 ++cp;
88 arg2 = cp;
89 ++arg2;
90 *cp = '\0';
91 if (strlen (arg2) > 0)
93 while (isspace (*arg2) && *arg2 != '\0')
94 ++arg2;
95 cp = arg2;
96 while (!isspace (*cp) && *cp != '\0')
97 ++cp;
98 *cp = '\0';
101 if (strcmp (entry, "positive-time-to-live") == 0)
103 for (cnt = 0; cnt < lastdb; ++cnt)
104 if (strcmp (arg1, dbnames[cnt]) == 0)
106 dbs[cnt].postimeout = atol (arg2);
107 break;
109 if (cnt == lastdb)
110 dbg_log ("server %s is not supported\n", arg1);
112 else if (strcmp (entry, "negative-time-to-live") == 0)
114 for (cnt = 0; cnt < lastdb; ++cnt)
115 if (strcmp (arg1, dbnames[cnt]) == 0)
117 dbs[cnt].negtimeout = atol (arg2);
118 break;
120 if (cnt == lastdb)
121 dbg_log ("server %s is not supported\n", arg1);
123 else if (strcmp (entry, "suggested-size") == 0)
125 for (cnt = 0; cnt < lastdb; ++cnt)
126 if (strcmp (arg1, dbnames[cnt]) == 0)
128 dbs[cnt].module = atol (arg2);
129 break;
131 if (cnt == lastdb)
132 dbg_log ("server %s is not supported\n", arg1);
134 else if (strcmp (entry, "enable-cache") == 0)
136 for (cnt = 0; cnt < lastdb; ++cnt)
137 if (strcmp (arg1, dbnames[cnt]) == 0)
139 if (strcmp (arg2, "no") == 0)
140 dbs[cnt].enabled = 0;
141 else if (strcmp (arg2, "yes") == 0)
142 dbs[cnt].enabled = 1;
143 break;
145 if (cnt == lastdb)
146 dbg_log ("server %s is not supported\n", arg1);
148 else if (strcmp (entry, "check-files") == 0)
150 for (cnt = 0; cnt < lastdb; ++cnt)
151 if (strcmp (arg1, dbnames[cnt]) == 0)
153 if (strcmp (arg2, "no") == 0)
154 dbs[cnt].check_file = 0;
155 else if (strcmp (arg2, "yes") == 0)
156 dbs[cnt].check_file = 1;
157 break;
159 if (cnt == lastdb)
160 dbg_log ("server %s is not supported\n", arg1);
162 else if (strcmp (entry, "logfile") == 0)
164 if (!set_logfile (arg1))
165 dbg_log (_("Could not create log file \"%s\""), arg1);
167 else if (strcmp (entry, "debug-level") == 0)
169 int level = atoi (arg1);
170 if (level > 0)
171 debug_level = level;
173 else if (strcmp (entry, "threads") == 0)
175 if (nthreads == -1)
176 nthreads = MAX (atol (arg1), lastdb);
178 else
179 dbg_log (_("Unknown option: %s %s %s"), entry, arg1, arg2);
181 while (!feof (fp));
183 /* Free the buffer. */
184 free (line);
185 /* Close configuration file. */
186 fclose (fp);
188 return 0;