Skip to content

Instantly share code, notes, and snippets.

@mherod
Created January 4, 2021 11:32
Show Gist options
  • Save mherod/964205b1a4d0b8956da5b930fd33fb98 to your computer and use it in GitHub Desktop.
Save mherod/964205b1a4d0b8956da5b930fd33fb98 to your computer and use it in GitHub Desktop.
Execute program and read output using Kotlin/Native
fun exec(command: String): String {
val returnBuffer = StringBuilder()
val file = popen(command, "r")
try {
memScoped {
val readBufferLength = 128
val buffer = allocArray<ByteVar>(readBufferLength)
var line = fgets(buffer, readBufferLength, file)?.toKString()
while (line != null) {
returnBuffer.append(line)
line = fgets(buffer, readBufferLength, file)?.toKString()
}
}
} finally {
pclose(file)
}
return returnBuffer.toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment