diff --git a/src/common/util.c b/src/common/util.c
index 02a638e4ca..c8af6029ec 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -3853,6 +3853,30 @@ get_current_process_environment_variables(void)
return sl;
}
+/** For each string s in env_vars such that
+ * environment_variable_names_equal(s, new_var), remove it; if
+ * free_p is non-zero, call free_old(s). If
+ * new_var contains '=', insert it into env_vars. */
+void
+set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
+ const char *new_var,
+ void (*free_old)(void*),
+ int free_p)
+{
+ SMARTLIST_FOREACH_BEGIN(env_vars, const char *, s) {
+ if (environment_variable_names_equal(s, new_var)) {
+ SMARTLIST_DEL_CURRENT(env_vars, s);
+ if (free_p) {
+ free_old((void *)s);
+ }
+ }
+ } SMARTLIST_FOREACH_END(s);
+
+ if (strchr(new_var, '=') != NULL) {
+ smartlist_add(env_vars, (void *)new_var);
+ }
+}
+
#ifdef _WIN32
/** Read from a handle h into buf, up to count bytes. If
* hProcess is NULL, the function will return immediately if there is
diff --git a/src/common/util.h b/src/common/util.h
index f485446069..9d1baf0a25 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -396,6 +396,11 @@ void process_environment_free(process_environment_t *env);
struct smartlist_t *get_current_process_environment_variables(void);
+void set_environment_variable_in_smartlist(struct smartlist_t *env_vars,
+ const char *new_var,
+ void (*free_old)(void*),
+ int free_p);
+
/* Values of process_handle_t.status. PROCESS_STATUS_NOTRUNNING must be
* 0 because tor_check_port_forwarding depends on this being the initial
* statue of the static instance of process_handle_t */