[flang-commits] [clang] [flang] [clang-tools-extra] [llvm] [flang ]GETLOG runtime and extension implementation: get login username (PR #70917)
via flang-commits
flang-commits at lists.llvm.org
Wed Nov 15 01:26:39 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; }
----------------
jeanPerier wrote:
Actually, it seems the gfortran extension for getlog says that: https://gcc.gnu.org/onlinedocs/gcc-4.2.4/gfortran/GETLOG.html
> On systems where the getlogin(3) function is not implemented, this will return a blank string
So I think the fallback version could still return zero and set the buffer to blanks.
https://github.com/llvm/llvm-project/pull/70917
More information about the flang-commits
mailing list