Imported Upstream version 2.21.0
[platform/upstream/git.git] / compat / mingw.c
index 496e6f8..8141f77 100644 (file)
@@ -5,6 +5,11 @@
 #include "../strbuf.h"
 #include "../run-command.h"
 #include "../cache.h"
+#include "win32/lazyload.h"
+#include "../config.h"
+#include "dir.h"
+
+#define HCAST(type, handle) ((type)(intptr_t)handle)
 
 static const int delay[] = { 0, 1, 10, 20, 40 };
 
@@ -200,6 +205,60 @@ static int ask_yes_no_if_possible(const char *format, ...)
        }
 }
 
+/* Windows only */
+enum hide_dotfiles_type {
+       HIDE_DOTFILES_FALSE = 0,
+       HIDE_DOTFILES_TRUE,
+       HIDE_DOTFILES_DOTGITONLY
+};
+
+static enum hide_dotfiles_type hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
+static char *unset_environment_variables;
+
+int mingw_core_config(const char *var, const char *value, void *cb)
+{
+       if (!strcmp(var, "core.hidedotfiles")) {
+               if (value && !strcasecmp(value, "dotgitonly"))
+                       hide_dotfiles = HIDE_DOTFILES_DOTGITONLY;
+               else
+                       hide_dotfiles = git_config_bool(var, value);
+               return 0;
+       }
+
+       if (!strcmp(var, "core.unsetenvvars")) {
+               free(unset_environment_variables);
+               unset_environment_variables = xstrdup(value);
+               return 0;
+       }
+
+       return 0;
+}
+
+/* Normalizes NT paths as returned by some low-level APIs. */
+static wchar_t *normalize_ntpath(wchar_t *wbuf)
+{
+       int i;
+       /* fix absolute path prefixes */
+       if (wbuf[0] == '\\') {
+               /* strip NT namespace prefixes */
+               if (!wcsncmp(wbuf, L"\\??\\", 4) ||
+                   !wcsncmp(wbuf, L"\\\\?\\", 4))
+                       wbuf += 4;
+               else if (!wcsnicmp(wbuf, L"\\DosDevices\\", 12))
+                       wbuf += 12;
+               /* replace remaining '...UNC\' with '\\' */
+               if (!wcsnicmp(wbuf, L"UNC\\", 4)) {
+                       wbuf += 2;
+                       *wbuf = '\\';
+               }
+       }
+       /* convert backslashes to slashes */
+       for (i = 0; wbuf[i]; i++)
+               if (wbuf[i] == '\\')
+                       wbuf[i] = '/';
+       return wbuf;
+}
+
 int mingw_unlink(const char *pathname)
 {
        int ret, tries = 0;
@@ -284,6 +343,49 @@ int mingw_rmdir(const char *pathname)
        return ret;
 }
 
+static inline int needs_hiding(const char *path)
+{
+       const char *basename;
+
+       if (hide_dotfiles == HIDE_DOTFILES_FALSE)
+               return 0;
+
+       /* We cannot use basename(), as it would remove trailing slashes */
+       win32_skip_dos_drive_prefix((char **)&path);
+       if (!*path)
+               return 0;
+
+       for (basename = path; *path; path++)
+               if (is_dir_sep(*path)) {
+                       do {
+                               path++;
+                       } while (is_dir_sep(*path));
+                       /* ignore trailing slashes */
+                       if (*path)
+                               basename = path;
+               }
+
+       if (hide_dotfiles == HIDE_DOTFILES_TRUE)
+               return *basename == '.';
+
+       assert(hide_dotfiles == HIDE_DOTFILES_DOTGITONLY);
+       return !strncasecmp(".git", basename, 4) &&
+               (!basename[4] || is_dir_sep(basename[4]));
+}
+
+static int set_hidden_flag(const wchar_t *path, int set)
+{
+       DWORD original = GetFileAttributesW(path), modified;
+       if (set)
+               modified = original | FILE_ATTRIBUTE_HIDDEN;
+       else
+               modified = original & ~FILE_ATTRIBUTE_HIDDEN;
+       if (original == modified || SetFileAttributesW(path, modified))
+               return 0;
+       errno = err_win_to_posix(GetLastError());
+       return -1;
+}
+
 int mingw_mkdir(const char *path, int mode)
 {
        int ret;
@@ -291,15 +393,79 @@ int mingw_mkdir(const char *path, int mode)
        if (xutftowcs_path(wpath, path) < 0)
                return -1;
        ret = _wmkdir(wpath);
+       if (!ret && needs_hiding(path))
+               return set_hidden_flag(wpath, 1);
        return ret;
 }
 
+/*
+ * Calling CreateFile() using FILE_APPEND_DATA and without FILE_WRITE_DATA
+ * is documented in [1] as opening a writable file handle in append mode.
+ * (It is believed that) this is atomic since it is maintained by the
+ * kernel unlike the O_APPEND flag which is racily maintained by the CRT.
+ *
+ * [1] https://docs.microsoft.com/en-us/windows/desktop/fileio/file-access-rights-constants
+ *
+ * This trick does not appear to work for named pipes.  Instead it creates
+ * a named pipe client handle that cannot be written to.  Callers should
+ * just use the regular _wopen() for them.  (And since client handle gets
+ * bound to a unique server handle, it isn't really an issue.)
+ */
+static int mingw_open_append(wchar_t const *wfilename, int oflags, ...)
+{
+       HANDLE handle;
+       int fd;
+       DWORD create = (oflags & O_CREAT) ? OPEN_ALWAYS : OPEN_EXISTING;
+
+       /* only these flags are supported */
+       if ((oflags & ~O_CREAT) != (O_WRONLY | O_APPEND))
+               return errno = ENOSYS, -1;
+
+       /*
+        * FILE_SHARE_WRITE is required to permit child processes
+        * to append to the file.
+        */
+       handle = CreateFileW(wfilename, FILE_APPEND_DATA,
+                       FILE_SHARE_WRITE | FILE_SHARE_READ,
+                       NULL, create, FILE_ATTRIBUTE_NORMAL, NULL);
+       if (handle == INVALID_HANDLE_VALUE)
+               return errno = err_win_to_posix(GetLastError()), -1;
+
+       /*
+        * No O_APPEND here, because the CRT uses it only to reset the
+        * file pointer to EOF before each write(); but that is not
+        * necessary (and may lead to races) for a file created with
+        * FILE_APPEND_DATA.
+        */
+       fd = _open_osfhandle((intptr_t)handle, O_BINARY);
+       if (fd < 0)
+               CloseHandle(handle);
+       return fd;
+}
+
+/*
+ * Does the pathname map to the local named pipe filesystem?
+ * That is, does it have a "//./pipe/" prefix?
+ */
+static int is_local_named_pipe_path(const char *filename)
+{
+       return (is_dir_sep(filename[0]) &&
+               is_dir_sep(filename[1]) &&
+               filename[2] == '.'  &&
+               is_dir_sep(filename[3]) &&
+               !strncasecmp(filename+4, "pipe", 4) &&
+               is_dir_sep(filename[8]) &&
+               filename[9]);
+}
+
 int mingw_open (const char *filename, int oflags, ...)
 {
+       typedef int (*open_fn_t)(wchar_t const *wfilename, int oflags, ...);
        va_list args;
        unsigned mode;
        int fd;
        wchar_t wfilename[MAX_PATH];
+       open_fn_t open_fn;
 
        va_start(args, oflags);
        mode = va_arg(args, int);
@@ -308,15 +474,35 @@ int mingw_open (const char *filename, int oflags, ...)
        if (filename && !strcmp(filename, "/dev/null"))
                filename = "nul";
 
+       if ((oflags & O_APPEND) && !is_local_named_pipe_path(filename))
+               open_fn = mingw_open_append;
+       else
+               open_fn = _wopen;
+
        if (xutftowcs_path(wfilename, filename) < 0)
                return -1;
-       fd = _wopen(wfilename, oflags, mode);
+       fd = open_fn(wfilename, oflags, mode);
 
        if (fd < 0 && (oflags & O_ACCMODE) != O_RDONLY && errno == EACCES) {
                DWORD attrs = GetFileAttributesW(wfilename);
                if (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY))
                        errno = EISDIR;
        }
+       if ((oflags & O_CREAT) && needs_hiding(filename)) {
+               /*
+                * Internally, _wopen() uses the CreateFile() API which errors
+                * out with an ERROR_ACCESS_DENIED if CREATE_ALWAYS was
+                * specified and an already existing file's attributes do not
+                * match *exactly*. As there is no mode or flag we can set that
+                * would correspond to FILE_ATTRIBUTE_HIDDEN, let's just try
+                * again *without* the O_CREAT flag (that corresponds to the
+                * CREATE_ALWAYS flag of CreateFile()).
+                */
+               if (fd < 0 && errno == EACCES)
+                       fd = open_fn(wfilename, oflags & ~O_CREAT, mode);
+               if (fd >= 0 && set_hidden_flag(wfilename, 1))
+                       warning("could not mark '%s' as hidden.", filename);
+       }
        return fd;
 }
 
@@ -348,6 +534,7 @@ int mingw_fgetc(FILE *stream)
 #undef fopen
 FILE *mingw_fopen (const char *filename, const char *otype)
 {
+       int hide = needs_hiding(filename);
        FILE *file;
        wchar_t wfilename[MAX_PATH], wotype[4];
        if (filename && !strcmp(filename, "/dev/null"))
@@ -355,12 +542,21 @@ FILE *mingw_fopen (const char *filename, const char *otype)
        if (xutftowcs_path(wfilename, filename) < 0 ||
                xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
                return NULL;
+       if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
+               error("could not unhide %s", filename);
+               return NULL;
+       }
        file = _wfopen(wfilename, wotype);
+       if (!file && GetLastError() == ERROR_INVALID_NAME)
+               errno = ENOENT;
+       if (file && hide && set_hidden_flag(wfilename, 1))
+               warning("could not mark '%s' as hidden.", filename);
        return file;
 }
 
 FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
 {
+       int hide = needs_hiding(filename);
        FILE *file;
        wchar_t wfilename[MAX_PATH], wotype[4];
        if (filename && !strcmp(filename, "/dev/null"))
@@ -368,7 +564,13 @@ FILE *mingw_freopen (const char *filename, const char *otype, FILE *stream)
        if (xutftowcs_path(wfilename, filename) < 0 ||
                xutftowcs(wotype, otype, ARRAY_SIZE(wotype)) < 0)
                return NULL;
+       if (hide && !access(filename, F_OK) && set_hidden_flag(wfilename, 0)) {
+               error("could not unhide %s", filename);
+               return NULL;
+       }
        file = _wfreopen(wfilename, wotype, stream);
+       if (file && hide && set_hidden_flag(wfilename, 1))
+               warning("could not mark '%s' as hidden.", filename);
        return file;
 }
 
@@ -394,6 +596,23 @@ int mingw_fflush(FILE *stream)
        return ret;
 }
 
+#undef write
+ssize_t mingw_write(int fd, const void *buf, size_t len)
+{
+       ssize_t result = write(fd, buf, len);
+
+       if (result < 0 && errno == EINVAL && buf) {
+               /* check if fd is a pipe */
+               HANDLE h = (HANDLE) _get_osfhandle(fd);
+               if (GetFileType(h) == FILE_TYPE_PIPE)
+                       errno = EPIPE;
+               else
+                       errno = EINVAL;
+       }
+
+       return result;
+}
+
 int mingw_access(const char *filename, int mode)
 {
        wchar_t wfilename[MAX_PATH];
@@ -430,9 +649,44 @@ static inline long long filetime_to_hnsec(const FILETIME *ft)
        return winTime - 116444736000000000LL;
 }
 
-static inline time_t filetime_to_time_t(const FILETIME *ft)
+static inline void filetime_to_timespec(const FILETIME *ft, struct timespec *ts)
 {
-       return (time_t)(filetime_to_hnsec(ft) / 10000000);
+       long long hnsec = filetime_to_hnsec(ft);
+       ts->tv_sec = (time_t)(hnsec / 10000000);
+       ts->tv_nsec = (hnsec % 10000000) * 100;
+}
+
+/**
+ * Verifies that safe_create_leading_directories() would succeed.
+ */
+static int has_valid_directory_prefix(wchar_t *wfilename)
+{
+       int n = wcslen(wfilename);
+
+       while (n > 0) {
+               wchar_t c = wfilename[--n];
+               DWORD attributes;
+
+               if (!is_dir_sep(c))
+                       continue;
+
+               wfilename[n] = L'\0';
+               attributes = GetFileAttributesW(wfilename);
+               wfilename[n] = c;
+               if (attributes == FILE_ATTRIBUTE_DIRECTORY ||
+                               attributes == FILE_ATTRIBUTE_DEVICE)
+                       return 1;
+               if (attributes == INVALID_FILE_ATTRIBUTES)
+                       switch (GetLastError()) {
+                       case ERROR_PATH_NOT_FOUND:
+                               continue;
+                       case ERROR_FILE_NOT_FOUND:
+                               /* This implies parent directory exists. */
+                               return 1;
+                       }
+               return 0;
+       }
+       return 1;
 }
 
 /* We keep the do_lstat code in a separate function to avoid recursion.
@@ -458,9 +712,9 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
                buf->st_size = fdata.nFileSizeLow |
                        (((off_t)fdata.nFileSizeHigh)<<32);
                buf->st_dev = buf->st_rdev = 0; /* not used by Git */
-               buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
-               buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
-               buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
+               filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
+               filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
+               filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
                if (fdata.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
                        WIN32_FIND_DATAW findbuf;
                        HANDLE handle = FindFirstFileW(wfilename, &findbuf);
@@ -495,6 +749,12 @@ static int do_lstat(int follow, const char *file_name, struct stat *buf)
        case ERROR_NOT_ENOUGH_MEMORY:
                errno = ENOMEM;
                break;
+       case ERROR_PATH_NOT_FOUND:
+               if (!has_valid_directory_prefix(wfilename)) {
+                       errno = ENOTDIR;
+                       break;
+               }
+               /* fallthru */
        default:
                errno = ENOENT;
                break;
@@ -535,6 +795,29 @@ static int do_stat_internal(int follow, const char *file_name, struct stat *buf)
        return do_lstat(follow, alt_name, buf);
 }
 
+static int get_file_info_by_handle(HANDLE hnd, struct stat *buf)
+{
+       BY_HANDLE_FILE_INFORMATION fdata;
+
+       if (!GetFileInformationByHandle(hnd, &fdata)) {
+               errno = err_win_to_posix(GetLastError());
+               return -1;
+       }
+
+       buf->st_ino = 0;
+       buf->st_gid = 0;
+       buf->st_uid = 0;
+       buf->st_nlink = 1;
+       buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
+       buf->st_size = fdata.nFileSizeLow |
+               (((off_t)fdata.nFileSizeHigh)<<32);
+       buf->st_dev = buf->st_rdev = 0; /* not used by Git */
+       filetime_to_timespec(&(fdata.ftLastAccessTime), &(buf->st_atim));
+       filetime_to_timespec(&(fdata.ftLastWriteTime), &(buf->st_mtim));
+       filetime_to_timespec(&(fdata.ftCreationTime), &(buf->st_ctim));
+       return 0;
+}
+
 int mingw_lstat(const char *file_name, struct stat *buf)
 {
        return do_stat_internal(0, file_name, buf);
@@ -547,32 +830,31 @@ int mingw_stat(const char *file_name, struct stat *buf)
 int mingw_fstat(int fd, struct stat *buf)
 {
        HANDLE fh = (HANDLE)_get_osfhandle(fd);
-       BY_HANDLE_FILE_INFORMATION fdata;
+       DWORD avail, type = GetFileType(fh) & ~FILE_TYPE_REMOTE;
 
-       if (fh == INVALID_HANDLE_VALUE) {
-               errno = EBADF;
-               return -1;
-       }
-       /* direct non-file handles to MS's fstat() */
-       if (GetFileType(fh) != FILE_TYPE_DISK)
-               return _fstati64(fd, buf);
+       switch (type) {
+       case FILE_TYPE_DISK:
+               return get_file_info_by_handle(fh, buf);
 
-       if (GetFileInformationByHandle(fh, &fdata)) {
-               buf->st_ino = 0;
-               buf->st_gid = 0;
-               buf->st_uid = 0;
+       case FILE_TYPE_CHAR:
+       case FILE_TYPE_PIPE:
+               /* initialize stat fields */
+               memset(buf, 0, sizeof(*buf));
                buf->st_nlink = 1;
-               buf->st_mode = file_attr_to_st_mode(fdata.dwFileAttributes);
-               buf->st_size = fdata.nFileSizeLow |
-                       (((off_t)fdata.nFileSizeHigh)<<32);
-               buf->st_dev = buf->st_rdev = 0; /* not used by Git */
-               buf->st_atime = filetime_to_time_t(&(fdata.ftLastAccessTime));
-               buf->st_mtime = filetime_to_time_t(&(fdata.ftLastWriteTime));
-               buf->st_ctime = filetime_to_time_t(&(fdata.ftCreationTime));
+
+               if (type == FILE_TYPE_CHAR) {
+                       buf->st_mode = _S_IFCHR;
+               } else {
+                       buf->st_mode = _S_IFIFO;
+                       if (PeekNamedPipe(fh, NULL, 0, NULL, &avail, NULL))
+                               buf->st_size = avail;
+               }
                return 0;
+
+       default:
+               errno = EBADF;
+               return -1;
        }
-       errno = EBADF;
-       return -1;
 }
 
 static inline void time_t_to_filetime(time_t t, FILETIME *ft)
@@ -627,6 +909,17 @@ revert_attrs:
        return rc;
 }
 
+#undef strftime
+size_t mingw_strftime(char *s, size_t max,
+                     const char *format, const struct tm *tm)
+{
+       size_t ret = strftime(s, max, format, tm);
+
+       if (!ret && errno == EINVAL)
+               die("invalid strftime format: '%s'", format);
+       return ret;
+}
+
 unsigned int sleep (unsigned int seconds)
 {
        Sleep(seconds*1000);
@@ -674,14 +967,14 @@ int pipe(int filedes[2])
                errno = err_win_to_posix(GetLastError());
                return -1;
        }
-       filedes[0] = _open_osfhandle((int)h[0], O_NOINHERIT);
+       filedes[0] = _open_osfhandle(HCAST(int, h[0]), O_NOINHERIT);
        if (filedes[0] < 0) {
                CloseHandle(h[0]);
                CloseHandle(h[1]);
                return -1;
        }
-       filedes[1] = _open_osfhandle((int)h[1], O_NOINHERIT);
-       if (filedes[0] < 0) {
+       filedes[1] = _open_osfhandle(HCAST(int, h[1]), O_NOINHERIT);
+       if (filedes[1] < 0) {
                close(filedes[0]);
                CloseHandle(h[1]);
                return -1;
@@ -705,23 +998,41 @@ struct tm *localtime_r(const time_t *timep, struct tm *result)
 
 char *mingw_getcwd(char *pointer, int len)
 {
-       int i;
-       wchar_t wpointer[MAX_PATH];
-       if (!_wgetcwd(wpointer, ARRAY_SIZE(wpointer)))
+       wchar_t cwd[MAX_PATH], wpointer[MAX_PATH];
+       DWORD ret = GetCurrentDirectoryW(ARRAY_SIZE(cwd), cwd);
+
+       if (!ret || ret >= ARRAY_SIZE(cwd)) {
+               errno = ret ? ENAMETOOLONG : err_win_to_posix(GetLastError());
+               return NULL;
+       }
+       ret = GetLongPathNameW(cwd, wpointer, ARRAY_SIZE(wpointer));
+       if (!ret && GetLastError() == ERROR_ACCESS_DENIED) {
+               HANDLE hnd = CreateFileW(cwd, 0,
+                       FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
+                       OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
+               if (hnd == INVALID_HANDLE_VALUE)
+                       return NULL;
+               ret = GetFinalPathNameByHandleW(hnd, wpointer, ARRAY_SIZE(wpointer), 0);
+               CloseHandle(hnd);
+               if (!ret || ret >= ARRAY_SIZE(wpointer))
+                       return NULL;
+               if (xwcstoutf(pointer, normalize_ntpath(wpointer), len) < 0)
+                       return NULL;
+               return pointer;
+       }
+       if (!ret || ret >= ARRAY_SIZE(wpointer))
                return NULL;
        if (xwcstoutf(pointer, wpointer, len) < 0)
                return NULL;
-       for (i = 0; pointer[i]; i++)
-               if (pointer[i] == '\\')
-                       pointer[i] = '/';
+       convert_slashes(pointer);
        return pointer;
 }
 
 /*
- * See http://msdn2.microsoft.com/en-us/library/17w5ykft(vs.71).aspx
- * (Parsing C++ Command-Line Arguments)
+ * See "Parsing C++ Command-Line Arguments" at Microsoft's Docs:
+ * https://docs.microsoft.com/en-us/cpp/cpp/parsing-cpp-command-line-arguments
  */
-static const char *quote_arg(const char *arg)
+static const char *quote_arg_msvc(const char *arg)
 {
        /* count chars to quote */
        int len = 0, n = 0;
@@ -752,7 +1063,7 @@ static const char *quote_arg(const char *arg)
                return arg;
 
        /* insert \ where necessary */
-       d = q = xmalloc(len+n+3);
+       d = q = xmalloc(st_add3(len, n, 3));
        *d++ = '"';
        while (*arg) {
                if (*arg == '"')
@@ -776,6 +1087,37 @@ static const char *quote_arg(const char *arg)
        return q;
 }
 
+#include "quote.h"
+
+static const char *quote_arg_msys2(const char *arg)
+{
+       struct strbuf buf = STRBUF_INIT;
+       const char *p2 = arg, *p;
+
+       for (p = arg; *p; p++) {
+               int ws = isspace(*p);
+               if (!ws && *p != '\\' && *p != '"' && *p != '{')
+                       continue;
+               if (!buf.len)
+                       strbuf_addch(&buf, '"');
+               if (p != p2)
+                       strbuf_add(&buf, p2, p - p2);
+               if (!ws && *p != '{')
+                       strbuf_addch(&buf, '\\');
+               p2 = p;
+       }
+
+       if (p == arg)
+               strbuf_addch(&buf, '"');
+       else if (!buf.len)
+               return arg;
+       else
+               strbuf_add(&buf, p2, p - p2),
+
+       strbuf_addch(&buf, '"');
+       return strbuf_detach(&buf, 0);
+}
+
 static const char *parse_interpreter(const char *cmd)
 {
        static char buf[100];
@@ -812,64 +1154,14 @@ static const char *parse_interpreter(const char *cmd)
 }
 
 /*
- * Splits the PATH into parts.
- */
-static char **get_path_split(void)
-{
-       char *p, **path, *envpath = mingw_getenv("PATH");
-       int i, n = 0;
-
-       if (!envpath || !*envpath)
-               return NULL;
-
-       envpath = xstrdup(envpath);
-       p = envpath;
-       while (p) {
-               char *dir = p;
-               p = strchr(p, ';');
-               if (p) *p++ = '\0';
-               if (*dir) {     /* not earlier, catches series of ; */
-                       ++n;
-               }
-       }
-       if (!n)
-               return NULL;
-
-       path = xmalloc((n+1)*sizeof(char *));
-       p = envpath;
-       i = 0;
-       do {
-               if (*p)
-                       path[i++] = xstrdup(p);
-               p = p+strlen(p)+1;
-       } while (i < n);
-       path[i] = NULL;
-
-       free(envpath);
-
-       return path;
-}
-
-static void free_path_split(char **path)
-{
-       char **p = path;
-
-       if (!path)
-               return;
-
-       while (*p)
-               free(*p++);
-       free(path);
-}
-
-/*
  * exe_only means that we only want to detect .exe files, but not scripts
  * (which do not have an extension)
  */
-static char *lookup_prog(const char *dir, const char *cmd, int isexe, int exe_only)
+static char *lookup_prog(const char *dir, int dirlen, const char *cmd,
+                        int isexe, int exe_only)
 {
        char path[MAX_PATH];
-       snprintf(path, sizeof(path), "%s/%s.exe", dir, cmd);
+       snprintf(path, sizeof(path), "%.*s\\%s.exe", dirlen, dir, cmd);
 
        if (!isexe && access(path, F_OK) == 0)
                return xstrdup(path);
@@ -884,59 +1176,169 @@ static char *lookup_prog(const char *dir, const char *cmd, int isexe, int exe_on
  * Determines the absolute path of cmd using the split path in path.
  * If cmd contains a slash or backslash, no lookup is performed.
  */
-static char *path_lookup(const char *cmd, char **path, int exe_only)
+static char *path_lookup(const char *cmd, int exe_only)
 {
+       const char *path;
        char *prog = NULL;
        int len = strlen(cmd);
        int isexe = len >= 4 && !strcasecmp(cmd+len-4, ".exe");
 
        if (strchr(cmd, '/') || strchr(cmd, '\\'))
-               prog = xstrdup(cmd);
+               return xstrdup(cmd);
 
-       while (!prog && *path)
-               prog = lookup_prog(*path++, cmd, isexe, exe_only);
+       path = mingw_getenv("PATH");
+       if (!path)
+               return NULL;
+
+       while (!prog) {
+               const char *sep = strchrnul(path, ';');
+               int dirlen = sep - path;
+               if (dirlen)
+                       prog = lookup_prog(path, dirlen, cmd, isexe, exe_only);
+               if (!*sep)
+                       break;
+               path = sep + 1;
+       }
 
        return prog;
 }
 
-static int do_putenv(char **env, const char *name, int size, int free_old);
+static const wchar_t *wcschrnul(const wchar_t *s, wchar_t c)
+{
+       while (*s && *s != c)
+               s++;
+       return s;
+}
+
+/* Compare only keys */
+static int wenvcmp(const void *a, const void *b)
+{
+       wchar_t *p = *(wchar_t **)a, *q = *(wchar_t **)b;
+       size_t p_len, q_len;
+
+       /* Find the keys */
+       p_len = wcschrnul(p, L'=') - p;
+       q_len = wcschrnul(q, L'=') - q;
 
-/* used number of elements of environ array, including terminating NULL */
-static int environ_size = 0;
-/* allocated size of environ array, in bytes */
-static int environ_alloc = 0;
+       /* If the length differs, include the shorter key's NUL */
+       if (p_len < q_len)
+               p_len++;
+       else if (p_len > q_len)
+               p_len = q_len + 1;
+
+       return _wcsnicmp(p, q, p_len);
+}
+
+/* We need a stable sort to convert the environment between UTF-16 <-> UTF-8 */
+#ifndef INTERNAL_QSORT
+#include "qsort.c"
+#endif
 
 /*
- * Create environment block suitable for CreateProcess. Merges current
- * process environment and the supplied environment changes.
+ * Build an environment block combining the inherited environment
+ * merged with the given list of settings.
+ *
+ * Values of the form "KEY=VALUE" in deltaenv override inherited values.
+ * Values of the form "KEY" in deltaenv delete inherited values.
+ *
+ * Multiple entries in deltaenv for the same key are explicitly allowed.
+ *
+ * We return a contiguous block of UNICODE strings with a final trailing
+ * zero word.
  */
 static wchar_t *make_environment_block(char **deltaenv)
 {
-       wchar_t *wenvblk = NULL;
-       char **tmpenv;
-       int i = 0, size = environ_size, wenvsz = 0, wenvpos = 0;
+       wchar_t *wenv = GetEnvironmentStringsW(), *wdeltaenv, *result, *p;
+       size_t wlen, s, delta_size, size;
+
+       wchar_t **array = NULL;
+       size_t alloc = 0, nr = 0, i;
+
+       size = 1; /* for extra NUL at the end */
+
+       /* If there is no deltaenv to apply, simply return a copy. */
+       if (!deltaenv || !*deltaenv) {
+               for (p = wenv; p && *p; ) {
+                       size_t s = wcslen(p) + 1;
+                       size += s;
+                       p += s;
+               }
+
+               ALLOC_ARRAY(result, size);
+               memcpy(result, wenv, size * sizeof(*wenv));
+               FreeEnvironmentStringsW(wenv);
+               return result;
+       }
+
+       /*
+        * If there is a deltaenv, let's accumulate all keys into `array`,
+        * sort them using the stable git_qsort() and then copy, skipping
+        * duplicate keys
+        */
+       for (p = wenv; p && *p; ) {
+               ALLOC_GROW(array, nr + 1, alloc);
+               s = wcslen(p) + 1;
+               array[nr++] = p;
+               p += s;
+               size += s;
+       }
 
-       while (deltaenv && deltaenv[i])
-               i++;
+       /* (over-)assess size needed for wchar version of deltaenv */
+       for (delta_size = 0, i = 0; deltaenv[i]; i++)
+               delta_size += strlen(deltaenv[i]) * 2 + 1;
+       ALLOC_ARRAY(wdeltaenv, delta_size);
 
-       /* copy the environment, leaving space for changes */
-       tmpenv = xmalloc((size + i) * sizeof(char*));
-       memcpy(tmpenv, environ, size * sizeof(char*));
+       /* convert the deltaenv, appending to array */
+       for (i = 0, p = wdeltaenv; deltaenv[i]; i++) {
+               ALLOC_GROW(array, nr + 1, alloc);
+               wlen = xutftowcs(p, deltaenv[i], wdeltaenv + delta_size - p);
+               array[nr++] = p;
+               p += wlen + 1;
+       }
 
-       /* merge supplied environment changes into the temporary environment */
-       for (i = 0; deltaenv && deltaenv[i]; i++)
-               size = do_putenv(tmpenv, deltaenv[i], size, 0);
+       git_qsort(array, nr, sizeof(*array), wenvcmp);
+       ALLOC_ARRAY(result, size + delta_size);
 
-       /* create environment block from temporary environment */
-       for (i = 0; tmpenv[i]; i++) {
-               size = 2 * strlen(tmpenv[i]) + 2; /* +2 for final \0 */
-               ALLOC_GROW(wenvblk, (wenvpos + size) * sizeof(wchar_t), wenvsz);
-               wenvpos += xutftowcs(&wenvblk[wenvpos], tmpenv[i], size) + 1;
+       for (p = result, i = 0; i < nr; i++) {
+               /* Skip any duplicate keys; last one wins */
+               while (i + 1 < nr && !wenvcmp(array + i, array + i + 1))
+                      i++;
+
+               /* Skip "to delete" entry */
+               if (!wcschr(array[i], L'='))
+                       continue;
+
+               size = wcslen(array[i]) + 1;
+               memcpy(p, array[i], size * sizeof(*p));
+               p += size;
+       }
+       *p = L'\0';
+
+       free(array);
+       free(wdeltaenv);
+       FreeEnvironmentStringsW(wenv);
+       return result;
+}
+
+static void do_unset_environment_variables(void)
+{
+       static int done;
+       char *p = unset_environment_variables;
+
+       if (done || !p)
+               return;
+       done = 1;
+
+       for (;;) {
+               char *comma = strchr(p, ',');
+
+               if (comma)
+                       *comma = '\0';
+               unsetenv(p);
+               if (!comma)
+                       break;
+               p = comma + 1;
        }
-       /* add final \0 terminator */
-       wenvblk[wenvpos] = 0;
-       free(tmpenv);
-       return wenvblk;
 }
 
 struct pinfo_t {
@@ -947,6 +1349,47 @@ struct pinfo_t {
 static struct pinfo_t *pinfo = NULL;
 CRITICAL_SECTION pinfo_cs;
 
+/* Used to match and chomp off path components */
+static inline int match_last_path_component(const char *path, size_t *len,
+                                           const char *component)
+{
+       size_t component_len = strlen(component);
+       if (*len < component_len + 1 ||
+           !is_dir_sep(path[*len - component_len - 1]) ||
+           fspathncmp(path + *len - component_len, component, component_len))
+               return 0;
+       *len -= component_len + 1;
+       /* chomp off repeated dir separators */
+       while (*len > 0 && is_dir_sep(path[*len - 1]))
+               (*len)--;
+       return 1;
+}
+
+static int is_msys2_sh(const char *cmd)
+{
+       if (cmd && !strcmp(cmd, "sh")) {
+               static int ret = -1;
+               char *p;
+
+               if (ret >= 0)
+                       return ret;
+
+               p = path_lookup(cmd, 0);
+               if (!p)
+                       ret = 0;
+               else {
+                       size_t len = strlen(p);
+
+                       ret = match_last_path_component(p, &len, "sh.exe") &&
+                               match_last_path_component(p, &len, "bin") &&
+                               match_last_path_component(p, &len, "usr");
+                       free(p);
+               }
+               return ret;
+       }
+       return 0;
+}
+
 static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaenv,
                              const char *dir,
                              int prepend_cmd, int fhin, int fhout, int fherr)
@@ -957,9 +1400,14 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
        wchar_t wcmd[MAX_PATH], wdir[MAX_PATH], *wargs, *wenvblk = NULL;
        unsigned flags = CREATE_UNICODE_ENVIRONMENT;
        BOOL ret;
+       HANDLE cons;
+       const char *(*quote_arg)(const char *arg) =
+               is_msys2_sh(*argv) ? quote_arg_msys2 : quote_arg_msvc;
+
+       do_unset_environment_variables();
 
        /* Determine whether or not we are associated to a console */
-       HANDLE cons = CreateFile("CONOUT$", GENERIC_WRITE,
+       cons = CreateFile("CONOUT$", GENERIC_WRITE,
                        FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
                        FILE_ATTRIBUTE_NORMAL, NULL);
        if (cons == INVALID_HANDLE_VALUE) {
@@ -1011,7 +1459,7 @@ static pid_t mingw_spawnve_fd(const char *cmd, const char **argv, char **deltaen
                        free(quoted);
        }
 
-       wargs = xmalloc((2 * args.len + 1) * sizeof(wchar_t));
+       ALLOC_ARRAY(wargs, st_add(st_mult(2, args.len), 1));
        xutftowcs(wargs, args.buf, 2 * args.len + 1);
        strbuf_release(&args);
 
@@ -1061,8 +1509,7 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
                     int fhin, int fhout, int fherr)
 {
        pid_t pid;
-       char **path = get_path_split();
-       char *prog = path_lookup(cmd, path, 0);
+       char *prog = path_lookup(cmd, 0);
 
        if (!prog) {
                errno = ENOENT;
@@ -1073,7 +1520,7 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
 
                if (interpr) {
                        const char *argv0 = argv[0];
-                       char *iprog = path_lookup(interpr, path, 1);
+                       char *iprog = path_lookup(interpr, 1);
                        argv[0] = prog;
                        if (!iprog) {
                                errno = ENOENT;
@@ -1091,26 +1538,23 @@ pid_t mingw_spawnvpe(const char *cmd, const char **argv, char **deltaenv,
                                               fhin, fhout, fherr);
                free(prog);
        }
-       free_path_split(path);
        return pid;
 }
 
 static int try_shell_exec(const char *cmd, char *const *argv)
 {
        const char *interpr = parse_interpreter(cmd);
-       char **path;
        char *prog;
        int pid = 0;
 
        if (!interpr)
                return 0;
-       path = get_path_split();
-       prog = path_lookup(interpr, path, 1);
+       prog = path_lookup(interpr, 1);
        if (prog) {
                int argc = 0;
                const char **argv2;
                while (argv[argc]) argc++;
-               argv2 = xmalloc(sizeof(*argv) * (argc+1));
+               ALLOC_ARRAY(argv2, argc + 1);
                argv2[0] = (char *)cmd; /* full path to the script file */
                memcpy(&argv2[1], &argv[1], sizeof(*argv) * argc);
                pid = mingw_spawnv(prog, argv2, 1);
@@ -1124,7 +1568,6 @@ static int try_shell_exec(const char *cmd, char *const *argv)
                free(prog);
                free(argv2);
        }
-       free_path_split(path);
        return pid;
 }
 
@@ -1146,8 +1589,7 @@ int mingw_execv(const char *cmd, char *const *argv)
 
 int mingw_execvp(const char *cmd, char *const *argv)
 {
-       char **path = get_path_split();
-       char *prog = path_lookup(cmd, path, 0);
+       char *prog = path_lookup(cmd, 0);
 
        if (prog) {
                mingw_execv(prog, argv);
@@ -1155,7 +1597,6 @@ int mingw_execvp(const char *cmd, char *const *argv)
        } else
                errno = ENOENT;
 
-       free_path_split(path);
        return -1;
 }
 
@@ -1185,87 +1626,83 @@ int mingw_kill(pid_t pid, int sig)
 }
 
 /*
- * Compare environment entries by key (i.e. stopping at '=' or '\0').
+ * UTF-8 versions of getenv(), putenv() and unsetenv().
+ * Internally, they use the CRT's stock UNICODE routines
+ * to avoid data loss.
  */
-static int compareenv(const void *v1, const void *v2)
+char *mingw_getenv(const char *name)
 {
-       const char *e1 = *(const char**)v1;
-       const char *e2 = *(const char**)v2;
+#define GETENV_MAX_RETAIN 64
+       static char *values[GETENV_MAX_RETAIN];
+       static int value_counter;
+       int len_key, len_value;
+       wchar_t *w_key;
+       char *value;
+       wchar_t w_value[32768];
 
-       for (;;) {
-               int c1 = *e1++;
-               int c2 = *e2++;
-               c1 = (c1 == '=') ? 0 : tolower(c1);
-               c2 = (c2 == '=') ? 0 : tolower(c2);
-               if (c1 > c2)
-                       return 1;
-               if (c1 < c2)
-                       return -1;
-               if (c1 == 0)
-                       return 0;
-       }
-}
+       if (!name || !*name)
+               return NULL;
 
-static int bsearchenv(char **env, const char *name, size_t size)
-{
-       unsigned low = 0, high = size;
-       while (low < high) {
-               unsigned mid = low + ((high - low) >> 1);
-               int cmp = compareenv(&env[mid], &name);
-               if (cmp < 0)
-                       low = mid + 1;
-               else if (cmp > 0)
-                       high = mid;
-               else
-                       return mid;
+       len_key = strlen(name) + 1;
+       /* We cannot use xcalloc() here because that uses getenv() itself */
+       w_key = calloc(len_key, sizeof(wchar_t));
+       if (!w_key)
+               die("Out of memory, (tried to allocate %u wchar_t's)", len_key);
+       xutftowcs(w_key, name, len_key);
+       len_value = GetEnvironmentVariableW(w_key, w_value, ARRAY_SIZE(w_value));
+       if (!len_value && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
+               free(w_key);
+               return NULL;
        }
-       return ~low; /* not found, return 1's complement of insert position */
+       free(w_key);
+
+       len_value = len_value * 3 + 1;
+       /* We cannot use xcalloc() here because that uses getenv() itself */
+       value = calloc(len_value, sizeof(char));
+       if (!value)
+               die("Out of memory, (tried to allocate %u bytes)", len_value);
+       xwcstoutf(value, w_value, len_value);
+
+       /*
+        * We return `value` which is an allocated value and the caller is NOT
+        * expecting to have to free it, so we keep a round-robin array,
+        * invalidating the buffer after GETENV_MAX_RETAIN getenv() calls.
+        */
+       free(values[value_counter]);
+       values[value_counter++] = value;
+       if (value_counter >= ARRAY_SIZE(values))
+               value_counter = 0;
+
+       return value;
 }
 
-/*
- * If name contains '=', then sets the variable, otherwise it unsets it
- * Size includes the terminating NULL. Env must have room for size + 1 entries
- * (in case of insert). Returns the new size. Optionally frees removed entries.
- */
-static int do_putenv(char **env, const char *name, int size, int free_old)
+int mingw_putenv(const char *namevalue)
 {
-       int i = bsearchenv(env, name, size - 1);
+       int size;
+       wchar_t *wide, *equal;
+       BOOL result;
 
-       /* optionally free removed / replaced entry */
-       if (i >= 0 && free_old)
-               free(env[i]);
+       if (!namevalue || !*namevalue)
+               return 0;
 
-       if (strchr(name, '=')) {
-               /* if new value ('key=value') is specified, insert or replace entry */
-               if (i < 0) {
-                       i = ~i;
-                       memmove(&env[i + 1], &env[i], (size - i) * sizeof(char*));
-                       size++;
-               }
-               env[i] = (char*) name;
-       } else if (i >= 0) {
-               /* otherwise ('key') remove existing entry */
-               size--;
-               memmove(&env[i], &env[i + 1], (size - i) * sizeof(char*));
+       size = strlen(namevalue) * 2 + 1;
+       wide = calloc(size, sizeof(wchar_t));
+       if (!wide)
+               die("Out of memory, (tried to allocate %u wchar_t's)", size);
+       xutftowcs(wide, namevalue, size);
+       equal = wcschr(wide, L'=');
+       if (!equal)
+               result = SetEnvironmentVariableW(wide, NULL);
+       else {
+               *equal = L'\0';
+               result = SetEnvironmentVariableW(wide, equal + 1);
        }
-       return size;
-}
+       free(wide);
 
-char *mingw_getenv(const char *name)
-{
-       char *value;
-       int pos = bsearchenv(environ, name, environ_size - 1);
-       if (pos < 0)
-               return NULL;
-       value = strchr(environ[pos], '=');
-       return value ? &value[1] : NULL;
-}
+       if (!result)
+               errno = err_win_to_posix(GetLastError());
 
-int mingw_putenv(const char *namevalue)
-{
-       ALLOC_GROW(environ, (environ_size + 1) * sizeof(char*), environ_alloc);
-       environ_size = do_putenv(environ, namevalue, environ_size, 1);
-       return 0;
+       return result ? 0 : -1;
 }
 
 /*
@@ -1413,7 +1850,8 @@ static void ensure_socket_initialization(void)
                        WSAGetLastError());
 
        for (name = libraries; *name; name++) {
-               ipv6_dll = LoadLibrary(*name);
+               ipv6_dll = LoadLibraryExA(*name, NULL,
+                                         LOAD_LIBRARY_SEARCH_SYSTEM32);
                if (!ipv6_dll)
                        continue;
 
@@ -1584,7 +2022,12 @@ repeat:
        if (gle == ERROR_ACCESS_DENIED &&
            (attrs = GetFileAttributesW(wpnew)) != INVALID_FILE_ATTRIBUTES) {
                if (attrs & FILE_ATTRIBUTE_DIRECTORY) {
-                       errno = EISDIR;
+                       DWORD attrsold = GetFileAttributesW(wpold);
+                       if (attrsold == INVALID_FILE_ATTRIBUTES ||
+                           !(attrsold & FILE_ATTRIBUTE_DIRECTORY))
+                               errno = EISDIR;
+                       else if (!_wrmdir(wpnew))
+                               goto repeat;
                        return -1;
                }
                if ((attrs & FILE_ATTRIBUTE_READONLY) &&
@@ -1629,18 +2072,63 @@ int mingw_getpagesize(void)
        return si.dwAllocationGranularity;
 }
 
+/* See https://msdn.microsoft.com/en-us/library/windows/desktop/ms724435.aspx */
+enum EXTENDED_NAME_FORMAT {
+       NameDisplay = 3,
+       NameUserPrincipal = 8
+};
+
+static char *get_extended_user_info(enum EXTENDED_NAME_FORMAT type)
+{
+       DECLARE_PROC_ADDR(secur32.dll, BOOL, GetUserNameExW,
+               enum EXTENDED_NAME_FORMAT, LPCWSTR, PULONG);
+       static wchar_t wbuffer[1024];
+       DWORD len;
+
+       if (!INIT_PROC_ADDR(GetUserNameExW))
+               return NULL;
+
+       len = ARRAY_SIZE(wbuffer);
+       if (GetUserNameExW(type, wbuffer, &len)) {
+               char *converted = xmalloc((len *= 3));
+               if (xwcstoutf(converted, wbuffer, len) >= 0)
+                       return converted;
+               free(converted);
+       }
+
+       return NULL;
+}
+
+char *mingw_query_user_email(void)
+{
+       return get_extended_user_info(NameUserPrincipal);
+}
+
 struct passwd *getpwuid(int uid)
 {
+       static unsigned initialized;
        static char user_name[100];
-       static struct passwd p;
+       static struct passwd *p;
+       DWORD len;
+
+       if (initialized)
+               return p;
 
-       DWORD len = sizeof(user_name);
-       if (!GetUserName(user_name, &len))
+       len = sizeof(user_name);
+       if (!GetUserName(user_name, &len)) {
+               initialized = 1;
                return NULL;
-       p.pw_name = user_name;
-       p.pw_gecos = "unknown";
-       p.pw_dir = NULL;
-       return &p;
+       }
+
+       p = xmalloc(sizeof(*p));
+       p->pw_name = user_name;
+       p->pw_gecos = get_extended_user_info(NameDisplay);
+       if (!p->pw_gecos)
+               p->pw_gecos = "unknown";
+       p->pw_dir = NULL;
+
+       initialized = 1;
+       return p;
 }
 
 static HANDLE timer_event;
@@ -1687,7 +2175,7 @@ static void stop_timer_thread(void)
        if (timer_event)
                SetEvent(timer_event);  /* tell thread to terminate */
        if (timer_thread) {
-               int rc = WaitForSingleObject(timer_thread, 1000);
+               int rc = WaitForSingleObject(timer_thread, 10000);
                if (rc == WAIT_TIMEOUT)
                        error("timer thread did not terminate timely");
                else if (rc != WAIT_OBJECT_0)
@@ -1796,67 +2284,14 @@ int mingw_raise(int sig)
        }
 }
 
-
-static const char *make_backslash_path(const char *path)
-{
-       static char buf[PATH_MAX + 1];
-       char *c;
-
-       if (strlcpy(buf, path, PATH_MAX) >= PATH_MAX)
-               die("Too long path: %.*s", 60, path);
-
-       for (c = buf; *c; c++) {
-               if (*c == '/')
-                       *c = '\\';
-       }
-       return buf;
-}
-
-void mingw_open_html(const char *unixpath)
-{
-       const char *htmlpath = make_backslash_path(unixpath);
-       typedef HINSTANCE (WINAPI *T)(HWND, const char *,
-                       const char *, const char *, const char *, INT);
-       T ShellExecute;
-       HMODULE shell32;
-       int r;
-
-       shell32 = LoadLibrary("shell32.dll");
-       if (!shell32)
-               die("cannot load shell32.dll");
-       ShellExecute = (T)GetProcAddress(shell32, "ShellExecuteA");
-       if (!ShellExecute)
-               die("cannot run browser");
-
-       printf("Launching default browser to display HTML ...\n");
-       r = (int)ShellExecute(NULL, "open", htmlpath, NULL, "\\", SW_SHOWNORMAL);
-       FreeLibrary(shell32);
-       /* see the MSDN documentation referring to the result codes here */
-       if (r <= 32) {
-               die("failed to launch browser for %.*s", MAX_PATH, unixpath);
-       }
-}
-
 int link(const char *oldpath, const char *newpath)
 {
-       typedef BOOL (WINAPI *T)(LPCWSTR, LPCWSTR, LPSECURITY_ATTRIBUTES);
-       static T create_hard_link = NULL;
        wchar_t woldpath[MAX_PATH], wnewpath[MAX_PATH];
        if (xutftowcs_path(woldpath, oldpath) < 0 ||
                xutftowcs_path(wnewpath, newpath) < 0)
                return -1;
 
-       if (!create_hard_link) {
-               create_hard_link = (T) GetProcAddress(
-                       GetModuleHandle("kernel32.dll"), "CreateHardLinkW");
-               if (!create_hard_link)
-                       create_hard_link = (T)-1;
-       }
-       if (create_hard_link == (T)-1) {
-               errno = ENOSYS;
-               return -1;
-       }
-       if (!create_hard_link(wnewpath, woldpath, NULL)) {
+       if (!CreateHardLinkW(wnewpath, woldpath, NULL)) {
                errno = err_win_to_posix(GetLastError());
                return -1;
        }
@@ -1915,30 +2350,6 @@ pid_t waitpid(pid_t pid, int *status, int options)
        return -1;
 }
 
-int mingw_offset_1st_component(const char *path)
-{
-       int offset = 0;
-       if (has_dos_drive_prefix(path))
-               offset = 2;
-
-       /* unc paths */
-       else if (is_dir_sep(path[0]) && is_dir_sep(path[1])) {
-
-               /* skip server name */
-               char *pos = strpbrk(path + 2, "\\/");
-               if (!pos)
-                       return 0; /* Error: malformed unc path */
-
-               do {
-                       pos++;
-               } while (*pos && !is_dir_sep(*pos));
-
-               offset = pos - path;
-       }
-
-       return offset + is_dir_sep(path[offset]);
-}
-
 int xutftowcsn(wchar_t *wcs, const char *utfs, size_t wcslen, int utflen)
 {
        int upos = 0, wpos = 0;
@@ -2024,6 +2435,35 @@ int xwcstoutf(char *utf, const wchar_t *wcs, size_t utflen)
        return -1;
 }
 
+static void setup_windows_environment(void)
+{
+       char *tmp = getenv("TMPDIR");
+
+       /* on Windows it is TMP and TEMP */
+       if (!tmp) {
+               if (!(tmp = getenv("TMP")))
+                       tmp = getenv("TEMP");
+               if (tmp) {
+                       setenv("TMPDIR", tmp, 1);
+                       tmp = getenv("TMPDIR");
+               }
+       }
+
+       if (tmp) {
+               /*
+                * Convert all dir separators to forward slashes,
+                * to help shell commands called from the Git
+                * executable (by not mistaking the dir separators
+                * for escape characters).
+                */
+               convert_slashes(tmp);
+       }
+
+       /* simulate TERM to enable auto-color (see color.c) */
+       if (!getenv("TERM"))
+               setenv("TERM", "cygwin", 1);
+}
+
 /*
  * Disable MSVCRT command line wildcard expansion (__getmainargs called from
  * mingw startup code, see init.c in mingw runtime).
@@ -2037,7 +2477,7 @@ typedef struct {
 extern int __wgetmainargs(int *argc, wchar_t ***argv, wchar_t ***env, int glob,
                _startupinfo *si);
 
-static NORETURN void die_startup()
+static NORETURN void die_startup(void)
 {
        fputs("fatal: not enough memory for initialization", stderr);
        exit(128);
@@ -2057,64 +2497,94 @@ static char *wcstoutfdup_startup(char *buffer, const wchar_t *wcs, size_t len)
        return memcpy(malloc_startup(len), buffer, len);
 }
 
-void mingw_startup()
+static void maybe_redirect_std_handle(const wchar_t *key, DWORD std_id, int fd,
+                                     DWORD desired_access, DWORD flags)
+{
+       DWORD create_flag = fd ? OPEN_ALWAYS : OPEN_EXISTING;
+       wchar_t buf[MAX_PATH];
+       DWORD max = ARRAY_SIZE(buf);
+       HANDLE handle;
+       DWORD ret = GetEnvironmentVariableW(key, buf, max);
+
+       if (!ret || ret >= max)
+               return;
+
+       /* make sure this does not leak into child processes */
+       SetEnvironmentVariableW(key, NULL);
+       if (!wcscmp(buf, L"off")) {
+               close(fd);
+               handle = GetStdHandle(std_id);
+               if (handle != INVALID_HANDLE_VALUE)
+                       CloseHandle(handle);
+               return;
+       }
+       if (std_id == STD_ERROR_HANDLE && !wcscmp(buf, L"2>&1")) {
+               handle = GetStdHandle(STD_OUTPUT_HANDLE);
+               if (handle == INVALID_HANDLE_VALUE) {
+                       close(fd);
+                       handle = GetStdHandle(std_id);
+                       if (handle != INVALID_HANDLE_VALUE)
+                               CloseHandle(handle);
+               } else {
+                       int new_fd = _open_osfhandle((intptr_t)handle, O_BINARY);
+                       SetStdHandle(std_id, handle);
+                       dup2(new_fd, fd);
+                       /* do *not* close the new_fd: that would close stdout */
+               }
+               return;
+       }
+       handle = CreateFileW(buf, desired_access, 0, NULL, create_flag,
+                            flags, NULL);
+       if (handle != INVALID_HANDLE_VALUE) {
+               int new_fd = _open_osfhandle((intptr_t)handle, O_BINARY);
+               SetStdHandle(std_id, handle);
+               dup2(new_fd, fd);
+               close(new_fd);
+       }
+}
+
+static void maybe_redirect_std_handles(void)
+{
+       maybe_redirect_std_handle(L"GIT_REDIRECT_STDIN", STD_INPUT_HANDLE, 0,
+                                 GENERIC_READ, FILE_ATTRIBUTE_NORMAL);
+       maybe_redirect_std_handle(L"GIT_REDIRECT_STDOUT", STD_OUTPUT_HANDLE, 1,
+                                 GENERIC_WRITE, FILE_ATTRIBUTE_NORMAL);
+       maybe_redirect_std_handle(L"GIT_REDIRECT_STDERR", STD_ERROR_HANDLE, 2,
+                                 GENERIC_WRITE, FILE_FLAG_NO_BUFFERING);
+}
+
+void mingw_startup(void)
 {
        int i, maxlen, argc;
        char *buffer;
        wchar_t **wenv, **wargv;
        _startupinfo si;
 
+       maybe_redirect_std_handles();
+
        /* get wide char arguments and environment */
        si.newmode = 0;
        if (__wgetmainargs(&argc, &wargv, &wenv, _CRT_glob, &si) < 0)
                die_startup();
 
        /* determine size of argv and environ conversion buffer */
-       maxlen = wcslen(_wpgmptr);
+       maxlen = wcslen(wargv[0]);
        for (i = 1; i < argc; i++)
                maxlen = max(maxlen, wcslen(wargv[i]));
-       for (i = 0; wenv[i]; i++)
-               maxlen = max(maxlen, wcslen(wenv[i]));
-
-       /*
-        * nedmalloc can't free CRT memory, allocate resizable environment
-        * list. Note that xmalloc / xmemdupz etc. call getenv, so we cannot
-        * use it while initializing the environment itself.
-        */
-       environ_size = i + 1;
-       environ_alloc = alloc_nr(environ_size * sizeof(char*));
-       environ = malloc_startup(environ_alloc);
 
        /* allocate buffer (wchar_t encodes to max 3 UTF-8 bytes) */
        maxlen = 3 * maxlen + 1;
        buffer = malloc_startup(maxlen);
 
        /* convert command line arguments and environment to UTF-8 */
-       __argv[0] = wcstoutfdup_startup(buffer, _wpgmptr, maxlen);
-       for (i = 1; i < argc; i++)
+       for (i = 0; i < argc; i++)
                __argv[i] = wcstoutfdup_startup(buffer, wargv[i], maxlen);
-       for (i = 0; wenv[i]; i++)
-               environ[i] = wcstoutfdup_startup(buffer, wenv[i], maxlen);
-       environ[i] = NULL;
        free(buffer);
 
-       /* sort environment for O(log n) getenv / putenv */
-       qsort(environ, i, sizeof(char*), compareenv);
-
        /* fix Windows specific environment settings */
+       setup_windows_environment();
 
-       /* on Windows it is TMP and TEMP */
-       if (!mingw_getenv("TMPDIR")) {
-               const char *tmp = mingw_getenv("TMP");
-               if (!tmp)
-                       tmp = mingw_getenv("TEMP");
-               if (tmp)
-                       setenv("TMPDIR", tmp, 1);
-       }
-
-       /* simulate TERM to enable auto-color (see color.c) */
-       if (!getenv("TERM"))
-               setenv("TERM", "cygwin", 1);
+       unset_environment_variables = xstrdup("PERL5LIB");
 
        /* initialize critical section for waitpid pinfo_t list */
        InitializeCriticalSection(&pinfo_cs);
@@ -2131,11 +2601,13 @@ void mingw_startup()
 
 int uname(struct utsname *buf)
 {
-       DWORD v = GetVersion();
+       unsigned v = (unsigned)GetVersion();
        memset(buf, 0, sizeof(*buf));
-       strcpy(buf->sysname, "Windows");
-       sprintf(buf->release, "%u.%u", v & 0xff, (v >> 8) & 0xff);
+       xsnprintf(buf->sysname, sizeof(buf->sysname), "Windows");
+       xsnprintf(buf->release, sizeof(buf->release),
+                "%u.%u", v & 0xff, (v >> 8) & 0xff);
        /* assuming NT variants only.. */
-       sprintf(buf->version, "%u", (v >> 16) & 0x7fff);
+       xsnprintf(buf->version, sizeof(buf->version),
+                 "%u", (v >> 16) & 0x7fff);
        return 0;
 }