oconfig.c: Fix compiler warning.
[collection4.git] / graph_config.c
1 #include <stdlib.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <strings.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <errno.h>
8
9 #include "graph_config.h"
10 #include "graph_list.h"
11 #include "oconfig.h"
12 #include "common.h"
13
14 #define CONFIG_FILE "/usr/lib/cgi-bin/octo/collection.conf"
15
16 time_t last_read_mtime = 0;
17
18 static int dispatch_config (const oconfig_item_t *ci) /* {{{ */
19 {
20   int i;
21
22   for (i = 0; i < ci->children_num; i++)
23   {
24     oconfig_item_t *child;
25
26     child = ci->children + i;
27     if (strcasecmp ("Graph", child->key) == 0)
28       graph_config_add (child);
29     else
30     {
31       DEBUG ("Unknown config option: %s", child->key);
32     }
33   }
34
35   return (0);
36 } /* }}} int dispatch_config */
37
38 static int internal_read_config (void) /* {{{ */
39 {
40   oconfig_item_t *ci;
41
42   ci = oconfig_parse_file (CONFIG_FILE);
43   if (ci == NULL)
44     return (-1);
45
46   dispatch_config (ci);
47
48   oconfig_free (ci);
49
50   gl_config_submit ();
51
52   return (0);
53 } /* }}} int internal_read_config */
54
55 static time_t get_config_mtime (void) /* {{{ */
56 {
57   struct stat statbuf;
58   int status;
59
60   memset (&statbuf, 0, sizeof (statbuf));
61   status = stat (CONFIG_FILE, &statbuf);
62   if (status != 0)
63     return (0);
64
65   return (statbuf.st_mtime);
66 } /* }}} time_t get_config_mtime */
67
68 int graph_read_config (void) /* {{{ */
69 {
70   time_t mtime;
71
72   mtime = get_config_mtime ();
73
74   if (mtime <= last_read_mtime)
75     return (0);
76
77   internal_read_config ();
78
79   last_read_mtime = mtime;
80
81   return (0);
82 } /* }}} int graph_read_config */
83
84 int graph_config_get_string (const oconfig_item_t *ci, /* {{{ */
85     char **ret_str)
86 {
87   char *tmp;
88
89   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
90     return (EINVAL);
91
92   tmp = strdup (ci->values[0].value.string);
93   if (tmp == NULL)
94     return (ENOMEM);
95
96   free (*ret_str);
97   *ret_str = tmp;
98
99   return (0);
100 } /* }}} int graph_config_get_string */
101
102 int graph_config_get_bool (const oconfig_item_t *ci, /* {{{ */
103     _Bool *ret_bool)
104 {
105   if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
106     return (EINVAL);
107
108   if (ci->values[0].value.boolean)
109     *ret_bool = 1;
110   else
111     *ret_bool = 0;
112
113   return (0);
114 } /* }}} int graph_config_get_bool */
115
116 /* vim: set sw=2 sts=2 et fdm=marker : */