1.1.0 Fix #3

Merged
Infendro merged 1 commits from develop into main 2026-02-16 16:54:30 +00:00
4 changed files with 60 additions and 14 deletions

View File

@@ -1,9 +1,8 @@
package com.infendro.shell package com.infendro.shell
import kotlinx.cinterop.CPointer import platform.posix.chdir
import kotlinx.cinterop.refTo import platform.posix.pclose
import kotlinx.cinterop.toKString import platform.posix.popen
import platform.posix.*
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
@@ -19,13 +18,3 @@ actual suspend fun execute(vararg command: String, directory: String?): Result =
val result = Result.from(code, value) val result = Result.from(code, value)
continuation.resume(result) continuation.resume(result)
} }
private fun CPointer<FILE>.readLines(): List<String> {
val lines = mutableListOf<String>()
val buffer = ByteArray(4096)
while (true) {
val line = fgets(buffer.refTo(0), buffer.size, this) ?: break
lines.add(line.toKString())
}
return lines
}

View File

@@ -0,0 +1,20 @@
package com.infendro.shell
import platform.posix.chdir
import platform.posix.pclose
import platform.posix.popen
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
actual suspend fun execute(vararg command: String, directory: String?): Result = suspendCoroutine { continuation ->
val command = command.joinToString(" ") { """"$it"""" }
if (directory != null) {
chdir(directory)
}
val stream = popen(command, "r")!!
val value = stream.readLines().joinToString("")
val code = pclose(stream)
val result = Result.from(code, value)
continuation.resume(result)
}

View File

@@ -0,0 +1,20 @@
package com.infendro.shell
import platform.posix._pclose
import platform.posix._popen
import platform.posix.chdir
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
actual suspend fun execute(vararg command: String, directory: String?): Result = suspendCoroutine { continuation ->
val command = command.joinToString(" ") { """"$it"""" }
if (directory != null) {
chdir(directory)
}
val stream = _popen(command, "r")!!
val value = stream.readLines().joinToString("")
val code = _pclose(stream)
val result = Result.from(code, value)
continuation.resume(result)
}

View File

@@ -0,0 +1,17 @@
package com.infendro.shell
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
import platform.posix.FILE
import platform.posix.fgets
internal fun CPointer<FILE>.readLines(): List<String> {
val lines = mutableListOf<String>()
val buffer = ByteArray(4096)
while (true) {
val line = fgets(buffer.refTo(0), buffer.size, this) ?: break
lines.add(line.toKString())
}
return lines
}