diff --git a/_cmptest/readfiledemo/readf.go b/_cmptest/readfiledemo/readf.go new file mode 100644 index 00000000..7f6c7bb4 --- /dev/null +++ b/_cmptest/readfiledemo/readf.go @@ -0,0 +1,25 @@ +package main + +import ( + "fmt" + "os" +) + +func main() { + fileName := "err.log" + os.WriteFile(fileName, []byte("123"), 0644) + + _, err := os.Stat(fileName) + if os.IsNotExist(err) { + fmt.Fprintf(os.Stderr, "File %s not found\n", fileName) + return + } + + data, err := os.ReadFile(fileName) + if err != nil { + fmt.Fprintf(os.Stderr, "ReadFile: %v\n", err) + return + } + + fmt.Printf("%s\n", data) +} diff --git a/c/syscall/syscall.go b/c/syscall/syscall.go index 69b266ec..e9a08e06 100644 --- a/c/syscall/syscall.go +++ b/c/syscall/syscall.go @@ -17,7 +17,7 @@ package syscall const ( - LLGoPackage = "decl" + LLGoPackage = "noinit" ) type Errno = uintptr @@ -25,3 +25,8 @@ type Errno = uintptr // A Signal is a number describing a process signal. // It implements the os.Signal interface. type Signal = int + +// Unix returns the time stored in ts as seconds plus nanoseconds. +func (ts *Timespec) Unix() (sec int64, nsec int64) { + return int64(ts.Sec), int64(ts.Nsec) +} diff --git a/internal/lib/os/stat_darwin.go b/internal/lib/os/stat_darwin.go index 0725f6bb..2f5dc575 100644 --- a/internal/lib/os/stat_darwin.go +++ b/internal/lib/os/stat_darwin.go @@ -5,8 +5,9 @@ package os import ( - "syscall" "time" + + "github.com/goplus/llgo/c/syscall" ) func fillFileStatFromSys(fs *fileStat, name string) { diff --git a/internal/lib/os/stat_unix.go b/internal/lib/os/stat_unix.go index 04ae7304..c0840d31 100644 --- a/internal/lib/os/stat_unix.go +++ b/internal/lib/os/stat_unix.go @@ -6,12 +6,15 @@ package os -import "syscall" +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/os" + "github.com/goplus/llgo/internal/lib/syscall" +) // Stat returns the FileInfo structure describing file. // If there is an error, it will be of type *PathError. func (f *File) Stat() (FileInfo, error) { - /* TODO(xsw): if f == nil { return nil, ErrInvalid } @@ -22,8 +25,6 @@ func (f *File) Stat() (FileInfo, error) { } fillFileStatFromSys(&fs, f.name) return &fs, nil - */ - panic("todo: os.File.Stat") } // statNolog stats a file with no test logging. diff --git a/internal/lib/os/types_unix.go b/internal/lib/os/types_unix.go index 1b90a5a1..602eee4c 100644 --- a/internal/lib/os/types_unix.go +++ b/internal/lib/os/types_unix.go @@ -7,8 +7,9 @@ package os import ( - "syscall" "time" + + "github.com/goplus/llgo/internal/lib/syscall" ) // A fileStat is the implementation of FileInfo returned by Stat and Lstat.