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;
90 struct uci_element *elem;
96 if ((conf_dir_name != NULL) &&
97 (conf_file_name != NULL) &&
98 (section_type != NULL) &&
99 (section_name != NULL) &&
102 b_count = strlen(conf_dir_name) + strlen(conf_file_name) + 10;
103 fname = (char *)calloc(1, b_count);
105 strncpy(fname, conf_dir_name, b_count);
106 strncat(fname, "/", 1);
107 strncat(fname, conf_file_name, strlen(conf_file_name) + 1);
108 ctx = uci_alloc_context();
111 uci_set_confdir(ctx, conf_dir_name);
112 if (access(fname, W_OK) != 0) {
113 if (access(fname, F_OK) != 0) {
114 fp = fopen(fname, "w+");
118 if (access(fname, W_OK) == 0) {
119 err_flg = uci_load(ctx, fname, &pkg);
120 uci_foreach_element(&pkg->sections, elem) {
121 tmp_sct = uci_to_section(elem);
122 if (strcmp(tmp_sct->type, section_type) == 0) {
128 //err_flg = uci_add_named_section(ctx, pkg, section_type, section_name, &sct);
129 //err_flg = uci_add_section(ctx, pkg, section_name, &sct);
130 err_flg = uci_create_named_section(ctx, conf_file_name, section_type, section_name);
131 if (err_flg == UCI_OK) {
132 uci_foreach_element(&pkg->sections, elem) {
133 tmp_sct = uci_to_section(elem);
134 if (strcmp(tmp_sct->type, section_type) == 0) {
142 memset(&ptr, 0, sizeof(ptr));
143 ptr.package = pkg->e.name;
144 ptr.section = sct->e.name;
147 if (uci_lookup_ptr(ctx, &ptr, NULL, false) == UCI_OK) {
152 log_debug("Created configuration section %s to configuration file: %s\n", section_name, fname);
155 log_error("Could not write to configuration file: %s\n. Could not look-up pointer for package %s section %s.\n", fname, pkg->e.name, sct->e.name);
157 uci_free_context(ctx);
160 log_error("Could not write to configuration file: %s\n. Could not create section %s.\n", fname, section_name);
164 log_error("Could not write to configuration file: %s. File does not exist or is not writable.\n", fname);
170 log_error("Could not change config value, out of memory\n");
174 log_error("Could not change config value, invalid parameters\n");
180 char work_dir[FILENAME_MAX];
182 getcwd(work_dir, sizeof(work_dir));
183 printf("working directory: %s\n", work_dir);
185 set_config_value2(work_dir,
191 set_config_value2(work_dir,
199 int main(int argc, char** argv) {