Finally one that reads from the called process yea!
Only tested on Windows, I will do unix (aka mac :) ) tomorrow.
import kotlin.native.OsFamily.*
import platform.posix.*
import kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
fun main(arguments: Array<String>) {
println("running")
if (arguments.size >= 1) {
arguments.forEach { a -> println(a) }
}
val platform = Platform
val os = platform.osFamily
println("os is " + os)
when (os) {
WINDOWS -> runWindows()
else -> runUnix()
}
}
fun runWindows() {
val result = execl("dir", "","")
//hmm emulate https://gist.github.com/a-cordier/33211eda92b8084a9e7e
//https://www.youtube.com/watch?v=6xbLgZpOBi8
val fp = _popen("dir", "r") ?: error("Failed to run command: dir")
val buffer = ByteArray(4096)
var counter = 0
println("hmm")
while (true) {
val input = fgets(buffer.refTo(0), buffer.size, fp) ?: break
print(input.toKString())
//println(counter++)
}
println("Ran on windows $result");
}
fun runUnix() {
execlp("date", "","")
println("Ran on UNIX")
}