Initial Commit
[libctiny.git] / argcargv.cc
blob851f8cb632ac60cdf4359992a9f8e4670f36d5d8
1 //==========================================
2 // LIBCTINY - Matt Pietrek 2001
3 // MSDN Magazine, January 2001
4 //==========================================
5 #include "libctiny.h"
6 #include <windows.h>
7 #include "argcargv.h"
9 #define _MAX_CMD_LINE_ARGS 128
11 char * _ppszArgv[_MAX_CMD_LINE_ARGS+1];
13 int __cdecl _ConvertCommandLineToArgcArgv() {
14 int cbCmdLine;
15 int argc;
16 PSTR pszSysCmdLine, pszCmdLine;
18 // Set to no argv elements, in case we have to bail out
19 _ppszArgv[0] = 0;
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 );
31 if (!pszCmdLine)
32 return 0;
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.
39 pszCmdLine++;
41 _ppszArgv[0] = pszCmdLine; // argv[0] == executable name
43 while (*pszCmdLine && (*pszCmdLine != '"'))
44 pszCmdLine++;
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
51 else // A regular (non-quoted) filename
53 _ppszArgv[0] = pszCmdLine; // argv[0] == executable name
55 while (*pszCmdLine && (' ' != *pszCmdLine) && ('\t' != *pszCmdLine))
56 pszCmdLine++;
58 if (*pszCmdLine)
59 *pszCmdLine++ = 0; // Null terminate and advance to next char
62 // Done processing argv[0] (i.e., the executable name). Now do th
63 // actual arguments
65 argc = 1;
67 while (1)
69 // Skip over any whitespace
70 while (*pszCmdLine && (' ' == *pszCmdLine) || ('\t' == *pszCmdLine))
71 pszCmdLine++;
73 if (0 == *pszCmdLine) // End of command line???
74 return argc;
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 != '"'))
85 pszCmdLine++;
87 if (0 == *pszCmdLine)
88 return argc;
90 if (*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))
100 pszCmdLine++;
102 if (0 == *pszCmdLine)
103 return argc;
105 if (*pszCmdLine)
106 *pszCmdLine++ = 0; // Null terminate and advance to next char
109 if (argc >= (_MAX_CMD_LINE_ARGS))
110 return argc;