summaryrefslogtreecommitdiff
path: root/lib/Support/Windows/Process.inc
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2013-09-10 19:45:51 +0000
committerRui Ueyama <ruiu@google.com>2013-09-10 19:45:51 +0000
commitf42d4247ae1138c6deed50f92dcd1a4f34e07dec (patch)
tree0e4fd79f9e891171d4b70ecfc5cc055876d91dc3 /lib/Support/Windows/Process.inc
parent8e12d95d15e4140311919a3b60461817baf68ca5 (diff)
downloadllvm-f42d4247ae1138c6deed50f92dcd1a4f34e07dec.tar.gz
llvm-f42d4247ae1138c6deed50f92dcd1a4f34e07dec.tar.bz2
llvm-f42d4247ae1138c6deed50f92dcd1a4f34e07dec.tar.xz
Add getenv() wrapper that works on multibyte environment variable.
On Windows, character encoding of multibyte environment variable varies depending on settings. The only reliable way to handle it I think is to use GetEnvironmentVariableW(). GetEnvironmentVariableW() works on wchar_t string, which is on Windows UTF16 string. That's not ideal because we use UTF-8 as the internal encoding in LLVM. This patch defines a wrapper function which takes and returns UTF-8 string for GetEnvironmentVariableW(). The wrapper function does not do any conversion and just forwards the argument to getenv() on Unix. Differential Revision: http://llvm-reviews.chandlerc.com/D1612 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@190423 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support/Windows/Process.inc')
-rw-r--r--lib/Support/Windows/Process.inc30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/Support/Windows/Process.inc b/lib/Support/Windows/Process.inc
index f840d064d8..0191751a82 100644
--- a/lib/Support/Windows/Process.inc
+++ b/lib/Support/Windows/Process.inc
@@ -140,6 +140,36 @@ void Process::PreventCoreFiles() {
SEM_NOOPENFILEERRORBOX);
}
+/// Returns the environment variable \arg Name's value as a string encoded in
+/// UTF-8. \arg Name is assumed to be in UTF-8 encoding.
+Optional<std::string> Process::GetEnv(StringRef Name) {
+ // Convert the argument to UTF-16 to pass it to _wgetenv().
+ SmallVector<wchar_t, 128> NameUTF16;
+ if (error_code ec = windows::UTF8ToUTF16(Name, NameUTF16))
+ return None;
+
+ // Environment variable can be encoded in non-UTF8 encoding, and there's no
+ // way to know what the encoding is. The only reliable way to look up
+ // multibyte environment variable is to use GetEnvironmentVariableW().
+ std::vector<wchar_t> Buf(16);
+ size_t Size = 0;
+ for (;;) {
+ Size = GetEnvironmentVariableW(&NameUTF16[0], &Buf[0], Buf.size());
+ if (Size < Buf.size())
+ break;
+ // Try again with larger buffer.
+ Buf.resize(Size + 1);
+ }
+ if (Size == 0)
+ return None;
+
+ // Convert the result from UTF-16 to UTF-8.
+ SmallVector<char, 128> Res;
+ if (error_code ec = windows::UTF16ToUTF8(&Buf[0], Size, Res))
+ return None;
+ return std::string(&Res[0]);
+}
+
bool Process::StandardInIsUserInput() {
return FileDescriptorIsDisplayed(0);
}