Rewrite getenv to use Win32 version on Windows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
diff --git a/src/config.c b/src/config.c
index f233e76..261beb4 100644
--- a/src/config.c
+++ b/src/config.c
@@ -306,20 +306,24 @@ int git_config_get_string(git_config *cfg, const char *name, const char **out)
int git_config_find_global(char *global_config_path)
{
- const char *home;
+ char *home;
- home = getenv("HOME");
+ home = p_getenv("HOME");
#ifdef GIT_WIN32
if (home == NULL)
- home = getenv("USERPROFILE");
+ home = p_getenv("USERPROFILE");
#endif
- if (home == NULL)
+ if (home == NULL) {
+ free(home);
return git__throw(GIT_EOSERR, "Failed to open global config file. Cannot locate the user's home directory");
+ }
git_path_join(global_config_path, home, GIT_CONFIG_FILENAME);
+ free(home);
+
if (git_futils_exists(global_config_path) < GIT_SUCCESS)
return git__throw(GIT_EOSERR, "Failed to open global config file. The file does not exist");
diff --git a/src/posix.c b/src/posix.c
index 1b85b05..fb8ce37 100644
--- a/src/posix.c
+++ b/src/posix.c
@@ -39,6 +39,15 @@ int p_getcwd(char *buffer_out, size_t size)
return GIT_SUCCESS;
}
+char* p_getenv(const char* name)
+{
+ char* buf = getenv(name);
+ if (!buf)
+ return buf;
+
+ return git__strdup(buf);
+}
+
#endif
int p_read(git_file fd, void *buf, size_t cnt)
diff --git a/src/posix.h b/src/posix.h
index 48b0255..d656e8e 100644
--- a/src/posix.h
+++ b/src/posix.h
@@ -44,6 +44,7 @@ extern int p_write(git_file fd, const void *buf, size_t cnt);
extern int p_open(const char *path, int flags);
extern int p_creat(const char *path, int mode);
extern int p_getcwd(char *buffer_out, size_t size);
+extern char* p_getenv(const char* name);
#ifndef GIT_WIN32
diff --git a/src/win32/posix_w32.c b/src/win32/posix_w32.c
index af54e7f..85a04bc 100644
--- a/src/win32/posix_w32.c
+++ b/src/win32/posix_w32.c
@@ -370,6 +370,30 @@ int p_mkstemp(char *tmp_path)
return p_creat(tmp_path, 0744);
}
+char* p_getenv(const char* name)
+{
+ wchar_t* buf;
+ wchar_t* name_w = conv_utf8_to_utf16(name);
+ char* ret;
+ DWORD len;
+
+ len = GetEnvironmentVariableW(name_w, NULL, 0);
+ if (len == 0) {
+ free(name_w);
+ return NULL;
+ }
+
+ len++; /* Null Terminator */
+ buf = malloc(sizeof(wchar_t) * len);
+ GetEnvironmentVariableW(name_w, buf, len);
+
+ ret = conv_utf16_to_utf8(buf);
+
+ free(name_w);
+ free(buf);
+ return ret;
+}
+
int p_setenv(const char* name, const char* value, int overwrite)
{
if (overwrite != 1)
diff --git a/tests-clay/clay_main.c b/tests-clay/clay_main.c
index 25342e7..b2849d5 100644
--- a/tests-clay/clay_main.c
+++ b/tests-clay/clay_main.c
@@ -388,14 +388,17 @@ find_tmp_path(char *buffer, size_t length)
size_t i;
for (i = 0; i < var_count; ++i) {
- const char *env = getenv(env_vars[i]);
+ char *env = p_getenv(env_vars[i]);
if (!env)
continue;
if (is_valid_tmp_path(env)) {
strncpy(buffer, env, length);
+ free(env);
return 0;
}
+
+ free(env);
}
/* If the environment doesn't say anything, try to use /tmp */
diff --git a/tests/t15-config.c b/tests/t15-config.c
index d912abb..ac460bc 100644
--- a/tests/t15-config.c
+++ b/tests/t15-config.c
@@ -217,7 +217,7 @@ BEGIN_TEST(config10, "a repo's config overrides the global config")
int version;
char *old_home;
- old_home = git__strdup(getenv("HOME"));
+ old_home = p_getenv("HOME");
p_setenv("HOME", CONFIG_BASE, 1);
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
@@ -237,7 +237,7 @@ BEGIN_TEST(config11, "fall back to the global config")
int num;
char *old_home;
- old_home = git__strdup(getenv("HOME"));
+ old_home = p_getenv("HOME");
p_setenv("HOME", CONFIG_BASE, 1);
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
diff --git a/tests/t16-remotes.c b/tests/t16-remotes.c
index af54f29..b76557f 100644
--- a/tests/t16-remotes.c
+++ b/tests/t16-remotes.c
@@ -34,7 +34,7 @@ BEGIN_TEST(remotes0, "remote parsing works")
git_config *cfg;
char *old_home;
- old_home = git__strdup(getenv("HOME"));
+ old_home = p_getenv("HOME");
p_setenv("HOME", "/dev/null", 1);
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
@@ -58,7 +58,7 @@ BEGIN_TEST(refspec0, "remote with refspec works")
const git_refspec *refspec = NULL;
char *old_home;
- old_home = git__strdup(getenv("HOME"));
+ old_home = p_getenv("HOME");
p_setenv("HOME", "/dev/null", 1);
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
@@ -83,7 +83,7 @@ BEGIN_TEST(refspec1, "remote fnmatch works as expected")
const git_refspec *refspec = NULL;
char *old_home;
- old_home = git__strdup(getenv("HOME"));
+ old_home = p_getenv("HOME");
p_setenv("HOME", "/dev/null", 1);
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));
@@ -109,7 +109,7 @@ BEGIN_TEST(refspec2, "refspec transform")
char ref[1024] = {0};
char *old_home;
- old_home = git__strdup(getenv("HOME"));
+ old_home = p_getenv("HOME");
p_setenv("HOME", "/dev/null", 1);
must_pass(git_repository_open(&repo, REPOSITORY_FOLDER));