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.c78
1 files changed, 66 insertions, 12 deletions
diff --git a/lib/parse_ini.c b/lib/parse_ini.c
index e318ec3..67d8367 100644
--- a/lib/parse_ini.c
+++ b/lib/parse_ini.c
@@ -30,6 +30,10 @@
30#include "utils_base.h" 30#include "utils_base.h"
31#include <ctype.h> 31#include <ctype.h>
32 32
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <unistd.h>
36
33/* FIXME: N::P dies if section is not found */ 37/* FIXME: N::P dies if section is not found */
34/* FIXME: N::P dies if config file is not found */ 38/* FIXME: N::P dies if config file is not found */
35 39
@@ -50,6 +54,8 @@ static int read_defaults(FILE *f, const char *stanza, np_arg_list **opts);
50static int add_option(FILE *f, np_arg_list **optlst); 54static int add_option(FILE *f, np_arg_list **optlst);
51/* internal function to find default file */ 55/* internal function to find default file */
52static char* default_file(void); 56static char* default_file(void);
57/* internal function to stat() files */
58static int test_file(const char* env, int len, const char* file, char* temp_file);
53 59
54/* parse_locator decomposes a string of the form 60/* parse_locator decomposes a string of the form
55 * [stanza][@filename] 61 * [stanza][@filename]
@@ -296,23 +302,71 @@ static int add_option(FILE *f, np_arg_list **optlst){
296} 302}
297 303
298static char* default_file(void){ 304static char* default_file(void){
299 char *np_env=NULL; 305 struct stat sb;
306 char *np_env=NULL, *default_file=NULL;
307 char temp_file[MAX_INPUT_BUFFER];
308 size_t len;
300 309
301 /* FIXME: STUB */
302 return "";
303#if 0
304 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) { 310 if((np_env=getenv("NAGIOS_CONFIG_PATH"))!=NULL) {
311 /* skip ant starting colon... */
312 while(*np_env==':') np_env++;
305 /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in 313 /* Look for NP_DEFAULT_INI_FILENAME1 and NP_DEFAULT_INI_FILENAME2 in
306 * every PATHs defined (colon-separated). 314 * every PATHs defined (colon-separated).
307 */ 315 */
316 while((len=strcspn(np_env,":"))>0){
317 /* Test NP_DEFAULT_INI_FILENAME[1-2] in current np_env token */
318 if(test_file(np_env,len,NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
319 test_file(np_env,len,NP_DEFAULT_INI_FILENAME2,temp_file)==1){
320 default_file=strdup(temp_file);
321 break;
322 }
323
324 /* Move on to the next token */
325 np_env+=len;
326 while(*np_env==':') np_env++;
327 } /* while(...) */
328 } /* if(getenv("NAGIOS_CONFIG_PATH")) */
329
330 /* Look for NP_DEFAULT_INI_FILENAME1 in NP_DEFAULT_INI_NAGIOS_PATH[1-4] */
331 if(!default_file){
332 if(test_file(NP_DEFAULT_INI_NAGIOS_PATH1,strlen(NP_DEFAULT_INI_NAGIOS_PATH1),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
333 test_file(NP_DEFAULT_INI_NAGIOS_PATH2,strlen(NP_DEFAULT_INI_NAGIOS_PATH2),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
334 test_file(NP_DEFAULT_INI_NAGIOS_PATH3,strlen(NP_DEFAULT_INI_NAGIOS_PATH3),NP_DEFAULT_INI_FILENAME1,temp_file)==1 ||
335 test_file(NP_DEFAULT_INI_NAGIOS_PATH4,strlen(NP_DEFAULT_INI_NAGIOS_PATH4),NP_DEFAULT_INI_FILENAME1,temp_file)==1)
336 default_file=strdup(temp_file);
337 }
338
339 /* Look for NP_DEFAULT_INI_FILENAME2 in NP_DEFAULT_INI_PATH[1-3] */
340 if(!default_file){
341 if(test_file(NP_DEFAULT_INI_PATH1,strlen(NP_DEFAULT_INI_PATH1),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
342 test_file(NP_DEFAULT_INI_PATH2,strlen(NP_DEFAULT_INI_PATH2),NP_DEFAULT_INI_FILENAME2,temp_file)==1 ||
343 test_file(NP_DEFAULT_INI_PATH3,strlen(NP_DEFAULT_INI_PATH3),NP_DEFAULT_INI_FILENAME2,temp_file)==1)
344 default_file=strdup(temp_file);
308 } 345 }
309 if !file_found 346
310 search NP_DEFAULT_INI_NAGIOS_PATH[1-4] for NP_DEFAULT_INI_FILENAME1; 347 /* Return default_file or empty string (should return NULL if we want to
311 if !file_found 348 * die there...
312 search NP_DEFAULT_INI_PATH[1-3] for NP_DEFAULT_INI_FILENAME2; 349 */
313 if !file_found 350 if(default_file)
314 return empty string (or null if we want to die); 351 return default_file;
315 return file name; 352 return "";
316#endif 353}
354
355/* put together len bytes from env and the filename and test for its
356 * existence. Returns 1 if found, 0 if not and -1 if test wasn't performed.
357 */
358static int test_file(const char* env, int len, const char* file, char* temp_file){
359 struct stat sb;
360
361 /* test for len + filelen + '/' + '\0' */
362 if((len+strlen(file)+2)>MAX_INPUT_BUFFER) return -1;
363
364 strncpy(temp_file,env,len);
365 temp_file[len]='\0';
366 strncat(temp_file,"/",len+1);
367 strncat(temp_file,file,len+strlen(file)+1);
368
369 if(stat(temp_file, &sb) != -1) return 1;
370 return 0;
317} 371}
318 372