Skip to content

Commit 3f04614

Browse files
kbleesgitster
authored andcommitted
Win32: Unicode arguments (incoming)
Convert command line arguments from UTF-16 to UTF-8 on startup. Signed-off-by: Karsten Blees <[email protected]> Signed-off-by: Stepan Kasal <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 99c3c76 commit 3f04614

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

compat/mingw.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,10 +1943,48 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
19431943
*/
19441944
int _CRT_glob = 0;
19451945

1946+
typedef struct {
1947+
int newmode;
1948+
} _startupinfo;
1949+
1950+
extern int __wgetmainargs(int *argc, wchar_t ***argv, wchar_t ***env, int glob,
1951+
_startupinfo *si);
1952+
1953+
static NORETURN void die_startup()
1954+
{
1955+
fputs("fatal: not enough memory for initialization", stderr);
1956+
exit(128);
1957+
}
1958+
19461959
void mingw_startup()
19471960
{
1948-
/* copy executable name to argv[0] */
1949-
__argv[0] = xstrdup(_pgmptr);
1961+
int i, len, maxlen, argc;
1962+
char *buffer;
1963+
wchar_t **wenv, **wargv;
1964+
_startupinfo si;
1965+
1966+
/* get wide char arguments and environment */
1967+
si.newmode = 0;
1968+
if (__wgetmainargs(&argc, &wargv, &wenv, _CRT_glob, &si) < 0)
1969+
die_startup();
1970+
1971+
/* determine size of argv and environ conversion buffer */
1972+
maxlen = wcslen(_wpgmptr);
1973+
for (i = 1; i < argc; i++)
1974+
maxlen = max(maxlen, wcslen(wargv[i]));
1975+
1976+
/* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
1977+
maxlen = 3 * maxlen + 1;
1978+
buffer = xmalloc(maxlen);
1979+
1980+
/* convert command line arguments and environment to UTF-8 */
1981+
len = xwcstoutf(buffer, _wpgmptr, maxlen);
1982+
__argv[0] = xmemdupz(buffer, len);
1983+
for (i = 1; i < argc; i++) {
1984+
len = xwcstoutf(buffer, wargv[i], maxlen);
1985+
__argv[i] = xmemdupz(buffer, len);
1986+
}
1987+
free(buffer);
19501988

19511989
/* initialize critical section for waitpid pinfo_t list */
19521990
InitializeCriticalSection(&pinfo_cs);

0 commit comments

Comments
 (0)