4 * Created on: Oct 20, 2010
17 static int uci_do_add(int argc, char **argv)
19 struct uci_package *p = NULL;
20 struct uci_section *s = NULL;
26 ret = uci_load(ctx, argv[1], &p);
30 ret = uci_add_section(ctx, p, argv[2], &s);
34 ret = uci_save(ctx, p);
40 fprintf(stdout, "%s\n", s->e.name);
45 static int uci_create_named_section(struct uci_context *ctx, const char *conf_file_name, const char *section_type, const char *section_name)
54 (conf_file_name != NULL) &&
55 (section_type != NULL) &&
56 (section_name != NULL)) {
57 len = strlen(conf_file_name);
59 len = len + strlen(section_type);
61 len = len + strlen(section_name);
63 cmd_data = malloc(len);
64 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
65 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
66 ret_val = uci_set(ctx, &ptr);
67 if (ret_val == UCI_OK) {
68 //ret_val = uci_save(ctx, ptr.p);
75 bool set_config_value2(const char *conf_dir_name,
76 const char *conf_file_name,
77 const char *section_type,
78 const char *section_name,
81 struct uci_context *ctx;
82 struct uci_package *pkg;
83 struct uci_section *sct;
84 struct uci_section *tmp_sct;
85 struct uci_option *opt;
90 struct uci_element *elem;
97 if ((conf_dir_name != NULL) &&
98 (conf_file_name != NULL) &&
99 (section_type != NULL) &&
100 (section_name != NULL) &&
103 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
104 fname = (char *)calloc(1, b_count);
106 strncpy(fname, conf_dir_name, b_count);
107 strncat(fname, "/", 1);
108 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
109 ctx = uci_alloc_context();
112 uci_set_confdir(ctx, conf_dir_name);
113 if (access(fname, W_OK) != 0) {
114 if (access(fname, F_OK) != 0) {
115 fp = fopen(fname, "w+");
119 if (access(fname, W_OK) == 0) {
120 err_flg = uci_load(ctx, fname, &pkg);
121 uci_foreach_element(&pkg->sections, elem) {
122 tmp_sct = uci_to_section(elem);
123 if (strcmp(tmp_sct->type, section_type) == 0) {
129 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
130 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
131 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
132 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
133 if (err_flg == UCI_OK) {
134 uci_foreach_element(&pkg->sections, elem) {
135 tmp_sct = uci_to_section(elem);
136 if (strcmp(tmp_sct->type, section_type) == 0) {
144 opt = uci_lookup_option(ctx, sct, key);
146 memset(&ptr, 0, sizeof(ptr));
147 ptr.package = pkg->e.name;
148 ptr.section = sct->e.name;
151 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
152 ptr.value = strdup(value);
158 opt = uci_alloc_option(sct, key, value);
164 uci_free_context(ctx);
168 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
172 log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
178 log_error("Could not change config value, out of memory");
182 log_error("Could not change config value, invalid parameters");
188 char work_dir[FILENAME_MAX];
190 getcwd(work_dir, sizeof(work_dir));
191 printf("working directory: %s\n", work_dir);
193 set_config_value2(work_dir,
199 set_config_value2(work_dir,
207 int main(int argc, char** argv) {