Files
TransPyC/includes/posix.py
2026-07-18 19:25:40 +08:00

266 lines
10 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import t, c
from stdint import *
# =============================================================================
# POSIX 类型别名
# 注意ssize_t 已在 stdint.py 中定义为 t.CPtrDiffT此处不再重复
# =============================================================================
DIR: t.CTypedef = t.CVoid | t.CPtr # 不透明目录流句柄
mode_t: t.CTypedef = t.CUInt32T # 文件模式/权限位
pid_t: t.CTypedef = t.CInt32T # 进程 ID
off_t: t.CTypedef = t.CInt64T # 文件偏移量Linux x86_64
dev_t: t.CTypedef = t.CUInt64T # 设备号
ino_t: t.CTypedef = t.CUInt64T # inode 号
nlink_t: t.CTypedef = t.CUInt64T # 硬链接数
uid_t: t.CTypedef = t.CUInt32T # 用户 ID
gid_t: t.CTypedef = t.CUInt32T # 组 ID
blksize_t: t.CTypedef = t.CInt64T # 文件系统 I/O 块大小
blkcnt_t: t.CTypedef = t.CInt64T # 已分配块数
time_t: t.CTypedef = t.CInt64T # 日历时间秒数
# =============================================================================
# open() 标志位
# =============================================================================
O_RDONLY: t.CDefine = 0
O_WRONLY: t.CDefine = 1
O_RDWR: t.CDefine = 2
O_CREAT: t.CDefine = 0o100
O_EXCL: t.CDefine = 0o200
O_TRUNC: t.CDefine = 0o1000
O_APPEND: t.CDefine = 0o2000
O_NONBLOCK: t.CDefine = 0o4000
O_CLOEXEC: t.CDefine = 0o2000000
# =============================================================================
# access() 测试标志
# =============================================================================
F_OK: t.CDefine = 0 # 文件存在
R_OK: t.CDefine = 4 # 可读
W_OK: t.CDefine = 2 # 可写
X_OK: t.CDefine = 1 # 可执行
# =============================================================================
# stat 结构体的 st_mode 掩码与类型位
# =============================================================================
S_IFMT: t.CDefine = 0o170000 # 类型掩码
S_IFSOCK: t.CDefine = 0o140000 # 套接字
S_IFLNK: t.CDefine = 0o120000 # 符号链接
S_IFREG: t.CDefine = 0o100000 # 普通文件
S_IFBLK: t.CDefine = 0o060000 # 块设备
S_IFDIR: t.CDefine = 0o040000 # 目录
S_IFCHR: t.CDefine = 0o020000 # 字符设备
S_IFIFO: t.CDefine = 0o010000 # FIFO/管道
# 权限位
S_ISUID: t.CDefine = 0o4000 # set-user-ID
S_ISGID: t.CDefine = 0o2000 # set-group-ID
S_ISVTX: t.CDefine = 0o1000 # sticky 位
S_IRWXU: t.CDefine = 0o700 # 用户读写执行
S_IRUSR: t.CDefine = 0o400 # 用户读
S_IWUSR: t.CDefine = 0o200 # 用户写
S_IXUSR: t.CDefine = 0o100 # 用户执行
S_IRWXG: t.CDefine = 0o070 # 组读写执行
S_IRGRP: t.CDefine = 0o040 # 组读
S_IWGRP: t.CDefine = 0o020 # 组写
S_IXGRP: t.CDefine = 0o010 # 组执行
S_IRWXO: t.CDefine = 0o007 # 其他读写执行
S_IROTH: t.CDefine = 0o004 # 其他读
S_IWOTH: t.CDefine = 0o002 # 其他写
S_IXOTH: t.CDefine = 0o001 # 其他执行
# =============================================================================
# lseek() whence 值
# =============================================================================
SEEK_SET: t.CDefine = 0
SEEK_CUR: t.CDefine = 1
SEEK_END: t.CDefine = 2
# =============================================================================
# 路径/名称长度限制
# =============================================================================
PATH_MAX: t.CDefine = 4096
NAME_MAX: t.CDefine = 255
# =============================================================================
# sysconf() 参数
# =============================================================================
_SC_NPROCESSORS_ONLN: t.CDefine = 84
_SC_PAGESIZE: t.CDefine = 30
_SC_CLK_TCK: t.CDefine = 2
# =============================================================================
# dirent 结构体的 d_type 值
# =============================================================================
DT_UNKNOWN: t.CDefine = 0
DT_FIFO: t.CDefine = 1
DT_CHR: t.CDefine = 2
DT_DIR: t.CDefine = 4
DT_BLK: t.CDefine = 6
DT_REG: t.CDefine = 8
DT_LNK: t.CDefine = 10
DT_SOCK: t.CDefine = 12
DT_WHT: t.CDefine = 14
# =============================================================================
# waitpid() 选项
# =============================================================================
WNOHANG: t.CDefine = 1
WUNTRACED: t.CDefine = 2
WCONTINUED: t.CDefine = 8
# =============================================================================
# 结构体Linux x86_64 布局
# =============================================================================
# struct stat144 字节Linux x86_64 glibc 布局)
# 注意st_atim/st_mtim/st_ctim 在 C 中是 struct timespec { tv_sec; tv_nsec; }
# 此处拆分为 _sec/_nsec 两个 LONGLONG 字段以保持二进制兼容
class Stat():
st_dev: dev_t # @ 0
st_ino: ino_t # @ 8
st_nlink: nlink_t # @ 16
st_mode: mode_t # @ 24
st_uid: uid_t # @ 28
st_gid: gid_t # @ 32
__pad0: INT # @ 36
st_rdev: dev_t # @ 40
st_size: off_t # @ 48
st_blksize: blksize_t # @ 56
st_blocks: blkcnt_t # @ 64
st_atim_sec: time_t # @ 72
st_atim_nsec: LONGLONG # @ 80
st_mtim_sec: time_t # @ 88
st_mtim_nsec: LONGLONG # @ 96
st_ctim_sec: time_t # @ 104
st_ctim_nsec: LONGLONG # @ 112
__unused0: LONGLONG # @ 120
__unused1: LONGLONG # @ 128
__unused2: LONGLONG # @ 136
# struct direntLinux x86_64d_name 为 char[256]
# 注意d_name 实际是定长 char[256]TransPyC 用 CHARPTR 占位8 字节)。
# 用户通常通过 readdir 返回的只读指针访问,不直接分配 Dirent 结构体。
class Dirent():
d_ino: ino_t # @ 0
d_off: off_t # @ 8
d_reclen: USHORT # @ 16
d_type: BYTE # @ 18
d_name: CHARPTR # @ 19实际为 char[256],此处占位)
# =============================================================================
# 文件操作函数10 个)
# =============================================================================
def open(path: t.CConst | str, oflag: INT, *args) -> INT | t.State: pass
def close(fd: INT) -> INT | t.State: pass
def read(fd: INT, buf: bytes, count: t.CSizeT) -> ssize_t | t.State: pass
def write(fd: INT, buf: t.CConst | bytes, count: t.CSizeT) -> ssize_t | t.State: pass
def lseek(fd: INT, offset: off_t, whence: INT) -> off_t | t.State: pass
def unlink(path: t.CConst | str) -> INT | t.State: pass
def access(path: t.CConst | str, amode: INT) -> INT | t.State: pass
def realpath(path: t.CConst | str, resolved: CHARPTR) -> CHARPTR | t.State: pass
def rename(oldpath: t.CConst | str, newpath: t.CConst | str) -> INT | t.State: pass
def chmod(path: t.CConst | str, mode: mode_t) -> INT | t.State: pass
# =============================================================================
# stat 系列2 个)
# =============================================================================
def stat(path: t.CConst | str, buf: Stat | t.CPtr) -> INT | t.State: pass
def fstat(fd: INT, buf: Stat | t.CPtr) -> INT | t.State: pass
# =============================================================================
# 目录操作6 个)
# =============================================================================
def opendir(name: t.CConst | str) -> DIR | t.CPtr | t.State: pass
def readdir(dirp: DIR | t.CPtr) -> Dirent | t.CPtr | t.State: pass
def closedir(dirp: DIR | t.CPtr) -> INT | t.State: pass
def mkdir(path: t.CConst | str, mode: mode_t) -> INT | t.State: pass
def rmdir(path: t.CConst | str) -> INT | t.State: pass
def getcwd(buf: CHARPTR, size: t.CSizeT) -> CHARPTR | t.State: pass
def chdir(path: t.CConst | str) -> INT | t.State: pass
# =============================================================================
# 进程/管道8 个)
# =============================================================================
def fork() -> pid_t | t.State: pass
def execvp(file: t.CConst | str, argv: CHARPTR | t.CPtr) -> INT | t.State: pass
def execv(path: t.CConst | str, argv: CHARPTR | t.CPtr) -> INT | t.State: pass
def waitpid(pid: pid_t, status: INT | t.CPtr, options: INT) -> pid_t | t.State: pass
def pipe(fds: INT | t.CPtr) -> INT | t.State: pass
def dup2(oldfd: INT, newfd: INT) -> INT | t.State: pass
def _exit(status: INT) -> t.CVoid | t.State: pass
def getpid() -> pid_t | t.State: pass
# =============================================================================
# 环境/系统4 个)
# =============================================================================
def getenv(name: t.CConst | str) -> CHARPTR | t.State: pass
def setenv(name: t.CConst | str, value: t.CConst | str, overwrite: INT) -> INT | t.State: pass
def sysconf(name: INT) -> LONG | t.State: pass
def isatty(fd: INT) -> INT | t.State: pass
# =============================================================================
# 内联宏C 宏,用 TransPyC 普通函数实现,不标记 t.CExtern
# =============================================================================
# st_mode 类型判定
def S_ISREG(mode: mode_t) -> bool:
return (mode & S_IFMT) == S_IFREG
def S_ISDIR(mode: mode_t) -> bool:
return (mode & S_IFMT) == S_IFDIR
def S_ISLNK(mode: mode_t) -> bool:
return (mode & S_IFMT) == S_IFLNK
def S_ISCHR(mode: mode_t) -> bool:
return (mode & S_IFMT) == S_IFCHR
def S_ISBLK(mode: mode_t) -> bool:
return (mode & S_IFMT) == S_IFBLK
def S_ISFIFO(mode: mode_t) -> bool:
return (mode & S_IFMT) == S_IFIFO
def S_ISSOCK(mode: mode_t) -> bool:
return (mode & S_IFMT) == S_IFSOCK
# waitpid 状态解析
def WIFEXITED(status: INT) -> bool:
return (status & 0x7F) == 0
def WEXITSTATUS(status: INT) -> INT:
return (status >> 8) & 0xFF
def WIFSIGNALED(status: INT) -> bool:
return ((status & 0x7F) != 0) and ((status & 0x7F) != 0x7F)
def WTERMSIG(status: INT) -> INT:
return status & 0x7F