From 95493a89a2f82e8f13576e912dc99d3451521c21 Mon Sep 17 00:00:00 2001 From: Nakidai Date: Fri, 25 Oct 2024 14:36:49 +0300 Subject: Fix env breaking I was thinking that changing `env` (the third argument of the `main`) won't change the env. I was wrong. Now it uses its own buffer for copying environment variable names into it. --- througha.c | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/througha.c b/througha.c index f12793c..1d0c3d5 100644 --- a/througha.c +++ b/througha.c @@ -18,23 +18,26 @@ void usage(const char *name, const char *message) exit(1); } +size_t get_envname(char *dest, const char *var) +{ + size_t i = 0; + while ((dest[i] = tolower(var[i])) != '=') ++i; + dest[i] = '\0'; + return i; +} + int main(int argc, char **argv, char **env) { + char envname[128]; + if (argc < 2) usage(argv[0], "no proxy was specified"); else if (argc < 3) usage(argv[0], "no command was specified"); - for (char **envp = env; *envp != NULL; ++envp) - { - *strchr(*envp, '=') = '\0'; - - for (char *cp = *envp; *cp != '\0'; ++cp) - *cp = tolower(*cp); - - if (strstr(*envp, "_proxy")) - unsetenv(*envp); - } + for (char **envp = env; *envp != NULL; ++envp, get_envname(envname, *envp)) + if (strstr(envname, "_proxy")) + unsetenv(envname); if (strcmp(argv[1], "-")) setenv("all_proxy", argv[1], 1); -- cgit 1.4.1