+static struct ec_config *
+ec_config_list_dup(const struct ec_config_list *list)
+{
+ struct ec_config *dup = NULL, *v, *value;
+
+ dup = ec_config_list();
+ if (dup == NULL)
+ goto fail;
+
+ TAILQ_FOREACH(v, list, next) {
+ value = ec_config_dup(v);
+ if (value == NULL)
+ goto fail;
+ if (ec_config_list_add(dup, value) < 0)
+ goto fail;
+ }
+
+ return dup;
+
+fail:
+ ec_config_free(dup);
+ return NULL;
+}
+
+static struct ec_config *
+ec_config_dict_dup(const struct ec_keyval *dict)
+{
+ struct ec_config *dup = NULL, *value;
+ struct ec_keyval_iter *iter = NULL;
+ const char *key;
+
+ dup = ec_config_dict();
+ if (dup == NULL)
+ goto fail;
+
+ for (iter = ec_keyval_iter(dict);
+ ec_keyval_iter_valid(iter);
+ ec_keyval_iter_next(iter)) {
+ key = ec_keyval_iter_get_key(iter);
+ value = ec_config_dup(ec_keyval_iter_get_val(iter));
+ if (value == NULL)
+ goto fail;
+ if (ec_config_dict_set(dup, key, value) < 0)
+ goto fail;
+ }
+ ec_keyval_iter_free(iter);
+
+ return dup;
+
+fail:
+ ec_config_free(dup);
+ ec_keyval_iter_free(iter);
+ return NULL;
+}
+
+struct ec_config *
+ec_config_dup(const struct ec_config *config)
+{
+ if (config == NULL) {
+ errno = EINVAL;
+ return NULL;
+ }
+
+ switch (config->type) {
+ case EC_CONFIG_TYPE_BOOL:
+ return ec_config_bool(config->boolean);
+ case EC_CONFIG_TYPE_INT64:
+ return ec_config_i64(config->i64);
+ case EC_CONFIG_TYPE_UINT64:
+ return ec_config_u64(config->u64);
+ case EC_CONFIG_TYPE_STRING:
+ return ec_config_string(config->string);
+ case EC_CONFIG_TYPE_NODE:
+ return ec_config_node(ec_node_clone(config->node));
+ case EC_CONFIG_TYPE_LIST:
+ return ec_config_list_dup(&config->list);
+ case EC_CONFIG_TYPE_DICT:
+ return ec_config_dict_dup(config->dict);
+ default:
+ errno = EINVAL;
+ break;
+ }
+
+ return NULL;
+}
+