Last active
October 4, 2024 17:31
-
-
Save oguz-ismail/707c875d48f0324bd265ba6cff244214 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#define DELIM(c) ((c) == ' ' || (c) == '\t') | |
static int | |
maybe_unescape(const TCHAR **src, volatile TCHAR **dst, int quot) { | |
const TCHAR *p; | |
int i, n; | |
p = *src; | |
if (*p == '\\') { | |
while (*++p == '\\'); | |
if (*p != '"') | |
return 0; | |
n = p-*src; | |
for (i = 0; i < n/2; i++) | |
*(*dst)++ = '\\'; | |
if (n%2 != 0) | |
*(*dst)++ = '"'; | |
else | |
p--; | |
} | |
else if (quot && *p == '"') { | |
for (; *(p+1) == '"'; p++); | |
if (p == *src) | |
return 0; | |
n = p-*src + 2; | |
for (i = 0; i < n/3; i++) | |
*(*dst)++ = '"'; | |
if (n%3 != 1) | |
p--; | |
} | |
else { | |
return 0; | |
} | |
*src = p; | |
return 1; | |
} | |
int | |
mainCRTStartup(void) { | |
static TCHAR buf[32767]; | |
static TCHAR *argv[ARRAYSIZE(buf)/2]; | |
int argc; | |
const TCHAR *p; | |
TCHAR *q; | |
int quot; | |
p = GetCommandLine(); | |
argv[0] = q = buf; | |
quot = 0; | |
for (; DELIM(*p); p++); | |
for (; *p; p++) | |
if (!quot && DELIM(*p)) | |
break; | |
else if (*p == '"') | |
quot = !quot; | |
else | |
*q++ = *p; | |
*q++ = 0; | |
for (argc = 1; ; argc++) { | |
for (; DELIM(*p); p++); | |
if (!*p) | |
break; | |
argv[argc] = q; | |
quot = 0; | |
for (; *p; p++) | |
if (!quot && DELIM(*p)) | |
break; | |
else if (maybe_unescape(&p, &q, quot)) | |
continue; | |
else if (*p == '"') | |
quot = !quot; | |
else | |
*q++ = *p; | |
*q++ = 0; | |
} | |
argv[argc] = 0; | |
ExitProcess( | |
#ifdef UNICODE | |
wmain | |
#else | |
main | |
#endif | |
(argc, argv)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment