Skip to content

Commit

Permalink
fix(hostname): use own impl in favour of broken gethostname4j
Browse files Browse the repository at this point in the history
  • Loading branch information
bluelhf committed Apr 19, 2023
1 parent 5f690c3 commit aaa2496
Showing 1 changed file with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.project.guessProjectDir
import com.intellij.openapi.roots.ModuleRootManager
import com.intellij.psi.util.PsiUtilBase
import com.kstruct.gethostname4j.Hostname
import com.sun.jna.Library
import com.sun.jna.Native
import com.sun.jna.Platform
import com.sun.jna.platform.win32.Kernel32Util
import kotlinx.coroutines.runBlocking
import org.jetbrains.concurrency.await

Expand Down Expand Up @@ -56,9 +59,21 @@ object ContextInformation {
}
}

fun getHostname(): String? = try {
Hostname.getHostname()
} catch (e: RuntimeException) {
null
interface UnixCLibrary : Library {
companion object {
val INSTANCE: UnixCLibrary = Native.load("c", UnixCLibrary::class.java)
}

fun gethostname(name: ByteArray, len: Int): Int
}

fun getHostname(): String? {
return if (Platform.isWindows()) {
Kernel32Util.getComputerName();
} else {
val buffer = ByteArray(4097)
val result = UnixCLibrary.INSTANCE.gethostname(buffer, buffer.size)
if (result == 0) String(buffer).trim { it <= ' ' } else null
}
}
}

0 comments on commit aaa2496

Please sign in to comment.