//读取配置文件 void parseconf_load_file(const char *path) { FILE *fp = fopen(path, "r"); if (fp == NULL) ERR_EXIT("fopen"); char setting_line[1024] = {0}; while (fgets(setting_line, sizeof(setting_line), fp) != NULL) { //去掉空行、全是空格的、以#号开头的注释行 if (strlen(setting_line) == 0 || setting_line[0] == '#' || str_all_space(setting_line)) continue; //去掉后面的/r/n str_trim_crlf(setting_line); //解析配置信息 parseconf_load_setting(setting_line); memset(setting_line, 0, sizeof(setting_line)); } fclose(fp); }
void parseconf_load_file(const char *path){ FILE *fp = fopen(path, "r"); if(fp == NULL) ERR_EXIT("fopen"); char setting_line[MAX_SETTING_LINE] = {0}; while(fgets(setting_line, sizeof(setting_line), fp) != NULL){ if(strlen(setting_line) == 0 || setting_line[0] == '#' || str_all_space(setting_line)) continue; str_trim_crlf(setting_line); parseconf_load_setting(setting_line); memset(setting_line, 0, sizeof setting_line); } fclose(fp); }
/* * parseconf_load_file -- 加载配置文件配置 * 参数: @path: 配置文件路径 */ void parseconf_load_file(const char *path) { FILE *fp = fopen(path, "r"); if(fp == NULL) handle_error("fopen"); char setting_line[1024] = {0}; while(fgets(setting_line, sizeof(setting_line), fp) != NULL) { if((strlen(setting_line) == 0) || (setting_line[0] == '#') || str_all_space(setting_line)) { memset(setting_line, 0, sizeof(setting_line)); continue; } //将得到的有效字符串去除\r\n str_trim_crlf(setting_line); //解析配置行,将信息保存在对应的变量中 parseconf_load_setting(setting_line); memset(setting_line, 0, sizeof(setting_line)); } fclose(fp); }
void vsf_parseconf_load_file(const char* p_filename, int errs_fatal) { struct mystr config_file_str = INIT_MYSTR; struct mystr config_setting_str = INIT_MYSTR; struct mystr config_value_str = INIT_MYSTR; unsigned int str_pos = 0; int retval; if (!p_filename) { p_filename = s_p_saved_filename; } else { if (s_p_saved_filename) { vsf_sysutil_free((char*)s_p_saved_filename); } s_p_saved_filename = vsf_sysutil_strdup(p_filename); } if (!p_filename) { bug("null filename in vsf_parseconf_load_file"); } retval = str_fileread(&config_file_str, p_filename, VSFTP_CONF_FILE_MAX); if (vsf_sysutil_retval_is_error(retval)) { if (errs_fatal) { die2("cannot read config file: ", p_filename); } else { str_free(&config_file_str); return; } } { struct vsf_sysutil_statbuf* p_statbuf = 0; retval = vsf_sysutil_stat(p_filename, &p_statbuf); /* Security: check current user owns the config file. These are sanity * checks for the admin, and are NOT designed to be checks safe from * race conditions. */ if (vsf_sysutil_retval_is_error(retval) || vsf_sysutil_statbuf_get_uid(p_statbuf) != vsf_sysutil_getuid() || !vsf_sysutil_statbuf_is_regfile(p_statbuf)) { die("config file not owned by correct user, or not a file"); } vsf_sysutil_free(p_statbuf); } while (str_getline(&config_file_str, &config_setting_str, &str_pos)) { if (str_isempty(&config_setting_str) || str_get_char_at(&config_setting_str, 0) == '#' || str_all_space(&config_setting_str)) { continue; } vsf_parseconf_load_setting(str_getbuf(&config_setting_str), errs_fatal); } str_free(&config_file_str); str_free(&config_setting_str); str_free(&config_value_str); }