posix
nimony/lib/std/posix/posix.nim
This is a raw POSIX interface module. It does not provide any convenience: cstrings are used instead of proper Nim strings and return codes indicate errors. If you want exceptions and a proper Nim-like interface, use the OS module or write a wrapper.
Every binding is header-free: types and constants are ABI transcriptions (see linux/, macos/, freebsd/, and illumos/) and procs bind real libc/kernel symbols. -d:useLibc changes which allocator and stdio implementation the stdlib uses, never which declarations exist. ABI declarations are checked by tests/nimony/stdlib/tposixabi.nim.
const EPERM: int32const ENOENT: int32const ESRCH: int32const EINTR: int32const EIO: int32const ENXIO: int32const E2BIG: int32const ENOEXEC: int32const EBADF: int32const ECHILD: int32const EAGAIN: int32const ENOMEM: int32const EACCES: int32const EFAULT: int32const EBUSY: int32const EEXIST: int32const EXDEV: int32const ENODEV: int32const ENOTDIR: int32const EISDIR: int32const EINVAL: int32const ENFILE: int32const EMFILE: int32const ENOTTY: int32const ETXTBSY: int32const EFBIG: int32const ENOSPC: int32const ESPIPE: int32const EROFS: int32const EMLINK: int32const EPIPE: int32const EDOM: int32const ERANGE: int32const EDEADLK: int32const ENAMETOOLONG: int32const ENOLCK: int32const ENOSYS: int32const ENOTEMPTY: int32const ELOOP: int32const EWOULDBLOCK: int32const ENOMSG: int32const EIDRM: int32const ENOSTR: int32const ENODATA: int32const ETIME: int32const ENOSR: int32const EPROTO: int32const EBADMSG: int32const EOVERFLOW: int32const EILSEQ: int32const ENOTSOCK: int32const EDESTADDRREQ: int32const EMSGSIZE: int32const EPROTOTYPE: int32const ENOPROTOOPT: int32const EPROTONOSUPPORT: int32const EOPNOTSUPP: int32const ENOTSUP: int32const EAFNOSUPPORT: int32const EADDRINUSE: int32const EADDRNOTAVAIL: int32const ENETDOWN: int32const ENETUNREACH: int32const ENETRESET: int32const ECONNABORTED: int32const ECONNRESET: int32const ENOBUFS: int32const EISCONN: int32const ENOTCONN: int32const ETIMEDOUT: int32const ECONNREFUSED: int32const EHOSTUNREACH: int32const EALREADY: int32const EINPROGRESS: int32const ECANCELED: int32const O_RDONLY: int32const O_WRONLY: int32const O_RDWR: int32const O_CREAT: int32const O_EXCL: int32const O_TRUNC: int32const O_APPEND: int32const O_NONBLOCK: int32const O_CLOEXEC: int32const F_GETFL: int32const F_SETFL: int32const RTLD_NOW: int32const RTLD_GLOBAL: int32const PTHREAD_MUTEX_RECURSIVE: int32const SIGABRT: int32const SIGKILL: int32const SIGTERM: int32const SIGCONT: int32const SIGSTOP: int32const PROT_READ: int32const PROT_WRITE: int32const MAP_SHARED: int32const MAP_PRIVATE: int32const MAP_ANONYMOUS: int32const MAP_POPULATE: int32const AF_UNSPEC: int32const AF_UNIX: int32const AF_INET: int32const AF_INET6: int32const SOCK_STREAM: int32const SOCK_DGRAM: int32const SOCK_RAW: int32const SOCK_SEQPACKET: int32const SOL_SOCKET: int32const SO_REUSEADDR: int32const IPPROTO_IP: int32const IPPROTO_ICMP: int32const IPPROTO_TCP: int32const IPPROTO_UDP: int32const IPPROTO_IPV6: int32const IPPROTO_ICMPV6: int32const IPPROTO_RAW: int32const INADDR_ANY: uint32const WNOHANG: int32const WCONTINUED: int32const CLOCK_REALTIME: int32const CLOCK_MONOTONIC: int32const TIMER_ABSTIME: int32const SEEK_SET: int32const SEEK_CUR: int32const SEEK_END: int32const SC_NPROCESSORS_ONLN: int32const IOFBF: int32const S_IRUSR: int32const S_IWUSR: int32const linuxA64Raw: boolLinux/AArch64 on the truly freestanding backend (
nimony n: arkham + nifasm, no libc linked). The asm-generic syscall table lacks legacy operations such as open/stat/fork; linux/arm64/raw implements them using the available at/2/clone calls.nimNoLibc, NOTnimNativeIo: the C backend still LINKS libc, so there open/stat are real symbols. Using nimNativeIo here would emit newfstatat calls, which glibc does not export under that name.const freebsdRaw: boolFreeBSD freestanding bindings use raw syscalls; nifasm normalizes their carry-flag errors to -errno. Legacy libc operations are rebuilt in freebsd/bindings and freebsd/dirs over the available traps.
const rawSyscalls: boolDarwin still links libSystem even on the freestanding backend.
const StatHasNanoseconds: boolAll supported targets store nanosecond-resolution timestamps.
const DT_UNKNOWN: uint8const DT_FIFO: uint8const DT_CHR: uint8const DT_DIR: uint8const DT_BLK: uint8const DT_REG: uint8const DT_LNK: uint8const DT_SOCK: uint8const DT_WHT: uint8const direntReclenAt: int64const direntTypeAt: int64const direntNameAt: int64const direntSkipsZeroIno: booltype ClockId = int32clockid_t (int on Linux, Darwin and FreeBSD)
type Time = distinct int64time_t
type Timespec = object tv_sec: Time tv_nsec: int64
type IOVec = object iov_base: pointer not nil iov_len: uint64
type SockLen = uint32socklen_t
type InAddr = object s_addr: uint32
type InAddrScalar = uint32type Sighandler = proc (a: int32) {.noconv.}type FileHandle = int32type SocketHandle = int32type Pid = int32pid_t
type Sockaddr_storage = object abi: array[0..15, uint64]
type TSa_Family = uint16type Sockaddr_in = object sin_family: uint16 sin_port: uint16 sin_addr: InAddr sin_zero: array[0..7, char]
type SockAddr = object sa_family: uint16 sa_data: array[0..255, char]
type Tmsghdr = object msg_name: pointer not nil msg_namelen: uint32 msg_iov: ptr IOVec not nil msg_iovlen: uint64 msg_control: pointer not nil msg_controllen: uint64 msg_flags: int32
type Mode = uint32type Off = int64type Dev = uint64type Ino = uint64type Stat = object st_dev: uint64 st_ino: uint64 st_nlink: uint64 st_mode: uint32 st_uid: uint32 st_gid: uint32 pad0: int32 st_rdev: uint64 st_size: int64 st_blksize: int64 st_blocks: int64 st_atim: Timespec st_mtim: Timespec st_ctim: Timespec glibcReserved: array[0..2, int64]
template st_mtime(s: Stat): int64Time of last data modification (seconds since epoch).
proc fcntl(a1: int32; a2: int32; vanon: varargs): int32proc close(a1: int32): int32proc S_ISBLK(m: uint32): boolproc S_ISCHR(m: uint32): boolproc S_ISDIR(m: uint32): boolproc S_ISFIFO(m: uint32): boolproc S_ISREG(m: uint32): boolproc S_ISLNK(m: uint32): boolproc S_ISSOCK(m: uint32): boolproc munmap(a1: pointer; a2: uint64): int32proc errno(): int32The last error code (libc's errno).
template pcall(x: untyped): int64Normalize a syscall-style call: non-negative result or -errno. Freestanding calls already return -errno; libc returns -1 and sets its errno slot. Consumers should use this instead of reading errno.
template mmapFailed(p: pointer not nil): boolKernel errors are addresses in [-4095, -1]; libc's MAP_FAILED (-1) is in the same range.
template mmapErrno(p: pointer not nil): int32Error code for a failed mmap.
proc clock_gettime(a1: int32; a2: var Timespec): int32proc getcwd(a1: cstring not nil; a2: int64): cstringproc chdir(path: cstring not nil): int32proc realpath(path: cstring not nil; resolved: cstring not nil): cstring not nilproc open(a1: cstring not nil; a2: int32; mode: uint32): int32proc ftruncate(a1: int32; a2: int64): int32proc fstat(a1: int32; a2: var Stat): int32proc lstat(a1: cstring not nil; a2: var Stat): int32proc stat(a1: cstring not nil; a2: var Stat): int32proc mmap(a1: pointer; a2: uint64; a3: int32; a4: int32; a5: int32; a6: int64): pointer not nilproc readlink(a1: cstring not nil; a2: cstring not nil; a3: int64): int64proc symlink(a1: cstring not nil; a2: cstring not nil): int32proc rename(a1: cstring not nil; a2: cstring not nil): int32proc link(a1: cstring not nil; a2: cstring not nil): int32proc chmod(path: cstring not nil; mode: uint32): int32proc mkdir(path: cstring not nil; mode: uint32): int32proc rmdir(path: cstring not nil): int32proc unlink(path: cstring not nil): int32proc pipe(a: ptr int32 not nil): int32proc dup2(oldfd: int32; newfd: int32): int32proc fork(): int32proc posix_fallocate(a1: int32; a2: int64; a3: int64): int32type DirStream = object isOpen: bool fd: int32 bpos: int32 nread: int32 cookie: int64 buf: array[0..4095, uint8]
proc openDirStream(d: var DirStream; path: cstring not nil): int64proc nextDirEntry(d: var DirStream; name: var cstring not nil; dType: var uint8): int64proc closeDirStream(d: var DirStream): int64proc tryGetcwd(buf: cstring not nil; size: int64): int64Writes the current directory, NUL-terminated, into
buf. Returns 0, or -errno (-ERANGE whensizeis too small).proc sysconf(a1: int32): int64proc WEXITSTATUS(s: int32): int32proc WTERMSIG(s: int32): int32proc WSTOPSIG(s: int32): int32proc WIFEXITED(s: int32): boolproc WIFSTOPPED(s: int32): boolproc WIFSIGNALED(s: int32): boolproc WIFCONTINUED(s: int32): booltype CChar = int8type CCharArray = ptr UncheckedArray[ptr int8]proc execve(path: cstring not nil; argv: ptr UncheckedArray[ptr int8]; env: ptr UncheckedArray[ptr int8]): int32proc waitpid(pid: int32; status: var int32; options: int32): int32proc kill(pid: int32; sig: int32): int32proc setpgid(pid: int32; pgid: int32): int32proc exitnow(status: int32)proc read(fildes: int32; buf: pointer not nil; nbyte: int64): int64proc write(fildes: int32; buf: pointer not nil; nbyte: int64): int64let posix_environ: ptr UncheckedArray[cstring not nil] not nilproc strerror(errnum: int32): cstring not nilproc nanosleep(req: var Timespec; rem: var Timespec): int32