1 //==========================================
2 // LIBCTINY - Matt Pietrek 2001
3 // MSDN Magazine, January 2001
4 //==========================================
9 #define _MAX_CMD_LINE_ARGS 128
11 char * _ppszArgv
[_MAX_CMD_LINE_ARGS
+1];
13 int __cdecl
_ConvertCommandLineToArgcArgv() {
16 PSTR pszSysCmdLine
, pszCmdLine
;
18 // Set to no argv elements, in case we have to bail out
21 // First get a pointer to the system's version of the command line, and
22 // figure out how long it is.
23 pszSysCmdLine
= GetCommandLine();
24 cbCmdLine
= lstrlen( pszSysCmdLine
);
26 // Allocate memory to store a copy of the command line. We'll modify
27 // this copy, rather than the original command line. Yes, this memory
28 // currently doesn't explicitly get freed, but it goes away when the
29 // process terminates.
30 pszCmdLine
= (PSTR
)HeapAlloc( GetProcessHeap(), 0, cbCmdLine
+1 );
34 // Copy the system version of the command line into our copy
35 lstrcpyn( pszCmdLine
, pszSysCmdLine
, cbCmdLine
+1);
37 if ('"' == *pszCmdLine
) // If command line starts with a quote ("),
38 { // it's a quoted filename. Skip to next quote.
41 _ppszArgv
[0] = pszCmdLine
; // argv[0] == executable name
43 while (*pszCmdLine
&& (*pszCmdLine
!= '"'))
46 if (*pszCmdLine
) // Did we see a non-NULL ending?
47 *pszCmdLine
++ = 0; // Null terminate and advance to next char
49 return 0; // Oops! We didn't see the end quote
51 else // A regular (non-quoted) filename
53 _ppszArgv
[0] = pszCmdLine
; // argv[0] == executable name
55 while (*pszCmdLine
&& (' ' != *pszCmdLine
) && ('\t' != *pszCmdLine
))
59 *pszCmdLine
++ = 0; // Null terminate and advance to next char
62 // Done processing argv[0] (i.e., the executable name). Now do th
69 // Skip over any whitespace
70 while (*pszCmdLine
&& (' ' == *pszCmdLine
) || ('\t' == *pszCmdLine
))
73 if (0 == *pszCmdLine
) // End of command line???
76 if ('"' == *pszCmdLine
) // Argument starting with a quote???
78 pszCmdLine
++; // Advance past quote character
80 _ppszArgv
[ argc
++ ] = pszCmdLine
;
81 _ppszArgv
[ argc
] = 0;
83 // Scan to end quote, or NULL terminator
84 while (*pszCmdLine
&& (*pszCmdLine
!= '"'))
91 *pszCmdLine
++ = 0; // Null terminate and advance to next char
93 else // Non-quoted argument
95 _ppszArgv
[ argc
++ ] = pszCmdLine
;
96 _ppszArgv
[ argc
] = 0;
98 // Skip till whitespace or NULL terminator
99 while (*pszCmdLine
&& (' '!=*pszCmdLine
) && ('\t'!=*pszCmdLine
))
102 if (0 == *pszCmdLine
)
106 *pszCmdLine
++ = 0; // Null terminate and advance to next char
109 if (argc
>= (_MAX_CMD_LINE_ARGS
))