From 41ee5911f41d9654027f57b15b4a691ee520854b Mon Sep 17 00:00:00 2001 From: Laszlo Csomor Date: Thu, 15 Dec 2016 12:26:06 +0000 Subject: Bazel client, Windows: implement GetUserName See https://github.com/bazelbuild/bazel/issues/2107 -- PiperOrigin-RevId: 142128121 MOS_MIGRATED_REVID=142128121 --- src/main/cpp/BUILD | 3 ++- src/main/cpp/blaze_util_windows.cc | 22 ++++++---------------- 2 files changed, 8 insertions(+), 17 deletions(-) (limited to 'src/main') 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 -#include #include #include #include @@ -28,6 +27,7 @@ #endif // COMPILER_MSVC #include +#include // UNLEN #include #include @@ -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() { -- cgit v1.2.3