Skip to content

Commit aaa2496

Browse files
committed
fix(hostname): use own impl in favour of broken gethostname4j
1 parent 5f690c3 commit aaa2496

1 file changed

Lines changed: 20 additions & 5 deletions

File tree

src/main/kotlin/fi/testaustime/plugin_intellij/utils/ContextInformation.kt

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ import com.intellij.openapi.project.Project
1010
import com.intellij.openapi.project.guessProjectDir
1111
import com.intellij.openapi.roots.ModuleRootManager
1212
import com.intellij.psi.util.PsiUtilBase
13-
import com.kstruct.gethostname4j.Hostname
13+
import com.sun.jna.Library
14+
import com.sun.jna.Native
15+
import com.sun.jna.Platform
16+
import com.sun.jna.platform.win32.Kernel32Util
1417
import kotlinx.coroutines.runBlocking
1518
import org.jetbrains.concurrency.await
1619

@@ -56,9 +59,21 @@ object ContextInformation {
5659
}
5760
}
5861

59-
fun getHostname(): String? = try {
60-
Hostname.getHostname()
61-
} catch (e: RuntimeException) {
62-
null
62+
interface UnixCLibrary : Library {
63+
companion object {
64+
val INSTANCE: UnixCLibrary = Native.load("c", UnixCLibrary::class.java)
65+
}
66+
67+
fun gethostname(name: ByteArray, len: Int): Int
68+
}
69+
70+
fun getHostname(): String? {
71+
return if (Platform.isWindows()) {
72+
Kernel32Util.getComputerName();
73+
} else {
74+
val buffer = ByteArray(4097)
75+
val result = UnixCLibrary.INSTANCE.gethostname(buffer, buffer.size)
76+
if (result == 0) String(buffer).trim { it <= ' ' } else null
77+
}
6378
}
6479
}

0 commit comments

Comments
 (0)