(sem_timedwait): Fix a typo.
[glibc/pb-stable.git] / nscd / nscd_conf.c
blob23c28ceef01197e6da7815facf93ad72b93d50a7
1 /* Copyright (c) 1998, 2000, 2003 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <ctype.h>
21 #include <malloc.h>
22 #include <pwd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <libintl.h>
27 #include <sys/param.h>
28 #include <sys/types.h>
30 #include "dbg_log.h"
31 #include "nscd.h"
33 /* Wrapper functions with error checking for standard functions. */
34 extern char *xstrdup (const char *s);
37 /* Names of the databases. */
38 const char *dbnames[lastdb] =
40 [pwddb] = "passwd",
41 [grpdb] = "group",
42 [hstdb] = "hosts"
45 int
46 nscd_parse_file (const char *fname, struct database dbs[lastdb])
48 FILE *fp;
49 char *line, *cp, *entry, *arg1, *arg2;
50 size_t len;
51 int cnt;
53 /* Open the configuration file. */
54 fp = fopen (fname, "r");
55 if (fp == NULL)
56 return -1;
58 line = NULL;
59 len = 0;
63 ssize_t n = getline (&line, &len, fp);
64 if (n < 0)
65 break;
66 if (line[n - 1] == '\n')
67 line[n - 1] = '\0';
69 /* Because the file format does not know any form of quoting we
70 can search forward for the next '#' character and if found
71 make it terminating the line. */
72 *strchrnul (line, '#') = '\0';
74 /* If the line is blank it is ignored. */
75 if (line[0] == '\0')
76 continue;
78 entry = line;
79 while (isspace (*entry) && *entry != '\0')
80 ++entry;
81 cp = entry;
82 while (!isspace (*cp) && *cp != '\0')
83 ++cp;
84 arg1 = cp;
85 ++arg1;
86 *cp = '\0';
87 if (strlen (entry) == 0)
88 dbg_log (_("Parse error: %s"), line);
89 while (isspace (*arg1) && *arg1 != '\0')
90 ++arg1;
91 cp = arg1;
92 while (!isspace (*cp) && *cp != '\0')
93 ++cp;
94 arg2 = cp;
95 ++arg2;
96 *cp = '\0';
97 if (strlen (arg2) > 0)
99 while (isspace (*arg2) && *arg2 != '\0')
100 ++arg2;
101 cp = arg2;
102 while (!isspace (*cp) && *cp != '\0')
103 ++cp;
104 *cp = '\0';
107 if (strcmp (entry, "positive-time-to-live") == 0)
109 for (cnt = 0; cnt < lastdb; ++cnt)
110 if (strcmp (arg1, dbnames[cnt]) == 0)
112 dbs[cnt].postimeout = atol (arg2);
113 break;
115 if (cnt == lastdb)
116 dbg_log ("server %s is not supported\n", arg1);
118 else if (strcmp (entry, "negative-time-to-live") == 0)
120 for (cnt = 0; cnt < lastdb; ++cnt)
121 if (strcmp (arg1, dbnames[cnt]) == 0)
123 dbs[cnt].negtimeout = atol (arg2);
124 break;
126 if (cnt == lastdb)
127 dbg_log ("server %s is not supported\n", arg1);
129 else if (strcmp (entry, "suggested-size") == 0)
131 for (cnt = 0; cnt < lastdb; ++cnt)
132 if (strcmp (arg1, dbnames[cnt]) == 0)
134 dbs[cnt].module = atol (arg2);
135 break;
137 if (cnt == lastdb)
138 dbg_log ("server %s is not supported\n", arg1);
140 else if (strcmp (entry, "enable-cache") == 0)
142 for (cnt = 0; cnt < lastdb; ++cnt)
143 if (strcmp (arg1, dbnames[cnt]) == 0)
145 if (strcmp (arg2, "no") == 0)
146 dbs[cnt].enabled = 0;
147 else if (strcmp (arg2, "yes") == 0)
148 dbs[cnt].enabled = 1;
149 break;
151 if (cnt == lastdb)
152 dbg_log ("server %s is not supported\n", arg1);
154 else if (strcmp (entry, "check-files") == 0)
156 for (cnt = 0; cnt < lastdb; ++cnt)
157 if (strcmp (arg1, dbnames[cnt]) == 0)
159 if (strcmp (arg2, "no") == 0)
160 dbs[cnt].check_file = 0;
161 else if (strcmp (arg2, "yes") == 0)
162 dbs[cnt].check_file = 1;
163 break;
165 if (cnt == lastdb)
166 dbg_log ("server %s is not supported\n", arg1);
168 else if (strcmp (entry, "logfile") == 0)
170 if (!set_logfile (arg1))
171 dbg_log (_("Could not create log file \"%s\""), arg1);
173 else if (strcmp (entry, "debug-level") == 0)
175 int level = atoi (arg1);
176 if (level > 0)
177 debug_level = level;
179 else if (strcmp (entry, "threads") == 0)
181 if (nthreads == -1)
182 nthreads = MAX (atol (arg1), lastdb);
184 else if (strcmp (entry, "server-user") == 0)
186 if (!arg1)
187 dbg_log (_("Must specify user name for server-user option"));
188 else
189 server_user = xstrdup (arg1);
191 else if (strcmp (entry, "stat-user") == 0)
193 if (!arg1)
194 dbg_log (_("Must specify user name for stat-user option"));
195 else
197 stat_user = xstrdup (arg1);
199 struct passwd *pw = getpwnam (stat_user);
200 if (pw != NULL)
201 stat_uid = pw->pw_uid;
204 else
205 dbg_log (_("Unknown option: %s %s %s"), entry, arg1, arg2);
207 while (!feof (fp));
209 /* Free the buffer. */
210 free (line);
211 /* Close configuration file. */
212 fclose (fp);
214 return 0;