summary refs log tree commit diff
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2024-10-25 14:36:49 +0300
committerNakidai <nakidai@disroot.org>2024-10-25 14:36:49 +0300
commit95493a89a2f82e8f13576e912dc99d3451521c21 (patch)
tree2a004233ff7f1141d551d1b70e00d0c06cbd1708
parentafd4daf9217ba7e8dcc9ca31ceb32f93ded7f12c (diff)
downloadthrougha-1.0.1.tar.gz
througha-1.0.1.zip
Fix env breaking v1.0.1
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.
-rw-r--r--througha.c23
1 files 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);