Compare commits

..

3 Commits

Author SHA1 Message Date
db7a05f88b Update README.md 2026-04-16 16:12:18 +00:00
13b8597810 Merge pull request '1.1.0 Fix' (#3) from develop into main
All checks were successful
/ publish (push) Successful in 3m23s
Reviewed-on: Infendro/shell-kt#3
2026-02-16 16:54:29 +00:00
a80bb11652 Fix native implementation 2026-02-16 17:53:50 +01:00
5 changed files with 62 additions and 16 deletions

View File

@@ -1,6 +1,6 @@
# shell-kt
# shell.kt
`shell-kt` is a Kotlin Multiplatform library that enables the execution of shell commands.
`shell.kt` is a Kotlin Multiplatform library that enables the execution of shell commands.
## Installation

View File

@@ -1,9 +1,8 @@
package com.infendro.shell
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.refTo
import kotlinx.cinterop.toKString
import platform.posix.*
import platform.posix.chdir
import platform.posix.pclose
import platform.posix.popen
import kotlin.coroutines.resume
import kotlin.coroutines.suspendCoroutine
@@ -19,13 +18,3 @@ actual suspend fun execute(vararg command: String, directory: String?): Result =
val result = Result.from(code, value)
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
}