}\r
\r
if (envlen) {\r
- /* Replace NULL with CRLF. Leave NULL NULL as the end marker. */\r
- unsigned long i, j;\r
- unsigned long newlen = envlen;\r
- for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) newlen++;\r
-\r
- TCHAR *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR));\r
- if (formatted) {\r
- for (i = 0, j = 0; i < envlen; i++) {\r
- formatted[j] = env[i];\r
- if (! env[i]) {\r
- if (env[i + 1]) {\r
- formatted[j] = _T('\r');\r
- formatted[++j] = _T('\n');\r
- }\r
- }\r
- j++;\r
- }\r
+ TCHAR *formatted;\r
+ unsigned long newlen;\r
+ if (format_environment(env, envlen, &formatted, &newlen)) {\r
+ popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("nssm_dlg()"));\r
+ }\r
+ else {\r
SetDlgItemText(tablist[NSSM_TAB_ENVIRONMENT], IDC_ENVIRONMENT, formatted);\r
HeapFree(GetProcessHeap(), 0, formatted);\r
}\r
- else popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("nssm_dlg()"));\r
}\r
if (service->envlen && service->env_extralen) popup_message(MB_OK | MB_ICONWARNING, NSSM_GUI_WARN_ENVIRONMENT);\r
}\r
return 5;\r
}\r
\r
- /* Strip CR and replace LF with NULL. */\r
- unsigned long newlen = 0;\r
- unsigned long i, j;\r
- for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) newlen++;\r
- /* Must end with two NULLs. */\r
- newlen += 2;\r
-\r
- TCHAR *newenv = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, newlen * sizeof(TCHAR));\r
- if (! newenv) {\r
+ TCHAR *newenv;\r
+ unsigned long newlen;\r
+ if (unformat_environment(env, envlen, &newenv, &newlen)) {\r
HeapFree(GetProcessHeap(), 0, env);\r
popup_message(MB_OK | MB_ICONEXCLAMATION, NSSM_EVENT_OUT_OF_MEMORY, _T("environment"), _T("install()"));\r
cleanup_nssm_service(service);\r
return 5;\r
}\r
\r
- for (i = 0, j = 0; i < envlen; i++) {\r
- if (env[i] == _T('\r')) continue;\r
- if (env[i] == _T('\n')) newenv[j] = _T('\0');\r
- else newenv[j] = env[i];\r
- j++;\r
- }\r
-\r
HeapFree(GetProcessHeap(), 0, env);\r
env = newenv;\r
envlen = newlen;\r
return 0;\r
}\r
\r
+/* Replace NULL with CRLF. Leave NULL NULL as the end marker. */\r
+int format_environment(TCHAR *env, unsigned long envlen, TCHAR **formatted, unsigned long *newlen) {\r
+ unsigned long i, j;\r
+ *newlen = envlen;\r
+\r
+ if (! *newlen) {\r
+ *formatted = 0;\r
+ return 0;\r
+ }\r
+\r
+ for (i = 0; i < envlen; i++) if (! env[i] && env[i + 1]) ++*newlen;\r
+\r
+ *formatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));\r
+ if (! *formatted) {\r
+ *newlen = 0;\r
+ return 1;\r
+ }\r
+\r
+ for (i = 0, j = 0; i < envlen; i++) {\r
+ (*formatted)[j] = env[i];\r
+ if (! env[i]) {\r
+ if (env[i + 1]) {\r
+ (*formatted)[j] = _T('\r');\r
+ (*formatted)[++j] = _T('\n');\r
+ }\r
+ }\r
+ j++;\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+/* Strip CR and replace LF with NULL. */\r
+int unformat_environment(TCHAR *env, unsigned long envlen, TCHAR **unformatted, unsigned long *newlen) {\r
+ unsigned long i, j;\r
+ *newlen = 0;\r
+\r
+ if (! envlen) {\r
+ *unformatted = 0;\r
+ return 0;\r
+ }\r
+\r
+ for (i = 0; i < envlen; i++) if (env[i] != _T('\r')) ++*newlen;\r
+ /* Must end with two NULLs. */\r
+ *newlen += 2;\r
+\r
+ *unformatted = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *newlen * sizeof(TCHAR));\r
+ if (! *unformatted) return 1;\r
+\r
+ for (i = 0, j = 0; i < envlen; i++) {\r
+ if (env[i] == _T('\r')) continue;\r
+ if (env[i] == _T('\n')) (*unformatted)[j] = _T('\0');\r
+ else (*unformatted)[j] = env[i];\r
+ j++;\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) {\r
TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen);\r
if (! buffer) {\r
int create_parameters(nssm_service_t *, bool);\r
int create_exit_action(TCHAR *, const TCHAR *, bool);\r
int set_environment(TCHAR *, HKEY, TCHAR *, TCHAR **, unsigned long *);\r
+int format_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);\r
+int unformat_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *);\r
int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool, bool);\r
int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool);\r
int set_expand_string(HKEY, TCHAR *, TCHAR *);\r