diff --git a/src/main/scala/ch04/01nativeFork.scala b/src/main/scala/ch04/01nativeFork.scala index 511dce2..584226f 100644 --- a/src/main/scala/ch04/01nativeFork.scala +++ b/src/main/scala/ch04/01nativeFork.scala @@ -1,8 +1,7 @@ -package ch04.nativeFork +package ch04 +package nativeFork import scalanative.libc.stdlib -import ch04.common -import common.util @main def nativeFork(args: String*): Unit = @@ -12,9 +11,9 @@ def nativeFork(args: String*): Unit = println("about to fork") - val status = common.doAndAwait: () => + val status = doAndAwait: () => println(s"in child, about to exec command: ${args.toSeq}") - common.runCommand(args) + runCommand(args) println(s"wait status ${status}") diff --git a/src/main/scala/ch04/02badExec.scala b/src/main/scala/ch04/02badExec.scala index f211f15..475521b 100644 --- a/src/main/scala/ch04/02badExec.scala +++ b/src/main/scala/ch04/02badExec.scala @@ -1,11 +1,10 @@ -package ch04.badExec - -import ch04.common +package ch04 +package badExec @main def badExec(args: String*): Unit = println("about to exec") - common.runCommand(Seq("/bin/ls", "-l", ".")) + runCommand(Seq("/bin/ls", "-l", ".")) println("exec returned, we're done!") // It is executed inside the .scala-build folder (does not see hidden .bloop folder). diff --git a/src/main/scala/ch04/03nativePipe.scala b/src/main/scala/ch04/03nativePipe.scala index e39ca5d..b0ed036 100644 --- a/src/main/scala/ch04/03nativePipe.scala +++ b/src/main/scala/ch04/03nativePipe.scala @@ -1,10 +1,9 @@ -package ch04.nativePipe +package ch04 +package nativePipe import scalanative.unsafe.stackalloc import scalanative.posix.unistd // getpid import collection.mutable.ArrayBuffer -import ch04.common -import common.util @main def nativePipe(args: String*): Unit = @@ -31,7 +30,7 @@ def pipeMany(input: Int, output: Int, procs: Seq[Seq[String]]): Int = val procsWithFds = procs.lazyZip(inputFds).lazyZip(outputFds) val pids = for (proc, inputFd, outputFd) <- procsWithFds - yield common.doFork: () => + yield doFork: () => // close all pipes that this process won't be using. for p <- 0 until 2 * (procs.size - 1) do if pipeArray(p) != inputFd && pipeArray(p) != outputFd then @@ -47,7 +46,7 @@ def pipeMany(input: Int, output: Int, procs: Seq[Seq[String]]): Int = unistd.close(unistd.STDOUT_FILENO) util.dup2(outputFd, unistd.STDOUT_FILENO) - common.runCommand(proc) + runCommand(proc) for i <- 0 until 2 * (procs.size - 1) do unistd.close(pipeArray(i)) unistd.close(input) diff --git a/src/main/scala/ch04/04nativePipeTwo.scala b/src/main/scala/ch04/04nativePipeTwo.scala index 45cdbbb..57098cb 100644 --- a/src/main/scala/ch04/04nativePipeTwo.scala +++ b/src/main/scala/ch04/04nativePipeTwo.scala @@ -1,10 +1,9 @@ -package ch04.nativePipeTwo +package ch04 +package nativePipeTwo import scalanative.unsafe.{stackalloc, CQuote} import scalanative.posix.unistd // getpid -import scalanative.libc.{stdio} -import ch04.common -import common.util +import scalanative.libc.stdio @main def nativePipeTwo(args: String*): Unit = @@ -22,7 +21,7 @@ def runTwoAndPipe(input: Int, output: Int, proc1: Seq[String], proc2: Seq[String val outputPipe = pipeArray(1) val inputPipe = pipeArray(0) - val proc1Pid = common.doFork: () => + val proc1Pid = doFork: () => if input != 0 then println(s"proc ${unistd.getpid()}: about to dup ${input} to stdin") util.dup2(input, 0) @@ -30,16 +29,16 @@ def runTwoAndPipe(input: Int, output: Int, proc1: Seq[String], proc2: Seq[String println(s"proc 1 about to dup ${outputPipe} to stdout") util.dup2(outputPipe, 1) stdio.printf(c"process %d about to runCommand\n", unistd.getpid()) - common.runCommand(proc1) + runCommand(proc1) - val proc2Pid = common.doFork: () => + val proc2Pid = doFork: () => println(s"proc ${unistd.getpid()}: about to dup") util.dup2(inputPipe, 0) if output != 1 then util.dup2(output, 1) unistd.close(outputPipe) stdio.printf(c"process %d about to runCommand\n", unistd.getpid()) - common.runCommand(proc2) + runCommand(proc2) unistd.close(input) unistd.close(outputPipe) diff --git a/src/main/scala/ch04/common.scala b/src/main/scala/ch04/common.scala index 5867944..d1c75cb 100644 --- a/src/main/scala/ch04/common.scala +++ b/src/main/scala/ch04/common.scala @@ -1,4 +1,4 @@ -package ch04.common +package ch04 import scalanative.unsigned.{USize, UnsignedRichInt} // .toUSize import scalanative.unsafe.{CQuote, toCString, Ptr, CString}