Skip to content

Commit 3428cc9

Browse files
[flang] Implement external routine usage of hostnm() (llvm#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.
1 parent e4d951d commit 3428cc9

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

flang-rt/lib/runtime/extensions.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,38 @@ int RTNAME(Chdir)(const char *name) {
264264
#endif
265265
}
266266

267+
int FORTRAN_PROCEDURE_NAME(hostnm)(char *hn, int length) {
268+
std::int32_t status{0};
269+
270+
if (!hn || length < 0) {
271+
return EINVAL;
272+
}
273+
274+
#ifdef _WIN32
275+
DWORD dwSize{static_cast<DWORD>(length)};
276+
277+
// Note: Winsock has gethostname(), but use Win32 API GetComputerNameEx(),
278+
// in order to avoid adding dependency on Winsock.
279+
if (!GetComputerNameExA(ComputerNameDnsHostname, hn, &dwSize)) {
280+
status = GetLastError();
281+
}
282+
#else
283+
if (gethostname(hn, length) < 0) {
284+
status = errno;
285+
}
286+
#endif
287+
288+
if (status == 0) {
289+
// Find zero terminator and fill the string from the
290+
// zero terminator to the end with spaces
291+
char *str_end{hn + length};
292+
char *str_zero{std::find(hn, str_end, '\0')};
293+
std::fill(str_zero, str_end, ' ');
294+
}
295+
296+
return status;
297+
}
298+
267299
int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
268300

269301
void FORTRAN_PROCEDURE_NAME(qsort)(int *array, int *len, int *isize,

flang/include/flang/Runtime/extensions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ uid_t RTNAME(GetUID)();
6060
void FORTRAN_PROCEDURE_NAME(getlog)(char *name, std::int64_t length);
6161

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

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

0 commit comments

Comments
 (0)