static void handle_site_chmod(struct vsf_session* p_sess, struct mystr* p_arg_str) { static struct mystr s_chmod_file_str; unsigned int perms; int retval; if (str_isempty(p_arg_str)) { vsf_cmdio_write(p_sess, FTP_BADCMD, "SITE CHMOD needs 2 arguments."); return; } str_split_char(p_arg_str, &s_chmod_file_str, ' '); if (str_isempty(&s_chmod_file_str)) { vsf_cmdio_write(p_sess, FTP_BADCMD, "SITE CHMOD needs 2 arguments."); return; } /* Don't worry - our chmod() implementation only allows 0 - 0777 */ perms = str_octal_to_uint(p_arg_str); retval = str_chmod(&s_chmod_file_str, perms); if (vsf_sysutil_retval_is_error(retval)) { vsf_cmdio_write(p_sess, FTP_FILEFAIL, "SITE CHMOD command failed."); } else { vsf_cmdio_write(p_sess, FTP_CHMODOK, "SITE CHMOD command ok."); } }
void do_site_chmod(session_t *sess, char *args) { if (strlen(args) == 0) { ftp_reply(sess, FTP_BADCMD, "SITE CHMOD needs 2 arguments."); return; } char perm[100] = {0}; char file[100] = {0}; str_split(args , perm, file, ' '); if (strlen(file) == 0) { ftp_reply(sess, FTP_BADCMD, "SITE CHMOD needs 2 arguments."); return; } unsigned int mode = str_octal_to_uint(perm); if (chmod(file, mode) < 0) { ftp_reply(sess, FTP_CHMODOK, "SITE CHMOD command failed."); } else { ftp_reply(sess, FTP_CHMODOK, "SITE CHMOD command ok."); } }
void parseconf_load_setting(const char *setting){ while(isspace(*setting)) setting++; char key[128] = {0}, value[128] = {0}; str_split(setting, key, value, '='); if(strlen(value) == 0){ fprintf(stderr, "missing value in config file for : %s\n", key); exit(EXIT_FAILURE); } { const struct parseconf_str_setting *p_str_setting = parseconf_str_array; while(p_str_setting->p_setting_name != NULL){ if(strcmp(p_str_setting->p_setting_name, key) == 0){ const char **p_cur_setting = p_str_setting->p_variable; if(*p_cur_setting) free((char*)*p_cur_setting); *p_cur_setting = strdup(value); return; } p_str_setting++; } } { const struct parseconf_bool_setting *p_bool_setting = parseconf_bool_array; while(p_bool_setting->p_setting_name != NULL){ if(strcmp(p_bool_setting->p_setting_name, key) == 0){ str_upper(value); if(strcmp(value, "YES") == 0 || strcmp(value, "TRUE") == 0 || strcmp(value, "1") == 0) *(p_bool_setting->p_variable) = 1; else if(strcmp(value, "NO") == 0 || strcmp(value, "FALSE") == 0 || strcmp(value, "0") == 0) *(p_bool_setting->p_variable) = 0; else{ fprintf(stderr, "Wrong bool value in config file for : %s\n", key); exit(EXIT_FAILURE); } return; } p_bool_setting++; } } { const struct parseconf_uint_setting *p_uint_setting = parseconf_uint_array; while(p_uint_setting->p_setting_name != NULL){ if(strcmp(p_uint_setting->p_setting_name, key) == 0){ if(value[0] == '0'){ *(p_uint_setting->p_variable) = str_octal_to_uint(value); } else{ *(p_uint_setting->p_variable) = atoi(value); } return; } p_uint_setting++; } } }
void do_site_umask(session_t *sess, char *args) { // SITE UMASK [umask] if (strlen(args) == 0) { char text[1024] = {0}; sprintf(text, "Your current UMASK is 0%o", tunable_local_umask); ftp_reply(sess, FTP_UMASKOK, text); } else { unsigned int um = str_octal_to_uint(args); umask(um); char text[1024] = {0}; sprintf(text, "UMASK set to 0%o", um); ftp_reply(sess, FTP_UMASKOK, text); } }
static void handle_site_umask(struct vsf_session* p_sess, struct mystr* p_arg_str) { static struct mystr s_umask_resp_str; if (str_isempty(p_arg_str)) { /* Empty arg => report current umask */ str_alloc_text(&s_umask_resp_str, "Your current UMASK is "); str_append_text(&s_umask_resp_str, vsf_sysutil_uint_to_octal(vsf_sysutil_get_umask())); } else { /* Set current umask */ unsigned int new_umask = str_octal_to_uint(p_arg_str); vsf_sysutil_set_umask(new_umask); str_alloc_text(&s_umask_resp_str, "UMASK set to "); str_append_text(&s_umask_resp_str, vsf_sysutil_uint_to_octal(vsf_sysutil_get_umask())); } vsf_cmdio_write_str(p_sess, FTP_UMASKOK, &s_umask_resp_str); }
void vsf_parseconf_load_setting(const char* p_setting, int errs_fatal) { static struct mystr s_setting_str; static struct mystr s_value_str; while (vsf_sysutil_isspace(*p_setting)) { p_setting++; } str_alloc_text(&s_setting_str, p_setting); str_split_char(&s_setting_str, &s_value_str, '='); /* Is it a string setting? */ { const struct parseconf_str_setting* p_str_setting = parseconf_str_array; while (p_str_setting->p_setting_name != 0) { if (str_equal_text(&s_setting_str, p_str_setting->p_setting_name)) { /* Got it */ const char** p_curr_setting = p_str_setting->p_variable; if (*p_curr_setting) { vsf_sysutil_free((char*) *p_curr_setting); } if (str_isempty(&s_value_str)) { *p_curr_setting = 0; } else { *p_curr_setting = str_strdup(&s_value_str); } return; } p_str_setting++; } } if (str_isempty(&s_value_str)) { if (errs_fatal) { die2("missing value in config file for: ", str_getbuf(&s_setting_str)); } else { return; } } /* Is it a boolean value? */ { const struct parseconf_bool_setting* p_bool_setting = parseconf_bool_array; while (p_bool_setting->p_setting_name != 0) { if (str_equal_text(&s_setting_str, p_bool_setting->p_setting_name)) { /* Got it */ str_upper(&s_value_str); if (str_equal_text(&s_value_str, "YES") || str_equal_text(&s_value_str, "TRUE") || str_equal_text(&s_value_str, "1")) { *(p_bool_setting->p_variable) = 1; } else if (str_equal_text(&s_value_str, "NO") || str_equal_text(&s_value_str, "FALSE") || str_equal_text(&s_value_str, "0")) { *(p_bool_setting->p_variable) = 0; } else if (errs_fatal) { die2("bad bool value in config file for: ", str_getbuf(&s_setting_str)); } return; } p_bool_setting++; } } /* Is it an unsigned integer setting? */ { const struct parseconf_uint_setting* p_uint_setting = parseconf_uint_array; while (p_uint_setting->p_setting_name != 0) { if (str_equal_text(&s_setting_str, p_uint_setting->p_setting_name)) { /* Got it */ /* If the value starts with 0, assume it's an octal value */ if (!str_isempty(&s_value_str) && str_get_char_at(&s_value_str, 0) == '0') { *(p_uint_setting->p_variable) = str_octal_to_uint(&s_value_str); } else { /* TODO: we could reject negatives instead of converting them? */ *(p_uint_setting->p_variable) = (unsigned int) str_atoi(&s_value_str); } return; } p_uint_setting++; } } if (errs_fatal) { die2("unrecognised variable in config file: ", str_getbuf(&s_setting_str)); } }
/* 对读取的配置选项进行解析,对相应的程序变量进行设置 */ void parseconf_load_setting(const char *setting) { // 去除左空格 while (isspace(*setting)) setting++; char key[128] ={0}; char value[128] = {0}; str_split(setting, key, value, '='); if (strlen(value) == 0) { fprintf(stderr, "mising value in config file for: %s\n", key); exit(EXIT_FAILURE); } //加载字符串变量值 const struct parseconf_str_setting *p_str_setting = parseconf_str_array; while (p_str_setting->p_setting_name != NULL) { if (strcmp(key, p_str_setting->p_setting_name) == 0) { const char **p_cur_setting = p_str_setting->p_variable; if (*p_cur_setting) { //原来在程序中赋值不算,以在配置文件中读取的值为标准 free((char*)*p_cur_setting); } *p_cur_setting = strdup(value); return; } p_str_setting++; } //加载真假判断 const struct parseconf_bool_setting *p_bool_setting = parseconf_bool_array; while (p_bool_setting->p_setting_name != NULL) { if (strcmp(key, p_bool_setting->p_setting_name) == 0) { str_upper(value); if (strcmp(value, "YES") == 0 || strcmp(value, "TRUE") == 0 || strcmp(value, "1") == 0) { *(p_bool_setting->p_variable) = 1; } else if (strcmp(value, "NO") == 0 || strcmp(value, "FALSE") == 0 || strcmp(value, "0") == 0) { *(p_bool_setting->p_variable) = 0; } else { fprintf(stderr, "bad bool value in config file for: %s\n", key); exit(EXIT_FAILURE); } return; } p_bool_setting++; } //加载无符号整数 const struct parseconf_uint_setting *p_uint_setting = parseconf_uint_array; while (p_uint_setting->p_setting_name != NULL) { if (strcmp(key, p_uint_setting->p_setting_name) == 0) { if (value[0] == '0') *(p_uint_setting->p_variable) = str_octal_to_uint(value); else *(p_uint_setting->p_variable) = atoi(value); return; } p_uint_setting++; } }
static void handle_config_setting(struct mystr* p_setting_str, struct mystr* p_value_str) { if (str_isempty(p_value_str)) { die("missing value in config file"); } /* Is it a boolean value? */ { const struct parseconf_bool_setting* p_bool_setting = parseconf_bool_array; while (p_bool_setting->p_setting_name != 0) { if (str_equal_text(p_setting_str, p_bool_setting->p_setting_name)) { /* Got it */ str_upper(p_value_str); if (str_equal_text(p_value_str, "YES") || str_equal_text(p_value_str, "TRUE") || str_equal_text(p_value_str, "1")) { *(p_bool_setting->p_variable) = 1; } else if (str_equal_text(p_value_str, "NO") || str_equal_text(p_value_str, "FALSE") || str_equal_text(p_value_str, "0")) { *(p_bool_setting->p_variable) = 0; } else { die("bad bool value in config file"); } return; } p_bool_setting++; } } /* Is it an unsigned integer setting? */ { const struct parseconf_uint_setting* p_uint_setting = parseconf_uint_array; while (p_uint_setting->p_setting_name != 0) { if (str_equal_text(p_setting_str, p_uint_setting->p_setting_name)) { /* Got it */ /* If the value starts with 0, assume it's an octal value */ if (!str_isempty(p_value_str) && str_get_char_at(p_value_str, 0) == '0') { *(p_uint_setting->p_variable) = str_octal_to_uint(p_value_str); } else { *(p_uint_setting->p_variable) = str_atoi(p_value_str); } return; } p_uint_setting++; } } /* Is it a string setting? */ { const struct parseconf_str_setting* p_str_setting = parseconf_str_array; while (p_str_setting->p_setting_name != 0) { if (str_equal_text(p_setting_str, p_str_setting->p_setting_name)) { /* Got it */ const char** p_curr_setting = p_str_setting->p_variable; if (*p_curr_setting) { vsf_sysutil_free((char*)*p_curr_setting); } *p_curr_setting = str_strdup(p_value_str); return; } p_str_setting++; } } die("unrecognised variable in config file"); }
/* * parseconf_load_setting -- 解析配置信息 * 参数: @setting: 一行配置文件数据 */ void parseconf_load_setting(const char *setting) { //去除字符串中的左空格 while(isspace(*setting)) { setting++; } //解析key与value char key[128] = {0}; char value[128] = {0}; str_split(setting, key, value, '='); if(strlen(value) == 0) { fprintf(stderr, "mising value in config file for: %s\n", key); exit(EXIT_FAILURE); } { const struct parseconf_str_setting *p_str_setting = parseconf_str_array; while(p_str_setting->p_setting_name != NULL) { if(strcmp(key, p_str_setting->p_setting_name) == 0) { if(*(p_str_setting->p_variable) != NULL) free((char*)*(p_str_setting->p_variable)); *(p_str_setting->p_variable) = strdup(value); //申请内存空间并复制值 return; } p_str_setting++; } } { const struct parseconf_bool_setting *p_bool_setting = parseconf_bool_array; while(p_bool_setting->p_setting_name != NULL) { if(strcmp(key, p_bool_setting->p_setting_name) == 0) { str_upper(value); if((strcmp(value, "YES") == 0) || (strcmp(value, "TRUE") == 0) || (strcmp(value, "1") == 0)) { *p_bool_setting->p_variable = 1; } else if((strcmp(value, "NO") == 0) || (strcmp(value, "FALSE") == 0) || (strcmp(value, "0") == 0)) { *p_bool_setting->p_variable = 0; } else { fprintf(stderr, "bad bool value in config file for: %s\n", key); exit(EXIT_FAILURE); } return; } p_bool_setting++; } } { const struct parseconf_uint_setting *p_uint_setting = parseconf_uint_array; while(p_uint_setting->p_setting_name != NULL) { if(strcmp(key, p_uint_setting->p_setting_name) == 0) { if(value[0] == '0') *(p_uint_setting->p_variable) = str_octal_to_uint(value); //8进制转换为10进制无符号整型 else *(p_uint_setting->p_variable) = atoi(value); return; } p_uint_setting++; } } }