#include "nssm.h"
+/* Copy an environment block. */
+TCHAR *copy_environment_block(TCHAR *env) {
+ unsigned long len;
+
+ if (! env) return 0;
+ for (len = 0; env[len]; len++) while (env[len]) len++;
+ if (! len++) return 0;
+
+ TCHAR *newenv = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
+ if (! newenv) {
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("copy_environment_block()"), 0);
+ return 0;
+ }
+
+ memmove(newenv, env, len * sizeof(TCHAR));
+ return newenv;
+}
+
/*
The environment block starts with variables of the form
=C:=C:\Windows\System32 which we ignore.
else {
if (! SetEnvironmentVariable(s, NULL)) ret++;
}
- for (t++ ; *t; t++);
+ for (t++; *t; t++);
}
s = t;
}
-1 on error.
*/
int test_environment(TCHAR *env) {
- TCHAR path[PATH_LENGTH];
- GetModuleFileName(0, path, _countof(path));
+ TCHAR *path = (TCHAR *) nssm_imagepath();
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
return 0;
}
+
+/*
+ Duplicate an environment block returned by GetEnvironmentStrings().
+ Since such a block is by definition readonly, and duplicate_environment()
+ modifies its inputs, this function takes a copy of the input and operates
+ on that.
+*/
+void duplicate_environment_strings(TCHAR *env) {
+ TCHAR *newenv = copy_environment_block(env);
+ if (! newenv) return;
+
+ duplicate_environment(newenv);
+ HeapFree(GetProcessHeap(), 0, newenv);
+}
+
+/* Safely get a copy of the current environment. */
+TCHAR *copy_environment() {
+ TCHAR *rawenv = GetEnvironmentStrings();
+ if (! rawenv) return NULL;
+ TCHAR *env = copy_environment_block(rawenv);
+ FreeEnvironmentStrings(rawenv);
+ return env;
+}