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