Merge pull request '1.1.0 Fix' (#3) from develop into main
All checks were successful
/ publish (push) Successful in 3m23s
All checks were successful
/ publish (push) Successful in 3m23s
Reviewed-on: Infendro/shell-kt#3
This commit was merged in pull request #3.
This commit is contained in:
@@ -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
|
||||
}
|
||||
20
src/macosMain/kotlin/com/infendro/shell/Shell.macos.kt
Normal file
20
src/macosMain/kotlin/com/infendro/shell/Shell.macos.kt
Normal 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)
|
||||
}
|
||||
20
src/mingwMain/kotlin/shell/Shell.mingw.kt
Normal file
20
src/mingwMain/kotlin/shell/Shell.mingw.kt
Normal 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)
|
||||
}
|
||||
17
src/nativeMain/kotlin/com.infendro.shell/C.kt
Normal file
17
src/nativeMain/kotlin/com.infendro.shell/C.kt
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user