llgo/c/fcntl
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/goplus/llgo/c"
|
"github.com/goplus/llgo/c"
|
||||||
"github.com/goplus/llgo/c/fcntl"
|
|
||||||
"github.com/goplus/llgo/c/os"
|
"github.com/goplus/llgo/c/os"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
@@ -10,12 +9,11 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
filename := c.Str("testfile.txt")
|
filename := c.Str("testfile.txt")
|
||||||
data := c.Str("Hello, fcntl!")
|
data := c.Str("Hello, os!")
|
||||||
|
|
||||||
var buffer [20]c.Char
|
var buffer [20]c.Char
|
||||||
|
|
||||||
// Open a file, O_CREAT|O_WRONLY|O_TRUNC means create, write only, or clear the file
|
// 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 {
|
if fd == -1 {
|
||||||
c.Printf(c.Str("open error\n"))
|
c.Printf(c.Str("open error\n"))
|
||||||
return
|
return
|
||||||
@@ -31,28 +29,30 @@ func main() {
|
|||||||
c.Printf(c.Str("Written %ld bytes to %s\n"), bytesWritten, filename)
|
c.Printf(c.Str("Written %ld bytes to %s\n"), bytesWritten, filename)
|
||||||
|
|
||||||
// Get file status flags
|
// Get file status flags
|
||||||
flags := fcntl.FcNtl(fd, fcntl.F_GETFL)
|
flags := os.Fcntl(fd, os.F_GETFL)
|
||||||
if flags == -1 {
|
if flags == -1 {
|
||||||
c.Printf(c.Str("fcntl error\n"))
|
c.Printf(c.Str("os error\n"))
|
||||||
os.Close(fd)
|
os.Close(fd)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Printf(c.Str("File flags: %d\n"), flags)
|
c.Printf(c.Str("File flags: %d\n"), flags)
|
||||||
|
|
||||||
// Set the file status flag to non-blocking mode
|
// Set the file status flag to non-blocking mode
|
||||||
if fcntl.FcNtl(fd, fcntl.F_SETFL, flags|fcntl.O_NONBLOCK) == -1 {
|
if os.Fcntl(fd, os.F_SETFL, flags|os.O_NONBLOCK) == -1 {
|
||||||
c.Printf(c.Str("fcntl error\n"))
|
c.Printf(c.Str("os error\n"))
|
||||||
os.Close(fd)
|
os.Close(fd)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
c.Printf(c.Str("set file status successfully\n"))
|
c.Printf(c.Str("set file status successfully\n"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
c.Printf(c.Str("111"))
|
c.Printf(c.Str("111"))
|
||||||
// Close file
|
// Close file
|
||||||
os.Close(fd)
|
os.Close(fd)
|
||||||
|
|
||||||
// Reopen the file, O_RDONLY means read-only
|
// 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 {
|
if fd == -1 {
|
||||||
c.Printf(c.Str("open error\n"))
|
c.Printf(c.Str("open error\n"))
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
Hello, fcntl!
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
package fcntl
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "unsafe"
|
|
||||||
|
|
||||||
"github.com/goplus/llgo/c"
|
|
||||||
)
|
|
||||||
|
|
||||||
// #include <fdntl.h>
|
|
||||||
|
|
||||||
const (
|
|
||||||
LLGoPackage = "decl"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
/* get file status flags */
|
|
||||||
F_GETFL = 3
|
|
||||||
/* set file status flags */
|
|
||||||
F_SETFL = 4
|
|
||||||
|
|
||||||
/* open for reading only */
|
|
||||||
O_RDONLY = 0x0000
|
|
||||||
/* open for writing only */
|
|
||||||
O_WRONLY = 0x0001
|
|
||||||
/* open for reading and writing */
|
|
||||||
O_RDWR = 0x0002
|
|
||||||
/* mask for above modes */
|
|
||||||
O_ACCMODE = 0x0003
|
|
||||||
|
|
||||||
/* no delay */
|
|
||||||
O_NONBLOCK = 0x00000004
|
|
||||||
/* create if nonexistant */
|
|
||||||
O_CREAT = 0x00000200
|
|
||||||
/* truncate to zero length */
|
|
||||||
O_TRUNC = 0x00000400
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:linkname FcNtl C.fcntl
|
|
||||||
func FcNtl(a c.Int, b c.Int, vars ...any) c.Int
|
|
||||||
|
|
||||||
//go:linkname Open C.open
|
|
||||||
func Open(path *c.Char, op c.Int, vars ...any) c.Int
|
|
||||||
28
c/os/os.go
28
c/os/os.go
@@ -35,6 +35,29 @@ const (
|
|||||||
PATH_MAX = C.PATH_MAX
|
PATH_MAX = C.PATH_MAX
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
/* get file status flags */
|
||||||
|
F_GETFL = 3
|
||||||
|
/* set file status flags */
|
||||||
|
F_SETFL = 4
|
||||||
|
|
||||||
|
/* open for reading only */
|
||||||
|
O_RDONLY = 0x0000
|
||||||
|
/* open for writing only */
|
||||||
|
O_WRONLY = 0x0001
|
||||||
|
/* open for reading and writing */
|
||||||
|
O_RDWR = 0x0002
|
||||||
|
/* mask for above modes */
|
||||||
|
O_ACCMODE = 0x0003
|
||||||
|
|
||||||
|
/* no delay */
|
||||||
|
O_NONBLOCK = 0x00000004
|
||||||
|
/* create if nonexistant */
|
||||||
|
O_CREAT = 0x00000200
|
||||||
|
/* truncate to zero length */
|
||||||
|
O_TRUNC = 0x00000400
|
||||||
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
ModeT C.mode_t
|
ModeT C.mode_t
|
||||||
UidT C.uid_t
|
UidT C.uid_t
|
||||||
@@ -146,7 +169,7 @@ func Fstatat(dirfd c.Int, path *c.Char, buf *StatT, flags c.Int) c.Int
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|
||||||
//go:linkname Open C.open
|
//go:linkname Open C.open
|
||||||
func Open(path *c.Char, flags c.Int, mode ModeT) c.Int
|
func Open(path *c.Char, flags c.Int, __llgo_va_list ...any) c.Int
|
||||||
|
|
||||||
//go:linkname Openat C.openat
|
//go:linkname Openat C.openat
|
||||||
func Openat(dirfd c.Int, path *c.Char, flags c.Int, mode ModeT) c.Int
|
func Openat(dirfd c.Int, path *c.Char, flags c.Int, mode ModeT) c.Int
|
||||||
@@ -154,6 +177,9 @@ func Openat(dirfd c.Int, path *c.Char, flags c.Int, mode ModeT) c.Int
|
|||||||
//go:linkname Creat C.creat
|
//go:linkname Creat C.creat
|
||||||
func Creat(path *c.Char, mode ModeT) c.Int
|
func Creat(path *c.Char, mode ModeT) c.Int
|
||||||
|
|
||||||
|
//go:linkname Fcntl C.fcntl
|
||||||
|
func Fcntl(a c.Int, b c.Int, __llgo_va_list ...any) c.Int
|
||||||
|
|
||||||
//go:linkname Dup C.dup
|
//go:linkname Dup C.dup
|
||||||
func Dup(fd c.Int) c.Int
|
func Dup(fd c.Int) c.Int
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user