@@ -3015,6 +3015,59 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
3015
3015
return -1 ;
3016
3016
}
3017
3017
3018
+ #ifdef ENSURE_MSYSTEM_IS_SET
3019
+ static size_t append_system_bin_dirs (char * path , size_t size )
3020
+ {
3021
+ #if !defined(RUNTIME_PREFIX ) || !defined(HAVE_WPGMPTR )
3022
+ return 0 ;
3023
+ #else
3024
+ char prefix [32768 ];
3025
+ const char * slash ;
3026
+ size_t len = xwcstoutf (prefix , _wpgmptr , sizeof (prefix )), off = 0 ;
3027
+
3028
+ if (len == 0 || len >= sizeof (prefix ) ||
3029
+ !(slash = find_last_dir_sep (prefix )))
3030
+ return 0 ;
3031
+ /* strip trailing `git.exe` */
3032
+ len = slash - prefix ;
3033
+
3034
+ /* strip trailing `cmd` or `mingw64\bin` or `mingw32\bin` or `bin` or `libexec\git-core` */
3035
+ if (strip_suffix_mem (prefix , & len , "\\mingw64\\libexec\\git-core" ) ||
3036
+ strip_suffix_mem (prefix , & len , "\\mingw64\\bin" ))
3037
+ off += xsnprintf (path + off , size - off ,
3038
+ "%.*s\\mingw64\\bin;" , (int )len , prefix );
3039
+ else if (strip_suffix_mem (prefix , & len , "\\mingw32\\libexec\\git-core" ) ||
3040
+ strip_suffix_mem (prefix , & len , "\\mingw32\\bin" ))
3041
+ off += xsnprintf (path + off , size - off ,
3042
+ "%.*s\\mingw32\\bin;" , (int )len , prefix );
3043
+ else if (strip_suffix_mem (prefix , & len , "\\cmd" ) ||
3044
+ strip_suffix_mem (prefix , & len , "\\bin" ) ||
3045
+ strip_suffix_mem (prefix , & len , "\\libexec\\git-core" ))
3046
+ off += xsnprintf (path + off , size - off ,
3047
+ "%.*s\\mingw%d\\bin;" , (int )len , prefix ,
3048
+ (int )(sizeof (void * ) * 8 ));
3049
+ else
3050
+ return 0 ;
3051
+
3052
+ off += xsnprintf (path + off , size - off ,
3053
+ "%.*s\\usr\\bin;" , (int )len , prefix );
3054
+ return off ;
3055
+ #endif
3056
+ }
3057
+ #endif
3058
+
3059
+ static int is_system32_path (const char * path )
3060
+ {
3061
+ WCHAR system32 [MAX_PATH ], wpath [MAX_PATH ];
3062
+
3063
+ if (xutftowcs_path (wpath , path ) < 0 ||
3064
+ !GetSystemDirectoryW (system32 , ARRAY_SIZE (system32 )) ||
3065
+ _wcsicmp (system32 , wpath ))
3066
+ return 0 ;
3067
+
3068
+ return 1 ;
3069
+ }
3070
+
3018
3071
static void setup_windows_environment (void )
3019
3072
{
3020
3073
char * tmp = getenv ("TMPDIR" );
@@ -3055,7 +3108,8 @@ static void setup_windows_environment(void)
3055
3108
strbuf_addstr (& buf , tmp );
3056
3109
if ((tmp = getenv ("HOMEPATH" ))) {
3057
3110
strbuf_addstr (& buf , tmp );
3058
- if (is_directory (buf .buf ))
3111
+ if (!is_system32_path (buf .buf ) &&
3112
+ is_directory (buf .buf ))
3059
3113
setenv ("HOME" , buf .buf , 1 );
3060
3114
else
3061
3115
tmp = NULL ; /* use $USERPROFILE */
@@ -3066,6 +3120,37 @@ static void setup_windows_environment(void)
3066
3120
if (!tmp && (tmp = getenv ("USERPROFILE" )))
3067
3121
setenv ("HOME" , tmp , 1 );
3068
3122
}
3123
+
3124
+ if (!getenv ("PLINK_PROTOCOL" ))
3125
+ setenv ("PLINK_PROTOCOL" , "ssh" , 0 );
3126
+
3127
+ #ifdef ENSURE_MSYSTEM_IS_SET
3128
+ if (!(tmp = getenv ("MSYSTEM" )) || !tmp [0 ]) {
3129
+ const char * home = getenv ("HOME" ), * path = getenv ("PATH" );
3130
+ char buf [32768 ];
3131
+ size_t off = 0 ;
3132
+
3133
+ xsnprintf (buf , sizeof (buf ),
3134
+ "MINGW%d" , (int )(sizeof (void * ) * 8 ));
3135
+ setenv ("MSYSTEM" , buf , 1 );
3136
+
3137
+ if (home )
3138
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3139
+ "%s\\bin;" , home );
3140
+ off += append_system_bin_dirs (buf + off , sizeof (buf ) - off );
3141
+ if (path )
3142
+ off += xsnprintf (buf + off , sizeof (buf ) - off ,
3143
+ "%s" , path );
3144
+ else if (off > 0 )
3145
+ buf [off - 1 ] = '\0' ;
3146
+ else
3147
+ buf [0 ] = '\0' ;
3148
+ setenv ("PATH" , buf , 1 );
3149
+ }
3150
+ #endif
3151
+
3152
+ if (!getenv ("LC_ALL" ) && !getenv ("LC_CTYPE" ) && !getenv ("LANG" ))
3153
+ setenv ("LC_CTYPE" , "C.UTF-8" , 1 );
3069
3154
}
3070
3155
3071
3156
static PSID get_current_user_sid (void )
0 commit comments