Konstantin8105 / c4go

Transpiling C code to Go code
MIT License
363 stars 37 forks source link

stdlib.system #160

Closed Konstantin8105 closed 5 years ago

Konstantin8105 commented 5 years ago

Add test and implementation on Go for function stdlib.h system Description: https://en.cppreference.com/w/c/program/system File for test code add : c4go/tests/stdlib.c

Konstantin8105 commented 5 years ago

example see #155

alebcay commented 5 years ago

I'll take this! PR coming later today.

Konstantin8105 commented 5 years ago

How to run test for that issue:

go test -v -tags=integration -run=TestIntegrationScripts/tests/stdlib.c
go test -v -run=TestCSTD
Konstantin8105 commented 5 years ago

Try simplify code like that: https://blog.kowalczyk.info/article/wOYk/advanced-command-execution-in-go-with-osexec.html

    cmd := exec.Command("ls", "-lah")
    var stdout, stderr bytes.Buffer
    cmd.Stdout = &stdout
    cmd.Stderr = &stderr
    err := cmd.Run()
    if err != nil {
        log.Fatalf("cmd.Run() failed with %s\n", err)
    }
    outStr, errStr := string(stdout.Bytes()), string(stderr.Bytes())
    fmt.Printf("out:\n%s\nerr:\n%s\n", outStr, errStr)
alebcay commented 5 years ago

That site is the example that I worked from, but I found that this only shows the output after the command finishes, which is not how the system function in C works. For example, a loop/other long running command will never show any output if we follow this example.

JalonSolov commented 5 years ago

Saw this example on another site. It simply forces the output from the exec'd command to go directly to your stdout/stderr. No need to capture in a buffer and print it out after, and there is no delay in output.

cmd := exec.Command("ls", "-lah")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
    log.Fatalf("cmd.Run() failed with %s\n", err)
}
Konstantin8105 commented 5 years ago

Dear @JalonSolov , feel free for creating PR.