c/os: fork/exec
This commit is contained in:
20
_demo/cexec/exec.go
Normal file
20
_demo/cexec/exec.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/c/os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ls := c.Str("ls")
|
||||
args := []*c.Char{ls, c.Str("-l"), nil}
|
||||
if runtime.GOOS == "windows" {
|
||||
ls = c.Str("dir")
|
||||
args = []*c.Char{ls, nil}
|
||||
}
|
||||
|
||||
os.Execvp(ls, unsafe.SliceData(args))
|
||||
}
|
||||
26
_demo/sysexec/exec.go
Normal file
26
_demo/sysexec/exec.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"runtime"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ls := "ls"
|
||||
args := []string{"ls", "-l"}
|
||||
if runtime.GOOS == "windows" {
|
||||
ls = "dir"
|
||||
args = nil
|
||||
}
|
||||
lspath, _ := exec.LookPath(ls)
|
||||
if lspath != "" {
|
||||
ls = lspath
|
||||
}
|
||||
err := syscall.Exec(ls, args, nil)
|
||||
if err != nil {
|
||||
fmt.Println("syscall.Exec error:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user