summaryrefslogtreecommitdiffstats
path: root/lib/parse_ini.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/parse_ini.c')
-rw-r--r--lib/parse_ini.c53
1 files changed, 34 insertions, 19 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index 547af433..09c0dc4f 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -1,24 +1,24 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2*
3* Monitoring Plugins parse_ini library 3* Monitoring Plugins parse_ini library
4* 4*
5* License: GPL 5* License: GPL
6* Copyright (c) 2007 Monitoring Plugins Development Team 6* Copyright (c) 2007 Monitoring Plugins Development Team
7* 7*
8* This program is free software: you can redistribute it and/or modify 8* This program is free software: you can redistribute it and/or modify
9* it under the terms of the GNU General Public License as published by 9* it under the terms of the GNU General Public License as published by
10* the Free Software Foundation, either version 3 of the License, or 10* the Free Software Foundation, either version 3 of the License, or
11* (at your option) any later version. 11* (at your option) any later version.
12* 12*
13* This program is distributed in the hope that it will be useful, 13* This program is distributed in the hope that it will be useful,
14* but WITHOUT ANY WARRANTY; without even the implied warranty of 14* but WITHOUT ANY WARRANTY; without even the implied warranty of
15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16* GNU General Public License for more details. 16* GNU General Public License for more details.
17* 17*
18* You should have received a copy of the GNU General Public License 18* You should have received a copy of the GNU General Public License
19* along with this program. If not, see <http://www.gnu.org/licenses/>. 19* along with this program. If not, see <http://www.gnu.org/licenses/>.
20* 20*
21* 21*
22*****************************************************************************/ 22*****************************************************************************/
23 23
24#include "common.h" 24#include "common.h"
@@ -36,6 +36,7 @@
36 */ 36 */
37typedef struct { 37typedef struct {
38 char *file; 38 char *file;
39 bool file_string_on_heap;
39 char *stanza; 40 char *stanza;
40} np_ini_info; 41} np_ini_info;
41 42
@@ -95,16 +96,22 @@ parse_locator(const char *locator, const char *def_stanza, np_ini_info *i)
95 i->stanza = malloc(sizeof(char) * (stanza_len + 1)); 96 i->stanza = malloc(sizeof(char) * (stanza_len + 1));
96 strncpy(i->stanza, locator, stanza_len); 97 strncpy(i->stanza, locator, stanza_len);
97 i->stanza[stanza_len] = '\0'; 98 i->stanza[stanza_len] = '\0';
98 } else /* otherwise we use the default stanza */ 99 } else {/* otherwise we use the default stanza */
99 i->stanza = strdup(def_stanza); 100 i->stanza = strdup(def_stanza);
101 }
100 102
101 if (i->stanza == NULL) 103 if (i->stanza == NULL)
102 die(STATE_UNKNOWN, _("malloc() failed!\n")); 104 die(STATE_UNKNOWN, _("malloc() failed!\n"));
103 105
104 /* check whether there's an @file part */ 106 /* check whether there's an @file part */
105 i->file = stanza_len == locator_len 107 if (stanza_len == locator_len) {
106 ? default_file() 108 i->file = default_file();
107 : strdup(&(locator[stanza_len + 1])); 109 i->file_string_on_heap = false;
110 } else {
111 i->file = strdup(&(locator[stanza_len + 1]));
112 i->file_string_on_heap = true;
113 }
114
108 if (i->file == NULL || i->file[0] == '\0') 115 if (i->file == NULL || i->file[0] == '\0')
109 die(STATE_UNKNOWN, 116 die(STATE_UNKNOWN,
110 _("Cannot find config file in any standard location.\n")); 117 _("Cannot find config file in any standard location.\n"));
@@ -131,12 +138,15 @@ np_get_defaults(const char *locator, const char *default_section)
131 if (inifile == NULL) 138 if (inifile == NULL)
132 die(STATE_UNKNOWN, _("Can't read config file: %s\n"), 139 die(STATE_UNKNOWN, _("Can't read config file: %s\n"),
133 strerror(errno)); 140 strerror(errno));
134 if (read_defaults(inifile, i.stanza, &defaults) == FALSE) 141 if (!read_defaults(inifile, i.stanza, &defaults))
135 die(STATE_UNKNOWN, 142 die(STATE_UNKNOWN,
136 _("Invalid section '%s' in config file '%s'\n"), i.stanza, 143 _("Invalid section '%s' in config file '%s'\n"), i.stanza,
137 i.file); 144 i.file);
138 145
139 free(i.file); 146 if (i.file_string_on_heap) {
147 free(i.file);
148 }
149
140 if (inifile != stdin) 150 if (inifile != stdin)
141 fclose(inifile); 151 fclose(inifile);
142 free(i.stanza); 152 free(i.stanza);
@@ -157,7 +167,8 @@ np_get_defaults(const char *locator, const char *default_section)
157static int 167static int
158read_defaults(FILE *f, const char *stanza, np_arg_list **opts) 168read_defaults(FILE *f, const char *stanza, np_arg_list **opts)
159{ 169{
160 int c, status = FALSE; 170 int c = 0;
171 bool status = false;
161 size_t i, stanza_len; 172 size_t i, stanza_len;
162 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA; 173 enum { NOSTANZA, WRONGSTANZA, RIGHTSTANZA } stanzastate = NOSTANZA;
163 174
@@ -219,7 +230,7 @@ read_defaults(FILE *f, const char *stanza, np_arg_list **opts)
219 die(STATE_UNKNOWN, "%s\n", 230 die(STATE_UNKNOWN, "%s\n",
220 _("Config file error")); 231 _("Config file error"));
221 } 232 }
222 status = TRUE; 233 status = true;
223 break; 234 break;
224 } 235 }
225 break; 236 break;
@@ -357,14 +368,18 @@ add_option(FILE *f, np_arg_list **optlst)
357static char * 368static char *
358default_file(void) 369default_file(void)
359{ 370{
360 char **p, *ini_file; 371 char *ini_file;
361 372
362 if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL || 373 if ((ini_file = getenv("MP_CONFIG_FILE")) != NULL ||
363 (ini_file = default_file_in_path()) != NULL) 374 (ini_file = default_file_in_path()) != NULL) {
364 return ini_file; 375 return ini_file;
365 for (p = default_ini_path_names; *p != NULL; p++) 376 }
366 if (access(*p, F_OK) == 0) 377
378 for (char **p = default_ini_path_names; *p != NULL; p++) {
379 if (access(*p, F_OK) == 0) {
367 return *p; 380 return *p;
381 }
382 }
368 return NULL; 383 return NULL;
369} 384}
370 385