From 35fcdb30a6e91f4a0126fed884530b0442ce6f83 Mon Sep 17 00:00:00 2001 From: Cristian Chiru Date: Thu, 12 Oct 2023 11:33:19 +0300 Subject: [PATCH 1/2] read from stdin --- c/seg.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/c/seg.c b/c/seg.c index 89ad8cd..6dc3dca 100644 --- a/c/seg.c +++ b/c/seg.c @@ -6,6 +6,10 @@ int main(int argc, char* argv[]) setvbuf(stdout, NULL, _IOLBF, 0); setvbuf(stderr, NULL, _IOLBF, 0); + char buffer[100]; + fgets(buffer, 100, stdin); + printf("From stdin: %s\n", buffer); + printf("C test\n"); sleep(1); printf("1\n"); From 97b9c89e933f2b531c4722c5ed33707fdda85924 Mon Sep 17 00:00:00 2001 From: Cristian Chiru Date: Thu, 12 Oct 2023 11:33:30 +0300 Subject: [PATCH 2/2] use io.Pipe --- gocmd/gocmd.go | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/gocmd/gocmd.go b/gocmd/gocmd.go index 462e2ec..f00c188 100644 --- a/gocmd/gocmd.go +++ b/gocmd/gocmd.go @@ -1,30 +1,23 @@ package main import ( + "bytes" "fmt" + "io" "time" "github.com/go-cmd/cmd" ) -//Dummy reader which does nothing -type TestReader struct { -} - -//Read get's called by go-cmd -func (rt *TestReader) Read(p []byte) (n int, err error) { - return 0, nil -} - -func NewTestReader() *TestReader { - rt := TestReader{} - return &rt -} - func main() { testCmd := cmd.NewCmd("../c/seg") - rt := NewTestReader() - statusChan := testCmd.StartWithStdin(rt) + reader, writer := io.Pipe() + go func() { + _, _ = io.Copy(writer, bytes.NewBufferString("hello from the other side\n")) + // close immediately + _ = writer.Close() + }() + statusChan := testCmd.StartWithStdin(reader) ticker := time.NewTicker(1 * time.Second)