diff options
Diffstat (limited to 'app/src/main/java/org/pacien/tincapp/commands/Executor.kt')
-rw-r--r-- | app/src/main/java/org/pacien/tincapp/commands/Executor.kt | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/app/src/main/java/org/pacien/tincapp/commands/Executor.kt b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt new file mode 100644 index 0000000..160f0cd --- /dev/null +++ b/app/src/main/java/org/pacien/tincapp/commands/Executor.kt | |||
@@ -0,0 +1,43 @@ | |||
1 | package org.pacien.tincapp.commands | ||
2 | |||
3 | import java.io.BufferedReader | ||
4 | import java.io.IOException | ||
5 | import java.io.InputStreamReader | ||
6 | import java.util.* | ||
7 | |||
8 | /** | ||
9 | * @author pacien | ||
10 | */ | ||
11 | internal object Executor { | ||
12 | |||
13 | init { | ||
14 | System.loadLibrary("exec") | ||
15 | } | ||
16 | |||
17 | /** | ||
18 | * @return -1 on error, forked child PID otherwise | ||
19 | */ | ||
20 | private external fun forkExec(argcv: Array<String>): Int | ||
21 | |||
22 | @Throws(IOException::class) | ||
23 | fun forkExec(cmd: Command) { | ||
24 | if (forkExec(cmd.asArray()) == -1) | ||
25 | throw IOException() | ||
26 | } | ||
27 | |||
28 | @Throws(IOException::class) | ||
29 | fun call(cmd: Command): List<String> { | ||
30 | val proc = ProcessBuilder(cmd.asList()).start() | ||
31 | val outputReader = BufferedReader(InputStreamReader(proc.inputStream)) | ||
32 | |||
33 | var line: String? = outputReader.readLine() | ||
34 | val list = LinkedList<String>() | ||
35 | while (line != null) { | ||
36 | line = outputReader.readLine() | ||
37 | list.add(line) | ||
38 | } | ||
39 | |||
40 | return list | ||
41 | } | ||
42 | |||
43 | } | ||