llgo/c/fcntl

This commit is contained in:
赵英杰
2024-07-15 10:19:55 +08:00
parent 7cc857233f
commit c22427b8fd
4 changed files with 36 additions and 53 deletions

View File

@@ -2,7 +2,6 @@ package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/c/fcntl"
"github.com/goplus/llgo/c/os"
"unsafe"
)
@@ -10,12 +9,11 @@ import (
func main() {
filename := c.Str("testfile.txt")
data := c.Str("Hello, fcntl!")
data := c.Str("Hello, os!")
var buffer [20]c.Char
// Open a file, O_CREAT|O_WRONLY|O_TRUNC means create, write only, or clear the file
fd := fcntl.Open(filename, fcntl.O_CREAT|fcntl.O_WRONLY|fcntl.O_TRUNC, 0644)
fd := os.Open(filename, os.O_CREAT|os.O_WRONLY|os.O_TRUNC, 0644)
if fd == -1 {
c.Printf(c.Str("open error\n"))
return
@@ -31,28 +29,30 @@ func main() {
c.Printf(c.Str("Written %ld bytes to %s\n"), bytesWritten, filename)
// Get file status flags
flags := fcntl.FcNtl(fd, fcntl.F_GETFL)
flags := os.Fcntl(fd, os.F_GETFL)
if flags == -1 {
c.Printf(c.Str("fcntl error\n"))
c.Printf(c.Str("os error\n"))
os.Close(fd)
return
}
c.Printf(c.Str("File flags: %d\n"), flags)
// Set the file status flag to non-blocking mode
if fcntl.FcNtl(fd, fcntl.F_SETFL, flags|fcntl.O_NONBLOCK) == -1 {
c.Printf(c.Str("fcntl error\n"))
if os.Fcntl(fd, os.F_SETFL, flags|os.O_NONBLOCK) == -1 {
c.Printf(c.Str("os error\n"))
os.Close(fd)
return
}
c.Printf(c.Str("set file status successfully\n"))
c.Printf(c.Str("111"))
// Close file
os.Close(fd)
// Reopen the file, O_RDONLY means read-only
fd = fcntl.Open(filename, fcntl.O_RDONLY)
fd = os.Open(filename, os.O_RDONLY)
if fd == -1 {
c.Printf(c.Str("open error\n"))
return

View File

@@ -1 +0,0 @@
Hello, fcntl!