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 if (cmd_data != NULL) {
65 snprintf(cmd_data, len, "%s.%s=%s", conf_file_name, section_name, section_type);
66 if (uci_lookup_ptr(ctx, &ptr, cmd_data, true) == UCI_OK) {
67 ret_val = uci_set(ctx, &ptr);
68 if (ret_val == UCI_OK) {
77 bool set_config_value2(const char *conf_dir_name,
78 const char *conf_file_name,
79 const char *section_type,
80 const char *section_name,
83 struct uci_context *ctx;
84 struct uci_package *pkg;
85 struct uci_section *sct;
86 struct uci_section *tmp_sct;
87 struct uci_option *opt;
92 struct uci_element *elem;
99 if ((conf_dir_name != NULL) &&
100 (conf_file_name != NULL) &&
101 (section_type != NULL) &&
102 (section_name != NULL) &&
105 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
106 fname = (char *)calloc(1, b_count);
108 strncpy(fname, conf_dir_name, b_count);
109 strncat(fname, "/", 1);
110 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
111 ctx = uci_alloc_context();
114 uci_set_confdir(ctx, conf_dir_name);
115 if (access(fname, W_OK) != 0) {
116 if (access(fname, F_OK) != 0) {
117 fp = fopen(fname, "w+");
121 if (access(fname, W_OK) == 0) {
122 err_flg = uci_load(ctx, fname, &pkg);
123 uci_foreach_element(&pkg->sections, elem) {
124 tmp_sct = uci_to_section(elem);
125 if (strcmp(tmp_sct->type, section_type) == 0) {
131 log_debug("Creating configuration section %s to configuration file: %s\n", section_name, fname);
132 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
133 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
134 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
135 if (err_flg == UCI_OK) {
136 uci_foreach_element(&pkg->sections, elem) {
137 tmp_sct = uci_to_section(elem);
138 if (strcmp(tmp_sct->type, section_type) == 0) {
146 opt = uci_lookup_option(ctx, sct, key);
148 memset(&ptr, 0, sizeof(ptr));
149 ptr.package = pkg->e.name;
150 ptr.section = sct->e.name;
153 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
160 opt = uci_alloc_option(sct, key, value);
166 uci_free_context(ctx);
170 log_error("Could not write to configuration file: %s\n. Could not create section %s.", fname, section_name);
174 log_error("Could not write to configuration file: %s\n. File does not exist or is not writable.", fname);
180 log_error("Could not change config value, out of memory");
184 log_error("Could not change config value, invalid parameters");
190 char work_dir[FILENAME_MAX];
192 getcwd(work_dir, sizeof(work_dir));
193 printf("working directory: %s\n", work_dir);
195 set_config_value2(work_dir,
201 set_config_value2(work_dir,
209 int main(int argc, char** argv) {