From 7cc857233f4dfd1d9cc7ff63f72c6a4ebb8b6378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E8=8B=B1=E6=9D=B0?= Date: Fri, 12 Jul 2024 16:58:52 +0800 Subject: [PATCH 1/3] llgo/c/fcntl:demo --- _demo/fcntl/fcntl.go | 77 ++++++++++++++++++++++++++++++++++++++++ _demo/fcntl/testfile.txt | 1 + c/fcntl/fcntl.go | 42 ++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 _demo/fcntl/fcntl.go create mode 100644 _demo/fcntl/testfile.txt create mode 100644 c/fcntl/fcntl.go diff --git a/_demo/fcntl/fcntl.go b/_demo/fcntl/fcntl.go new file mode 100644 index 00000000..11b7918c --- /dev/null +++ b/_demo/fcntl/fcntl.go @@ -0,0 +1,77 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/fcntl" + "github.com/goplus/llgo/c/os" + "unsafe" +) + +func main() { + + filename := c.Str("testfile.txt") + data := c.Str("Hello, fcntl!") + + 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) + if fd == -1 { + c.Printf(c.Str("open error\n")) + return + } + + // Writing data to a file + bytesWritten := os.Write(fd, c.Pointer(data), c.Strlen(data)) + if bytesWritten == -1 { + c.Printf(c.Str("write error\n")) + os.Close(fd) + return + } + c.Printf(c.Str("Written %ld bytes to %s\n"), bytesWritten, filename) + + // Get file status flags + flags := fcntl.FcNtl(fd, fcntl.F_GETFL) + if flags == -1 { + c.Printf(c.Str("fcntl 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")) + 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) + if fd == -1 { + c.Printf(c.Str("open error\n")) + return + } + + // Reading data from a file + // &buffer[:][0] + // unsafe.SliceData(buffer[:]) + bytesRead := os.Read(fd, c.Pointer(unsafe.SliceData(buffer[:])), unsafe.Sizeof(buffer)-1) + if bytesRead == -1 { + c.Printf(c.Str("read error\n")) + os.Close(fd) + return + } + + // Ensure that the buffer is null-terminated + buffer[bytesRead] = c.Char(0) + c.Printf(c.Str("Read %ld bytes: %s\n"), bytesRead, buffer) + + // Close file + os.Close(fd) +} diff --git a/_demo/fcntl/testfile.txt b/_demo/fcntl/testfile.txt new file mode 100644 index 00000000..48858db3 --- /dev/null +++ b/_demo/fcntl/testfile.txt @@ -0,0 +1 @@ +Hello, fcntl! \ No newline at end of file diff --git a/c/fcntl/fcntl.go b/c/fcntl/fcntl.go new file mode 100644 index 00000000..2c31b8e1 --- /dev/null +++ b/c/fcntl/fcntl.go @@ -0,0 +1,42 @@ +package fcntl + +import ( + _ "unsafe" + + "github.com/goplus/llgo/c" +) + +// #include + +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 From c22427b8fda2cc634a7ccd3602db5b7193970262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E8=8B=B1=E6=9D=B0?= Date: Mon, 15 Jul 2024 10:19:55 +0800 Subject: [PATCH 2/3] llgo/c/fcntl --- _demo/fcntl/fcntl.go | 18 ++++++++--------- _demo/fcntl/testfile.txt | 1 - c/fcntl/fcntl.go | 42 ---------------------------------------- c/os/os.go | 28 ++++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 53 deletions(-) delete mode 100644 _demo/fcntl/testfile.txt delete mode 100644 c/fcntl/fcntl.go diff --git a/_demo/fcntl/fcntl.go b/_demo/fcntl/fcntl.go index 11b7918c..b53686e5 100644 --- a/_demo/fcntl/fcntl.go +++ b/_demo/fcntl/fcntl.go @@ -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 diff --git a/_demo/fcntl/testfile.txt b/_demo/fcntl/testfile.txt deleted file mode 100644 index 48858db3..00000000 --- a/_demo/fcntl/testfile.txt +++ /dev/null @@ -1 +0,0 @@ -Hello, fcntl! \ No newline at end of file diff --git a/c/fcntl/fcntl.go b/c/fcntl/fcntl.go deleted file mode 100644 index 2c31b8e1..00000000 --- a/c/fcntl/fcntl.go +++ /dev/null @@ -1,42 +0,0 @@ -package fcntl - -import ( - _ "unsafe" - - "github.com/goplus/llgo/c" -) - -// #include - -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 diff --git a/c/os/os.go b/c/os/os.go index 04ce435b..583cf631 100644 --- a/c/os/os.go +++ b/c/os/os.go @@ -35,6 +35,29 @@ const ( 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 ( ModeT C.mode_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 -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 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 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 func Dup(fd c.Int) c.Int From 315c9285de00631644f423c5fa79a8beee5b9d47 Mon Sep 17 00:00:00 2001 From: spongehah <114657596+spongehah@users.noreply.github.com> Date: Mon, 15 Jul 2024 11:10:40 +0800 Subject: [PATCH 3/3] fix: fcntl output error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 张之阳 <51194195+luoliwoshang@users.noreply.github.com> --- _demo/fcntl/fcntl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_demo/fcntl/fcntl.go b/_demo/fcntl/fcntl.go index b53686e5..8e312cea 100644 --- a/_demo/fcntl/fcntl.go +++ b/_demo/fcntl/fcntl.go @@ -70,7 +70,7 @@ func main() { // Ensure that the buffer is null-terminated buffer[bytesRead] = c.Char(0) - c.Printf(c.Str("Read %ld bytes: %s\n"), bytesRead, buffer) + c.Printf(c.Str("Read %ld bytes: %s\n"), bytesRead, &buffer[0]) // Close file os.Close(fd)