- if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
- ptr.value = strdup(value);
- uci_set(ctx, &ptr);
- save = true;
- }
- }
- else {
- opt = uci_alloc_option(sct, key, value);
- save = true;
- }
- if (save == true) {
- uci_save(ctx, pkg);
- }
- uci_free_context(ctx);
- ret_val = true;
+char* get_config_value_and_close(const char *conf_dir_name,
+ const char *conf_file_basename,
+ const char *section_name,
+ const char *key) {
+ struct uci_context *ctx;
+ struct uci_package *pkg;
+ int err_flg;
+ char *conf_fname_full;
+ int b_count;
+ FILE *fp;
+ char *ret_val;
+
+ ret_val = NULL;
+ pkg = NULL;
+ if ((conf_dir_name != NULL) &&
+ (conf_file_basename != NULL) &&
+ (section_name != NULL) &&
+ (key != NULL)) {
+ b_count = strlen(conf_dir_name) + strlen(conf_file_basename) + 10;
+ conf_fname_full = (char *)calloc(1, b_count);
+ if (conf_fname_full != NULL) {
+ strncpy(conf_fname_full, conf_dir_name, b_count);
+ strncat(conf_fname_full, "/", 1);
+ strncat(conf_fname_full, conf_file_basename, strlen(conf_file_basename) + 1);
+ ctx = uci_alloc_context();
+ if (ctx != NULL) {
+ if (access(conf_fname_full, R_OK) != 0) {
+ // if file is not writable, try to change it to be writable
+ if (access(conf_fname_full, R_OK) != 0) {
+ fp = fopen(conf_fname_full, "w+");
+ fclose(fp);
+ }
+ }
+ if (access(conf_fname_full, R_OK) == 0) {
+ uci_set_confdir(ctx,
+ conf_dir_name);
+ err_flg = uci_load(ctx, conf_fname_full, &pkg);
+ if ((err_flg == UCI_OK) &&
+ (pkg != NULL)) {
+ ret_val = uci_get_config_value(ctx,
+ pkg,
+ section_name,
+ key);
+ // need to duplicate response val, as uci_free_context() would free the value otherwise
+ if (ret_val != NULL)
+ ret_val = strdup(ret_val);