[flang] Implement external routine usage of hostnm() (#134900)

Previously, `hostnm` extended intrinsic was implemented as proper
intrinsic. Since then we found out that some applications use `hostnm`
as external routine via `external hostnm`. This prevents `hostnm` from
being recognized as an intrinsic. This PR implements `hostnm` as
external routine.
This commit is contained in:
Eugene Epshteyn
2025-04-15 19:04:59 -04:00
committed by GitHub
parent e4d951d2e4
commit 3428cc94c8
2 changed files with 33 additions and 1 deletions

View File

@@ -264,6 +264,38 @@ int RTNAME(Chdir)(const char *name) {
#endif
}
int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length) {
std::int32_t status{0};
if (!hn || length < 0) {
return EINVAL;
}
#ifdef _WIN32
DWORD dwSize{static_cast<DWORD>(length)};
// Note: Winsock has gethostname(), but use Win32 API GetComputerNameEx(),
// in order to avoid adding dependency on Winsock.
if (!GetComputerNameExA(ComputerNameDnsHostname, hn, &dwSize)) {
status = GetLastError();
}
#else
if (gethostname(hn, length) < 0) {
status = errno;
}
#endif
if (status == 0) {
// Find zero terminator and fill the string from the
// zero terminator to the end with spaces
char *str_end{hn + length};
char *str_zero{std::find(hn, str_end, '\0')};
std::fill(str_zero, str_end, ' ');
}
return status;
}
int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize,