[clang-tools-extra] [llvm] [flang] [clang] [flang ]GETLOG runtime and extension implementation: get login username (PR #70917)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 15 01:26:39 PST 2023
================
@@ -37,5 +75,19 @@ void FORTRAN_PROCEDURE_NAME(getarg)(
(void)RTNAME(GetCommandArgument)(
n, &value, nullptr, nullptr, __FILE__, __LINE__);
}
+
+void FORTRAN_PROCEDURE_NAME(getlog)(std::int8_t *arg, std::int64_t length) {
+ std::array<char, LOGIN_NAME_MAX + 1> str;
+ int error = getlogin_r(str.data(), str.size());
+ assert(error == 0 && "getlogin_r returned an error");
+
+ // Trim space from right/end
+ int i = str.size();
+ while (' ' == str[--i]) {
+ str[i] = 0;
+ }
+ strncpy(reinterpret_cast<char *>(arg), str.data(), length);
+}
----------------
jeanPerier wrote:
I think you also need to pad the result argument with blanks for all the characters of the result that were not filled with the login (including the copied `0` terminator) (if it was copied).
Padding with blanks is the usual Fortran convention for INTENT(OUT) string passed to intrinsics, GETLOG does not mention it, but I tested with gfortran and it does pad with blanks.
Example on some system I am called `jp`
```
character(10) :: string
string = "ZZZZZZZZZZ"
call getlog(string)
write(*, "(AA)") string, "X"
end
```
outputs `jp X`, so the 8 characters after the login name were padded with blanks.
Maybe you could share and re-use the copyBufferAndPad logic at:
https://github.com/llvm/llvm-project/blob/1b781ee9284ded54aabd88690a726b615c29cc2d/flang/runtime/time-intrinsic.cpp#L282
https://github.com/llvm/llvm-project/pull/70917
More information about the cfe-commits
mailing list