posix
nimony/lib/std/posix/posix.nim
This is a raw POSIX interface module. It does not 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 posix_other.nim) and procs are bare importc of real libc/kernel symbols. -d:useLibc changes which allocator and stdio implementation the stdlib uses, never which declarations exist.
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_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: int32type ClockId = int32type Time = distinct int64type Timespec = object tv_sec: Time tv_nsec: int64
type IOVec = object iov_base: pointer not nil iov_len: uint64
type SockLen = uint32type InAddr = object s_addr: uint32
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
const linuxA64Raw: boolLinux/AArch64 on the truly freestanding backend (
nimony n: arkham + nifasm, no libc linked): every binding below becomes a RAW syscall (arkham lowers a recognizedimportcname straight tosvc), and AArch64's asm-generic syscall table simply has noopen,stat,lstat,readlink,mkdir,rmdir,unlink,rename,pipe,dup2orfork— the kernel offers only the*at/*2/cloneforms. Where that is true the legacy name is reimplemented here on top of the form that exists, precisely as glibc's wrapper does.nimNoLibc, NOTnimNativeIo: the C backend uses the same raw-syscall stdlib but still LINKS libc, so there the plainopen/statare real symbols and must keep being bound directly — gating onnimNativeIomade the C backend emit calls tonewfstatat, which glibc does not export under that name (it isfstatat), and every C-backend link that touchedgetFileSizefailed.type InAddrScalar = uint32type Sighandler = proc (a: int32) {.noconv.}type FileHandle = int32type SocketHandle = int32type Pid = int32pid_t
type Mode = uint32mode_t
type Off = int64off_t
type Dev = uint64dev_t
type Ino = uint64ino_t
type 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]
const StatHasNanoseconds: bool\ Boolean flag that indicates if the system supports nanosecond time resolution in the fields of
Stat. All supported targets do.template st_mtime(s: Stat): int64Time of last data modification (seconds since epoch). All hardcoded layouts store the full
st_mtimTimespec; this accessor mirrors the POSIXst_mtimefield/macro.proc fcntl(a1: int32; a2: int32; vanon: varargs): int32proc 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 close(a1: int32): int32proc S_ISBLK(m: uint32): boolblock special
proc S_ISCHR(m: uint32): boolchar special
proc S_ISDIR(m: uint32): booldirectory
proc S_ISFIFO(m: uint32): boolFIFO/pipe
proc S_ISREG(m: uint32): boolregular file
proc S_ISLNK(m: uint32): boolsymlink
proc S_ISSOCK(m: uint32): boolsocket
proc mmap(a1: pointer; a2: uint64; a3: int32; a4: int32; a5: int32; a6: int64): pointer not nilproc munmap(a1: pointer; a2: uint64): int32proc errno(): int32The last error code (libc's
errno).template pcall(x: untyped): int64Normalizes a syscall-style call to the Linux raw convention: returns the non-negative result on success, or
-errnoon failure. Hides whether the error is signalled by the raw syscall's negative return (freestanding / arkham,-d:nimNoLibc) or by libc's-1+ theerrnoglobal.Wrap every syscall-shaped call in this and read the error out of the result. Nothing above this module should touch an errno global.
template mmapFailed(p: pointer not nil): boolTrue if an
mmapresult indicates failure. The kernel signals failure as an address in[-4095, -1]; libc maps that toMAP_FAILED((void*)-1, itself in range), so one range check covers both conventions.template mmapErrno(p: pointer not nil): int32errnofor a failedmmap(seemmapFailed).proc clock_gettime(a1: int32; a2: var Timespec): int32proc getcwd(a1: cstring not nil; a2: int64): cstring not nilproc chdir(path: cstring not nil): int32proc realpath(path: cstring not nil; resolved: cstring not nil): cstring not nilproc readlink(a1: cstring not nil; a2: cstring not nil; a3: int64): int64proc symlink(a1: cstring not nil; a2: cstring not nil): int32type Dirent = object d_type: uint8 d_name: array[0..255, char]
type DIR = object fd: int32 bpos: int32 nread: int32 ent: Dirent buf: array[0..4095, uint8]
proc opendir(name: cstring not nil): ptr DIRproc closedir(dirp: ptr DIR): int32proc readdir(dirp: ptr DIR): ptr Direntproc mkdir(path: cstring not nil; mode: uint32): int32proc rmdir(path: cstring not nil): int32proc unlink(path: cstring not nil): int32const DT_UNKNOWN: uint8Unknown file type.
const DT_FIFO: uint8Named pipe, or FIFO.
const DT_CHR: uint8Character device.
const DT_DIR: uint8Directory.
const DT_BLK: uint8Block device.
const DT_REG: uint8Regular file.
const DT_LNK: uint8Symbolic link.
const DT_SOCK: uint8UNIX domain socket.
const DT_WHT: uint8proc sysconf(a1: int32): int64proc posix_fallocate(a1: int32; a2: int64; a3: int64): int32proc WEXITSTATUS(s: int32): int32proc WTERMSIG(s: int32): int32proc WSTOPSIG(s: int32): int32proc WIFEXITED(s: int32): boolproc WIFSIGNALED(s: int32): boolproc WIFSTOPPED(s: int32): boolproc WIFCONTINUED(s: int32): booltype CChar = int8type CCharArray = ptr UncheckedArray[ptr int8]proc pipe(a: ptr int32 not nil): int32proc dup2(oldfd: int32; newfd: int32): int32proc fork(): int32proc 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): int64var posix_environ: ptr UncheckedArray[cstring not nil] not nilproc strerror(errnum: int32): cstring not nilproc nanosleep(req: var Timespec; rem: var Timespec): int32