Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions flang-rt/lib/runtime/extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion flang/include/flang/Runtime/extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ uid_t RTNAME(GetUID)();
void FORTRAN_PROCEDURE_NAME(getlog)(char *name, std::int64_t length);

// GNU extension subroutine HOSTNM(C)
void FORTRAN_PROCEDURE_NAME(hostnm)(char *name, std::int64_t length);
int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length);

std::intptr_t RTNAME(Malloc)(std::size_t size);

Expand Down
Loading