summaryrefslogtreecommitdiff
path: root/Shell
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-05-30 02:51:15 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-05-30 03:22:18 +0200
commit96db775ac1ff5df64957d1a9873f0955c3d4cb17 (patch)
tree8b788ebcd509910dde90862fc22688565d3ea48d /Shell
parentd31ce9eccdc051cb38b414c1c72aeee21af8a086 (diff)
downloadserenity-96db775ac1ff5df64957d1a9873f0955c3d4cb17.zip
LibC: Add setenv().
If I'm understanding the standard C library correctly, setenv() copies while putenv() does not. That's really confusing and putenv() basically sucks. To know which environment variables to free on replacement and which ones to leave alone, we keep track of the ones malloced by setenv in a side table. This patch also moves Shell to using setenv() instead of putenv(). Fixes #29.
Diffstat (limited to 'Shell')
-rw-r--r--Shell/main.cpp12
1 files changed, 2 insertions, 10 deletions
diff --git a/Shell/main.cpp b/Shell/main.cpp
index b596fc2c93..47a2bc2ef9 100644
--- a/Shell/main.cpp
+++ b/Shell/main.cpp
@@ -72,13 +72,7 @@ static int sh_export(int argc, char** argv)
return 1;
}
- // FIXME: Yes, this leaks.
- // Maybe LibCore should grow a CEnvironment which is secretly a map to char*,
- // so it can keep track of the environment pointers as needed?
- const auto& s = String::format("%s=%s", parts[0].characters(), parts[1].characters());
- char *ev = strndup(s.characters(), s.length());
- putenv(ev);
- return 0;
+ return setenv(parts[0].characters(), parts[1].characters(), 1);
}
static int sh_unset(int argc, char** argv)
@@ -494,9 +488,7 @@ int main(int argc, char** argv)
if (pw) {
g.username = pw->pw_name;
g.home = pw->pw_dir;
- const auto& s = String::format("HOME=%s", pw->pw_dir);
- char *ev = strndup(s.characters(), s.length());
- putenv(ev);
+ setenv("HOME", pw->pw_dir, 1);
}
endpwent();
}