Commit 80d2eac1 authored by Rémy Oudompheng's avatar Rémy Oudompheng Committed by Russ Cox

cmd/cgo: don't reimplement os/exec in util.go.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7450049
parent e02168f6
...@@ -5,9 +5,9 @@ ...@@ -5,9 +5,9 @@
package main package main
import ( import (
"bytes"
"fmt" "fmt"
"go/token" "go/token"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
) )
...@@ -16,50 +16,17 @@ import ( ...@@ -16,50 +16,17 @@ import (
// It returns the output to standard output and standard error. // It returns the output to standard output and standard error.
// ok indicates whether the command exited successfully. // ok indicates whether the command exited successfully.
func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) { func run(stdin []byte, argv []string) (stdout, stderr []byte, ok bool) {
cmd, err := exec.LookPath(argv[0]) p := exec.Command(argv[0], argv[1:]...)
if err != nil { p.Stdin = bytes.NewReader(stdin)
fatalf("exec %s: %s", argv[0], err) var bout, berr bytes.Buffer
} p.Stdout = &bout
r0, w0, err := os.Pipe() p.Stderr = &berr
if err != nil { err := p.Run()
fatalf("%s", err) if _, ok := err.(*exec.ExitError); err != nil && !ok {
}
r1, w1, err := os.Pipe()
if err != nil {
fatalf("%s", err)
}
r2, w2, err := os.Pipe()
if err != nil {
fatalf("%s", err)
}
p, err := os.StartProcess(cmd, argv, &os.ProcAttr{Files: []*os.File{r0, w1, w2}})
if err != nil {
fatalf("%s", err)
}
r0.Close()
w1.Close()
w2.Close()
c := make(chan bool)
go func() {
w0.Write(stdin)
w0.Close()
c <- true
}()
go func() {
stdout, _ = ioutil.ReadAll(r1)
r1.Close()
c <- true
}()
stderr, _ = ioutil.ReadAll(r2)
r2.Close()
<-c
<-c
state, err := p.Wait()
if err != nil {
fatalf("%s", err) fatalf("%s", err)
} }
ok = state.Success() ok = p.ProcessState.Success()
stdout, stderr = bout.Bytes(), berr.Bytes()
return return
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment