aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp/blaze_util_windows.cc
diff options
context:
space:
mode:
authorGravatar Laszlo Csomor <laszlocsomor@google.com>2016-12-15 12:26:06 +0000
committerGravatar John Cater <jcater@google.com>2016-12-15 20:37:53 +0000
commit41ee5911f41d9654027f57b15b4a691ee520854b (patch)
tree34de55205b2f0738ae9166b61c55f44a2d184752 /src/main/cpp/blaze_util_windows.cc
parentaa1614b6a081ea435c536ef2b93c270dd5b071b8 (diff)
Bazel client, Windows: implement GetUserName
See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 142128121 MOS_MIGRATED_REVID=142128121
Diffstat (limited to 'src/main/cpp/blaze_util_windows.cc')
-rw-r--r--src/main/cpp/blaze_util_windows.cc22
1 files changed, 6 insertions, 16 deletions
diff --git a/src/main/cpp/blaze_util_windows.cc b/src/main/cpp/blaze_util_windows.cc
index 50afc7fb3d..d74bd8d9a2 100644
--- a/src/main/cpp/blaze_util_windows.cc
+++ b/src/main/cpp/blaze_util_windows.cc
@@ -18,7 +18,6 @@
#ifndef COMPILER_MSVC
#include <fcntl.h>
-#include <pwd.h>
#include <sys/cygwin.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
@@ -28,6 +27,7 @@
#endif // COMPILER_MSVC
#include <windows.h>
+#include <lmcons.h> // UNLEN
#include <cstdio>
#include <cstdlib>
@@ -1309,23 +1309,13 @@ void ReleaseLock(BlazeLock* blaze_lock) {
#endif
string GetUserName() {
-#ifdef COMPILER_MSVC
- // TODO(bazel-team): implement this.
- pdie(255, "blaze::GetUserName is not implemented on Windows");
- return "";
-#else // not COMPILER_MSVC
- string user = GetEnv("USER");
- if (!user.empty()) {
- return user;
- }
- errno = 0;
- passwd *pwent = getpwuid(getuid()); // NOLINT (single-threaded)
- if (pwent == NULL || pwent->pw_name == NULL) {
+ WCHAR buffer[UNLEN + 1];
+ DWORD len = UNLEN + 1;
+ if (!GetUserNameW(buffer, &len)) {
pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "$USER is not set, and unable to look up name of current user");
+ "ERROR: GetUserNameW failed, err=%d\n", GetLastError());
}
- return pwent->pw_name;
-#endif // COMPILER_MSVC
+ return string(blaze_util::WstringToCstring(buffer).get());
}
bool IsEmacsTerminal() {