Brandon Williams wrote: > On 04/10, Jonathan Nieder wrote: >> struct argv_array result = ARGV_ARRAY_INIT; >> struct string_list mods = STRING_LIST_INIT_DUP; >> struct strbuf key = STRBUF_INIT; >> const char **p; >> >> for (p = cmd_env; *p; p++) { >> const char *equals = strchr(*p, '='); >> if (equals) { >> strbuf_reset(&key); >> strbuf_add(&key, *p, equals - *p); >> string_list_append(&mods, key.buf)->util = *p; >> } else { >> string_list_append(&mods, *p); >> } >> } >> string_list_sort(&mods); >> >> for (p = environ; *p; p++) { >> struct string_list_item *item; >> const char *equals = strchr(*p, '='); >> if (!equals) >> continue; >> strbuf_reset(&key); >> strbuf_add(&key, *p, equals - *p); >> item = string_list_lookup(&mods, key.buf); >> >> if (!item) /* no change */ >> argv_array_push(&result, *p); >> else if (!item->util) /* unsetenv */ >> ; /* skip */ >> else /* setenv */ >> argv_array_push(&result, item->util); >> } >> >> strbuf_release(&key); >> string_list_clear(&mods); >> return argv_array_detach(&result); > > This is probably still incomplete as I don't see how this accounts for > entries in 'cmd_env' which are being added to the environment and not > just replacing existing ones. Yes, that's true. This sample code is incomplete since it doesn't handle those. Jonathan