@@ -2878,6 +2878,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
2878
2878
return -1 ;
2879
2879
}
2880
2880
2881
+ #ifdef ENSURE_MSYSTEM_IS_SET
2882
+ static size_t append_system_bin_dirs (char * path , size_t size )
2883
+ {
2884
+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR )
2885
+ return 0 ;
2886
+ #else
2887
+ char prefix [32768 ];
2888
+ const char * slash ;
2889
+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
2890
+
2891
+ if (len == 0 || len >= sizeof (prefix ) ||
2892
+ !(slash = find_last_dir_sep (prefix )))
2893
+ return 0 ;
2894
+ /* strip trailing `git.exe` */
2895
+ len = slash - prefix ;
2896
+
2897
+ /* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2898
+ if (strip_suffix_mem (prefix , & len , "\\mingw64\\libexec\\git-core" ) ||
2899
+ strip_suffix_mem (prefix , & len , "\\mingw64\\bin" ))
2900
+ off += xsnprintf (path + off , size - off ,
2901
+ "%.*s\\mingw64\\bin;" , (int )len , prefix );
2902
+ else if (strip_suffix_mem (prefix , & len , "\\mingw32\\libexec\\git-core" ) ||
2903
+ strip_suffix_mem (prefix , & len , "\\mingw32\\bin" ))
2904
+ off += xsnprintf (path + off , size - off ,
2905
+ "%.*s\\mingw32\\bin;" , (int )len , prefix );
2906
+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
2907
+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
2908
+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
2909
+ off += xsnprintf (path + off , size - off ,
2910
+ "%.*s\\mingw%d\\bin;" , (int )len , prefix ,
2911
+ (int )(sizeof (void * ) * 8 ));
2912
+ else
2913
+ return 0 ;
2914
+
2915
+ off += xsnprintf (path + off , size - off ,
2916
+ "%.*s\\usr\\bin;" , (int )len , prefix );
2917
+ return off ;
2918
+ #endif
2919
+ }
2920
+ #endif
2921
+
2922
+ static int is_system32_path (const char * path )
2923
+ {
2924
+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2925
+
2926
+ if (xutftowcs_path (wpath , path ) < 0 ||
2927
+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2928
+ _wcsicmp (system32 , wpath ))
2929
+ return 0 ;
2930
+
2931
+ return 1 ;
2932
+ }
2933
+
2881
2934
static void setup_windows_environment (void )
2882
2935
{
2883
2936
char * tmp = getenv ("TMPDIR" );
@@ -2918,7 +2971,8 @@ static void setup_windows_environment(void)
2918
2971
strbuf_addstr (& buf , tmp );
2919
2972
if ((tmp = getenv ("HOMEPATH" ))) {
2920
2973
strbuf_addstr (& buf , tmp );
2921
- if (is_directory (buf .buf ))
2974
+ if (!is_system32_path (buf .buf ) &&
2975
+ is_directory (buf .buf ))
2922
2976
setenv ("HOME" , buf .buf , 1 );
2923
2977
else
2924
2978
tmp = NULL ; /* use $USERPROFILE */
@@ -2929,6 +2983,37 @@ static void setup_windows_environment(void)
2929
2983
if (!tmp && (tmp = getenv ("USERPROFILE" )))
2930
2984
setenv ("HOME" , tmp , 1 );
2931
2985
}
2986
+
2987
+ if (!getenv ("PLINK_PROTOCOL" ))
2988
+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
2989
+
2990
+ #ifdef ENSURE_MSYSTEM_IS_SET
2991
+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
2992
+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
2993
+ char buf [32768 ];
2994
+ size_t off = 0 ;
2995
+
2996
+ xsnprintf (buf , sizeof (buf ),
2997
+ "MINGW%d" , (int )(sizeof (void * ) * 8 ));
2998
+ setenv ("MSYSTEM" , buf , 1 );
2999
+
3000
+ if (home )
3001
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3002
+ "%s\\bin;" , home );
3003
+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
3004
+ if (path )
3005
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3006
+ "%s" , path );
3007
+ else if (off > 0 )
3008
+ buf [off - 1 ] = '\0' ;
3009
+ else
3010
+ buf [0 ] = '\0' ;
3011
+ setenv ("PATH" , buf , 1 );
3012
+ }
3013
+ #endif
3014
+
3015
+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
3016
+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
2932
3017
}
2933
3018
2934
3019
static PSID get_current_user_sid (void )
0 commit comments