diff options
author | Laszlo Csomor <laszlocsomor@google.com> | 2016-12-15 12:26:06 +0000 |
---|---|---|
committer | John Cater <jcater@google.com> | 2016-12-15 20:37:53 +0000 |
commit | 41ee5911f41d9654027f57b15b4a691ee520854b (patch) | |
tree | 34de55205b2f0738ae9166b61c55f44a2d184752 /src | |
parent | aa1614b6a081ea435c536ef2b93c270dd5b071b8 (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')
-rw-r--r-- | src/main/cpp/BUILD | 3 | ||||
-rw-r--r-- | src/main/cpp/blaze_util_windows.cc | 22 |
2 files changed, 8 insertions, 17 deletions
diff --git a/src/main/cpp/BUILD b/src/main/cpp/BUILD index 82ed5510cf..e6bc52b085 100644 --- a/src/main/cpp/BUILD +++ b/src/main/cpp/BUILD @@ -48,7 +48,8 @@ cc_library( "//src:freebsd": [ ], "//src:windows_msvc": [ - "-Wl,ws2_32.lib", # for grpc + "-Wl,advapi32.lib", # GetUserNameW + "-Wl,ws2_32.lib", # grpc ], "//conditions:default": [ "-lrt", 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() { |