patch: fmt, os, runtime, syscall, time

This commit is contained in:
xushiwei
2024-06-26 17:17:10 +08:00
parent fd0cb4c458
commit 48a1384197
30 changed files with 5373 additions and 24 deletions

View File

@@ -24,17 +24,13 @@ import (
"github.com/goplus/llgo/c/os"
)
func errnoErr(errno c.Int) error {
panic("todo")
}
func Getcwd(buf []byte) (n int, err error) {
ptr := unsafe.Pointer(unsafe.SliceData(buf))
ret := os.Getcwd(ptr, uintptr(len(buf)))
if ret != nil {
return int(c.Strlen(ret)), nil
}
return 0, errnoErr(os.Errno)
return 0, Errno(os.Errno)
}
func Getwd() (string, error) {
@@ -42,5 +38,57 @@ func Getwd() (string, error) {
if wd != nil {
return c.GoString(wd), nil
}
return "", errnoErr(os.Errno)
return "", Errno(os.Errno)
}
func Getenv(key string) (value string, found bool) {
ret := os.Getenv(c.AllocaCStr(key))
if ret != nil {
return c.GoString(ret), true
}
return "", false
}
func Getpid() (pid int) {
return int(os.Getpid())
}
func Kill(pid int, signum Signal) (err error) {
ret := os.Kill(c.Int(pid), c.Int(signum))
if ret == 0 {
return nil
}
return Errno(os.Errno)
}
func Open(path string, mode int, perm uint32) (fd int, err error) {
ret := os.Open(c.AllocaCStr(path), c.Int(mode), os.ModeT(perm))
if ret >= 0 {
return int(ret), nil
}
return 0, Errno(os.Errno)
}
func Seek(fd int, offset int64, whence int) (newoffset int64, err error) {
ret := os.Lseek(c.Int(fd), os.OffT(offset), c.Int(whence))
if ret >= 0 {
return int64(ret), nil
}
return -1, Errno(os.Errno)
}
func Read(fd int, p []byte) (n int, err error) {
ret := os.Read(c.Int(fd), unsafe.Pointer(unsafe.SliceData(p)), uintptr(len(p)))
if ret >= 0 {
return ret, nil // TODO(xsw): confirm err == nil (not io.EOF) when ret == 0
}
return 0, Errno(os.Errno)
}
func Close(fd int) (err error) {
ret := os.Close(c.Int(fd))
if ret == 0 {
return nil
}
return Errno(os.Errno)
}

View File

@@ -57,3 +57,22 @@ func (e Errno) Temporary() bool {
func (e Errno) Timeout() bool {
return e == EAGAIN || e == EWOULDBLOCK || e == ETIMEDOUT
}
// A Signal is a number describing a process signal.
// It implements the os.Signal interface.
type Signal int
func (s Signal) Signal() {}
func (s Signal) String() string {
/*
if 0 <= s && int(s) < len(signals) {
str := signals[s]
if str != "" {
return str
}
}
return "signal " + itoa.Itoa(int(s))
*/
panic("todo: syscall.Signal.String")
}

View File

@@ -127,3 +127,39 @@ const (
EWOULDBLOCK = Errno(0x23)
EXDEV = Errno(0x12)
)
// Signals
const (
SIGABRT = Signal(0x6)
SIGALRM = Signal(0xe)
SIGBUS = Signal(0xa)
SIGCHLD = Signal(0x14)
SIGCONT = Signal(0x13)
SIGEMT = Signal(0x7)
SIGFPE = Signal(0x8)
SIGHUP = Signal(0x1)
SIGILL = Signal(0x4)
SIGINFO = Signal(0x1d)
SIGINT = Signal(0x2)
SIGIO = Signal(0x17)
SIGIOT = Signal(0x6)
SIGKILL = Signal(0x9)
SIGPIPE = Signal(0xd)
SIGPROF = Signal(0x1b)
SIGQUIT = Signal(0x3)
SIGSEGV = Signal(0xb)
SIGSTOP = Signal(0x11)
SIGSYS = Signal(0xc)
SIGTERM = Signal(0xf)
SIGTRAP = Signal(0x5)
SIGTSTP = Signal(0x12)
SIGTTIN = Signal(0x15)
SIGTTOU = Signal(0x16)
SIGURG = Signal(0x10)
SIGUSR1 = Signal(0x1e)
SIGUSR2 = Signal(0x1f)
SIGVTALRM = Signal(0x1a)
SIGWINCH = Signal(0x1c)
SIGXCPU = Signal(0x18)
SIGXFSZ = Signal(0x19)
)

View File

@@ -152,3 +152,42 @@ const (
EXDEV = Errno(0x12)
EXFULL = Errno(0x36)
)
// Signals
const (
SIGABRT = Signal(0x6)
SIGALRM = Signal(0xe)
SIGBUS = Signal(0x7)
SIGCHLD = Signal(0x11)
SIGCLD = Signal(0x11)
SIGCONT = Signal(0x12)
SIGFPE = Signal(0x8)
SIGHUP = Signal(0x1)
SIGILL = Signal(0x4)
SIGINT = Signal(0x2)
SIGIO = Signal(0x1d)
SIGIOT = Signal(0x6)
SIGKILL = Signal(0x9)
SIGPIPE = Signal(0xd)
SIGPOLL = Signal(0x1d)
SIGPROF = Signal(0x1b)
SIGPWR = Signal(0x1e)
SIGQUIT = Signal(0x3)
SIGSEGV = Signal(0xb)
SIGSTKFLT = Signal(0x10)
SIGSTOP = Signal(0x13)
SIGSYS = Signal(0x1f)
SIGTERM = Signal(0xf)
SIGTRAP = Signal(0x5)
SIGTSTP = Signal(0x14)
SIGTTIN = Signal(0x15)
SIGTTOU = Signal(0x16)
SIGUNUSED = Signal(0x1f)
SIGURG = Signal(0x17)
SIGUSR1 = Signal(0xa)
SIGUSR2 = Signal(0xc)
SIGVTALRM = Signal(0x1a)
SIGWINCH = Signal(0x1c)
SIGXCPU = Signal(0x18)
SIGXFSZ = Signal(0x19)
)