📄 conf.c
字号:
/* Westell 6100 multicast data collection utility * Copyright: Josh Carroll (josh.carroll@gmail.com) * 10/13/2004 * * Thanks to LSorensen, mith, rkeene, and bgr on EFNet * #Linux for the help with my rusty C skills, and thanks * to dmonnier, Yock, no_strings, freerock, hondje, * shenion, and shray on #ATU freenode. * * Also thanks to aedinius on #c on EFNet for helping clean the code * up. * * conf.c is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, version 2. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA **/#include "conf.h"char *search_path[SEARCH_LEN] = {"/usr/local/etc/wdiag.conf", "/etc/wdiag.conf"};int read_config(wconf_t *conf) { int i; char *conf_path = NULL; struct stat file; /* first path that is found wins, if you want to change the search order or put the config file in a non-standard location, edit the above array*/ for(i = 0; i < SEARCH_LEN; i++) { if(stat(search_path[i], &file) == 0) { conf_path = malloc(strlen(search_path[i]) + 1); strcpy(conf_path, search_path[i]); break; } } if(conf_path == NULL) { /* finally, check home dir */ /* 13 extra chars for /.wdiag.conf */ conf_path = malloc(strlen(getenv("HOME")) + 13); sprintf(conf_path, "%s/.wdiag.conf", getenv("HOME")); if(stat(conf_path, &file) != 0) { free(conf_path); conf_path = NULL; } } if(conf_path != NULL) { if( (strlen(conf_path) + 1) > 80) { printf("conf path: %s too long\n", conf_path); exit(EXIT_FAILURE); } strcpy(conf->path, conf_path); free(conf_path); /* initialize to detect lack of option later */ conf->model = -1; conf->bcast = NULL; conf->decimal = 0; conf->laddr = NULL; if(parse_config(conf)) { printf("Failed to parse configuration file\n"); return 0; } return 0; } free(conf_path); return 1;}int parse_config(wconf_t *conf) { FILE *fh; char errmsg[80]; char buffer[READ_BUFF]; int model, decimal; char *bcast, *laddr; if( (fh = fopen(conf->path, "r")) == NULL ) { sprintf(errmsg, "Could not open config: %s", conf->path); perror(errmsg); conf = NULL; return 1; } else { while( fgets(buffer, READ_BUFF, fh) != NULL) { /* strip \n */ if(buffer[strlen(buffer)-1] == '\n') buffer[strlen(buffer)-1] = '\0'; if(rematch("^ *#", buffer) || rematch("^ *$", buffer)) { /* comment or blank line, skip it */ continue; } if((model = model_def(buffer)) != -1) { conf->model = model; continue; } if((decimal = decimal_def(buffer)) != -1) { conf->decimal = decimal; continue; } if((bcast = bcast_def(buffer)) != NULL) { conf->bcast = malloc(strlen(bcast)+1); strncpy(conf->bcast, bcast, strlen(bcast)+1); continue; } if((laddr = laddr_def(buffer)) != NULL) { conf->laddr = malloc(strlen(laddr)+1); strncpy(conf->laddr, laddr, strlen(laddr)+1); continue; } } fclose(fh); } return 0;}int rematch(const char *regexp, const char *string) { regex_t reg; if(regcomp(®, regexp, REG_EXTENDED|REG_NOSUB) != 0) { perror("regular expression compile failed"); } else { if(regexec(®, string, 0, NULL, 0) == 0) { regfree(®); return 1; } } regfree(®); return 0;}int model_def(char *line) { char *match_ret; match_ret = match_config("^[ \t]*model[ \t]*=[ \t]*([0-9]+)", line); if(match_ret == NULL) return -1; else return atoi(match_ret);}char *bcast_def(char *line) { char *match_ret; match_ret = match_config("^[ \t]*broadcast[ \t]*=[ \t]*([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) *$", line); /* NULL if not found */ return match_ret;}int decimal_def(char *line) { char *match_ret; int dec; match_ret = match_config("^[ \t]*decimal[ \t]*=[ \t]*([0-9]+)", line); if(match_ret == NULL) return -1; else dec = atoi(match_ret); if(dec == 1) return 1; else return 0;}char *match_config(char *regex, char * line) { char *match_str; regmatch_t match[2]; int reg_ret; regex_t reg; /* look for model or broadcast configuration */ if(regcomp(®, regex, REG_EXTENDED) != 0) { perror("regular expression compile failed"); return NULL; } if( (reg_ret = regexec(®, line, 2, match, 0)) == 0) { /* extract the value */ match_str = &line[match[1].rm_so]; return match_str; } return NULL;}char *laddr_def(char *line) { char *match_ret; match_ret = match_config("^[ \t]*listen[ \t]*=[ \t]*([0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}) *$", line); /* NULL if not found */ return match_ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -