Win32: Unicode arguments (incoming)

Convert command line arguments from UTF-16 to UTF-8 on startup. Signed-off-by: Karsten Blees <blees@dcon.de> Signed-off-by: Stepan Kasal <kasal@ucw.cz> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karsten Blees committed Jan 16, 2011 at 18:28 UTC 3f046148d92e1aa6c0872a6f11f39a4a5740f939
1 file changed +40 -2
compat/mingw.c
+40 -2
@@ -1943,10 +1943,48 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
1943 */
1944 int _CRT_glob = 0;
1945
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 +
1959 void mingw_startup()
1960 {
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);
1988
1989 /* initialize critical section for waitpid pinfo_t list */
1990 InitializeCriticalSection(&pinfo_cs);