3 /* Replace NULL with CRLF. Leave NULL NULL as the end marker. */
4 int format_environment(TCHAR *env, unsigned long envlen, TCHAR **formatted, unsigned long *newlen) {
13 for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) ++*newlen;
15 *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
21 for (i = 0, j = 0; i < envlen; i++) {
22 (*formatted)[j] = env[i];
25 (*formatted)[j] = _T('\r');
26 (*formatted)[++j] = _T('\n');
35 /* Strip CR and replace LF with NULL. */
36 int unformat_environment(TCHAR *env, unsigned long envlen, TCHAR **unformatted, unsigned long *newlen) {
45 for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) ++*newlen;
46 /* Must end with two NULLs. */
49 *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));
50 if (! *unformatted) return 1;
52 for (i = 0, j = 0; i < envlen; i++) {
53 if (env[i] == _T('\r')) continue;
54 if (env[i] == _T('\n')) (*unformatted)[j] = _T('\0');
55 else (*unformatted)[j] = env[i];
63 Verify an environment block.
64 Returns: 1 if environment is invalid.
65 0 if environment is OK.
68 int test_environment(TCHAR *env) {
69 TCHAR path[PATH_LENGTH];
70 GetModuleFileName(0, path, _countof(path));
72 ZeroMemory(&si, sizeof(si));
74 PROCESS_INFORMATION pi;
75 ZeroMemory(&pi, sizeof(pi));
76 unsigned long flags = CREATE_SUSPENDED;
78 flags |= CREATE_UNICODE_ENVIRONMENT;
82 Try to relaunch ourselves but with the candidate environment set.
83 Assuming no solar flare activity, the only reason this would fail is if
84 the environment were invalid.
86 if (CreateProcess(0, path, 0, 0, 0, flags, env, 0, &si, &pi)) {
87 TerminateProcess(pi.hProcess, 0);
90 unsigned long error = GetLastError();
91 if (error == ERROR_INVALID_PARAMETER) return 1;