Created
January 4, 2021 11:32
-
-
Save mherod/964205b1a4d0b8956da5b930fd33fb98 to your computer and use it in GitHub Desktop.
Execute program and read output using Kotlin/Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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