Skip to content

Commit

Permalink
examples/varargs/libc: fflush added
Browse files Browse the repository at this point in the history
  • Loading branch information
JupiterRider committed Dec 27, 2024
1 parent 06fb279 commit 17bf013
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion examples/varargs/libc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@ func main() {
panic(err)
}

text := &[]byte("Pi is %f\n\x00")[0] // C requires a null-terminated string
fflush, err := libc.Prep("fflush", &ffi.TypeSint32, &ffi.TypePointer)
if err != nil {
panic(err)
}

// C requires a null-terminated string
text := &[]byte("Pi is %f\n\x00")[0]
pi := math.Pi
var nCharsPrinted ffi.Arg
printf.Call(&nCharsPrinted, &text, &pi)

// we call fflush with NULL as argument to flush all open streams,
// because printf is buffered
var ok ffi.Arg
var stream uintptr
fflush.Call(&ok, &stream)

fmt.Printf("%d characters printed\n", int32(nCharsPrinted))
}

0 comments on commit 17bf013

Please sign in to comment.