2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <apr_general.h>
29 #include "hostinfod.h"
31 typedef int (*process_line_fn) (const char *key, const char *value);
32 static void process_conf_file (const char *path, int exit_if_not_exist, process_line_fn process_line);
34 /* Read the main configuration file. This file is NOT reread if it
35 * changes, but the auxiliary guests.conf file IS reread (and so is
36 * the socket directory of course).
39 read_main_conf_file (void)
41 process_conf_file (conf_file, 0, NULL);
45 process_conf_file (const char *path, int exit_if_not_exist,
46 process_line_fn process_line)
48 static const char *whitespace = " \t\n\v";
52 size_t real_len, key_len;
54 const char *key, *value;
56 fp = fopen (path, "r");
58 if (exit_if_not_exist) {
65 while ((r = getline (&line, &len, fp)) != -1) {
66 /* Remove trailing \n */
68 if (real_len > 0 && line[real_len-1] == '\n')
69 line[--real_len] = '\0';
71 /* Ignore blank lines and comments. */
74 if (line[0] == '#' || strspn (line, whitespace) == real_len)
77 /* Split into key value. */
78 key_len = strcspn (line, whitespace);
81 value = key_len < real_len ? &line[key_len+1] : NULL;
83 value += strspn (line, whitespace);
88 if (process_line && process_line (key, value) == -1)
95 fprintf (stderr, "error reading configuration file: %s\n", path);
98 if (fclose (fp) == EOF) {