OLD | NEW |
| (Empty) |
1 //========================================== | |
2 // LIBCTINY - Matt Pietrek 2001 | |
3 // MSDN Magazine, January 2001 | |
4 //========================================== | |
5 #include "libctiny.h" | |
6 #include <windows.h> | |
7 #include "argcargv.h" | |
8 | |
9 #define _MAX_CMD_LINE_ARGS 128 | |
10 | |
11 char * _ppszArgv[_MAX_CMD_LINE_ARGS+1]; | |
12 | |
13 int __cdecl _ConvertCommandLineToArgcArgv() { | |
14 int cbCmdLine; | |
15 int argc; | |
16 PSTR pszSysCmdLine, pszCmdLine; | |
17 | |
18 // Set to no argv elements, in case we have to bail out | |
19 _ppszArgv[0] = 0; | |
20 | |
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 ); | |
25 | |
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 ); | |
31 if (!pszCmdLine) | |
32 return 0; | |
33 | |
34 // Copy the system version of the command line into our copy | |
35 lstrcpyn( pszCmdLine, pszSysCmdLine , cbCmdLine+1); | |
36 | |
37 if ('"' == *pszCmdLine) // If command line starts with a quote ("), | |
38 { // it's a quoted filename. Skip to next quote. | |
39 pszCmdLine++; | |
40 | |
41 _ppszArgv[0] = pszCmdLine; // argv[0] == executable name | |
42 | |
43 while (*pszCmdLine && (*pszCmdLine != '"')) | |
44 pszCmdLine++; | |
45 | |
46 if (*pszCmdLine) // Did we see a non-NULL ending? | |
47 *pszCmdLine++ = 0; // Null terminate and advance to next char | |
48 else | |
49 return 0; // Oops! We didn't see the end quote | |
50 } | |
51 else // A regular (non-quoted) filename | |
52 { | |
53 _ppszArgv[0] = pszCmdLine; // argv[0] == executable name | |
54 | |
55 while (*pszCmdLine && (' ' != *pszCmdLine) && ('\t' != *pszCmdLine)) | |
56 pszCmdLine++; | |
57 | |
58 if (*pszCmdLine) | |
59 *pszCmdLine++ = 0; // Null terminate and advance to next char | |
60 } | |
61 | |
62 // Done processing argv[0] (i.e., the executable name). Now do th | |
63 // actual arguments | |
64 | |
65 argc = 1; | |
66 | |
67 while (1) | |
68 { | |
69 // Skip over any whitespace | |
70 while (*pszCmdLine && (' ' == *pszCmdLine) || ('\t' == *pszCmdLine)) | |
71 pszCmdLine++; | |
72 | |
73 if (0 == *pszCmdLine) // End of command line??? | |
74 return argc; | |
75 | |
76 if ('"' == *pszCmdLine) // Argument starting with a quote??? | |
77 { | |
78 pszCmdLine++; // Advance past quote character | |
79 | |
80 _ppszArgv[ argc++ ] = pszCmdLine; | |
81 _ppszArgv[ argc ] = 0; | |
82 | |
83 // Scan to end quote, or NULL terminator | |
84 while (*pszCmdLine && (*pszCmdLine != '"')) | |
85 pszCmdLine++; | |
86 | |
87 if (0 == *pszCmdLine) | |
88 return argc; | |
89 | |
90 if (*pszCmdLine) | |
91 *pszCmdLine++ = 0; // Null terminate and advance to next char | |
92 } | |
93 else // Non-quoted argument | |
94 { | |
95 _ppszArgv[ argc++ ] = pszCmdLine; | |
96 _ppszArgv[ argc ] = 0; | |
97 | |
98 // Skip till whitespace or NULL terminator | |
99 while (*pszCmdLine && (' '!=*pszCmdLine) && ('\t'!=*pszCmdLine)) | |
100 pszCmdLine++; | |
101 | |
102 if (0 == *pszCmdLine) | |
103 return argc; | |
104 | |
105 if (*pszCmdLine) | |
106 *pszCmdLine++ = 0; // Null terminate and advance to next char | |
107 } | |
108 | |
109 if (argc >= (_MAX_CMD_LINE_ARGS)) | |
110 return argc; | |
111 } | |
112 } | |
OLD | NEW |