diff --git a/src/dmd/dinifile.d b/src/dmd/dinifile.d index 4da1a10e452e..dba43ff6a3c3 100644 --- a/src/dmd/dinifile.d +++ b/src/dmd/dinifile.d @@ -153,7 +153,7 @@ void updateRealEnvironment(StringTable* environment) memcpy(s + namelen + 1, value, valuelen); s[namelen + 1 + valuelen] = 0; //printf("envput('%s')\n", s); - putenv(s); + //putenv(s); return 0; // do all of them } diff --git a/src/dmd/link.d b/src/dmd/link.d index 33b7f0791588..466ca7b94370 100644 --- a/src/dmd/link.d +++ b/src/dmd/link.d @@ -32,6 +32,23 @@ version (Windows) extern (C) int putenv(const char*); version (Windows) extern (C) int spawnlp(int, const char*, const char*, const char*, const char*); version (Windows) extern (C) int spawnl(int, const char*, const char*, const char*, const char*); version (Windows) extern (C) int spawnv(int, const char*, const char**); + +string getenv2(const(char)* name) +{ + auto result = getenv(name); + return cast(string)result[0 .. strlen(result)]; +} +int putenv2(const char[] name, const char[] value) +{ + auto fullLength = name.length + 1 + value.length; + auto buffer = cast(char*)alloca(fullLength + 1); + buffer[0 .. name.length] = name[]; + buffer[name.length] = '='; + buffer[name.length + 1 .. fullLength ] = value[]; + buffer[fullLength] = '\0'; + return putenv(buffer); +} + version (CRuntime_Microsoft) { // until the new windows bindings are available when building dmd. @@ -151,6 +168,37 @@ version (Posix) } } +/** +Use to restore an environment variable when this structure goes out of scope. +*/ +struct EnvRestore(string varName) +{ + private string _saved; + private bool restore; + + /** + Access the saved environment variable value. + */ + string saved() const { return _saved; } + + /** + Call before the overriding the environment variable to save and restore it later. + */ + void saveToRestoreLater() + { + _saved = getenv2(varName); + restore = true; + } + + ~this() + { + if (restore) + { + putenv2(varName, _saved); + } + } +} + /***************************** * Run the linker. Return status of execution. */ @@ -264,8 +312,9 @@ public int runLINK() const(char)* linkcmd = getenv(global.params.is64bit ? "LINKCMD64" : "LINKCMD"); if (!linkcmd) linkcmd = getenv("LINKCMD"); // backward compatible + restorePATH = EnvRestore!"PATH"; if (!linkcmd) - linkcmd = vsopt.linkerPath(global.params.is64bit); + linkcmd = vsopt.linkerPath(global.params.is64bit, restorePATH); int status = executecmd(linkcmd, p); if (lnkfilename) @@ -738,15 +787,16 @@ version (Windows) { int status; size_t len; + auto restore_CMDLINE = EnvRestore!"_CMDLINE"; + if (global.params.verbose) message("%s %s", cmd, args); if (!global.params.mscoff) { if ((len = strlen(args)) > 255) { - char* q = cast(char*)alloca(8 + len + 1); - sprintf(q, "_CMDLINE=%s", args); - status = putenv(q); + restore_CMDLINE.saveToRestoreLater(); + int status = putenv2("_CMDLINE", args); if (status == 0) { args = "@_CMDLINE"; @@ -1018,7 +1068,7 @@ version (Windows) * Returns: * absolute path to link.exe, just "link.exe" if not found */ - const(char)* linkerPath(bool x64) + const(char)* linkerPath(bool x64, ref EnvRestore!"PATH" restorePATH) { const(char)* addpath; if (auto p = getVCBinDir(x64, addpath)) @@ -1030,15 +1080,15 @@ version (Windows) { // debug info needs DLLs from $(VSInstallDir)\Common7\IDE for most linker versions // so prepend it too the PATH environment variable - const char* path = getenv("PATH"); - const pathlen = strlen(path); + restorePATH.saveToRestoreLater(); + const char[] path = restorePATH.saved; const addpathlen = strlen(addpath); - char* npath = cast(char*)mem.xmalloc(5 + pathlen + 1 + addpathlen + 1); + char* npath = cast(char*)mem.xmalloc(5 + path.length + 1 + addpathlen + 1); memcpy(npath, "PATH=".ptr, 5); memcpy(npath + 5, addpath, addpathlen); npath[5 + addpathlen] = ';'; - memcpy(npath + 5 + addpathlen + 1, path, pathlen + 1); + memcpy(npath + 5 + addpathlen + 1, path.ptr, path.length + 1); putenv(npath); } return cmdbuf.extractString(); diff --git a/test/runnable/extra-files/printenv.d b/test/runnable/extra-files/printenv.d new file mode 100644 index 000000000000..b8536d200c05 --- /dev/null +++ b/test/runnable/extra-files/printenv.d @@ -0,0 +1,11 @@ +import std.array, std.stdio, std.process, std.algorithm; +void main() +{ + foreach (varPair; environment.toAA().byKeyValue.array.sort!"a.key < b.key") + { + if (varPair.key != "_") + { + writeln(varPair.key, "=", varPair.value); + } + } +} diff --git a/test/runnable/sameenv.sh b/test/runnable/sameenv.sh new file mode 100644 index 000000000000..476809300357 --- /dev/null +++ b/test/runnable/sameenv.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +$DMD -m${MODEL} -of${OUTPUT_BASE}/printenv${EXE} ${EXTRA_FILES}/printenv.d +${OUTPUT_BASE}/printenv${EXE} > ${OUTPUT_BASE}/envFromExe.txt + +$DMD -m${MODEL} -run ${EXTRA_FILES}/printenv.d > ${OUTPUT_BASE}/envFromRun.txt + +diff -p ${OUTPUT_BASE}/envFromExe.txt ${OUTPUT_BASE}/envFromRun.txt + +rm -f ${OUTPUT_BASE}/*