@@ -2865,6 +2865,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
2865
2865
return -1 ;
2866
2866
}
2867
2867
2868
+ #ifdef ENSURE_MSYSTEM_IS_SET
2869
+ static size_t append_system_bin_dirs (char * path , size_t size )
2870
+ {
2871
+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR )
2872
+ return 0 ;
2873
+ #else
2874
+ char prefix [32768 ];
2875
+ const char * slash ;
2876
+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
2877
+
2878
+ if (len == 0 || len >= sizeof (prefix ) ||
2879
+ !(slash = find_last_dir_sep (prefix )))
2880
+ return 0 ;
2881
+ /* strip trailing `git.exe` */
2882
+ len = slash - prefix ;
2883
+
2884
+ /* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
2885
+ if (strip_suffix_mem (prefix , & len , "\\mingw64\\libexec\\git-core" ) ||
2886
+ strip_suffix_mem (prefix , & len , "\\mingw64\\bin" ))
2887
+ off += xsnprintf (path + off , size - off ,
2888
+ "%.*s\\mingw64\\bin;" , (int )len , prefix );
2889
+ else if (strip_suffix_mem (prefix , & len , "\\mingw32\\libexec\\git-core" ) ||
2890
+ strip_suffix_mem (prefix , & len , "\\mingw32\\bin" ))
2891
+ off += xsnprintf (path + off , size - off ,
2892
+ "%.*s\\mingw32\\bin;" , (int )len , prefix );
2893
+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
2894
+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
2895
+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
2896
+ off += xsnprintf (path + off , size - off ,
2897
+ "%.*s\\mingw%d\\bin;" , (int )len , prefix ,
2898
+ (int )(sizeof (void * ) * 8 ));
2899
+ else
2900
+ return 0 ;
2901
+
2902
+ off += xsnprintf (path + off , size - off ,
2903
+ "%.*s\\usr\\bin;" , (int )len , prefix );
2904
+ return off ;
2905
+ #endif
2906
+ }
2907
+ #endif
2908
+
2909
+ static int is_system32_path (const char * path )
2910
+ {
2911
+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
2912
+
2913
+ if (xutftowcs_path (wpath , path ) < 0 ||
2914
+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
2915
+ _wcsicmp (system32 , wpath ))
2916
+ return 0 ;
2917
+
2918
+ return 1 ;
2919
+ }
2920
+
2868
2921
static void setup_windows_environment (void )
2869
2922
{
2870
2923
char * tmp = getenv ("TMPDIR" );
@@ -2905,7 +2958,8 @@ static void setup_windows_environment(void)
2905
2958
strbuf_addstr (& buf , tmp );
2906
2959
if ((tmp = getenv ("HOMEPATH" ))) {
2907
2960
strbuf_addstr (& buf , tmp );
2908
- if (is_directory (buf .buf ))
2961
+ if (!is_system32_path (buf .buf ) &&
2962
+ is_directory (buf .buf ))
2909
2963
setenv ("HOME" , buf .buf , 1 );
2910
2964
else
2911
2965
tmp = NULL ; /* use $USERPROFILE */
@@ -2916,6 +2970,37 @@ static void setup_windows_environment(void)
2916
2970
if (!tmp && (tmp = getenv ("USERPROFILE" )))
2917
2971
setenv ("HOME" , tmp , 1 );
2918
2972
}
2973
+
2974
+ if (!getenv ("PLINK_PROTOCOL" ))
2975
+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
2976
+
2977
+ #ifdef ENSURE_MSYSTEM_IS_SET
2978
+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
2979
+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
2980
+ char buf [32768 ];
2981
+ size_t off = 0 ;
2982
+
2983
+ xsnprintf (buf , sizeof (buf ),
2984
+ "MINGW%d" , (int )(sizeof (void * ) * 8 ));
2985
+ setenv ("MSYSTEM" , buf , 1 );
2986
+
2987
+ if (home )
2988
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
2989
+ "%s\\bin;" , home );
2990
+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
2991
+ if (path )
2992
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
2993
+ "%s" , path );
2994
+ else if (off > 0 )
2995
+ buf [off - 1 ] = '\0' ;
2996
+ else
2997
+ buf [0 ] = '\0' ;
2998
+ setenv ("PATH" , buf , 1 );
2999
+ }
3000
+ #endif
3001
+
3002
+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
3003
+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
2919
3004
}
2920
3005
2921
3006
static PSID get_current_user_sid (void )
0 commit comments