@@ -2834,6 +2834,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
2834
2834
return -1 ;
2835
2835
}
2836
2836
2837
+ #ifdef ENSURE_MSYSTEM_IS_SET
2838
+ static size_t append_system_bin_dirs (char * path , size_t size )
2839
+ {
2840
+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR )
2841
+ return 0 ;
2842
+ #else
2843
+ char prefix [32768 ];
2844
+ const char * slash ;
2845
+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
2846
+
2847
+ if (len == 0 || len >= sizeof (prefix ) ||
2848
+ !(slash = find_last_dir_sep (prefix )))
2849
+ return 0 ;
2850
+ /* strip trailing `git.exe` */
2851
+ len = slash - prefix ;
2852
+
2853
+ /* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2854
+ if (strip_suffix_mem (prefix , & len , "\\mingw64\\libexec\\git-core" ) ||
2855
+ strip_suffix_mem (prefix , & len , "\\mingw64\\bin" ))
2856
+ off += xsnprintf (path + off , size - off ,
2857
+ "%.*s\\mingw64\\bin;" , (int )len , prefix );
2858
+ else if (strip_suffix_mem (prefix , & len , "\\mingw32\\libexec\\git-core" ) ||
2859
+ strip_suffix_mem (prefix , & len , "\\mingw32\\bin" ))
2860
+ off += xsnprintf (path + off , size - off ,
2861
+ "%.*s\\mingw32\\bin;" , (int )len , prefix );
2862
+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
2863
+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
2864
+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
2865
+ off += xsnprintf (path + off , size - off ,
2866
+ "%.*s\\mingw%d\\bin;" , (int )len , prefix ,
2867
+ (int )(sizeof (void * ) * 8 ));
2868
+ else
2869
+ return 0 ;
2870
+
2871
+ off += xsnprintf (path + off , size - off ,
2872
+ "%.*s\\usr\\bin;" , (int )len , prefix );
2873
+ return off ;
2874
+ #endif
2875
+ }
2876
+ #endif
2877
+
2878
+ static int is_system32_path (const char * path )
2879
+ {
2880
+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2881
+
2882
+ if (xutftowcs_path (wpath , path ) < 0 ||
2883
+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2884
+ _wcsicmp (system32 , wpath ))
2885
+ return 0 ;
2886
+
2887
+ return 1 ;
2888
+ }
2889
+
2837
2890
static void setup_windows_environment (void )
2838
2891
{
2839
2892
char * tmp = getenv ("TMPDIR" );
@@ -2874,7 +2927,8 @@ static void setup_windows_environment(void)
2874
2927
strbuf_addstr (& buf , tmp );
2875
2928
if ((tmp = getenv ("HOMEPATH" ))) {
2876
2929
strbuf_addstr (& buf , tmp );
2877
- if (is_directory (buf .buf ))
2930
+ if (!is_system32_path (buf .buf ) &&
2931
+ is_directory (buf .buf ))
2878
2932
setenv ("HOME" , buf .buf , 1 );
2879
2933
else
2880
2934
tmp = NULL ; /* use $USERPROFILE */
@@ -2885,6 +2939,37 @@ static void setup_windows_environment(void)
2885
2939
if (!tmp && (tmp = getenv ("USERPROFILE" )))
2886
2940
setenv ("HOME" , tmp , 1 );
2887
2941
}
2942
+
2943
+ if (!getenv ("PLINK_PROTOCOL" ))
2944
+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
2945
+
2946
+ #ifdef ENSURE_MSYSTEM_IS_SET
2947
+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
2948
+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
2949
+ char buf [32768 ];
2950
+ size_t off = 0 ;
2951
+
2952
+ xsnprintf (buf , sizeof (buf ),
2953
+ "MINGW%d" , (int )(sizeof (void * ) * 8 ));
2954
+ setenv ("MSYSTEM" , buf , 1 );
2955
+
2956
+ if (home )
2957
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
2958
+ "%s\\bin;" , home );
2959
+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
2960
+ if (path )
2961
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
2962
+ "%s" , path );
2963
+ else if (off > 0 )
2964
+ buf [off - 1 ] = '\0' ;
2965
+ else
2966
+ buf [0 ] = '\0' ;
2967
+ setenv ("PATH" , buf , 1 );
2968
+ }
2969
+ #endif
2970
+
2971
+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
2972
+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
2888
2973
}
2889
2974
2890
2975
static PSID get_current_user_sid (void )
0 commit comments