summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/fileops.c45
-rw-r--r--src/win32/utf-conv.c23
2 files changed, 52 insertions, 16 deletions
diff --git a/src/fileops.c b/src/fileops.c
index d6960ca1a..6dd9270b5 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -353,13 +353,48 @@ int git_futils_rmdir_r(const char *path, git_directory_removal_type removal_type
return error;
}
+#ifdef GIT_WIN32
+static char *win32_getenv(const wchar_t *name)
+{
+ char *val_utf8;
+ wchar_t *val_utf16;
+ DWORD len = GetEnvironmentVariableW(name, NULL, 0);
+
+ if (len <= 0)
+ return NULL;
+
+ val_utf16 = git__calloc(len, sizeof(wchar_t));
+ if (!val_utf16)
+ return NULL;
+
+ if (GetEnvironmentVariableW(name, val_utf16, len) != len - 1) {
+ giterr_set(GITERR_OS, "Could not read environment variable");
+ git__free(val_utf16);
+ return NULL;
+ }
+
+ val_utf8 = gitwin_from_utf16(val_utf16);
+
+ git__free(val_utf16);
+
+ return val_utf8;
+}
+#endif
+
int git_futils_find_global_file(git_buf *path, const char *filename)
{
- const char *home = getenv("HOME");
+ char *home;
#ifdef GIT_WIN32
- if (home == NULL)
- home = getenv("USERPROFILE");
+ home = win32_getenv(L"HOME");
+
+ if (!home)
+ home = win32_getenv(L"USERPROFILE");
+
+ if (home)
+ git_path_mkposix(home);
+#else
+ home = getenv("HOME");
#endif
if (home == NULL) {
@@ -371,6 +406,10 @@ int git_futils_find_global_file(git_buf *path, const char *filename)
if (git_buf_joinpath(path, home, filename) < 0)
return -1;
+#ifdef GIT_WIN32
+ git__free(home);
+#endif
+
if (git_path_exists(path->ptr) == false) {
git_buf_clear(path);
return GIT_ENOTFOUND;
diff --git a/src/win32/utf-conv.c b/src/win32/utf-conv.c
index 76f1e4237..0a705c0ad 100644
--- a/src/win32/utf-conv.c
+++ b/src/win32/utf-conv.c
@@ -32,19 +32,16 @@ void gitwin_set_utf8(void)
wchar_t* gitwin_to_utf16(const char* str)
{
wchar_t* ret;
- size_t cb;
+ int cb;
if (!str)
return NULL;
- cb = strlen(str) * sizeof(wchar_t);
+ cb = MultiByteToWideChar(_active_codepage, 0, str, -1, NULL, 0);
if (cb == 0)
return (wchar_t *)git__calloc(1, sizeof(wchar_t));
- /* Add space for null terminator */
- cb += sizeof(wchar_t);
-
- ret = (wchar_t *)git__malloc(cb);
+ ret = (wchar_t *)git__malloc(cb * sizeof(wchar_t));
if (!ret)
return NULL;
@@ -59,7 +56,8 @@ wchar_t* gitwin_to_utf16(const char* str)
int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
{
- int result = MultiByteToWideChar(_active_codepage, 0, str, -1, buffer, (int)len);
+ int result = MultiByteToWideChar(
+ _active_codepage, 0, str, -1, buffer, (int)len);
if (result == 0)
giterr_set(GITERR_OS, "Could not convert string to UTF-16");
return result;
@@ -68,23 +66,22 @@ int gitwin_append_utf16(wchar_t *buffer, const char *str, size_t len)
char* gitwin_from_utf16(const wchar_t* str)
{
char* ret;
- size_t cb;
+ int cb;
if (!str)
return NULL;
- cb = wcslen(str) * sizeof(char);
+ cb = WideCharToMultiByte(_active_codepage, 0, str, -1, NULL, 0, NULL, NULL);
if (cb == 0)
return (char *)git__calloc(1, sizeof(char));
- /* Add space for null terminator */
- cb += sizeof(char);
-
ret = (char*)git__malloc(cb);
if (!ret)
return NULL;
- if (WideCharToMultiByte(_active_codepage, 0, str, -1, ret, (int)cb, NULL, NULL) == 0) {
+ if (WideCharToMultiByte(
+ _active_codepage, 0, str, -1, ret, (int)cb, NULL, NULL) == 0)
+ {
giterr_set(GITERR_OS, "Could not convert string to UTF-8");
git__free(ret);
ret = NULL;