@@ -34,49 +34,48 @@ dup_handle(HANDLE input, HANDLE *output)
3434
3535
3636int
37- launch (const wchar_t *executable, const wchar_t *insert_args, int skip_argc, DWORD *exitCode)
38- {
37+ launch (
38+ const wchar_t *executable,
39+ const wchar_t *origCmdLine,
40+ const wchar_t *insert_args,
41+ int skip_argc,
42+ DWORD *exitCode
43+ ) {
3944 HANDLE job;
4045 JOBOBJECT_EXTENDED_LIMIT_INFORMATION info;
4146 DWORD info_len;
4247 STARTUPINFOW si;
4348 PROCESS_INFORMATION pi;
4449 int lastError = 0 ;
45- const wchar_t *arg_space = L" " ;
46- LPCWSTR origCmdLine = GetCommandLineW ();
4750 const wchar_t *cmdLine = NULL ;
4851
49- if (insert_args == NULL ) {
50- insert_args = L" " ;
52+ if (origCmdLine[0 ] == L' "' ) {
53+ cmdLine = wcschr (origCmdLine + 1 , L' "' );
54+ } else {
55+ cmdLine = wcschr (origCmdLine, L' ' );
5156 }
5257
53- size_t n = wcslen (executable) + wcslen (origCmdLine) + wcslen (insert_args) + 5 ;
58+ size_t n = wcslen (executable) + wcslen (origCmdLine) + wcslen (insert_args) + 6 ;
5459 wchar_t *newCmdLine = (wchar_t *)HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, n * sizeof (wchar_t ));
5560 if (!newCmdLine) {
5661 lastError = GetLastError ();
5762 goto exit;
5863 }
5964
60- if (origCmdLine[0 ] == L' "' ) {
61- cmdLine = wcschr (origCmdLine + 1 , L' "' );
62- } else {
63- cmdLine = wcschr (origCmdLine, L' ' );
64- }
65-
65+ // Skip any requested args, deliberately leaving any trailing spaces
66+ // (we'll skip one later one and add our own space, and preserve multiple)
6667 while (skip_argc-- > 0 ) {
6768 wchar_t c;
6869 while (*++cmdLine && *cmdLine == L' ' ) { }
6970 while (*++cmdLine && *cmdLine != L' ' ) { }
7071 }
7172
72- if (!insert_args || !*insert_args) {
73- arg_space = L" " ;
74- }
75- if (cmdLine && *cmdLine) {
76- swprintf_s (newCmdLine, n + 1 , L" \" %s\" %s%s %s" , executable, arg_space, insert_args, cmdLine + 1 );
77- } else {
78- swprintf_s (newCmdLine, n + 1 , L" \" %s\" %s%s" , executable, arg_space, insert_args);
79- }
73+ swprintf_s (newCmdLine, n, L" \" %s\" %s%s%s%s" ,
74+ executable,
75+ (insert_args && *insert_args) ? L" " : L" " ,
76+ (insert_args && *insert_args) ? insert_args : L" " ,
77+ (cmdLine && *cmdLine) ? L" " : L" " ,
78+ (cmdLine && *cmdLine) ? cmdLine + 1 : L" " );
8079
8180#if defined(_WINDOWS)
8281 /*
0 commit comments