[flang-commits] [flang] [clang] [clang-tools-extra] [llvm] [flang ]GETLOG runtime and extension implementation: get login username (PR #70917)
Yi Wu via flang-commits
flang-commits at lists.llvm.org
Wed Nov 15 06:06:21 PST 2023
================
@@ -13,6 +13,44 @@
#include "flang/Runtime/command.h"
#include "flang/Runtime/descriptor.h"
#include "flang/Runtime/io-api.h"
+#include <string.h>
+
+#ifdef _WIN32
+#define WIN32_LEAN_AND_MEAN
+#define NOMINMAX
+#include <windows.h>
+
+#include <lmcons.h> // UNLEN=256
+#include <stdlib.h> // wcstombs_s
+#include <wchar.h> // wchar_t cast to LPWSTR
+#pragma comment(lib, "Advapi32.lib") // Link Advapi32.lib for GetUserName
+#define LOGIN_NAME_MAX UNLEN
+
+inline int getlogin_r(char *buf, size_t bufSize) {
+ wchar_t w_username[UNLEN + 1];
+ DWORD nameLen = UNLEN + 1;
+
+ if (GetUserNameW(w_username, &nameLen)) {
+ // Convert the wchar_t string to a regular C string using wcstombs_s
+ if (wcstombs_s(nullptr, buf, sizeof(w_username), w_username, _TRUNCATE) !=
+ 0) {
+ // Conversion failed
+ return -1;
+ }
+ return (buf[0] == 0 ? -1 : 0);
+ } else {
+ return -1;
+ }
+ return -1;
+}
+
+#elif _REENTRANT || _POSIX_C_SOURCE >= 199506L
+// System is posix-compliant and has getlogin_r
+#include <unistd.h>
+#else
+// System is not posix-compliant
+inline int getlogin_r(char *buf, size_t bufsize) { return -1; }
----------------
PAX-12-WU wrote:
Good suggestion!
https://github.com/llvm/llvm-project/pull/70917
More information about the flang-commits
mailing list