+ UCI_HANDLE_ERR(ctx);
+ UCI_ASSERT(ctx, res != NULL);
+ UCI_ASSERT(ctx, p != NULL);
+ UCI_ASSERT(ctx, section != NULL);
+
+ e = uci_lookup_list(ctx, &p->sections, section);
+ if (!e)
+ goto notfound;
+
+ if (option) {
+ s = uci_to_section(e);
+ e = uci_lookup_list(ctx, &s->options, option);
+ }
+
+ *res = e;
+ return 0;
+
+notfound:
+ UCI_THROW(ctx, UCI_ERR_NOTFOUND);
+ return 0;
+}
+
+int uci_set_element_value(struct uci_context *ctx, struct uci_element **element, char *value)
+{
+ int size;
+ char *str;
+ struct uci_list *list;
+ struct uci_element *e;
+
+ UCI_HANDLE_ERR(ctx);
+ UCI_ASSERT(ctx, value != NULL);
+ UCI_ASSERT(ctx, element != NULL);
+ UCI_ASSERT(ctx, *element != NULL);
+
+ /* what the 'value' of an element means depends on the type
+ * for a section, the 'value' means its type
+ * for an option, the 'value' means its value string
+ * when changing the value, shrink the element to its actual size
+ * (it may have been allocated with a bigger size, to include
+ * its buffer)
+ * then duplicate the string passed on the command line and
+ * insert it into the structure.
+ */
+ e = *element;
+ list = e->list.prev;
+ switch(e->type) {
+ case UCI_TYPE_SECTION:
+ size = sizeof(struct uci_section);
+ break;
+ case UCI_TYPE_OPTION:
+ size = sizeof(struct uci_option);
+ break;
+ default:
+ UCI_THROW(ctx, UCI_ERR_INVAL);
+ break;
+ }
+
+ uci_list_del(&e->list);
+ e = uci_realloc(ctx, e, size);
+ str = uci_strdup(ctx, value);
+ uci_list_insert(list, &e->list);
+ *element = e;
+
+ switch(e->type) {
+ case UCI_TYPE_SECTION:
+ uci_to_section(e)->type = value;
+ break;
+ case UCI_TYPE_OPTION:
+ uci_to_option(e)->value = value;
+ break;
+ default:
+ break;