napperley
06/11/2022, 11:55 PMfun main() {
runProgram("/bin/sh")
}
fun runProgram(cmd: String, vararg args: String): Int = memScoped {
val programArgs = if (args.isNotEmpty()) arrayOf(cmd, *args) else arrayOf(cmd)
execvp(cmd, programArgs.toCStringArray(this))
}
After running the Kotlin Native program the following error appears: /bin/sh: 0: Can't open p
If the old memory model is used instead then the correct behaviour is exhibited by the Kotlin Native program, where the shell prompt appears (eg $).Alexey Belkov [JB]
06/15/2022, 10:04 AMnapperley
06/15/2022, 10:35 PMtoCStringArray
function has behaviour (is undefined) that differs between Kotlin Native's old and new memory models. In order to solve this an array has to be manually allocated in a memory scope, each Kotlin String is mapped to the array as a CPointer, and a null
is appended at the end. Below is an example of the solution:
// ...
val programArgs = if (args.isNotEmpty()) {
allocArrayOf(arrayOf(cmd, *args).map { it.cstr.getPointer(this) } + null)
} else {
allocArrayOf(arrayOf(cmd).map { it.cstr.getPointer(this) } + null)
}
execvp(cmd, programArgs)
// ...