diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-09-15 14:46:31 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-09-15 14:51:08 -0700 |
| commit | f3ebfcae3882c03da84821abed40167ea07a8c78 (patch) | |
| tree | f1b759c94cba5b020a9ffb141fc62cb686fc5f04 /lib/std/c | |
| parent | 111a2dcf3ad53c0c8ad2c9e7c9bd042b81e90c82 (diff) | |
| parent | 0395b35cee8d4082cc40b0dcd0298f797f42309d (diff) | |
| download | zig-f3ebfcae3882c03da84821abed40167ea07a8c78.tar.gz zig-f3ebfcae3882c03da84821abed40167ea07a8c78.zip | |
Merge remote-tracking branch 'origin/master' into llvm13
Conflicts:
* cmake/Findclang.cmake
* cmake/Findlld.cmake
* cmake/Findllvm.cmake
In master branch, more search paths were added to these files with "12"
in the path. In this commit I updated them to "13".
* src/stage1/codegen.cpp
* src/zig_llvm.cpp
* src/zig_llvm.h
In master branch, ZigLLVMBuildCmpXchg is improved to add
`is_single_threaded`. However, the LLVM 13 C API has this already, and
in the llvm13 branch, ZigLLVMBuildCmpXchg is deleted in favor of the C
API. In this commit I updated stage2 to use the LLVM 13 C API rather
than depending on an improved ZigLLVMBuildCmpXchg.
Additionally, src/target.zig largestAtomicBits needed to be updated to
include the new m68k ISA.
Diffstat (limited to 'lib/std/c')
| -rw-r--r-- | lib/std/c/darwin.zig | 1775 | ||||
| -rw-r--r-- | lib/std/c/dragonfly.zig | 1088 | ||||
| -rw-r--r-- | lib/std/c/freebsd.zig | 1495 | ||||
| -rw-r--r-- | lib/std/c/haiku.zig | 1303 | ||||
| -rw-r--r-- | lib/std/c/hermit.zig | 5 | ||||
| -rw-r--r-- | lib/std/c/linux.zig | 229 | ||||
| -rw-r--r-- | lib/std/c/netbsd.zig | 1399 | ||||
| -rw-r--r-- | lib/std/c/openbsd.zig | 1321 | ||||
| -rw-r--r-- | lib/std/c/wasi.zig | 49 | ||||
| -rw-r--r-- | lib/std/c/windows.zig | 244 |
10 files changed, 8791 insertions, 117 deletions
diff --git a/lib/std/c/darwin.zig b/lib/std/c/darwin.zig index 7fa07719a8..8bb30efab3 100644 --- a/lib/std/c/darwin.zig +++ b/lib/std/c/darwin.zig @@ -3,8 +3,8 @@ const assert = std.debug.assert; const builtin = @import("builtin"); const macho = std.macho; const native_arch = builtin.target.cpu.arch; - -usingnamespace @import("../os/bits.zig"); +const maxInt = std.math.maxInt; +const iovec_const = std.os.iovec_const; extern "c" fn __error() *c_int; pub extern "c" fn NSVersionOfRunTimeLibrary(library_name: [*:0]const u8) u32; @@ -23,20 +23,25 @@ pub const copyfile_state_t = *opaque {}; pub extern "c" fn fcopyfile(from: fd_t, to: fd_t, state: ?copyfile_state_t, flags: u32) c_int; pub extern "c" fn @"realpath$DARWIN_EXTSN"(noalias file_name: [*:0]const u8, noalias resolved_name: [*]u8) ?[*:0]u8; +pub const realpath = @"realpath$DARWIN_EXTSN"; pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) isize; -extern "c" fn fstat(fd: fd_t, buf: *libc_stat) c_int; -/// On x86_64 Darwin, fstat has to be manully linked with $INODE64 suffix to force 64bit version. -/// Note that this is fixed on aarch64 and no longer necessary. -extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *libc_stat) c_int; -pub const _fstat = if (native_arch == .aarch64) fstat else @"fstat$INODE64"; +const private = struct { + extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int; + /// On x86_64 Darwin, fstat has to be manully linked with $INODE64 suffix to + /// force 64bit version. + /// Note that this is fixed on aarch64 and no longer necessary. + extern "c" fn @"fstat$INODE64"(fd: fd_t, buf: *Stat) c_int; -extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, stat_buf: *libc_stat, flags: u32) c_int; -/// On x86_64 Darwin, fstatat has to be manully linked with $INODE64 suffix to force 64bit version. -/// Note that this is fixed on aarch64 and no longer necessary. -extern "c" fn @"fstatat$INODE64"(dirfd: fd_t, path_name: [*:0]const u8, buf: *libc_stat, flags: u32) c_int; -pub const _fstatat = if (native_arch == .aarch64) fstatat else @"fstatat$INODE64"; + extern "c" fn fstatat(dirfd: fd_t, path: [*:0]const u8, stat_buf: *Stat, flags: u32) c_int; + /// On x86_64 Darwin, fstatat has to be manully linked with $INODE64 suffix to + /// force 64bit version. + /// Note that this is fixed on aarch64 and no longer necessary. + extern "c" fn @"fstatat$INODE64"(dirfd: fd_t, path_name: [*:0]const u8, buf: *Stat, flags: u32) c_int; +}; +pub const fstat = if (native_arch == .aarch64) private.fstat else private.@"fstat$INODE64"; +pub const fstatat = if (native_arch == .aarch64) private.fstatat else private.@"fstatat$INODE64"; pub extern "c" fn mach_absolute_time() u64; pub extern "c" fn mach_timebase_info(tinfo: ?*mach_timebase_info_data) void; @@ -80,8 +85,6 @@ pub const _errno = __error; pub extern "c" fn @"close$NOCANCEL"(fd: fd_t) c_int; pub extern "c" fn mach_host_self() mach_port_t; pub extern "c" fn clock_get_time(clock_serv: clock_serv_t, cur_time: *mach_timespec_t) kern_return_t; -pub extern "c" fn host_get_clock_service(host: host_t, clock_id: clock_id_t, clock_serv: ?[*]clock_serv_t) kern_return_t; -pub extern "c" fn mach_port_deallocate(task: ipc_space_t, name: mach_port_name_t) kern_return_t; pub const sf_hdtr = extern struct { headers: [*]const iovec_const, @@ -105,17 +108,16 @@ pub fn sigaddset(set: *sigset_t, signo: u5) void { pub extern "c" fn sigaltstack(ss: ?*stack_t, old_ss: ?*stack_t) c_int; -/// get address to use bind() -pub const AI_PASSIVE = 0x00000001; - -/// fill ai_canonname -pub const AI_CANONNAME = 0x00000002; - -/// prevent host name resolution -pub const AI_NUMERICHOST = 0x00000004; - -/// prevent service name resolution -pub const AI_NUMERICSERV = 0x00001000; +pub const AI = struct { + /// get address to use bind() + pub const PASSIVE = 0x00000001; + /// fill ai_canonname + pub const CANONNAME = 0x00000002; + /// prevent host name resolution + pub const NUMERICHOST = 0x00000004; + /// prevent service name resolution + pub const NUMERICSERV = 0x00001000; +}; pub const EAI = enum(c_int) { /// address family for hostname not supported @@ -256,3 +258,1726 @@ pub extern "c" fn os_unfair_lock_unlock(o: os_unfair_lock_t) void; pub extern "c" fn os_unfair_lock_trylock(o: os_unfair_lock_t) bool; pub extern "c" fn os_unfair_lock_assert_owner(o: os_unfair_lock_t) void; pub extern "c" fn os_unfair_lock_assert_not_owner(o: os_unfair_lock_t) void; + +// XXX: close -> close$NOCANCEL +// XXX: getdirentries -> _getdirentries64 +pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int; +pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int; +pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int; +pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int; +pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int; +pub extern "c" fn sched_yield() c_int; +pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int; +pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int; +pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int; +pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int; +pub extern "c" fn sigfillset(set: ?*sigset_t) void; +pub extern "c" fn alarm(seconds: c_uint) c_uint; +pub extern "c" fn sigwait(set: ?*sigset_t, sig: ?*c_int) c_int; + +// See: https://opensource.apple.com/source/xnu/xnu-6153.141.1/bsd/sys/_types.h.auto.html +// TODO: audit mode_t/pid_t, should likely be u16/i32 +pub const fd_t = c_int; +pub const pid_t = c_int; +pub const mode_t = c_uint; +pub const uid_t = u32; +pub const gid_t = u32; + +pub const in_port_t = u16; +pub const sa_family_t = u8; +pub const socklen_t = u32; +pub const sockaddr = extern struct { + len: u8, + family: sa_family_t, + data: [14]u8, + + pub const storage = std.x.os.Socket.Address.Native.Storage; + pub const in = extern struct { + len: u8 = @sizeOf(in), + family: sa_family_t = AF.INET, + port: in_port_t, + addr: u32, + zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, + }; + pub const in6 = extern struct { + len: u8 = @sizeOf(in6), + family: sa_family_t = AF.INET6, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, + }; + + /// UNIX domain socket + pub const un = extern struct { + len: u8 = @sizeOf(un), + family: sa_family_t = AF.UNIX, + path: [104]u8, + }; +}; +pub const timeval = extern struct { + tv_sec: c_long, + tv_usec: i32, +}; + +pub const timezone = extern struct { + tz_minuteswest: i32, + tz_dsttime: i32, +}; + +pub const mach_timebase_info_data = extern struct { + numer: u32, + denom: u32, +}; + +pub const off_t = i64; +pub const ino_t = u64; + +pub const Flock = extern struct { + l_start: off_t, + l_len: off_t, + l_pid: pid_t, + l_type: i16, + l_whence: i16, +}; + +pub const Stat = extern struct { + dev: i32, + mode: u16, + nlink: u16, + ino: ino_t, + uid: uid_t, + gid: gid_t, + rdev: i32, + atimesec: isize, + atimensec: isize, + mtimesec: isize, + mtimensec: isize, + ctimesec: isize, + ctimensec: isize, + birthtimesec: isize, + birthtimensec: isize, + size: off_t, + blocks: i64, + blksize: i32, + flags: u32, + gen: u32, + lspare: i32, + qspare: [2]i64, + + pub fn atime(self: @This()) timespec { + return timespec{ + .tv_sec = self.atimesec, + .tv_nsec = self.atimensec, + }; + } + + pub fn mtime(self: @This()) timespec { + return timespec{ + .tv_sec = self.mtimesec, + .tv_nsec = self.mtimensec, + }; + } + + pub fn ctime(self: @This()) timespec { + return timespec{ + .tv_sec = self.ctimesec, + .tv_nsec = self.ctimensec, + }; + } +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; + +pub const sigset_t = u32; +pub const empty_sigset: sigset_t = 0; + +pub const SIG = struct { + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const HOLD = @intToPtr(?Sigaction.sigaction_fn, 5); + + /// block specified signal set + pub const _BLOCK = 1; + /// unblock specified signal set + pub const _UNBLOCK = 2; + /// set specified signal set + pub const _SETMASK = 3; + /// hangup + pub const HUP = 1; + /// interrupt + pub const INT = 2; + /// quit + pub const QUIT = 3; + /// illegal instruction (not reset when caught) + pub const ILL = 4; + /// trace trap (not reset when caught) + pub const TRAP = 5; + /// abort() + pub const ABRT = 6; + /// pollable event ([XSR] generated, not supported) + pub const POLL = 7; + /// compatibility + pub const IOT = ABRT; + /// EMT instruction + pub const EMT = 7; + /// floating point exception + pub const FPE = 8; + /// kill (cannot be caught or ignored) + pub const KILL = 9; + /// bus error + pub const BUS = 10; + /// segmentation violation + pub const SEGV = 11; + /// bad argument to system call + pub const SYS = 12; + /// write on a pipe with no one to read it + pub const PIPE = 13; + /// alarm clock + pub const ALRM = 14; + /// software termination signal from kill + pub const TERM = 15; + /// urgent condition on IO channel + pub const URG = 16; + /// sendable stop signal not from tty + pub const STOP = 17; + /// stop signal from tty + pub const TSTP = 18; + /// continue a stopped process + pub const CONT = 19; + /// to parent on child stop or exit + pub const CHLD = 20; + /// to readers pgrp upon background tty read + pub const TTIN = 21; + /// like TTIN for output if (tp->t_local<OSTOP) + pub const TTOU = 22; + /// input/output possible signal + pub const IO = 23; + /// exceeded CPU time limit + pub const XCPU = 24; + /// exceeded file size limit + pub const XFSZ = 25; + /// virtual time alarm + pub const VTALRM = 26; + /// profiling time alarm + pub const PROF = 27; + /// window size changes + pub const WINCH = 28; + /// information request + pub const INFO = 29; + /// user defined signal 1 + pub const USR1 = 30; + /// user defined signal 2 + pub const USR2 = 31; +}; + +pub const siginfo_t = extern struct { + signo: c_int, + errno: c_int, + code: c_int, + pid: pid_t, + uid: uid_t, + status: c_int, + addr: *c_void, + value: extern union { + int: c_int, + ptr: *c_void, + }, + si_band: c_long, + _pad: [7]c_ulong, +}; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name. +pub const Sigaction = extern struct { + pub const handler_fn = fn (c_int) callconv(.C) void; + pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; + + handler: extern union { + handler: ?handler_fn, + sigaction: ?sigaction_fn, + }, + mask: sigset_t, + flags: c_uint, +}; + +pub const dirent = extern struct { + d_ino: usize, + d_seekoff: usize, + d_reclen: u16, + d_namlen: u16, + d_type: u8, + d_name: u8, // field address is address of first byte of name + + pub fn reclen(self: dirent) u16 { + return self.d_reclen; + } +}; + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: i16, + flags: u16, + fflags: u32, + data: isize, + udata: usize, +}; + +// sys/types.h on macos uses #pragma pack(4) so these checks are +// to make sure the struct is laid out the same. These values were +// produced from C code using the offsetof macro. +comptime { + assert(@offsetOf(Kevent, "ident") == 0); + assert(@offsetOf(Kevent, "filter") == 8); + assert(@offsetOf(Kevent, "flags") == 10); + assert(@offsetOf(Kevent, "fflags") == 12); + assert(@offsetOf(Kevent, "data") == 16); + assert(@offsetOf(Kevent, "udata") == 24); +} + +pub const kevent64_s = extern struct { + ident: u64, + filter: i16, + flags: u16, + fflags: u32, + data: i64, + udata: u64, + ext: [2]u64, +}; + +// sys/types.h on macos uses #pragma pack() so these checks are +// to make sure the struct is laid out the same. These values were +// produced from C code using the offsetof macro. +comptime { + assert(@offsetOf(kevent64_s, "ident") == 0); + assert(@offsetOf(kevent64_s, "filter") == 8); + assert(@offsetOf(kevent64_s, "flags") == 10); + assert(@offsetOf(kevent64_s, "fflags") == 12); + assert(@offsetOf(kevent64_s, "data") == 16); + assert(@offsetOf(kevent64_s, "udata") == 24); + assert(@offsetOf(kevent64_s, "ext") == 32); +} + +pub const mach_port_t = c_uint; +pub const clock_serv_t = mach_port_t; +pub const clock_res_t = c_int; +pub const mach_port_name_t = natural_t; +pub const natural_t = c_uint; +pub const mach_timespec_t = extern struct { + tv_sec: c_uint, + tv_nsec: clock_res_t, +}; +pub const kern_return_t = c_int; +pub const host_t = mach_port_t; +pub const CALENDAR_CLOCK = 1; + +pub const PATH_MAX = 1024; +pub const IOV_MAX = 16; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT = struct { + /// [MC2] no permissions + pub const NONE = 0x00; + /// [MC2] pages can be read + pub const READ = 0x01; + /// [MC2] pages can be written + pub const WRITE = 0x02; + /// [MC2] pages can be executed + pub const EXEC = 0x04; +}; + +pub const MAP = struct { + /// allocated from memory, swap space + pub const ANONYMOUS = 0x1000; + /// map from file (default) + pub const FILE = 0x0000; + /// interpret addr exactly + pub const FIXED = 0x0010; + /// region may contain semaphores + pub const HASSEMAPHORE = 0x0200; + /// changes are private + pub const PRIVATE = 0x0002; + /// share changes + pub const SHARED = 0x0001; + /// don't cache pages for this mapping + pub const NOCACHE = 0x0400; + /// don't reserve needed swap area + pub const NORESERVE = 0x0040; + pub const FAILED = @intToPtr(*c_void, maxInt(usize)); +}; + +pub const SA = struct { + /// take signal on signal stack + pub const ONSTACK = 0x0001; + /// restart system on signal return + pub const RESTART = 0x0002; + /// reset to SIG.DFL when taking signal + pub const RESETHAND = 0x0004; + /// do not generate SIG.CHLD on child stop + pub const NOCLDSTOP = 0x0008; + /// don't mask the signal we're delivering + pub const NODEFER = 0x0010; + /// don't keep zombies around + pub const NOCLDWAIT = 0x0020; + /// signal handler with SIGINFO args + pub const SIGINFO = 0x0040; + /// do not bounce off kernel's sigtramp + pub const USERTRAMP = 0x0100; + /// signal handler with SIGINFO args with 64bit regs information + pub const @"64REGSET" = 0x0200; +}; + +pub const F_OK = 0; +pub const X_OK = 1; +pub const W_OK = 2; +pub const R_OK = 4; + +pub const O = struct { + pub const PATH = 0x0000; + /// open for reading only + pub const RDONLY = 0x0000; + /// open for writing only + pub const WRONLY = 0x0001; + /// open for reading and writing + pub const RDWR = 0x0002; + /// do not block on open or for data to become available + pub const NONBLOCK = 0x0004; + /// append on each write + pub const APPEND = 0x0008; + /// create file if it does not exist + pub const CREAT = 0x0200; + /// truncate size to 0 + pub const TRUNC = 0x0400; + /// error if CREAT and the file exists + pub const EXCL = 0x0800; + /// atomically obtain a shared lock + pub const SHLOCK = 0x0010; + /// atomically obtain an exclusive lock + pub const EXLOCK = 0x0020; + /// do not follow symlinks + pub const NOFOLLOW = 0x0100; + /// allow open of symlinks + pub const SYMLINK = 0x200000; + /// descriptor requested for event notifications only + pub const EVTONLY = 0x8000; + /// mark as close-on-exec + pub const CLOEXEC = 0x1000000; + pub const ACCMODE = 3; + pub const ALERT = 536870912; + pub const ASYNC = 64; + pub const DIRECTORY = 1048576; + pub const DP_GETRAWENCRYPTED = 1; + pub const DP_GETRAWUNENCRYPTED = 2; + pub const DSYNC = 4194304; + pub const FSYNC = SYNC; + pub const NOCTTY = 131072; + pub const POPUP = 2147483648; + pub const SYNC = 128; +}; + +pub const SEEK = struct { + pub const SET = 0x0; + pub const CUR = 0x1; + pub const END = 0x2; +}; + +pub const DT = struct { + pub const UNKNOWN = 0; + pub const FIFO = 1; + pub const CHR = 2; + pub const DIR = 4; + pub const BLK = 6; + pub const REG = 8; + pub const LNK = 10; + pub const SOCK = 12; + pub const WHT = 14; +}; + +/// no flag value +pub const KEVENT_FLAG_NONE = 0x000; + +/// immediate timeout +pub const KEVENT_FLAG_IMMEDIATE = 0x001; + +/// output events only include change +pub const KEVENT_FLAG_ERROR_EVENTS = 0x002; + +/// add event to kq (implies enable) +pub const EV_ADD = 0x0001; + +/// delete event from kq +pub const EV_DELETE = 0x0002; + +/// enable event +pub const EV_ENABLE = 0x0004; + +/// disable event (not reported) +pub const EV_DISABLE = 0x0008; + +/// only report one occurrence +pub const EV_ONESHOT = 0x0010; + +/// clear event state after reporting +pub const EV_CLEAR = 0x0020; + +/// force immediate event output +/// ... with or without EV_ERROR +/// ... use KEVENT_FLAG_ERROR_EVENTS +/// on syscalls supporting flags +pub const EV_RECEIPT = 0x0040; + +/// disable event after reporting +pub const EV_DISPATCH = 0x0080; + +/// unique kevent per udata value +pub const EV_UDATA_SPECIFIC = 0x0100; + +/// ... in combination with EV_DELETE +/// will defer delete until udata-specific +/// event enabled. EINPROGRESS will be +/// returned to indicate the deferral +pub const EV_DISPATCH2 = EV_DISPATCH | EV_UDATA_SPECIFIC; + +/// report that source has vanished +/// ... only valid with EV_DISPATCH2 +pub const EV_VANISHED = 0x0200; + +/// reserved by system +pub const EV_SYSFLAGS = 0xF000; + +/// filter-specific flag +pub const EV_FLAG0 = 0x1000; + +/// filter-specific flag +pub const EV_FLAG1 = 0x2000; + +/// EOF detected +pub const EV_EOF = 0x8000; + +/// error, data contains errno +pub const EV_ERROR = 0x4000; + +pub const EV_POLL = EV_FLAG0; +pub const EV_OOBAND = EV_FLAG1; + +pub const EVFILT_READ = -1; +pub const EVFILT_WRITE = -2; + +/// attached to aio requests +pub const EVFILT_AIO = -3; + +/// attached to vnodes +pub const EVFILT_VNODE = -4; + +/// attached to struct proc +pub const EVFILT_PROC = -5; + +/// attached to struct proc +pub const EVFILT_SIGNAL = -6; + +/// timers +pub const EVFILT_TIMER = -7; + +/// Mach portsets +pub const EVFILT_MACHPORT = -8; + +/// Filesystem events +pub const EVFILT_FS = -9; + +/// User events +pub const EVFILT_USER = -10; + +/// Virtual memory events +pub const EVFILT_VM = -12; + +/// Exception events +pub const EVFILT_EXCEPT = -15; + +pub const EVFILT_SYSCOUNT = 17; + +/// On input, NOTE_TRIGGER causes the event to be triggered for output. +pub const NOTE_TRIGGER = 0x01000000; + +/// ignore input fflags +pub const NOTE_FFNOP = 0x00000000; + +/// and fflags +pub const NOTE_FFAND = 0x40000000; + +/// or fflags +pub const NOTE_FFOR = 0x80000000; + +/// copy fflags +pub const NOTE_FFCOPY = 0xc0000000; + +/// mask for operations +pub const NOTE_FFCTRLMASK = 0xc0000000; +pub const NOTE_FFLAGSMASK = 0x00ffffff; + +/// low water mark +pub const NOTE_LOWAT = 0x00000001; + +/// OOB data +pub const NOTE_OOB = 0x00000002; + +/// vnode was removed +pub const NOTE_DELETE = 0x00000001; + +/// data contents changed +pub const NOTE_WRITE = 0x00000002; + +/// size increased +pub const NOTE_EXTEND = 0x00000004; + +/// attributes changed +pub const NOTE_ATTRIB = 0x00000008; + +/// link count changed +pub const NOTE_LINK = 0x00000010; + +/// vnode was renamed +pub const NOTE_RENAME = 0x00000020; + +/// vnode access was revoked +pub const NOTE_REVOKE = 0x00000040; + +/// No specific vnode event: to test for EVFILT_READ activation +pub const NOTE_NONE = 0x00000080; + +/// vnode was unlocked by flock(2) +pub const NOTE_FUNLOCK = 0x00000100; + +/// process exited +pub const NOTE_EXIT = 0x80000000; + +/// process forked +pub const NOTE_FORK = 0x40000000; + +/// process exec'd +pub const NOTE_EXEC = 0x20000000; + +/// shared with EVFILT_SIGNAL +pub const NOTE_SIGNAL = 0x08000000; + +/// exit status to be returned, valid for child process only +pub const NOTE_EXITSTATUS = 0x04000000; + +/// provide details on reasons for exit +pub const NOTE_EXIT_DETAIL = 0x02000000; + +/// mask for signal & exit status +pub const NOTE_PDATAMASK = 0x000fffff; +pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK); + +pub const NOTE_EXIT_DETAIL_MASK = 0x00070000; +pub const NOTE_EXIT_DECRYPTFAIL = 0x00010000; +pub const NOTE_EXIT_MEMORY = 0x00020000; +pub const NOTE_EXIT_CSERROR = 0x00040000; + +/// will react on memory pressure +pub const NOTE_VM_PRESSURE = 0x80000000; + +/// will quit on memory pressure, possibly after cleaning up dirty state +pub const NOTE_VM_PRESSURE_TERMINATE = 0x40000000; + +/// will quit immediately on memory pressure +pub const NOTE_VM_PRESSURE_SUDDEN_TERMINATE = 0x20000000; + +/// there was an error +pub const NOTE_VM_ERROR = 0x10000000; + +/// data is seconds +pub const NOTE_SECONDS = 0x00000001; + +/// data is microseconds +pub const NOTE_USECONDS = 0x00000002; + +/// data is nanoseconds +pub const NOTE_NSECONDS = 0x00000004; + +/// absolute timeout +pub const NOTE_ABSOLUTE = 0x00000008; + +/// ext[1] holds leeway for power aware timers +pub const NOTE_LEEWAY = 0x00000010; + +/// system does minimal timer coalescing +pub const NOTE_CRITICAL = 0x00000020; + +/// system does maximum timer coalescing +pub const NOTE_BACKGROUND = 0x00000040; +pub const NOTE_MACH_CONTINUOUS_TIME = 0x00000080; + +/// data is mach absolute time units +pub const NOTE_MACHTIME = 0x00000100; + +pub const AF = struct { + pub const UNSPEC = 0; + pub const LOCAL = 1; + pub const UNIX = LOCAL; + pub const INET = 2; + pub const SYS_CONTROL = 2; + pub const IMPLINK = 3; + pub const PUP = 4; + pub const CHAOS = 5; + pub const NS = 6; + pub const ISO = 7; + pub const OSI = ISO; + pub const ECMA = 8; + pub const DATAKIT = 9; + pub const CCITT = 10; + pub const SNA = 11; + pub const DECnet = 12; + pub const DLI = 13; + pub const LAT = 14; + pub const HYLINK = 15; + pub const APPLETALK = 16; + pub const ROUTE = 17; + pub const LINK = 18; + pub const XTP = 19; + pub const COIP = 20; + pub const CNT = 21; + pub const RTIP = 22; + pub const IPX = 23; + pub const SIP = 24; + pub const PIP = 25; + pub const ISDN = 28; + pub const E164 = ISDN; + pub const KEY = 29; + pub const INET6 = 30; + pub const NATM = 31; + pub const SYSTEM = 32; + pub const NETBIOS = 33; + pub const PPP = 34; + pub const MAX = 40; +}; + +pub const PF = struct { + pub const UNSPEC = AF.UNSPEC; + pub const LOCAL = AF.LOCAL; + pub const UNIX = PF.LOCAL; + pub const INET = AF.INET; + pub const IMPLINK = AF.IMPLINK; + pub const PUP = AF.PUP; + pub const CHAOS = AF.CHAOS; + pub const NS = AF.NS; + pub const ISO = AF.ISO; + pub const OSI = AF.ISO; + pub const ECMA = AF.ECMA; + pub const DATAKIT = AF.DATAKIT; + pub const CCITT = AF.CCITT; + pub const SNA = AF.SNA; + pub const DECnet = AF.DECnet; + pub const DLI = AF.DLI; + pub const LAT = AF.LAT; + pub const HYLINK = AF.HYLINK; + pub const APPLETALK = AF.APPLETALK; + pub const ROUTE = AF.ROUTE; + pub const LINK = AF.LINK; + pub const XTP = AF.XTP; + pub const COIP = AF.COIP; + pub const CNT = AF.CNT; + pub const SIP = AF.SIP; + pub const IPX = AF.IPX; + pub const RTIP = AF.RTIP; + pub const PIP = AF.PIP; + pub const ISDN = AF.ISDN; + pub const KEY = AF.KEY; + pub const INET6 = AF.INET6; + pub const NATM = AF.NATM; + pub const SYSTEM = AF.SYSTEM; + pub const NETBIOS = AF.NETBIOS; + pub const PPP = AF.PPP; + pub const MAX = AF.MAX; +}; + +pub const SYSPROTO_EVENT = 1; +pub const SYSPROTO_CONTROL = 2; + +pub const SOCK = struct { + pub const STREAM = 1; + pub const DGRAM = 2; + pub const RAW = 3; + pub const RDM = 4; + pub const SEQPACKET = 5; + pub const MAXADDRLEN = 255; + + /// Not actually supported by Darwin, but Zig supplies a shim. + /// This numerical value is not ABI-stable. It need only not conflict + /// with any other `SOCK` bits. + pub const CLOEXEC = 1 << 15; + /// Not actually supported by Darwin, but Zig supplies a shim. + /// This numerical value is not ABI-stable. It need only not conflict + /// with any other `SOCK` bits. + pub const NONBLOCK = 1 << 16; +}; + +pub const IPPROTO = struct { + pub const ICMP = 1; + pub const ICMPV6 = 58; + pub const TCP = 6; + pub const UDP = 17; + pub const IP = 0; + pub const IPV6 = 41; +}; + +pub const SOL = struct { + pub const SOCKET = 0xffff; +}; + +pub const SO = struct { + pub const DEBUG = 0x0001; + pub const ACCEPTCONN = 0x0002; + pub const REUSEADDR = 0x0004; + pub const KEEPALIVE = 0x0008; + pub const DONTROUTE = 0x0010; + pub const BROADCAST = 0x0020; + pub const USELOOPBACK = 0x0040; + pub const LINGER = 0x1080; + pub const OOBINLINE = 0x0100; + pub const REUSEPORT = 0x0200; + pub const ACCEPTFILTER = 0x1000; + pub const SNDBUF = 0x1001; + pub const RCVBUF = 0x1002; + pub const SNDLOWAT = 0x1003; + pub const RCVLOWAT = 0x1004; + pub const SNDTIMEO = 0x1005; + pub const RCVTIMEO = 0x1006; + pub const ERROR = 0x1007; + pub const TYPE = 0x1008; + + pub const NREAD = 0x1020; + pub const NKE = 0x1021; + pub const NOSIGPIPE = 0x1022; + pub const NOADDRERR = 0x1023; + pub const NWRITE = 0x1024; + pub const REUSESHAREUID = 0x1025; +}; + +pub const W = struct { + /// [XSI] no hang in wait/no child to reap + pub const NOHANG = 0x00000001; + /// [XSI] notify on stop, untraced child + pub const UNTRACED = 0x00000002; + + pub fn EXITSTATUS(x: u32) u8 { + return @intCast(u8, x >> 8); + } + pub fn TERMSIG(x: u32) u32 { + return status(x); + } + pub fn STOPSIG(x: u32) u32 { + return x >> 8; + } + pub fn IFEXITED(x: u32) bool { + return status(x) == 0; + } + pub fn IFSTOPPED(x: u32) bool { + return status(x) == stopped and STOPSIG(x) != 0x13; + } + pub fn IFSIGNALED(x: u32) bool { + return status(x) != stopped and status(x) != 0; + } + + fn status(x: u32) u32 { + return x & 0o177; + } + const stopped = 0o177; +}; + +pub const E = enum(u16) { + /// No error occurred. + SUCCESS = 0, + + /// Operation not permitted + PERM = 1, + + /// No such file or directory + NOENT = 2, + + /// No such process + SRCH = 3, + + /// Interrupted system call + INTR = 4, + + /// Input/output error + IO = 5, + + /// Device not configured + NXIO = 6, + + /// Argument list too long + @"2BIG" = 7, + + /// Exec format error + NOEXEC = 8, + + /// Bad file descriptor + BADF = 9, + + /// No child processes + CHILD = 10, + + /// Resource deadlock avoided + DEADLK = 11, + + /// Cannot allocate memory + NOMEM = 12, + + /// Permission denied + ACCES = 13, + + /// Bad address + FAULT = 14, + + /// Block device required + NOTBLK = 15, + + /// Device / Resource busy + BUSY = 16, + + /// File exists + EXIST = 17, + + /// Cross-device link + XDEV = 18, + + /// Operation not supported by device + NODEV = 19, + + /// Not a directory + NOTDIR = 20, + + /// Is a directory + ISDIR = 21, + + /// Invalid argument + INVAL = 22, + + /// Too many open files in system + NFILE = 23, + + /// Too many open files + MFILE = 24, + + /// Inappropriate ioctl for device + NOTTY = 25, + + /// Text file busy + TXTBSY = 26, + + /// File too large + FBIG = 27, + + /// No space left on device + NOSPC = 28, + + /// Illegal seek + SPIPE = 29, + + /// Read-only file system + ROFS = 30, + + /// Too many links + MLINK = 31, + + /// Broken pipe + PIPE = 32, + + // math software + + /// Numerical argument out of domain + DOM = 33, + + /// Result too large + RANGE = 34, + + // non-blocking and interrupt i/o + + /// Resource temporarily unavailable + /// This is the same code used for `WOULDBLOCK`. + AGAIN = 35, + + /// Operation now in progress + INPROGRESS = 36, + + /// Operation already in progress + ALREADY = 37, + + // ipc/network software -- argument errors + + /// Socket operation on non-socket + NOTSOCK = 38, + + /// Destination address required + DESTADDRREQ = 39, + + /// Message too long + MSGSIZE = 40, + + /// Protocol wrong type for socket + PROTOTYPE = 41, + + /// Protocol not available + NOPROTOOPT = 42, + + /// Protocol not supported + PROTONOSUPPORT = 43, + + /// Socket type not supported + SOCKTNOSUPPORT = 44, + + /// Operation not supported + /// The same code is used for `NOTSUP`. + OPNOTSUPP = 45, + + /// Protocol family not supported + PFNOSUPPORT = 46, + + /// Address family not supported by protocol family + AFNOSUPPORT = 47, + + /// Address already in use + ADDRINUSE = 48, + /// Can't assign requested address + + // ipc/network software -- operational errors + ADDRNOTAVAIL = 49, + + /// Network is down + NETDOWN = 50, + + /// Network is unreachable + NETUNREACH = 51, + + /// Network dropped connection on reset + NETRESET = 52, + + /// Software caused connection abort + CONNABORTED = 53, + + /// Connection reset by peer + CONNRESET = 54, + + /// No buffer space available + NOBUFS = 55, + + /// Socket is already connected + ISCONN = 56, + + /// Socket is not connected + NOTCONN = 57, + + /// Can't send after socket shutdown + SHUTDOWN = 58, + + /// Too many references: can't splice + TOOMANYREFS = 59, + + /// Operation timed out + TIMEDOUT = 60, + + /// Connection refused + CONNREFUSED = 61, + + /// Too many levels of symbolic links + LOOP = 62, + + /// File name too long + NAMETOOLONG = 63, + + /// Host is down + HOSTDOWN = 64, + + /// No route to host + HOSTUNREACH = 65, + /// Directory not empty + + // quotas & mush + NOTEMPTY = 66, + + /// Too many processes + PROCLIM = 67, + + /// Too many users + USERS = 68, + /// Disc quota exceeded + + // Network File System + DQUOT = 69, + + /// Stale NFS file handle + STALE = 70, + + /// Too many levels of remote in path + REMOTE = 71, + + /// RPC struct is bad + BADRPC = 72, + + /// RPC version wrong + RPCMISMATCH = 73, + + /// RPC prog. not avail + PROGUNAVAIL = 74, + + /// Program version wrong + PROGMISMATCH = 75, + + /// Bad procedure for program + PROCUNAVAIL = 76, + + /// No locks available + NOLCK = 77, + + /// Function not implemented + NOSYS = 78, + + /// Inappropriate file type or format + FTYPE = 79, + + /// Authentication error + AUTH = 80, + + /// Need authenticator + NEEDAUTH = 81, + + // Intelligent device errors + + /// Device power is off + PWROFF = 82, + + /// Device error, e.g. paper out + DEVERR = 83, + + /// Value too large to be stored in data type + OVERFLOW = 84, + + // Program loading errors + + /// Bad executable + BADEXEC = 85, + + /// Bad CPU type in executable + BADARCH = 86, + + /// Shared library version mismatch + SHLIBVERS = 87, + + /// Malformed Macho file + BADMACHO = 88, + + /// Operation canceled + CANCELED = 89, + + /// Identifier removed + IDRM = 90, + + /// No message of desired type + NOMSG = 91, + + /// Illegal byte sequence + ILSEQ = 92, + + /// Attribute not found + NOATTR = 93, + + /// Bad message + BADMSG = 94, + + /// Reserved + MULTIHOP = 95, + + /// No message available on STREAM + NODATA = 96, + + /// Reserved + NOLINK = 97, + + /// No STREAM resources + NOSR = 98, + + /// Not a STREAM + NOSTR = 99, + + /// Protocol error + PROTO = 100, + + /// STREAM ioctl timeout + TIME = 101, + + /// No such policy registered + NOPOLICY = 103, + + /// State not recoverable + NOTRECOVERABLE = 104, + + /// Previous owner died + OWNERDEAD = 105, + + /// Interface output queue is full + QFULL = 106, + + _, +}; + +pub const SIGSTKSZ = 131072; +pub const MINSIGSTKSZ = 32768; + +pub const SS_ONSTACK = 1; +pub const SS_DISABLE = 4; + +pub const stack_t = extern struct { + sp: [*]u8, + size: isize, + flags: i32, +}; + +pub const S = struct { + pub const IFMT = 0o170000; + + pub const IFIFO = 0o010000; + pub const IFCHR = 0o020000; + pub const IFDIR = 0o040000; + pub const IFBLK = 0o060000; + pub const IFREG = 0o100000; + pub const IFLNK = 0o120000; + pub const IFSOCK = 0o140000; + pub const IFWHT = 0o160000; + + pub const ISUID = 0o4000; + pub const ISGID = 0o2000; + pub const ISVTX = 0o1000; + pub const IRWXU = 0o700; + pub const IRUSR = 0o400; + pub const IWUSR = 0o200; + pub const IXUSR = 0o100; + pub const IRWXG = 0o070; + pub const IRGRP = 0o040; + pub const IWGRP = 0o020; + pub const IXGRP = 0o010; + pub const IRWXO = 0o007; + pub const IROTH = 0o004; + pub const IWOTH = 0o002; + pub const IXOTH = 0o001; + + pub fn ISFIFO(m: u32) bool { + return m & IFMT == IFIFO; + } + + pub fn ISCHR(m: u32) bool { + return m & IFMT == IFCHR; + } + + pub fn ISDIR(m: u32) bool { + return m & IFMT == IFDIR; + } + + pub fn ISBLK(m: u32) bool { + return m & IFMT == IFBLK; + } + + pub fn ISREG(m: u32) bool { + return m & IFMT == IFREG; + } + + pub fn ISLNK(m: u32) bool { + return m & IFMT == IFLNK; + } + + pub fn ISSOCK(m: u32) bool { + return m & IFMT == IFSOCK; + } + + pub fn IWHT(m: u32) bool { + return m & IFMT == IFWHT; + } +}; + +pub const HOST_NAME_MAX = 72; + +pub const AT = struct { + pub const FDCWD = -2; + /// Use effective ids in access check + pub const EACCESS = 0x0010; + /// Act on the symlink itself not the target + pub const SYMLINK_NOFOLLOW = 0x0020; + /// Act on target of symlink + pub const SYMLINK_FOLLOW = 0x0040; + /// Path refers to directory + pub const REMOVEDIR = 0x0080; +}; + +pub const addrinfo = extern struct { + flags: i32, + family: i32, + socktype: i32, + protocol: i32, + addrlen: socklen_t, + canonname: ?[*:0]u8, + addr: ?*sockaddr, + next: ?*addrinfo, +}; + +pub const RTLD = struct { + pub const LAZY = 0x1; + pub const NOW = 0x2; + pub const LOCAL = 0x4; + pub const GLOBAL = 0x8; + pub const NOLOAD = 0x10; + pub const NODELETE = 0x80; + pub const FIRST = 0x100; + + pub const NEXT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))); + pub const DEFAULT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -2))); + pub const SELF = @intToPtr(*c_void, @bitCast(usize, @as(isize, -3))); + pub const MAIN_ONLY = @intToPtr(*c_void, @bitCast(usize, @as(isize, -5))); +}; + +pub const F = struct { + /// duplicate file descriptor + pub const DUPFD = 0; + /// get file descriptor flags + pub const GETFD = 1; + /// set file descriptor flags + pub const SETFD = 2; + /// get file status flags + pub const GETFL = 3; + /// set file status flags + pub const SETFL = 4; + /// get SIGIO/SIGURG proc/pgrp + pub const GETOWN = 5; + /// set SIGIO/SIGURG proc/pgrp + pub const SETOWN = 6; + /// get record locking information + pub const GETLK = 7; + /// set record locking information + pub const SETLK = 8; + /// F.SETLK; wait if blocked + pub const SETLKW = 9; + /// F.SETLK; wait if blocked, return on timeout + pub const SETLKWTIMEOUT = 10; + pub const FLUSH_DATA = 40; + /// Used for regression test + pub const CHKCLEAN = 41; + /// Preallocate storage + pub const PREALLOCATE = 42; + /// Truncate a file without zeroing space + pub const SETSIZE = 43; + /// Issue an advisory read async with no copy to user + pub const RDADVISE = 44; + /// turn read ahead off/on for this fd + pub const RDAHEAD = 45; + /// turn data caching off/on for this fd + pub const NOCACHE = 48; + /// file offset to device offset + pub const LOG2PHYS = 49; + /// return the full path of the fd + pub const GETPATH = 50; + /// fsync + ask the drive to flush to the media + pub const FULLFSYNC = 51; + /// find which component (if any) is a package + pub const PATHPKG_CHECK = 52; + /// "freeze" all fs operations + pub const FREEZE_FS = 53; + /// "thaw" all fs operations + pub const THAW_FS = 54; + /// turn data caching off/on (globally) for this file + pub const GLOBAL_NOCACHE = 55; + /// add detached signatures + pub const ADDSIGS = 59; + /// add signature from same file (used by dyld for shared libs) + pub const ADDFILESIGS = 61; + /// used in conjunction with F.NOCACHE to indicate that DIRECT, synchonous writes + /// should not be used (i.e. its ok to temporaily create cached pages) + pub const NODIRECT = 62; + ///Get the protection class of a file from the EA, returns int + pub const GETPROTECTIONCLASS = 63; + ///Set the protection class of a file for the EA, requires int + pub const SETPROTECTIONCLASS = 64; + ///file offset to device offset, extended + pub const LOG2PHYS_EXT = 65; + ///get record locking information, per-process + pub const GETLKPID = 66; + ///Mark the file as being the backing store for another filesystem + pub const SETBACKINGSTORE = 70; + ///return the full path of the FD, but error in specific mtmd circumstances + pub const GETPATH_MTMINFO = 71; + ///Returns the code directory, with associated hashes, to the caller + pub const GETCODEDIR = 72; + ///No SIGPIPE generated on EPIPE + pub const SETNOSIGPIPE = 73; + ///Status of SIGPIPE for this fd + pub const GETNOSIGPIPE = 74; + ///For some cases, we need to rewrap the key for AKS/MKB + pub const TRANSCODEKEY = 75; + ///file being written to a by single writer... if throttling enabled, writes + ///may be broken into smaller chunks with throttling in between + pub const SINGLE_WRITER = 76; + ///Get the protection version number for this filesystem + pub const GETPROTECTIONLEVEL = 77; + ///Add detached code signatures (used by dyld for shared libs) + pub const FINDSIGS = 78; + ///Add signature from same file, only if it is signed by Apple (used by dyld for simulator) + pub const ADDFILESIGS_FOR_DYLD_SIM = 83; + ///fsync + issue barrier to drive + pub const BARRIERFSYNC = 85; + ///Add signature from same file, return end offset in structure on success + pub const ADDFILESIGS_RETURN = 97; + ///Check if Library Validation allows this Mach-O file to be mapped into the calling process + pub const CHECK_LV = 98; + ///Deallocate a range of the file + pub const PUNCHHOLE = 99; + ///Trim an active file + pub const TRIM_ACTIVE_FILE = 100; + ///mark the dup with FD_CLOEXEC + pub const DUPFD_CLOEXEC = 67; + /// shared or read lock + pub const RDLCK = 1; + /// unlock + pub const UNLCK = 2; + /// exclusive or write lock + pub const WRLCK = 3; +}; + +pub const FCNTL_FS_SPECIFIC_BASE = 0x00010000; + +///close-on-exec flag +pub const FD_CLOEXEC = 1; + +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const UN = 8; + pub const NB = 4; +}; + +pub const nfds_t = u32; +pub const pollfd = extern struct { + fd: fd_t, + events: i16, + revents: i16, +}; + +pub const POLL = struct { + pub const IN = 0x001; + pub const PRI = 0x002; + pub const OUT = 0x004; + pub const RDNORM = 0x040; + pub const WRNORM = OUT; + pub const RDBAND = 0x080; + pub const WRBAND = 0x100; + + pub const EXTEND = 0x0200; + pub const ATTRIB = 0x0400; + pub const NLINK = 0x0800; + pub const WRITE = 0x1000; + + pub const ERR = 0x008; + pub const HUP = 0x010; + pub const NVAL = 0x020; + + pub const STANDARD = IN | PRI | OUT | RDNORM | RDBAND | WRBAND | ERR | HUP | NVAL; +}; + +pub const CLOCK = struct { + pub const REALTIME = 0; + pub const MONOTONIC = 6; + pub const MONOTONIC_RAW = 4; + pub const MONOTONIC_RAW_APPROX = 5; + pub const UPTIME_RAW = 8; + pub const UPTIME_RAW_APPROX = 9; + pub const PROCESS_CPUTIME_ID = 12; + pub const THREAD_CPUTIME_ID = 16; +}; + +/// Max open files per process +/// https://opensource.apple.com/source/xnu/xnu-4903.221.2/bsd/sys/syslimits.h.auto.html +pub const OPEN_MAX = 10240; +pub const RUSAGE_SELF = 0; +pub const RUSAGE_CHILDREN = -1; + +pub const rusage = extern struct { + utime: timeval, + stime: timeval, + maxrss: isize, + ixrss: isize, + idrss: isize, + isrss: isize, + minflt: isize, + majflt: isize, + nswap: isize, + inblock: isize, + oublock: isize, + msgsnd: isize, + msgrcv: isize, + nsignals: isize, + nvcsw: isize, + nivcsw: isize, +}; + +pub const rlimit_resource = enum(c_int) { + CPU = 0, + FSIZE = 1, + DATA = 2, + STACK = 3, + CORE = 4, + RSS = 5, + MEMLOCK = 6, + NPROC = 7, + NOFILE = 8, + _, + + pub const AS: rlimit_resource = .RSS; +}; + +pub const rlim_t = u64; + +pub const RLIM = struct { + /// No limit + pub const INFINITY: rlim_t = (1 << 63) - 1; + + pub const SAVED_MAX = INFINITY; + pub const SAVED_CUR = INFINITY; +}; + +pub const rlimit = extern struct { + /// Soft limit + cur: rlim_t, + /// Hard limit + max: rlim_t, +}; + +pub const SHUT = struct { + pub const RD = 0; + pub const WR = 1; + pub const RDWR = 2; +}; + +// Term +pub const VEOF = 0; +pub const VEOL = 1; +pub const VEOL2 = 2; +pub const VERASE = 3; +pub const VWERASE = 4; +pub const VKILL = 5; +pub const VREPRINT = 6; +pub const VINTR = 8; +pub const VQUIT = 9; +pub const VSUSP = 10; +pub const VDSUSP = 11; +pub const VSTART = 12; +pub const VSTOP = 13; +pub const VLNEXT = 14; +pub const VDISCARD = 15; +pub const VMIN = 16; +pub const VTIME = 17; +pub const VSTATUS = 18; +pub const NCCS = 20; // 2 spares (7, 19) + +pub const IGNBRK = 0x00000001; // ignore BREAK condition +pub const BRKINT = 0x00000002; // map BREAK to SIGINTR +pub const IGNPAR = 0x00000004; // ignore (discard) parity errors +pub const PARMRK = 0x00000008; // mark parity and framing errors +pub const INPCK = 0x00000010; // enable checking of parity errors +pub const ISTRIP = 0x00000020; // strip 8th bit off chars +pub const INLCR = 0x00000040; // map NL into CR +pub const IGNCR = 0x00000080; // ignore CR +pub const ICRNL = 0x00000100; // map CR to NL (ala CRMOD) +pub const IXON = 0x00000200; // enable output flow control +pub const IXOFF = 0x00000400; // enable input flow control +pub const IXANY = 0x00000800; // any char will restart after stop +pub const IMAXBEL = 0x00002000; // ring bell on input queue full +pub const IUTF8 = 0x00004000; // maintain state for UTF-8 VERASE + +pub const OPOST = 0x00000001; //enable following output processing +pub const ONLCR = 0x00000002; // map NL to CR-NL (ala CRMOD) +pub const OXTABS = 0x00000004; // expand tabs to spaces +pub const ONOEOT = 0x00000008; // discard EOT's (^D) on output) + +pub const OCRNL = 0x00000010; // map CR to NL on output +pub const ONOCR = 0x00000020; // no CR output at column 0 +pub const ONLRET = 0x00000040; // NL performs CR function +pub const OFILL = 0x00000080; // use fill characters for delay +pub const NLDLY = 0x00000300; // \n delay +pub const TABDLY = 0x00000c04; // horizontal tab delay +pub const CRDLY = 0x00003000; // \r delay +pub const FFDLY = 0x00004000; // form feed delay +pub const BSDLY = 0x00008000; // \b delay +pub const VTDLY = 0x00010000; // vertical tab delay +pub const OFDEL = 0x00020000; // fill is DEL, else NUL + +pub const NL0 = 0x00000000; +pub const NL1 = 0x00000100; +pub const NL2 = 0x00000200; +pub const NL3 = 0x00000300; +pub const TAB0 = 0x00000000; +pub const TAB1 = 0x00000400; +pub const TAB2 = 0x00000800; +pub const TAB3 = 0x00000004; +pub const CR0 = 0x00000000; +pub const CR1 = 0x00001000; +pub const CR2 = 0x00002000; +pub const CR3 = 0x00003000; +pub const FF0 = 0x00000000; +pub const FF1 = 0x00004000; +pub const BS0 = 0x00000000; +pub const BS1 = 0x00008000; +pub const VT0 = 0x00000000; +pub const VT1 = 0x00010000; + +pub const CIGNORE = 0x00000001; // ignore control flags +pub const CSIZE = 0x00000300; // character size mask +pub const CS5 = 0x00000000; // 5 bits (pseudo) +pub const CS6 = 0x00000100; // 6 bits +pub const CS7 = 0x00000200; // 7 bits +pub const CS8 = 0x00000300; // 8 bits +pub const CSTOPB = 0x0000040; // send 2 stop bits +pub const CREAD = 0x00000800; // enable receiver +pub const PARENB = 0x00001000; // parity enable +pub const PARODD = 0x00002000; // odd parity, else even +pub const HUPCL = 0x00004000; // hang up on last close +pub const CLOCAL = 0x00008000; // ignore modem status lines +pub const CCTS_OFLOW = 0x00010000; // CTS flow control of output +pub const CRTSCTS = (CCTS_OFLOW | CRTS_IFLOW); +pub const CRTS_IFLOW = 0x00020000; // RTS flow control of input +pub const CDTR_IFLOW = 0x00040000; // DTR flow control of input +pub const CDSR_OFLOW = 0x00080000; // DSR flow control of output +pub const CCAR_OFLOW = 0x00100000; // DCD flow control of output +pub const MDMBUF = 0x00100000; // old name for CCAR_OFLOW + +pub const ECHOKE = 0x00000001; // visual erase for line kill +pub const ECHOE = 0x00000002; // visually erase chars +pub const ECHOK = 0x00000004; // echo NL after line kill +pub const ECHO = 0x00000008; // enable echoing +pub const ECHONL = 0x00000010; // echo NL even if ECHO is off +pub const ECHOPRT = 0x00000020; // visual erase mode for hardcopy +pub const ECHOCTL = 0x00000040; // echo control chars as ^(Char) +pub const ISIG = 0x00000080; // enable signals INTR, QUIT, [D]SUSP +pub const ICANON = 0x00000100; // canonicalize input lines +pub const ALTWERASE = 0x00000200; // use alternate WERASE algorithm +pub const IEXTEN = 0x00000400; // enable DISCARD and LNEXT +pub const EXTPROC = 0x00000800; // external processing +pub const TOSTOP = 0x00400000; // stop background jobs from output +pub const FLUSHO = 0x00800000; // output being flushed (state) +pub const NOKERNINFO = 0x02000000; // no kernel output from VSTATUS +pub const PENDIN = 0x20000000; // XXX retype pending input (state) +pub const NOFLSH = 0x80000000; // don't flush after interrupt + +pub const TCSANOW = 0; // make change immediate +pub const TCSADRAIN = 1; // drain output, then change +pub const TCSAFLUSH = 2; // drain output, flush input +pub const TCSASOFT = 0x10; // flag - don't alter h.w. state +pub const TCSA = enum(c_uint) { + NOW, + DRAIN, + FLUSH, + _, +}; + +pub const B0 = 0; +pub const B50 = 50; +pub const B75 = 75; +pub const B110 = 110; +pub const B134 = 134; +pub const B150 = 150; +pub const B200 = 200; +pub const B300 = 300; +pub const B600 = 600; +pub const B1200 = 1200; +pub const B1800 = 1800; +pub const B2400 = 2400; +pub const B4800 = 4800; +pub const B9600 = 9600; +pub const B19200 = 19200; +pub const B38400 = 38400; +pub const B7200 = 7200; +pub const B14400 = 14400; +pub const B28800 = 28800; +pub const B57600 = 57600; +pub const B76800 = 76800; +pub const B115200 = 115200; +pub const B230400 = 230400; +pub const EXTA = 19200; +pub const EXTB = 38400; + +pub const TCIFLUSH = 1; +pub const TCOFLUSH = 2; +pub const TCIOFLUSH = 3; +pub const TCOOFF = 1; +pub const TCOON = 2; +pub const TCIOFF = 3; +pub const TCION = 4; + +pub const cc_t = u8; +pub const speed_t = u64; +pub const tcflag_t = u64; + +pub const termios = extern struct { + iflag: tcflag_t, // input flags + oflag: tcflag_t, // output flags + cflag: tcflag_t, // control flags + lflag: tcflag_t, // local flags + cc: [NCCS]cc_t, // control chars + ispeed: speed_t align(8), // input speed + ospeed: speed_t, // output speed +}; + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +pub const T = struct { + pub const IOCGWINSZ = ior(0x40000000, 't', 104, @sizeOf(winsize)); +}; +pub const IOCPARM_MASK = 0x1fff; + +fn ior(inout: u32, group: usize, num: usize, len: usize) usize { + return (inout | ((len & IOCPARM_MASK) << 16) | ((group) << 8) | (num)); +} + +// CPU families mapping +pub const CPUFAMILY = enum(u32) { + UNKNOWN = 0, + POWERPC_G3 = 0xcee41549, + POWERPC_G4 = 0x77c184ae, + POWERPC_G5 = 0xed76d8aa, + INTEL_6_13 = 0xaa33392b, + INTEL_PENRYN = 0x78ea4fbc, + INTEL_NEHALEM = 0x6b5a4cd2, + INTEL_WESTMERE = 0x573b5eec, + INTEL_SANDYBRIDGE = 0x5490b78c, + INTEL_IVYBRIDGE = 0x1f65e835, + INTEL_HASWELL = 0x10b282dc, + INTEL_BROADWELL = 0x582ed09c, + INTEL_SKYLAKE = 0x37fc219f, + INTEL_KABYLAKE = 0x0f817246, + ARM_9 = 0xe73283ae, + ARM_11 = 0x8ff620d8, + ARM_XSCALE = 0x53b005f5, + ARM_12 = 0xbd1b0ae9, + ARM_13 = 0x0cc90e64, + ARM_14 = 0x96077ef1, + ARM_15 = 0xa8511bca, + ARM_SWIFT = 0x1e2d6381, + ARM_CYCLONE = 0x37a09642, + ARM_TYPHOON = 0x2c91a47e, + ARM_TWISTER = 0x92fb37c8, + ARM_HURRICANE = 0x67ceee93, + ARM_MONSOON_MISTRAL = 0xe81e7ef6, + ARM_VORTEX_TEMPEST = 0x07d34b9f, + ARM_LIGHTNING_THUNDER = 0x462504d2, + ARM_FIRESTORM_ICESTORM = 0x1b588bb3, + _, +}; diff --git a/lib/std/c/dragonfly.zig b/lib/std/c/dragonfly.zig index e5786d2bd4..35bbeac6a3 100644 --- a/lib/std/c/dragonfly.zig +++ b/lib/std/c/dragonfly.zig @@ -1,5 +1,7 @@ const std = @import("../std.zig"); -usingnamespace std.c; +const maxInt = std.math.maxInt; +const iovec = std.os.iovec; + extern "c" threadlocal var errno: c_int; pub fn _errno() *c_int { return &errno; @@ -30,3 +32,1087 @@ pub const pthread_attr_t = extern struct { // copied from freebsd }; pub const sem_t = ?*opaque {}; + +// See: +// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/include/unistd.h +// - https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/sys/types.h +// TODO: mode_t should probably be changed to a u16, audit pid_t/off_t as well +pub const fd_t = c_int; +pub const pid_t = c_int; +pub const off_t = c_long; +pub const mode_t = c_uint; +pub const uid_t = u32; +pub const gid_t = u32; +pub const time_t = isize; +pub const suseconds_t = c_long; + +pub const E = enum(u16) { + /// No error occurred. + SUCCESS = 0, + + PERM = 1, + NOENT = 2, + SRCH = 3, + INTR = 4, + IO = 5, + NXIO = 6, + @"2BIG" = 7, + NOEXEC = 8, + BADF = 9, + CHILD = 10, + DEADLK = 11, + NOMEM = 12, + ACCES = 13, + FAULT = 14, + NOTBLK = 15, + BUSY = 16, + EXIST = 17, + XDEV = 18, + NODEV = 19, + NOTDIR = 20, + ISDIR = 21, + INVAL = 22, + NFILE = 23, + MFILE = 24, + NOTTY = 25, + TXTBSY = 26, + FBIG = 27, + NOSPC = 28, + SPIPE = 29, + ROFS = 30, + MLINK = 31, + PIPE = 32, + DOM = 33, + RANGE = 34, + /// This code is also used for `WOULDBLOCK`. + AGAIN = 35, + INPROGRESS = 36, + ALREADY = 37, + NOTSOCK = 38, + DESTADDRREQ = 39, + MSGSIZE = 40, + PROTOTYPE = 41, + NOPROTOOPT = 42, + PROTONOSUPPORT = 43, + SOCKTNOSUPPORT = 44, + /// This code is also used for `NOTSUP`. + OPNOTSUPP = 45, + PFNOSUPPORT = 46, + AFNOSUPPORT = 47, + ADDRINUSE = 48, + ADDRNOTAVAIL = 49, + NETDOWN = 50, + NETUNREACH = 51, + NETRESET = 52, + CONNABORTED = 53, + CONNRESET = 54, + NOBUFS = 55, + ISCONN = 56, + NOTCONN = 57, + SHUTDOWN = 58, + TOOMANYREFS = 59, + TIMEDOUT = 60, + CONNREFUSED = 61, + LOOP = 62, + NAMETOOLONG = 63, + HOSTDOWN = 64, + HOSTUNREACH = 65, + NOTEMPTY = 66, + PROCLIM = 67, + USERS = 68, + DQUOT = 69, + STALE = 70, + REMOTE = 71, + BADRPC = 72, + RPCMISMATCH = 73, + PROGUNAVAIL = 74, + PROGMISMATCH = 75, + PROCUNAVAIL = 76, + NOLCK = 77, + NOSYS = 78, + FTYPE = 79, + AUTH = 80, + NEEDAUTH = 81, + IDRM = 82, + NOMSG = 83, + OVERFLOW = 84, + CANCELED = 85, + ILSEQ = 86, + NOATTR = 87, + DOOFUS = 88, + BADMSG = 89, + MULTIHOP = 90, + NOLINK = 91, + PROTO = 92, + NOMEDIUM = 93, + ASYNC = 99, + _, +}; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT = struct { + pub const NONE = 0; + pub const READ = 1; + pub const WRITE = 2; + pub const EXEC = 4; +}; + +pub const MAP = struct { + pub const FILE = 0; + pub const FAILED = @intToPtr(*c_void, maxInt(usize)); + pub const ANONYMOUS = ANON; + pub const COPY = PRIVATE; + pub const SHARED = 1; + pub const PRIVATE = 2; + pub const FIXED = 16; + pub const RENAME = 32; + pub const NORESERVE = 64; + pub const INHERIT = 128; + pub const NOEXTEND = 256; + pub const HASSEMAPHORE = 512; + pub const STACK = 1024; + pub const NOSYNC = 2048; + pub const ANON = 4096; + pub const VPAGETABLE = 8192; + pub const TRYFIXED = 65536; + pub const NOCORE = 131072; + pub const SIZEALIGN = 262144; +}; + +pub const W = struct { + pub const NOHANG = 0x0001; + pub const UNTRACED = 0x0002; + pub const CONTINUED = 0x0004; + pub const STOPPED = UNTRACED; + pub const NOWAIT = 0x0008; + pub const EXITED = 0x0010; + pub const TRAPPED = 0x0020; + + pub fn EXITSTATUS(s: u32) u8 { + return @intCast(u8, (s & 0xff00) >> 8); + } + pub fn TERMSIG(s: u32) u32 { + return s & 0x7f; + } + pub fn STOPSIG(s: u32) u32 { + return EXITSTATUS(s); + } + pub fn IFEXITED(s: u32) bool { + return TERMSIG(s) == 0; + } + pub fn IFSTOPPED(s: u32) bool { + return @truncate(u16, (((s & 0xffff) *% 0x10001) >> 8)) > 0x7f00; + } + pub fn IFSIGNALED(s: u32) bool { + return (s & 0xffff) -% 1 < 0xff; + } +}; + +pub const SA = struct { + pub const ONSTACK = 0x0001; + pub const RESTART = 0x0002; + pub const RESETHAND = 0x0004; + pub const NODEFER = 0x0010; + pub const NOCLDWAIT = 0x0020; + pub const SIGINFO = 0x0040; +}; + +pub const PATH_MAX = 1024; +pub const IOV_MAX = KERN.IOV_MAX; + +pub const ino_t = c_ulong; + +pub const Stat = extern struct { + ino: ino_t, + nlink: c_uint, + dev: c_uint, + mode: c_ushort, + padding1: u16, + uid: uid_t, + gid: gid_t, + rdev: c_uint, + atim: timespec, + mtim: timespec, + ctim: timespec, + size: c_ulong, + blocks: i64, + blksize: u32, + flags: u32, + gen: u32, + lspare: i32, + qspare1: i64, + qspare2: i64, + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } +}; + +pub const timespec = extern struct { + tv_sec: c_long, + tv_nsec: c_long, +}; + +pub const timeval = extern struct { + /// seconds + tv_sec: time_t, + /// microseconds + tv_usec: suseconds_t, +}; + +pub const CTL = struct { + pub const UNSPEC = 0; + pub const KERN = 1; + pub const VM = 2; + pub const VFS = 3; + pub const NET = 4; + pub const DEBUG = 5; + pub const HW = 6; + pub const MACHDEP = 7; + pub const USER = 8; + pub const LWKT = 10; + pub const MAXID = 11; + pub const MAXNAME = 12; +}; + +pub const KERN = struct { + pub const PROC_ALL = 0; + pub const OSTYPE = 1; + pub const PROC_PID = 1; + pub const OSRELEASE = 2; + pub const PROC_PGRP = 2; + pub const OSREV = 3; + pub const PROC_SESSION = 3; + pub const VERSION = 4; + pub const PROC_TTY = 4; + pub const MAXVNODES = 5; + pub const PROC_UID = 5; + pub const MAXPROC = 6; + pub const PROC_RUID = 6; + pub const MAXFILES = 7; + pub const PROC_ARGS = 7; + pub const ARGMAX = 8; + pub const PROC_CWD = 8; + pub const PROC_PATHNAME = 9; + pub const SECURELVL = 9; + pub const PROC_SIGTRAMP = 10; + pub const HOSTNAME = 10; + pub const HOSTID = 11; + pub const CLOCKRATE = 12; + pub const VNODE = 13; + pub const PROC = 14; + pub const FILE = 15; + pub const PROC_FLAGMASK = 16; + pub const PROF = 16; + pub const PROC_FLAG_LWP = 16; + pub const POSIX1 = 17; + pub const NGROUPS = 18; + pub const JOB_CONTROL = 19; + pub const SAVED_IDS = 20; + pub const BOOTTIME = 21; + pub const NISDOMAINNAME = 22; + pub const UPDATEINTERVAL = 23; + pub const OSRELDATE = 24; + pub const NTP_PLL = 25; + pub const BOOTFILE = 26; + pub const MAXFILESPERPROC = 27; + pub const MAXPROCPERUID = 28; + pub const DUMPDEV = 29; + pub const IPC = 30; + pub const DUMMY = 31; + pub const PS_STRINGS = 32; + pub const USRSTACK = 33; + pub const LOGSIGEXIT = 34; + pub const IOV_MAX = 35; + pub const MAXPOSIXLOCKSPERUID = 36; + pub const MAXID = 37; +}; + +pub const HOST_NAME_MAX = 255; + +// access function +pub const F_OK = 0; // test for existence of file +pub const X_OK = 1; // test for execute or search permission +pub const W_OK = 2; // test for write permission +pub const R_OK = 4; // test for read permission + +pub const O = struct { + pub const RDONLY = 0; + pub const NDELAY = NONBLOCK; + pub const WRONLY = 1; + pub const RDWR = 2; + pub const ACCMODE = 3; + pub const NONBLOCK = 4; + pub const APPEND = 8; + pub const SHLOCK = 16; + pub const EXLOCK = 32; + pub const ASYNC = 64; + pub const FSYNC = 128; + pub const SYNC = 128; + pub const NOFOLLOW = 256; + pub const CREAT = 512; + pub const TRUNC = 1024; + pub const EXCL = 2048; + pub const NOCTTY = 32768; + pub const DIRECT = 65536; + pub const CLOEXEC = 131072; + pub const FBLOCKING = 262144; + pub const FNONBLOCKING = 524288; + pub const FAPPEND = 1048576; + pub const FOFFSET = 2097152; + pub const FSYNCWRITE = 4194304; + pub const FASYNCWRITE = 8388608; + pub const DIRECTORY = 134217728; +}; + +pub const SEEK = struct { + pub const SET = 0; + pub const CUR = 1; + pub const END = 2; + pub const DATA = 3; + pub const HOLE = 4; +}; + +pub const F = struct { + pub const ULOCK = 0; + pub const LOCK = 1; + pub const TLOCK = 2; + pub const TEST = 3; + + pub const DUPFD = 0; + pub const GETFD = 1; + pub const RDLCK = 1; + pub const SETFD = 2; + pub const UNLCK = 2; + pub const WRLCK = 3; + pub const GETFL = 3; + pub const SETFL = 4; + pub const GETOWN = 5; + pub const SETOWN = 6; + pub const GETLK = 7; + pub const SETLK = 8; + pub const SETLKW = 9; + pub const DUP2FD = 10; + pub const DUPFD_CLOEXEC = 17; + pub const DUP2FD_CLOEXEC = 18; +}; + +pub const FD_CLOEXEC = 1; + +pub const AT = struct { + pub const FDCWD = -328243; + pub const SYMLINK_NOFOLLOW = 1; + pub const REMOVEDIR = 2; + pub const EACCESS = 4; + pub const SYMLINK_FOLLOW = 8; +}; + +pub const dirent = extern struct { + d_fileno: c_ulong, + d_namlen: u16, + d_type: u8, + d_unused1: u8, + d_unused2: u32, + d_name: [256]u8, + + pub fn reclen(self: dirent) u16 { + return (@offsetOf(dirent, "d_name") + self.d_namlen + 1 + 7) & ~@as(u16, 7); + } +}; + +pub const DT = struct { + pub const UNKNOWN = 0; + pub const FIFO = 1; + pub const CHR = 2; + pub const DIR = 4; + pub const BLK = 6; + pub const REG = 8; + pub const LNK = 10; + pub const SOCK = 12; + pub const WHT = 14; + pub const DBF = 15; +}; + +pub const CLOCK = struct { + pub const REALTIME = 0; + pub const VIRTUAL = 1; + pub const PROF = 2; + pub const MONOTONIC = 4; + pub const UPTIME = 5; + pub const UPTIME_PRECISE = 7; + pub const UPTIME_FAST = 8; + pub const REALTIME_PRECISE = 9; + pub const REALTIME_FAST = 10; + pub const MONOTONIC_PRECISE = 11; + pub const MONOTONIC_FAST = 12; + pub const SECOND = 13; + pub const THREAD_CPUTIME_ID = 14; + pub const PROCESS_CPUTIME_ID = 15; +}; + +pub const sockaddr = extern struct { + len: u8, + family: u8, + data: [14]u8, + + pub const storage = std.x.os.Socket.Address.Native.Storage; + + pub const in = extern struct { + len: u8 = @sizeOf(in), + family: sa_family_t = AF.INET, + port: in_port_t, + addr: u32, + zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, + }; + + pub const in6 = extern struct { + len: u8 = @sizeOf(in6), + family: sa_family_t = AF.INET6, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, + }; +}; + +pub const Kevent = extern struct { + ident: usize, + filter: c_short, + flags: c_ushort, + fflags: c_uint, + data: isize, + udata: usize, +}; + +pub const EVFILT_FS = -10; +pub const EVFILT_USER = -9; +pub const EVFILT_EXCEPT = -8; +pub const EVFILT_TIMER = -7; +pub const EVFILT_SIGNAL = -6; +pub const EVFILT_PROC = -5; +pub const EVFILT_VNODE = -4; +pub const EVFILT_AIO = -3; +pub const EVFILT_WRITE = -2; +pub const EVFILT_READ = -1; +pub const EVFILT_SYSCOUNT = 10; +pub const EVFILT_MARKER = 15; + +pub const EV_ADD = 1; +pub const EV_DELETE = 2; +pub const EV_ENABLE = 4; +pub const EV_DISABLE = 8; +pub const EV_ONESHOT = 16; +pub const EV_CLEAR = 32; +pub const EV_RECEIPT = 64; +pub const EV_DISPATCH = 128; +pub const EV_NODATA = 4096; +pub const EV_FLAG1 = 8192; +pub const EV_ERROR = 16384; +pub const EV_EOF = 32768; +pub const EV_SYSFLAGS = 61440; + +pub const NOTE_FFNOP = 0; +pub const NOTE_TRACK = 1; +pub const NOTE_DELETE = 1; +pub const NOTE_LOWAT = 1; +pub const NOTE_TRACKERR = 2; +pub const NOTE_OOB = 2; +pub const NOTE_WRITE = 2; +pub const NOTE_EXTEND = 4; +pub const NOTE_CHILD = 4; +pub const NOTE_ATTRIB = 8; +pub const NOTE_LINK = 16; +pub const NOTE_RENAME = 32; +pub const NOTE_REVOKE = 64; +pub const NOTE_PDATAMASK = 1048575; +pub const NOTE_FFLAGSMASK = 16777215; +pub const NOTE_TRIGGER = 16777216; +pub const NOTE_EXEC = 536870912; +pub const NOTE_FFAND = 1073741824; +pub const NOTE_FORK = 1073741824; +pub const NOTE_EXIT = 2147483648; +pub const NOTE_FFOR = 2147483648; +pub const NOTE_FFCTRLMASK = 3221225472; +pub const NOTE_FFCOPY = 3221225472; +pub const NOTE_PCTRLMASK = 4026531840; + +pub const stack_t = extern struct { + sp: [*]u8, + size: isize, + flags: i32, +}; + +pub const S = struct { + pub const IREAD = IRUSR; + pub const IEXEC = IXUSR; + pub const IWRITE = IWUSR; + pub const IXOTH = 1; + pub const IWOTH = 2; + pub const IROTH = 4; + pub const IRWXO = 7; + pub const IXGRP = 8; + pub const IWGRP = 16; + pub const IRGRP = 32; + pub const IRWXG = 56; + pub const IXUSR = 64; + pub const IWUSR = 128; + pub const IRUSR = 256; + pub const IRWXU = 448; + pub const ISTXT = 512; + pub const BLKSIZE = 512; + pub const ISVTX = 512; + pub const ISGID = 1024; + pub const ISUID = 2048; + pub const IFIFO = 4096; + pub const IFCHR = 8192; + pub const IFDIR = 16384; + pub const IFBLK = 24576; + pub const IFREG = 32768; + pub const IFDB = 36864; + pub const IFLNK = 40960; + pub const IFSOCK = 49152; + pub const IFWHT = 57344; + pub const IFMT = 61440; + + pub fn ISCHR(m: u32) bool { + return m & IFMT == IFCHR; + } +}; + +pub const BADSIG = SIG.ERR; + +pub const SIG = struct { + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + + pub const BLOCK = 1; + pub const UNBLOCK = 2; + pub const SETMASK = 3; + + pub const IOT = ABRT; + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const TRAP = 5; + pub const ABRT = 6; + pub const EMT = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const BUS = 10; + pub const SEGV = 11; + pub const SYS = 12; + pub const PIPE = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const URG = 16; + pub const STOP = 17; + pub const TSTP = 18; + pub const CONT = 19; + pub const CHLD = 20; + pub const TTIN = 21; + pub const TTOU = 22; + pub const IO = 23; + pub const XCPU = 24; + pub const XFSZ = 25; + pub const VTALRM = 26; + pub const PROF = 27; + pub const WINCH = 28; + pub const INFO = 29; + pub const USR1 = 30; + pub const USR2 = 31; + pub const THR = 32; + pub const CKPT = 33; + pub const CKPTEXIT = 34; +}; + +pub const siginfo_t = extern struct { + signo: c_int, + errno: c_int, + code: c_int, + pid: c_int, + uid: uid_t, + status: c_int, + addr: ?*c_void, + value: sigval, + band: c_long, + __spare__: [7]c_int, +}; + +pub const sigval = extern union { + sival_int: c_int, + sival_ptr: ?*c_void, +}; + +pub const _SIG_WORDS = 4; + +pub const sigset_t = extern struct { + __bits: [_SIG_WORDS]c_uint, +}; + +pub const empty_sigset = sigset_t{ .__bits = [_]c_uint{0} ** _SIG_WORDS }; + +pub const sig_atomic_t = c_int; + +pub const Sigaction = extern struct { + pub const handler_fn = fn (c_int) callconv(.C) void; + pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; + + /// signal handler + handler: extern union { + handler: ?handler_fn, + sigaction: ?sigaction_fn, + }, + flags: c_uint, + mask: sigset_t, +}; + +pub const sig_t = [*c]fn (c_int) callconv(.C) void; + +pub const SOCK = struct { + pub const STREAM = 1; + pub const DGRAM = 2; + pub const RAW = 3; + pub const RDM = 4; + pub const SEQPACKET = 5; + pub const MAXADDRLEN = 255; + pub const CLOEXEC = 0x10000000; + pub const NONBLOCK = 0x20000000; +}; + +pub const SO = struct { + pub const DEBUG = 0x0001; + pub const ACCEPTCONN = 0x0002; + pub const REUSEADDR = 0x0004; + pub const KEEPALIVE = 0x0008; + pub const DONTROUTE = 0x0010; + pub const BROADCAST = 0x0020; + pub const USELOOPBACK = 0x0040; + pub const LINGER = 0x0080; + pub const OOBINLINE = 0x0100; + pub const REUSEPORT = 0x0200; + pub const TIMESTAMP = 0x0400; + pub const NOSIGPIPE = 0x0800; + pub const ACCEPTFILTER = 0x1000; + pub const RERROR = 0x2000; + pub const PASSCRED = 0x4000; + + pub const SNDBUF = 0x1001; + pub const RCVBUF = 0x1002; + pub const SNDLOWAT = 0x1003; + pub const RCVLOWAT = 0x1004; + pub const SNDTIMEO = 0x1005; + pub const RCVTIMEO = 0x1006; + pub const ERROR = 0x1007; + pub const TYPE = 0x1008; + pub const SNDSPACE = 0x100a; + pub const CPUHINT = 0x1030; +}; + +pub const SOL = struct { + pub const SOCKET = 0xffff; +}; + +pub const PF = struct { + pub const INET6 = AF.INET6; + pub const IMPLINK = AF.IMPLINK; + pub const ROUTE = AF.ROUTE; + pub const ISO = AF.ISO; + pub const PIP = AF.pseudo_PIP; + pub const CHAOS = AF.CHAOS; + pub const DATAKIT = AF.DATAKIT; + pub const INET = AF.INET; + pub const APPLETALK = AF.APPLETALK; + pub const SIP = AF.SIP; + pub const OSI = AF.ISO; + pub const CNT = AF.CNT; + pub const LINK = AF.LINK; + pub const HYLINK = AF.HYLINK; + pub const MAX = AF.MAX; + pub const KEY = AF.pseudo_KEY; + pub const PUP = AF.PUP; + pub const COIP = AF.COIP; + pub const SNA = AF.SNA; + pub const LOCAL = AF.LOCAL; + pub const NETBIOS = AF.NETBIOS; + pub const NATM = AF.NATM; + pub const BLUETOOTH = AF.BLUETOOTH; + pub const UNSPEC = AF.UNSPEC; + pub const NETGRAPH = AF.NETGRAPH; + pub const ECMA = AF.ECMA; + pub const IPX = AF.IPX; + pub const DLI = AF.DLI; + pub const ATM = AF.ATM; + pub const CCITT = AF.CCITT; + pub const ISDN = AF.ISDN; + pub const RTIP = AF.pseudo_RTIP; + pub const LAT = AF.LAT; + pub const UNIX = PF.LOCAL; + pub const XTP = AF.pseudo_XTP; + pub const DECnet = AF.DECnet; +}; + +pub const AF = struct { + pub const UNSPEC = 0; + pub const OSI = ISO; + pub const UNIX = LOCAL; + pub const LOCAL = 1; + pub const INET = 2; + pub const IMPLINK = 3; + pub const PUP = 4; + pub const CHAOS = 5; + pub const NETBIOS = 6; + pub const ISO = 7; + pub const ECMA = 8; + pub const DATAKIT = 9; + pub const CCITT = 10; + pub const SNA = 11; + pub const DLI = 13; + pub const LAT = 14; + pub const HYLINK = 15; + pub const APPLETALK = 16; + pub const ROUTE = 17; + pub const LINK = 18; + pub const COIP = 20; + pub const CNT = 21; + pub const IPX = 23; + pub const SIP = 24; + pub const ISDN = 26; + pub const INET6 = 28; + pub const NATM = 29; + pub const ATM = 30; + pub const NETGRAPH = 32; + pub const BLUETOOTH = 33; + pub const MPLS = 34; + pub const MAX = 36; +}; + +pub const in_port_t = u16; +pub const sa_family_t = u8; +pub const socklen_t = u32; + +pub const EAI = enum(c_int) { + ADDRFAMILY = 1, + AGAIN = 2, + BADFLAGS = 3, + FAIL = 4, + FAMILY = 5, + MEMORY = 6, + NODATA = 7, + NONAME = 8, + SERVICE = 9, + SOCKTYPE = 10, + SYSTEM = 11, + BADHINTS = 12, + PROTOCOL = 13, + OVERFLOW = 14, + _, +}; + +pub const AI = struct { + pub const PASSIVE = 0x00000001; + pub const CANONNAME = 0x00000002; + pub const NUMERICHOST = 0x00000004; + pub const NUMERICSERV = 0x00000008; + pub const MASK = PASSIVE | CANONNAME | NUMERICHOST | NUMERICSERV | ADDRCONFIG; + pub const ALL = 0x00000100; + pub const V4MAPPED_CFG = 0x00000200; + pub const ADDRCONFIG = 0x00000400; + pub const V4MAPPED = 0x00000800; + pub const DEFAULT = V4MAPPED_CFG | ADDRCONFIG; +}; + +pub const RTLD = struct { + pub const LAZY = 1; + pub const NOW = 2; + pub const MODEMASK = 0x3; + pub const GLOBAL = 0x100; + pub const LOCAL = 0; + pub const TRACE = 0x200; + pub const NODELETE = 0x01000; + pub const NOLOAD = 0x02000; + + pub const NEXT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))); + pub const DEFAULT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -2))); + pub const SELF = @intToPtr(*c_void, @bitCast(usize, @as(isize, -3))); + pub const ALL = @intToPtr(*c_void, @bitCast(usize, @as(isize, -4))); +}; + +pub const dl_phdr_info = extern struct { + dlpi_addr: usize, + dlpi_name: ?[*:0]const u8, + dlpi_phdr: [*]std.elf.Phdr, + dlpi_phnum: u16, +}; +pub const cmsghdr = extern struct { + cmsg_len: socklen_t, + cmsg_level: c_int, + cmsg_type: c_int, +}; +pub const msghdr = extern struct { + msg_name: ?*c_void, + msg_namelen: socklen_t, + msg_iov: [*c]iovec, + msg_iovlen: c_int, + msg_control: ?*c_void, + msg_controllen: socklen_t, + msg_flags: c_int, +}; +pub const cmsgcred = extern struct { + cmcred_pid: pid_t, + cmcred_uid: uid_t, + cmcred_euid: uid_t, + cmcred_gid: gid_t, + cmcred_ngroups: c_short, + cmcred_groups: [16]gid_t, +}; +pub const sf_hdtr = extern struct { + headers: [*c]iovec, + hdr_cnt: c_int, + trailers: [*c]iovec, + trl_cnt: c_int, +}; + +pub const MS_SYNC = 0; +pub const MS_ASYNC = 1; +pub const MS_INVALIDATE = 2; + +pub const POSIX_MADV_SEQUENTIAL = 2; +pub const POSIX_MADV_RANDOM = 1; +pub const POSIX_MADV_DONTNEED = 4; +pub const POSIX_MADV_NORMAL = 0; +pub const POSIX_MADV_WILLNEED = 3; + +pub const MADV = struct { + pub const SEQUENTIAL = 2; + pub const CONTROL_END = SETMAP; + pub const DONTNEED = 4; + pub const RANDOM = 1; + pub const WILLNEED = 3; + pub const NORMAL = 0; + pub const CONTROL_START = INVAL; + pub const FREE = 5; + pub const NOSYNC = 6; + pub const AUTOSYNC = 7; + pub const NOCORE = 8; + pub const CORE = 9; + pub const INVAL = 10; + pub const SETMAP = 11; +}; + +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const UN = 8; + pub const NB = 4; +}; + +pub const Flock = extern struct { + l_start: off_t, + l_len: off_t, + l_pid: pid_t, + l_type: c_short, + l_whence: c_short, +}; + +pub const addrinfo = extern struct { + flags: i32, + family: i32, + socktype: i32, + protocol: i32, + addrlen: socklen_t, + canonname: ?[*:0]u8, + addr: ?*sockaddr, + next: ?*addrinfo, +}; + +pub const IPPROTO = struct { + pub const IP = 0; + pub const ICMP = 1; + pub const TCP = 6; + pub const UDP = 17; + pub const IPV6 = 41; + pub const RAW = 255; + pub const HOPOPTS = 0; + pub const IGMP = 2; + pub const GGP = 3; + pub const IPV4 = 4; + pub const IPIP = IPV4; + pub const ST = 7; + pub const EGP = 8; + pub const PIGP = 9; + pub const RCCMON = 10; + pub const NVPII = 11; + pub const PUP = 12; + pub const ARGUS = 13; + pub const EMCON = 14; + pub const XNET = 15; + pub const CHAOS = 16; + pub const MUX = 18; + pub const MEAS = 19; + pub const HMP = 20; + pub const PRM = 21; + pub const IDP = 22; + pub const TRUNK1 = 23; + pub const TRUNK2 = 24; + pub const LEAF1 = 25; + pub const LEAF2 = 26; + pub const RDP = 27; + pub const IRTP = 28; + pub const TP = 29; + pub const BLT = 30; + pub const NSP = 31; + pub const INP = 32; + pub const SEP = 33; + pub const @"3PC" = 34; + pub const IDPR = 35; + pub const XTP = 36; + pub const DDP = 37; + pub const CMTP = 38; + pub const TPXX = 39; + pub const IL = 40; + pub const SDRP = 42; + pub const ROUTING = 43; + pub const FRAGMENT = 44; + pub const IDRP = 45; + pub const RSVP = 46; + pub const GRE = 47; + pub const MHRP = 48; + pub const BHA = 49; + pub const ESP = 50; + pub const AH = 51; + pub const INLSP = 52; + pub const SWIPE = 53; + pub const NHRP = 54; + pub const MOBILE = 55; + pub const TLSP = 56; + pub const SKIP = 57; + pub const ICMPV6 = 58; + pub const NONE = 59; + pub const DSTOPTS = 60; + pub const AHIP = 61; + pub const CFTP = 62; + pub const HELLO = 63; + pub const SATEXPAK = 64; + pub const KRYPTOLAN = 65; + pub const RVD = 66; + pub const IPPC = 67; + pub const ADFS = 68; + pub const SATMON = 69; + pub const VISA = 70; + pub const IPCV = 71; + pub const CPNX = 72; + pub const CPHB = 73; + pub const WSN = 74; + pub const PVP = 75; + pub const BRSATMON = 76; + pub const ND = 77; + pub const WBMON = 78; + pub const WBEXPAK = 79; + pub const EON = 80; + pub const VMTP = 81; + pub const SVMTP = 82; + pub const VINES = 83; + pub const TTP = 84; + pub const IGP = 85; + pub const DGP = 86; + pub const TCF = 87; + pub const IGRP = 88; + pub const OSPFIGP = 89; + pub const SRPC = 90; + pub const LARP = 91; + pub const MTP = 92; + pub const AX25 = 93; + pub const IPEIP = 94; + pub const MICP = 95; + pub const SCCSP = 96; + pub const ETHERIP = 97; + pub const ENCAP = 98; + pub const APES = 99; + pub const GMTP = 100; + pub const IPCOMP = 108; + pub const PIM = 103; + pub const CARP = 112; + pub const PGM = 113; + pub const PFSYNC = 240; + pub const DIVERT = 254; + pub const MAX = 256; + pub const DONE = 257; + pub const UNKNOWN = 258; +}; + +pub const rlimit_resource = enum(c_int) { + CPU = 0, + FSIZE = 1, + DATA = 2, + STACK = 3, + CORE = 4, + RSS = 5, + MEMLOCK = 6, + NPROC = 7, + NOFILE = 8, + SBSIZE = 9, + VMEM = 10, + POSIXLOCKS = 11, + _, + + pub const AS: rlimit_resource = .VMEM; +}; + +pub const rlim_t = i64; + +pub const RLIM = struct { + /// No limit + pub const INFINITY: rlim_t = (1 << 63) - 1; + + pub const SAVED_MAX = INFINITY; + pub const SAVED_CUR = INFINITY; +}; + +pub const rlimit = extern struct { + /// Soft limit + cur: rlim_t, + /// Hard limit + max: rlim_t, +}; + +pub const SHUT = struct { + pub const RD = 0; + pub const WR = 1; + pub const RDWR = 2; +}; + +pub const nfds_t = u32; + +pub const pollfd = extern struct { + fd: fd_t, + events: i16, + revents: i16, +}; + +pub const POLL = struct { + /// Requestable events. + pub const IN = 0x0001; + pub const PRI = 0x0002; + pub const OUT = 0x0004; + pub const RDNORM = 0x0040; + pub const WRNORM = OUT; + pub const RDBAND = 0x0080; + pub const WRBAND = 0x0100; + + /// These events are set if they occur regardless of whether they were requested. + pub const ERR = 0x0008; + pub const HUP = 0x0010; + pub const NVAL = 0x0020; +}; diff --git a/lib/std/c/freebsd.zig b/lib/std/c/freebsd.zig index f7d1e07608..f65af3f915 100644 --- a/lib/std/c/freebsd.zig +++ b/lib/std/c/freebsd.zig @@ -1,5 +1,8 @@ const std = @import("../std.zig"); -usingnamespace std.c; +const builtin = @import("builtin"); +const maxInt = std.math.maxInt; +const iovec = std.os.iovec; +const iovec_const = std.os.iovec_const; extern "c" fn __error() *c_int; pub const _errno = __error; @@ -107,32 +110,1482 @@ pub const EAI = enum(c_int) { pub const EAI_MAX = 15; -/// get address to use bind() -pub const AI_PASSIVE = 0x00000001; +pub const AI = struct { + /// get address to use bind() + pub const PASSIVE = 0x00000001; + /// fill ai_canonname + pub const CANONNAME = 0x00000002; + /// prevent host name resolution + pub const NUMERICHOST = 0x00000004; + /// prevent service name resolution + pub const NUMERICSERV = 0x00000008; + /// valid flags for addrinfo (not a standard def, apps should not use it) + pub const MASK = (PASSIVE | CANONNAME | NUMERICHOST | NUMERICSERV | ADDRCONFIG | ALL | V4MAPPED); + /// IPv6 and IPv4-mapped (with V4MAPPED) + pub const ALL = 0x00000100; + /// accept IPv4-mapped if kernel supports + pub const V4MAPPED_CFG = 0x00000200; + /// only if any address is assigned + pub const ADDRCONFIG = 0x00000400; + /// accept IPv4-mapped IPv6 address + pub const V4MAPPED = 0x00000800; + /// special recommended flags for getipnodebyname + pub const DEFAULT = (V4MAPPED_CFG | ADDRCONFIG); +}; + +pub const blksize_t = i32; +pub const blkcnt_t = i64; +pub const clockid_t = i32; +pub const fsblkcnt_t = u64; +pub const fsfilcnt_t = u64; +pub const nlink_t = u64; +pub const fd_t = i32; +pub const pid_t = i32; +pub const uid_t = u32; +pub const gid_t = u32; +pub const mode_t = u16; +pub const off_t = i64; +pub const ino_t = u64; +pub const dev_t = u64; +pub const time_t = i64; +// The signedness is not constant across different architectures. +pub const clock_t = isize; + +pub const socklen_t = u32; +pub const suseconds_t = c_long; + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: i16, + flags: u16, + fflags: u32, + data: i64, + udata: usize, + // TODO ext +}; + +// Modes and flags for dlopen() +// include/dlfcn.h + +pub const RTLD = struct { + /// Bind function calls lazily. + pub const LAZY = 1; + /// Bind function calls immediately. + pub const NOW = 2; + pub const MODEMASK = 0x3; + /// Make symbols globally available. + pub const GLOBAL = 0x100; + /// Opposite of GLOBAL, and the default. + pub const LOCAL = 0; + /// Trace loaded objects and exit. + pub const TRACE = 0x200; + /// Do not remove members. + pub const NODELETE = 0x01000; + /// Do not load if not already loaded. + pub const NOLOAD = 0x02000; +}; +pub const dl_phdr_info = extern struct { + dlpi_addr: usize, + dlpi_name: ?[*:0]const u8, + dlpi_phdr: [*]std.elf.Phdr, + dlpi_phnum: u16, +}; + +pub const Flock = extern struct { + l_start: off_t, + l_len: off_t, + l_pid: pid_t, + l_type: i16, + l_whence: i16, + l_sysid: i32, + __unused: [4]u8, +}; + +pub const msghdr = extern struct { + /// optional address + msg_name: ?*sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const msghdr_const = extern struct { + /// optional address + msg_name: ?*const sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec_const, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const Stat = extern struct { + dev: dev_t, + ino: ino_t, + nlink: nlink_t, + + mode: mode_t, + __pad0: u16, + uid: uid_t, + gid: gid_t, + __pad1: u32, + rdev: dev_t, + + atim: timespec, + mtim: timespec, + ctim: timespec, + birthtim: timespec, + + size: off_t, + blocks: i64, + blksize: isize, + flags: u32, + gen: u64, + __spare: [10]u64, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; + +pub const timeval = extern struct { + /// seconds + tv_sec: time_t, + /// microseconds + tv_usec: suseconds_t, +}; + +pub const dirent = extern struct { + d_fileno: usize, + d_off: i64, + d_reclen: u16, + d_type: u8, + d_pad0: u8, + d_namlen: u16, + d_pad1: u16, + d_name: [256]u8, + + pub fn reclen(self: dirent) u16 { + return self.d_reclen; + } +}; + +pub const in_port_t = u16; +pub const sa_family_t = u8; + +pub const sockaddr = extern struct { + /// total length + len: u8, + /// address family + family: sa_family_t, + /// actually longer; address value + data: [14]u8, + + pub const storage = std.x.os.Socket.Address.Native.Storage; + + pub const in = extern struct { + len: u8 = @sizeOf(in), + family: sa_family_t = AF.INET, + port: in_port_t, + addr: u32, + zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, + }; + + pub const in6 = extern struct { + len: u8 = @sizeOf(in6), + family: sa_family_t = AF.INET6, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, + }; + + pub const un = extern struct { + len: u8 = @sizeOf(un), + family: sa_family_t = AF.UNIX, + path: [104]u8, + }; +}; + +pub const CTL = struct { + pub const KERN = 1; + pub const DEBUG = 5; +}; + +pub const KERN = struct { + pub const PROC = 14; // struct: process entries + pub const PROC_PATHNAME = 12; // path to executable + pub const IOV_MAX = 35; +}; + +pub const PATH_MAX = 1024; +pub const IOV_MAX = KERN.IOV_MAX; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT = struct { + pub const NONE = 0; + pub const READ = 1; + pub const WRITE = 2; + pub const EXEC = 4; +}; + +pub const CLOCK = struct { + pub const REALTIME = 0; + pub const VIRTUAL = 1; + pub const PROF = 2; + pub const MONOTONIC = 4; + pub const UPTIME = 5; + pub const UPTIME_PRECISE = 7; + pub const UPTIME_FAST = 8; + pub const REALTIME_PRECISE = 9; + pub const REALTIME_FAST = 10; + pub const MONOTONIC_PRECISE = 11; + pub const MONOTONIC_FAST = 12; + pub const SECOND = 13; + pub const THREAD_CPUTIME_ID = 14; + pub const PROCESS_CPUTIME_ID = 15; +}; + +pub const MAP = struct { + pub const FAILED = @intToPtr(*c_void, maxInt(usize)); + pub const SHARED = 0x0001; + pub const PRIVATE = 0x0002; + pub const FIXED = 0x0010; + pub const STACK = 0x0400; + pub const NOSYNC = 0x0800; + pub const ANON = 0x1000; + pub const ANONYMOUS = ANON; + pub const FILE = 0; + + pub const GUARD = 0x00002000; + pub const EXCL = 0x00004000; + pub const NOCORE = 0x00020000; + pub const PREFAULT_READ = 0x00040000; + pub const @"32BIT" = 0x00080000; +}; + +pub const W = struct { + pub const NOHANG = 1; + pub const UNTRACED = 2; + pub const STOPPED = UNTRACED; + pub const CONTINUED = 4; + pub const NOWAIT = 8; + pub const EXITED = 16; + pub const TRAPPED = 32; + + pub fn EXITSTATUS(s: u32) u8 { + return @intCast(u8, (s & 0xff00) >> 8); + } + pub fn TERMSIG(s: u32) u32 { + return s & 0x7f; + } + pub fn STOPSIG(s: u32) u32 { + return EXITSTATUS(s); + } + pub fn IFEXITED(s: u32) bool { + return TERMSIG(s) == 0; + } + pub fn IFSTOPPED(s: u32) bool { + return @truncate(u16, (((s & 0xffff) *% 0x10001) >> 8)) > 0x7f00; + } + pub fn IFSIGNALED(s: u32) bool { + return (s & 0xffff) -% 1 < 0xff; + } +}; + +pub const SA = struct { + pub const ONSTACK = 0x0001; + pub const RESTART = 0x0002; + pub const RESETHAND = 0x0004; + pub const NOCLDSTOP = 0x0008; + pub const NODEFER = 0x0010; + pub const NOCLDWAIT = 0x0020; + pub const SIGINFO = 0x0040; +}; + +pub const SIG = struct { + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const TRAP = 5; + pub const ABRT = 6; + pub const IOT = ABRT; + pub const EMT = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const BUS = 10; + pub const SEGV = 11; + pub const SYS = 12; + pub const PIPE = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const URG = 16; + pub const STOP = 17; + pub const TSTP = 18; + pub const CONT = 19; + pub const CHLD = 20; + pub const TTIN = 21; + pub const TTOU = 22; + pub const IO = 23; + pub const XCPU = 24; + pub const XFSZ = 25; + pub const VTALRM = 26; + pub const PROF = 27; + pub const WINCH = 28; + pub const INFO = 29; + pub const USR1 = 30; + pub const USR2 = 31; + pub const THR = 32; + pub const LWP = THR; + pub const LIBRT = 33; + + pub const RTMIN = 65; + pub const RTMAX = 126; + + pub const BLOCK = 1; + pub const UNBLOCK = 2; + pub const SETMASK = 3; + + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + + pub const WORDS = 4; + pub const MAXSIG = 128; + + pub inline fn IDX(sig: usize) usize { + return sig - 1; + } + pub inline fn WORD(sig: usize) usize { + return IDX(sig) >> 5; + } + pub inline fn BIT(sig: usize) usize { + return 1 << (IDX(sig) & 31); + } + pub inline fn VALID(sig: usize) usize { + return sig <= MAXSIG and sig > 0; + } +}; +pub const sigval = extern union { + int: c_int, + ptr: ?*c_void, +}; + +pub const sigset_t = extern struct { + __bits: [SIG.WORDS]u32, +}; + +pub const empty_sigset = sigset_t{ .__bits = [_]u32{0} ** SIG.WORDS }; + +// access function +pub const F_OK = 0; // test for existence of file +pub const X_OK = 1; // test for execute or search permission +pub const W_OK = 2; // test for write permission +pub const R_OK = 4; // test for read permission + +pub const O = struct { + pub const RDONLY = 0x0000; + pub const WRONLY = 0x0001; + pub const RDWR = 0x0002; + pub const ACCMODE = 0x0003; + + pub const SHLOCK = 0x0010; + pub const EXLOCK = 0x0020; + + pub const CREAT = 0x0200; + pub const EXCL = 0x0800; + pub const NOCTTY = 0x8000; + pub const TRUNC = 0x0400; + pub const APPEND = 0x0008; + pub const NONBLOCK = 0x0004; + pub const DSYNC = 0o10000; + pub const SYNC = 0x0080; + pub const RSYNC = 0o4010000; + pub const DIRECTORY = 0x20000; + pub const NOFOLLOW = 0x0100; + pub const CLOEXEC = 0x00100000; + + pub const ASYNC = 0x0040; + pub const DIRECT = 0x00010000; + pub const NOATIME = 0o1000000; + pub const PATH = 0o10000000; + pub const TMPFILE = 0o20200000; + pub const NDELAY = NONBLOCK; +}; + +pub const F = struct { + pub const DUPFD = 0; + pub const GETFD = 1; + pub const SETFD = 2; + pub const GETFL = 3; + pub const SETFL = 4; + + pub const GETOWN = 5; + pub const SETOWN = 6; + + pub const GETLK = 11; + pub const SETLK = 12; + pub const SETLKW = 13; + + pub const RDLCK = 1; + pub const WRLCK = 3; + pub const UNLCK = 2; + + pub const SETOWN_EX = 15; + pub const GETOWN_EX = 16; + + pub const GETOWNER_UIDS = 17; +}; + +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const UN = 8; + pub const NB = 4; +}; + +pub const FD_CLOEXEC = 1; + +pub const SEEK = struct { + pub const SET = 0; + pub const CUR = 1; + pub const END = 2; +}; + +pub const SOCK = struct { + pub const STREAM = 1; + pub const DGRAM = 2; + pub const RAW = 3; + pub const RDM = 4; + pub const SEQPACKET = 5; + + pub const CLOEXEC = 0x10000000; + pub const NONBLOCK = 0x20000000; +}; + +pub const SO = struct { + pub const DEBUG = 0x00000001; + pub const ACCEPTCONN = 0x00000002; + pub const REUSEADDR = 0x00000004; + pub const KEEPALIVE = 0x00000008; + pub const DONTROUTE = 0x00000010; + pub const BROADCAST = 0x00000020; + pub const USELOOPBACK = 0x00000040; + pub const LINGER = 0x00000080; + pub const OOBINLINE = 0x00000100; + pub const REUSEPORT = 0x00000200; + pub const TIMESTAMP = 0x00000400; + pub const NOSIGPIPE = 0x00000800; + pub const ACCEPTFILTER = 0x00001000; + pub const BINTIME = 0x00002000; + pub const NO_OFFLOAD = 0x00004000; + pub const NO_DDP = 0x00008000; + pub const REUSEPORT_LB = 0x00010000; + + pub const SNDBUF = 0x1001; + pub const RCVBUF = 0x1002; + pub const SNDLOWAT = 0x1003; + pub const RCVLOWAT = 0x1004; + pub const SNDTIMEO = 0x1005; + pub const RCVTIMEO = 0x1006; + pub const ERROR = 0x1007; + pub const TYPE = 0x1008; + pub const LABEL = 0x1009; + pub const PEERLABEL = 0x1010; + pub const LISTENQLIMIT = 0x1011; + pub const LISTENQLEN = 0x1012; + pub const LISTENINCQLEN = 0x1013; + pub const SETFIB = 0x1014; + pub const USER_COOKIE = 0x1015; + pub const PROTOCOL = 0x1016; + pub const PROTOTYPE = PROTOCOL; + pub const TS_CLOCK = 0x1017; + pub const MAX_PACING_RATE = 0x1018; + pub const DOMAIN = 0x1019; +}; + +pub const SOL = struct { + pub const SOCKET = 0xffff; +}; + +pub const PF = struct { + pub const UNSPEC = AF.UNSPEC; + pub const LOCAL = AF.LOCAL; + pub const UNIX = PF.LOCAL; + pub const INET = AF.INET; + pub const IMPLINK = AF.IMPLINK; + pub const PUP = AF.PUP; + pub const CHAOS = AF.CHAOS; + pub const NETBIOS = AF.NETBIOS; + pub const ISO = AF.ISO; + pub const OSI = AF.ISO; + pub const ECMA = AF.ECMA; + pub const DATAKIT = AF.DATAKIT; + pub const CCITT = AF.CCITT; + pub const DECnet = AF.DECnet; + pub const DLI = AF.DLI; + pub const LAT = AF.LAT; + pub const HYLINK = AF.HYLINK; + pub const APPLETALK = AF.APPLETALK; + pub const ROUTE = AF.ROUTE; + pub const LINK = AF.LINK; + pub const XTP = AF.pseudo_XTP; + pub const COIP = AF.COIP; + pub const CNT = AF.CNT; + pub const SIP = AF.SIP; + pub const IPX = AF.IPX; + pub const RTIP = AF.pseudo_RTIP; + pub const PIP = AF.pseudo_PIP; + pub const ISDN = AF.ISDN; + pub const KEY = AF.pseudo_KEY; + pub const INET6 = AF.pseudo_INET6; + pub const NATM = AF.NATM; + pub const ATM = AF.ATM; + pub const NETGRAPH = AF.NETGRAPH; + pub const SLOW = AF.SLOW; + pub const SCLUSTER = AF.SCLUSTER; + pub const ARP = AF.ARP; + pub const BLUETOOTH = AF.BLUETOOTH; + pub const IEEE80211 = AF.IEEE80211; + pub const INET_SDP = AF.INET_SDP; + pub const INET6_SDP = AF.INET6_SDP; + pub const MAX = AF.MAX; +}; + +pub const AF = struct { + pub const UNSPEC = 0; + pub const UNIX = 1; + pub const LOCAL = UNIX; + pub const FILE = LOCAL; + pub const INET = 2; + pub const IMPLINK = 3; + pub const PUP = 4; + pub const CHAOS = 5; + pub const NETBIOS = 6; + pub const ISO = 7; + pub const OSI = ISO; + pub const ECMA = 8; + pub const DATAKIT = 9; + pub const CCITT = 10; + pub const SNA = 11; + pub const DECnet = 12; + pub const DLI = 13; + pub const LAT = 14; + pub const HYLINK = 15; + pub const APPLETALK = 16; + pub const ROUTE = 17; + pub const LINK = 18; + pub const pseudo_XTP = 19; + pub const COIP = 20; + pub const CNT = 21; + pub const pseudo_RTIP = 22; + pub const IPX = 23; + pub const SIP = 24; + pub const pseudo_PIP = 25; + pub const ISDN = 26; + pub const E164 = ISDN; + pub const pseudo_KEY = 27; + pub const INET6 = 28; + pub const NATM = 29; + pub const ATM = 30; + pub const pseudo_HDRCMPLT = 31; + pub const NETGRAPH = 32; + pub const SLOW = 33; + pub const SCLUSTER = 34; + pub const ARP = 35; + pub const BLUETOOTH = 36; + pub const IEEE80211 = 37; + pub const INET_SDP = 40; + pub const INET6_SDP = 42; + pub const MAX = 42; +}; + +pub const DT = struct { + pub const UNKNOWN = 0; + pub const FIFO = 1; + pub const CHR = 2; + pub const DIR = 4; + pub const BLK = 6; + pub const REG = 8; + pub const LNK = 10; + pub const SOCK = 12; + pub const WHT = 14; +}; + +/// add event to kq (implies enable) +pub const EV_ADD = 0x0001; + +/// delete event from kq +pub const EV_DELETE = 0x0002; + +/// enable event +pub const EV_ENABLE = 0x0004; + +/// disable event (not reported) +pub const EV_DISABLE = 0x0008; -/// fill ai_canonname -pub const AI_CANONNAME = 0x00000002; +/// only report one occurrence +pub const EV_ONESHOT = 0x0010; -/// prevent host name resolution -pub const AI_NUMERICHOST = 0x00000004; +/// clear event state after reporting +pub const EV_CLEAR = 0x0020; -/// prevent service name resolution -pub const AI_NUMERICSERV = 0x00000008; +/// error, event data contains errno +pub const EV_ERROR = 0x4000; -/// valid flags for addrinfo (not a standard def, apps should not use it) -pub const AI_MASK = (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV | AI_ADDRCONFIG | AI_ALL | AI_V4MAPPED); +/// force immediate event output +/// ... with or without EV_ERROR +/// ... use KEVENT_FLAG_ERROR_EVENTS +/// on syscalls supporting flags +pub const EV_RECEIPT = 0x0040; -/// IPv6 and IPv4-mapped (with AI_V4MAPPED) -pub const AI_ALL = 0x00000100; +/// disable event after reporting +pub const EV_DISPATCH = 0x0080; -/// accept IPv4-mapped if kernel supports -pub const AI_V4MAPPED_CFG = 0x00000200; +pub const EVFILT_READ = -1; +pub const EVFILT_WRITE = -2; -/// only if any address is assigned -pub const AI_ADDRCONFIG = 0x00000400; +/// attached to aio requests +pub const EVFILT_AIO = -3; -/// accept IPv4-mapped IPv6 address -pub const AI_V4MAPPED = 0x00000800; +/// attached to vnodes +pub const EVFILT_VNODE = -4; -/// special recommended flags for getipnodebyname -pub const AI_DEFAULT = (AI_V4MAPPED_CFG | AI_ADDRCONFIG); +/// attached to struct proc +pub const EVFILT_PROC = -5; + +/// attached to struct proc +pub const EVFILT_SIGNAL = -6; + +/// timers +pub const EVFILT_TIMER = -7; + +/// Process descriptors +pub const EVFILT_PROCDESC = -8; + +/// Filesystem events +pub const EVFILT_FS = -9; + +pub const EVFILT_LIO = -10; + +/// User events +pub const EVFILT_USER = -11; + +/// Sendfile events +pub const EVFILT_SENDFILE = -12; + +pub const EVFILT_EMPTY = -13; + +/// On input, NOTE_TRIGGER causes the event to be triggered for output. +pub const NOTE_TRIGGER = 0x01000000; + +/// ignore input fflags +pub const NOTE_FFNOP = 0x00000000; + +/// and fflags +pub const NOTE_FFAND = 0x40000000; + +/// or fflags +pub const NOTE_FFOR = 0x80000000; + +/// copy fflags +pub const NOTE_FFCOPY = 0xc0000000; + +/// mask for operations +pub const NOTE_FFCTRLMASK = 0xc0000000; +pub const NOTE_FFLAGSMASK = 0x00ffffff; + +/// low water mark +pub const NOTE_LOWAT = 0x00000001; + +/// behave like poll() +pub const NOTE_FILE_POLL = 0x00000002; + +/// vnode was removed +pub const NOTE_DELETE = 0x00000001; + +/// data contents changed +pub const NOTE_WRITE = 0x00000002; + +/// size increased +pub const NOTE_EXTEND = 0x00000004; + +/// attributes changed +pub const NOTE_ATTRIB = 0x00000008; + +/// link count changed +pub const NOTE_LINK = 0x00000010; + +/// vnode was renamed +pub const NOTE_RENAME = 0x00000020; + +/// vnode access was revoked +pub const NOTE_REVOKE = 0x00000040; + +/// vnode was opened +pub const NOTE_OPEN = 0x00000080; + +/// file closed, fd did not allow write +pub const NOTE_CLOSE = 0x00000100; + +/// file closed, fd did allow write +pub const NOTE_CLOSE_WRITE = 0x00000200; + +/// file was read +pub const NOTE_READ = 0x00000400; + +/// process exited +pub const NOTE_EXIT = 0x80000000; + +/// process forked +pub const NOTE_FORK = 0x40000000; + +/// process exec'd +pub const NOTE_EXEC = 0x20000000; + +/// mask for signal & exit status +pub const NOTE_PDATAMASK = 0x000fffff; +pub const NOTE_PCTRLMASK = (~NOTE_PDATAMASK); + +/// data is seconds +pub const NOTE_SECONDS = 0x00000001; + +/// data is milliseconds +pub const NOTE_MSECONDS = 0x00000002; + +/// data is microseconds +pub const NOTE_USECONDS = 0x00000004; + +/// data is nanoseconds +pub const NOTE_NSECONDS = 0x00000008; + +/// timeout is absolute +pub const NOTE_ABSTIME = 0x00000010; + +pub const T = struct { + pub const IOCEXCL = 0x2000740d; + pub const IOCNXCL = 0x2000740e; + pub const IOCSCTTY = 0x20007461; + pub const IOCGPGRP = 0x40047477; + pub const IOCSPGRP = 0x80047476; + pub const IOCOUTQ = 0x40047473; + pub const IOCSTI = 0x80017472; + pub const IOCGWINSZ = 0x40087468; + pub const IOCSWINSZ = 0x80087467; + pub const IOCMGET = 0x4004746a; + pub const IOCMBIS = 0x8004746c; + pub const IOCMBIC = 0x8004746b; + pub const IOCMSET = 0x8004746d; + pub const FIONREAD = 0x4004667f; + pub const IOCCONS = 0x80047462; + pub const IOCPKT = 0x80047470; + pub const FIONBIO = 0x8004667e; + pub const IOCNOTTY = 0x20007471; + pub const IOCSETD = 0x8004741b; + pub const IOCGETD = 0x4004741a; + pub const IOCSBRK = 0x2000747b; + pub const IOCCBRK = 0x2000747a; + pub const IOCGSID = 0x40047463; + pub const IOCGPTN = 0x4004740f; + pub const IOCSIG = 0x2004745f; +}; + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +const NSIG = 32; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = extern struct { + pub const handler_fn = fn (c_int) callconv(.C) void; + pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; + + /// signal handler + handler: extern union { + handler: ?handler_fn, + sigaction: ?sigaction_fn, + }, + + /// see signal options + flags: c_uint, + + /// signal mask to apply + mask: sigset_t, +}; + +pub const siginfo_t = extern struct { + signo: c_int, + errno: c_int, + code: c_int, + pid: pid_t, + uid: uid_t, + status: c_int, + addr: ?*c_void, + value: sigval, + reason: extern union { + fault: extern struct { + trapno: c_int, + }, + timer: extern struct { + timerid: c_int, + overrun: c_int, + }, + mesgq: extern struct { + mqd: c_int, + }, + poll: extern struct { + band: c_long, + }, + spare: extern struct { + spare1: c_long, + spare2: [7]c_int, + }, + }, +}; + +pub usingnamespace switch (builtin.cpu.arch) { + .x86_64 => struct { + pub const ucontext_t = extern struct { + sigmask: sigset_t, + mcontext: mcontext_t, + link: ?*ucontext_t, + stack: stack_t, + flags: c_int, + __spare__: [4]c_int, + }; + + /// XXX x86_64 specific + pub const mcontext_t = extern struct { + onstack: u64, + rdi: u64, + rsi: u64, + rdx: u64, + rcx: u64, + r8: u64, + r9: u64, + rax: u64, + rbx: u64, + rbp: u64, + r10: u64, + r11: u64, + r12: u64, + r13: u64, + r14: u64, + r15: u64, + trapno: u32, + fs: u16, + gs: u16, + addr: u64, + flags: u32, + es: u16, + ds: u16, + err: u64, + rip: u64, + cs: u64, + rflags: u64, + rsp: u64, + ss: u64, + }; + }, + else => struct {}, +}; + +pub const E = enum(u16) { + /// No error occurred. + SUCCESS = 0, + + PERM = 1, // Operation not permitted + NOENT = 2, // No such file or directory + SRCH = 3, // No such process + INTR = 4, // Interrupted system call + IO = 5, // Input/output error + NXIO = 6, // Device not configured + @"2BIG" = 7, // Argument list too long + NOEXEC = 8, // Exec format error + BADF = 9, // Bad file descriptor + CHILD = 10, // No child processes + DEADLK = 11, // Resource deadlock avoided + // 11 was AGAIN + NOMEM = 12, // Cannot allocate memory + ACCES = 13, // Permission denied + FAULT = 14, // Bad address + NOTBLK = 15, // Block device required + BUSY = 16, // Device busy + EXIST = 17, // File exists + XDEV = 18, // Cross-device link + NODEV = 19, // Operation not supported by device + NOTDIR = 20, // Not a directory + ISDIR = 21, // Is a directory + INVAL = 22, // Invalid argument + NFILE = 23, // Too many open files in system + MFILE = 24, // Too many open files + NOTTY = 25, // Inappropriate ioctl for device + TXTBSY = 26, // Text file busy + FBIG = 27, // File too large + NOSPC = 28, // No space left on device + SPIPE = 29, // Illegal seek + ROFS = 30, // Read-only filesystem + MLINK = 31, // Too many links + PIPE = 32, // Broken pipe + + // math software + DOM = 33, // Numerical argument out of domain + RANGE = 34, // Result too large + + // non-blocking and interrupt i/o + + /// Resource temporarily unavailable + /// This code is also used for `WOULDBLOCK`: operation would block. + AGAIN = 35, + INPROGRESS = 36, // Operation now in progress + ALREADY = 37, // Operation already in progress + + // ipc/network software -- argument errors + NOTSOCK = 38, // Socket operation on non-socket + DESTADDRREQ = 39, // Destination address required + MSGSIZE = 40, // Message too long + PROTOTYPE = 41, // Protocol wrong type for socket + NOPROTOOPT = 42, // Protocol not available + PROTONOSUPPORT = 43, // Protocol not supported + SOCKTNOSUPPORT = 44, // Socket type not supported + /// Operation not supported + /// This code is also used for `NOTSUP`. + OPNOTSUPP = 45, + PFNOSUPPORT = 46, // Protocol family not supported + AFNOSUPPORT = 47, // Address family not supported by protocol family + ADDRINUSE = 48, // Address already in use + ADDRNOTAVAIL = 49, // Can't assign requested address + + // ipc/network software -- operational errors + NETDOWN = 50, // Network is down + NETUNREACH = 51, // Network is unreachable + NETRESET = 52, // Network dropped connection on reset + CONNABORTED = 53, // Software caused connection abort + CONNRESET = 54, // Connection reset by peer + NOBUFS = 55, // No buffer space available + ISCONN = 56, // Socket is already connected + NOTCONN = 57, // Socket is not connected + SHUTDOWN = 58, // Can't send after socket shutdown + TOOMANYREFS = 59, // Too many references: can't splice + TIMEDOUT = 60, // Operation timed out + CONNREFUSED = 61, // Connection refused + + LOOP = 62, // Too many levels of symbolic links + NAMETOOLONG = 63, // File name too long + + // should be rearranged + HOSTDOWN = 64, // Host is down + HOSTUNREACH = 65, // No route to host + NOTEMPTY = 66, // Directory not empty + + // quotas & mush + PROCLIM = 67, // Too many processes + USERS = 68, // Too many users + DQUOT = 69, // Disc quota exceeded + + // Network File System + STALE = 70, // Stale NFS file handle + REMOTE = 71, // Too many levels of remote in path + BADRPC = 72, // RPC struct is bad + RPCMISMATCH = 73, // RPC version wrong + PROGUNAVAIL = 74, // RPC prog. not avail + PROGMISMATCH = 75, // Program version wrong + PROCUNAVAIL = 76, // Bad procedure for program + + NOLCK = 77, // No locks available + NOSYS = 78, // Function not implemented + + FTYPE = 79, // Inappropriate file type or format + AUTH = 80, // Authentication error + NEEDAUTH = 81, // Need authenticator + IDRM = 82, // Identifier removed + NOMSG = 83, // No message of desired type + OVERFLOW = 84, // Value too large to be stored in data type + CANCELED = 85, // Operation canceled + ILSEQ = 86, // Illegal byte sequence + NOATTR = 87, // Attribute not found + + DOOFUS = 88, // Programming error + + BADMSG = 89, // Bad message + MULTIHOP = 90, // Multihop attempted + NOLINK = 91, // Link has been severed + PROTO = 92, // Protocol error + + NOTCAPABLE = 93, // Capabilities insufficient + CAPMODE = 94, // Not permitted in capability mode + NOTRECOVERABLE = 95, // State not recoverable + OWNERDEAD = 96, // Previous owner died + _, +}; + +pub const MINSIGSTKSZ = switch (builtin.cpu.arch) { + .i386, .x86_64 => 2048, + .arm, .aarch64 => 4096, + else => @compileError("MINSIGSTKSZ not defined for this architecture"), +}; +pub const SIGSTKSZ = MINSIGSTKSZ + 32768; + +pub const SS_ONSTACK = 1; +pub const SS_DISABLE = 4; + +pub const stack_t = extern struct { + sp: [*]u8, + size: isize, + flags: i32, +}; + +pub const S = struct { + pub const IFMT = 0o170000; + + pub const IFIFO = 0o010000; + pub const IFCHR = 0o020000; + pub const IFDIR = 0o040000; + pub const IFBLK = 0o060000; + pub const IFREG = 0o100000; + pub const IFLNK = 0o120000; + pub const IFSOCK = 0o140000; + pub const IFWHT = 0o160000; + + pub const ISUID = 0o4000; + pub const ISGID = 0o2000; + pub const ISVTX = 0o1000; + pub const IRWXU = 0o700; + pub const IRUSR = 0o400; + pub const IWUSR = 0o200; + pub const IXUSR = 0o100; + pub const IRWXG = 0o070; + pub const IRGRP = 0o040; + pub const IWGRP = 0o020; + pub const IXGRP = 0o010; + pub const IRWXO = 0o007; + pub const IROTH = 0o004; + pub const IWOTH = 0o002; + pub const IXOTH = 0o001; + + pub fn ISFIFO(m: u32) bool { + return m & IFMT == IFIFO; + } + + pub fn ISCHR(m: u32) bool { + return m & IFMT == IFCHR; + } + + pub fn ISDIR(m: u32) bool { + return m & IFMT == IFDIR; + } + + pub fn ISBLK(m: u32) bool { + return m & IFMT == IFBLK; + } + + pub fn ISREG(m: u32) bool { + return m & IFMT == IFREG; + } + + pub fn ISLNK(m: u32) bool { + return m & IFMT == IFLNK; + } + + pub fn ISSOCK(m: u32) bool { + return m & IFMT == IFSOCK; + } + + pub fn IWHT(m: u32) bool { + return m & IFMT == IFWHT; + } +}; + +pub const HOST_NAME_MAX = 255; + +pub const AT = struct { + /// Magic value that specify the use of the current working directory + /// to determine the target of relative file paths in the openat() and + /// similar syscalls. + pub const FDCWD = -100; + /// Check access using effective user and group ID + pub const EACCESS = 0x0100; + /// Do not follow symbolic links + pub const SYMLINK_NOFOLLOW = 0x0200; + /// Follow symbolic link + pub const SYMLINK_FOLLOW = 0x0400; + /// Remove directory instead of file + pub const REMOVEDIR = 0x0800; + /// Fail if not under dirfd + pub const BENEATH = 0x1000; +}; + +pub const addrinfo = extern struct { + flags: i32, + family: i32, + socktype: i32, + protocol: i32, + addrlen: socklen_t, + canonname: ?[*:0]u8, + addr: ?*sockaddr, + next: ?*addrinfo, +}; + +pub const IPPROTO = struct { + /// dummy for IP + pub const IP = 0; + /// control message protocol + pub const ICMP = 1; + /// tcp + pub const TCP = 6; + /// user datagram protocol + pub const UDP = 17; + /// IP6 header + pub const IPV6 = 41; + /// raw IP packet + pub const RAW = 255; + /// IP6 hop-by-hop options + pub const HOPOPTS = 0; + /// group mgmt protocol + pub const IGMP = 2; + /// gateway^2 (deprecated) + pub const GGP = 3; + /// IPv4 encapsulation + pub const IPV4 = 4; + /// for compatibility + pub const IPIP = IPV4; + /// Stream protocol II + pub const ST = 7; + /// exterior gateway protocol + pub const EGP = 8; + /// private interior gateway + pub const PIGP = 9; + /// BBN RCC Monitoring + pub const RCCMON = 10; + /// network voice protocol + pub const NVPII = 11; + /// pup + pub const PUP = 12; + /// Argus + pub const ARGUS = 13; + /// EMCON + pub const EMCON = 14; + /// Cross Net Debugger + pub const XNET = 15; + /// Chaos + pub const CHAOS = 16; + /// Multiplexing + pub const MUX = 18; + /// DCN Measurement Subsystems + pub const MEAS = 19; + /// Host Monitoring + pub const HMP = 20; + /// Packet Radio Measurement + pub const PRM = 21; + /// xns idp + pub const IDP = 22; + /// Trunk-1 + pub const TRUNK1 = 23; + /// Trunk-2 + pub const TRUNK2 = 24; + /// Leaf-1 + pub const LEAF1 = 25; + /// Leaf-2 + pub const LEAF2 = 26; + /// Reliable Data + pub const RDP = 27; + /// Reliable Transaction + pub const IRTP = 28; + /// tp-4 w/ class negotiation + pub const TP = 29; + /// Bulk Data Transfer + pub const BLT = 30; + /// Network Services + pub const NSP = 31; + /// Merit Internodal + pub const INP = 32; + /// Datagram Congestion Control Protocol + pub const DCCP = 33; + /// Third Party Connect + pub const @"3PC" = 34; + /// InterDomain Policy Routing + pub const IDPR = 35; + /// XTP + pub const XTP = 36; + /// Datagram Delivery + pub const DDP = 37; + /// Control Message Transport + pub const CMTP = 38; + /// TP++ Transport + pub const TPXX = 39; + /// IL transport protocol + pub const IL = 40; + /// Source Demand Routing + pub const SDRP = 42; + /// IP6 routing header + pub const ROUTING = 43; + /// IP6 fragmentation header + pub const FRAGMENT = 44; + /// InterDomain Routing + pub const IDRP = 45; + /// resource reservation + pub const RSVP = 46; + /// General Routing Encap. + pub const GRE = 47; + /// Mobile Host Routing + pub const MHRP = 48; + /// BHA + pub const BHA = 49; + /// IP6 Encap Sec. Payload + pub const ESP = 50; + /// IP6 Auth Header + pub const AH = 51; + /// Integ. Net Layer Security + pub const INLSP = 52; + /// IP with encryption + pub const SWIPE = 53; + /// Next Hop Resolution + pub const NHRP = 54; + /// IP Mobility + pub const MOBILE = 55; + /// Transport Layer Security + pub const TLSP = 56; + /// SKIP + pub const SKIP = 57; + /// ICMP6 + pub const ICMPV6 = 58; + /// IP6 no next header + pub const NONE = 59; + /// IP6 destination option + pub const DSTOPTS = 60; + /// any host internal protocol + pub const AHIP = 61; + /// CFTP + pub const CFTP = 62; + /// "hello" routing protocol + pub const HELLO = 63; + /// SATNET/Backroom EXPAK + pub const SATEXPAK = 64; + /// Kryptolan + pub const KRYPTOLAN = 65; + /// Remote Virtual Disk + pub const RVD = 66; + /// Pluribus Packet Core + pub const IPPC = 67; + /// Any distributed FS + pub const ADFS = 68; + /// Satnet Monitoring + pub const SATMON = 69; + /// VISA Protocol + pub const VISA = 70; + /// Packet Core Utility + pub const IPCV = 71; + /// Comp. Prot. Net. Executive + pub const CPNX = 72; + /// Comp. Prot. HeartBeat + pub const CPHB = 73; + /// Wang Span Network + pub const WSN = 74; + /// Packet Video Protocol + pub const PVP = 75; + /// BackRoom SATNET Monitoring + pub const BRSATMON = 76; + /// Sun net disk proto (temp.) + pub const ND = 77; + /// WIDEBAND Monitoring + pub const WBMON = 78; + /// WIDEBAND EXPAK + pub const WBEXPAK = 79; + /// ISO cnlp + pub const EON = 80; + /// VMTP + pub const VMTP = 81; + /// Secure VMTP + pub const SVMTP = 82; + /// Banyon VINES + pub const VINES = 83; + /// TTP + pub const TTP = 84; + /// NSFNET-IGP + pub const IGP = 85; + /// dissimilar gateway prot. + pub const DGP = 86; + /// TCF + pub const TCF = 87; + /// Cisco/GXS IGRP + pub const IGRP = 88; + /// OSPFIGP + pub const OSPFIGP = 89; + /// Strite RPC protocol + pub const SRPC = 90; + /// Locus Address Resoloution + pub const LARP = 91; + /// Multicast Transport + pub const MTP = 92; + /// AX.25 Frames + pub const AX25 = 93; + /// IP encapsulated in IP + pub const IPEIP = 94; + /// Mobile Int.ing control + pub const MICP = 95; + /// Semaphore Comm. security + pub const SCCSP = 96; + /// Ethernet IP encapsulation + pub const ETHERIP = 97; + /// encapsulation header + pub const ENCAP = 98; + /// any private encr. scheme + pub const APES = 99; + /// GMTP + pub const GMTP = 100; + /// payload compression (IPComp) + pub const IPCOMP = 108; + /// SCTP + pub const SCTP = 132; + /// IPv6 Mobility Header + pub const MH = 135; + /// UDP-Lite + pub const UDPLITE = 136; + /// IP6 Host Identity Protocol + pub const HIP = 139; + /// IP6 Shim6 Protocol + pub const SHIM6 = 140; + /// Protocol Independent Mcast + pub const PIM = 103; + /// CARP + pub const CARP = 112; + /// PGM + pub const PGM = 113; + /// MPLS-in-IP + pub const MPLS = 137; + /// PFSYNC + pub const PFSYNC = 240; + /// Reserved + pub const RESERVED_253 = 253; + /// Reserved + pub const RESERVED_254 = 254; +}; + +pub const rlimit_resource = enum(c_int) { + CPU = 0, + FSIZE = 1, + DATA = 2, + STACK = 3, + CORE = 4, + RSS = 5, + MEMLOCK = 6, + NPROC = 7, + NOFILE = 8, + SBSIZE = 9, + VMEM = 10, + NPTS = 11, + SWAP = 12, + KQUEUES = 13, + UMTXP = 14, + _, + + pub const AS: rlimit_resource = .VMEM; +}; + +pub const rlim_t = i64; + +pub const RLIM = struct { + /// No limit + pub const INFINITY: rlim_t = (1 << 63) - 1; + + pub const SAVED_MAX = INFINITY; + pub const SAVED_CUR = INFINITY; +}; + +pub const rlimit = extern struct { + /// Soft limit + cur: rlim_t, + /// Hard limit + max: rlim_t, +}; + +pub const SHUT = struct { + pub const RD = 0; + pub const WR = 1; + pub const RDWR = 2; +}; + +pub const nfds_t = u32; + +pub const pollfd = extern struct { + fd: fd_t, + events: i16, + revents: i16, +}; + +pub const POLL = struct { + /// any readable data available. + pub const IN = 0x0001; + /// OOB/Urgent readable data. + pub const PRI = 0x0002; + /// file descriptor is writeable. + pub const OUT = 0x0004; + /// non-OOB/URG data available. + pub const RDNORM = 0x0040; + /// no write type differentiation. + pub const WRNORM = OUT; + /// OOB/Urgent readable data. + pub const RDBAND = 0x0080; + /// OOB/Urgent data can be written. + pub const WRBAND = 0x0100; + /// like IN, except ignore EOF. + pub const INIGNEOF = 0x2000; + /// some poll error occurred. + pub const ERR = 0x0008; + /// file descriptor was "hung up". + pub const HUP = 0x0010; + /// requested events "invalid". + pub const NVAL = 0x0020; + + pub const STANDARD = IN | PRI | OUT | RDNORM | RDBAND | WRBAND | ERR | HUP | NVAL; +}; diff --git a/lib/std/c/haiku.zig b/lib/std/c/haiku.zig index be6c180bed..1ad51cfadd 100644 --- a/lib/std/c/haiku.zig +++ b/lib/std/c/haiku.zig @@ -1,8 +1,8 @@ -// const std = @import("../std.zig"); -const builtin = std.builtin; - -usingnamespace std.c; +const builtin = @import("builtin"); +const maxInt = std.math.maxInt; +const iovec = std.os.iovec; +const iovec_const = std.os.iovec_const; extern "c" fn _errnop() *c_int; @@ -19,7 +19,7 @@ pub extern "c" fn _get_next_image_info(team: c_int, cookie: *i32, image_info: *i pub extern "c" fn _kern_read_dir(fd: c_int, buf_ptr: [*]u8, nbytes: usize, maxcount: u32) usize; -pub extern "c" fn _kern_read_stat(fd: c_int, path_ptr: [*]u8, traverse_link: bool, libc_stat: *libc_stat, stat_size: i32) usize; +pub extern "c" fn _kern_read_stat(fd: c_int, path_ptr: [*]u8, traverse_link: bool, st: *Stat, stat_size: i32) usize; pub extern "c" fn _kern_get_current_team() i32; @@ -111,3 +111,1296 @@ pub const EAI = enum(c_int) { }; pub const EAI_MAX = 15; + +pub const fd_t = c_int; +pub const pid_t = c_int; +pub const uid_t = u32; +pub const gid_t = u32; +pub const mode_t = c_uint; + +pub const socklen_t = u32; + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: i16, + flags: u16, + fflags: u32, + data: i64, + udata: usize, + // TODO ext +}; + +// Modes and flags for dlopen() +// include/dlfcn.h + +pub const POLL = struct { + pub const IN = 0x0001; + pub const ERR = 0x0004; + pub const NVAL = 0x1000; + pub const HUP = 0x0080; +}; + +pub const RTLD = struct { + /// Bind function calls lazily. + pub const LAZY = 1; + /// Bind function calls immediately. + pub const NOW = 2; + pub const MODEMASK = 0x3; + /// Make symbols globally available. + pub const GLOBAL = 0x100; + /// Opposite of GLOBAL, and the default. + pub const LOCAL = 0; + /// Trace loaded objects and exit. + pub const TRACE = 0x200; + /// Do not remove members. + pub const NODELETE = 0x01000; + /// Do not load if not already loaded. + pub const NOLOAD = 0x02000; +}; + +pub const dl_phdr_info = extern struct { + dlpi_addr: usize, + dlpi_name: ?[*:0]const u8, + dlpi_phdr: [*]std.elf.Phdr, + dlpi_phnum: u16, +}; + +pub const Flock = extern struct { + l_start: off_t, + l_len: off_t, + l_pid: pid_t, + l_type: i16, + l_whence: i16, + l_sysid: i32, + __unused: [4]u8, +}; + +pub const msghdr = extern struct { + /// optional address + msg_name: ?*sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const msghdr_const = extern struct { + /// optional address + msg_name: ?*const sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec_const, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const off_t = i64; +pub const ino_t = u64; + +pub const nfds_t = u32; + +pub const pollfd = extern struct { + fd: i32, + events: i16, + revents: i16, +}; + +pub const Stat = extern struct { + dev: i32, + ino: u64, + mode: u32, + nlink: i32, + uid: i32, + gid: i32, + size: i64, + rdev: i32, + blksize: i32, + atim: timespec, + mtim: timespec, + ctim: timespec, + crtim: timespec, + st_type: u32, + blocks: i64, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + pub fn ctime(self: @This()) timespec { + return self.ctim; + } + pub fn crtime(self: @This()) timespec { + return self.crtim; + } +}; + +pub const timespec = extern struct { + tv_sec: isize, + tv_nsec: isize, +}; + +pub const dirent = extern struct { + d_dev: i32, + d_pdev: i32, + d_ino: i64, + d_pino: i64, + d_reclen: u16, + d_name: [256]u8, + + pub fn reclen(self: dirent) u16 { + return self.d_reclen; + } +}; + +pub const image_info = extern struct { + id: u32, + type: u32, + sequence: i32, + init_order: i32, + init_routine: *c_void, + term_routine: *c_void, + device: i32, + node: i32, + name: [1024]u8, + text: *c_void, + data: *c_void, + text_size: i32, + data_size: i32, + api_version: i32, + abi: i32, +}; + +pub const system_info = extern struct { + boot_time: i64, + cpu_count: u32, + max_pages: u64, + used_pages: u64, + cached_pages: u64, + block_cache_pages: u64, + ignored_pages: u64, + needed_memory: u64, + free_memory: u64, + max_swap_pages: u64, + free_swap_pages: u64, + page_faults: u32, + max_sems: u32, + used_sems: u32, + max_ports: u32, + used_ports: u32, + max_threads: u32, + used_threads: u32, + max_teams: u32, + used_teams: u32, + kernel_name: [256]u8, + kernel_build_date: [32]u8, + kernel_build_time: [32]u8, + kernel_version: i64, + abi: u32, +}; + +pub const in_port_t = u16; +pub const sa_family_t = u8; + +pub const sockaddr = extern struct { + /// total length + len: u8, + /// address family + family: sa_family_t, + /// actually longer; address value + data: [14]u8, + + pub const storage = std.x.os.Socket.Address.Native.Storage; + + pub const in = extern struct { + len: u8 = @sizeOf(in), + family: sa_family_t = AF.INET, + port: in_port_t, + addr: u32, + zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, + }; + + pub const in6 = extern struct { + len: u8 = @sizeOf(in6), + family: sa_family_t = AF.INET6, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, + }; + + pub const un = extern struct { + len: u8 = @sizeOf(un), + family: sa_family_t = AF.UNIX, + path: [104]u8, + }; +}; + +pub const CTL = struct { + pub const KERN = 1; + pub const DEBUG = 5; +}; + +pub const KERN = struct { + pub const PROC = 14; // struct: process entries + pub const PROC_PATHNAME = 12; // path to executable +}; + +pub const PATH_MAX = 1024; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT = struct { + pub const NONE = 0; + pub const READ = 1; + pub const WRITE = 2; + pub const EXEC = 4; +}; + +pub const CLOCK = struct { + pub const MONOTONIC = 0; + pub const REALTIME = -1; + pub const PROCESS_CPUTIME_ID = -2; + pub const THREAD_CPUTIME_ID = -3; +}; + +pub const MAP = struct { + pub const FAILED = @intToPtr(*c_void, maxInt(usize)); + pub const SHARED = 0x0001; + pub const PRIVATE = 0x0002; + pub const FIXED = 0x0010; + pub const STACK = 0x0400; + pub const NOSYNC = 0x0800; + pub const ANON = 0x1000; + pub const ANONYMOUS = ANON; + pub const FILE = 0; + + pub const GUARD = 0x00002000; + pub const EXCL = 0x00004000; + pub const NOCORE = 0x00020000; + pub const PREFAULT_READ = 0x00040000; + pub const @"32BIT" = 0x00080000; +}; + +pub const W = struct { + pub const NOHANG = 0x1; + pub const UNTRACED = 0x2; + pub const STOPPED = 0x10; + pub const CONTINUED = 0x4; + pub const NOWAIT = 0x20; + pub const EXITED = 0x08; + + pub fn EXITSTATUS(s: u32) u8 { + return @intCast(u8, s & 0xff); + } + + pub fn TERMSIG(s: u32) u32 { + return (s >> 8) & 0xff; + } + + pub fn STOPSIG(s: u32) u32 { + return EXITSTATUS(s); + } + + pub fn IFEXITED(s: u32) bool { + return TERMSIG(s) == 0; + } + + pub fn IFSTOPPED(s: u32) bool { + return ((s >> 16) & 0xff) != 0; + } + + pub fn IFSIGNALED(s: u32) bool { + return ((s >> 8) & 0xff) != 0; + } +}; + +pub const SA = struct { + pub const ONSTACK = 0x20; + pub const RESTART = 0x10; + pub const RESETHAND = 0x04; + pub const NOCLDSTOP = 0x01; + pub const NODEFER = 0x08; + pub const NOCLDWAIT = 0x02; + pub const SIGINFO = 0x40; + pub const NOMASK = NODEFER; + pub const STACK = ONSTACK; + pub const ONESHOT = RESETHAND; +}; + +pub const SIG = struct { + pub const ERR = @intToPtr(fn (i32) callconv(.C) void, maxInt(usize)); + pub const DFL = @intToPtr(fn (i32) callconv(.C) void, 0); + pub const IGN = @intToPtr(fn (i32) callconv(.C) void, 1); + + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const CHLD = 5; + pub const ABRT = 6; + pub const IOT = ABRT; + pub const PIPE = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const STOP = 10; + pub const SEGV = 11; + pub const CONT = 12; + pub const TSTP = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const TTIN = 16; + pub const TTOU = 17; + pub const USR1 = 18; + pub const USR2 = 19; + pub const WINCH = 20; + pub const KILLTHR = 21; + pub const TRAP = 22; + pub const POLL = 23; + pub const PROF = 24; + pub const SYS = 25; + pub const URG = 26; + pub const VTALRM = 27; + pub const XCPU = 28; + pub const XFSZ = 29; + pub const BUS = 30; + pub const RESERVED1 = 31; + pub const RESERVED2 = 32; + + // TODO: check + pub const RTMIN = 65; + pub const RTMAX = 126; + + pub const BLOCK = 1; + pub const UNBLOCK = 2; + pub const SETMASK = 3; + + pub const WORDS = 4; + pub const MAXSIG = 128; + pub inline fn IDX(sig: usize) usize { + return sig - 1; + } + pub inline fn WORD(sig: usize) usize { + return IDX(sig) >> 5; + } + pub inline fn BIT(sig: usize) usize { + return 1 << (IDX(sig) & 31); + } + pub inline fn VALID(sig: usize) usize { + return sig <= MAXSIG and sig > 0; + } +}; + +// access function +pub const F_OK = 0; // test for existence of file +pub const X_OK = 1; // test for execute or search permission +pub const W_OK = 2; // test for write permission +pub const R_OK = 4; // test for read permission + +pub const O = struct { + pub const RDONLY = 0x0000; + pub const WRONLY = 0x0001; + pub const RDWR = 0x0002; + pub const ACCMODE = 0x0003; + + pub const SHLOCK = 0x0010; + pub const EXLOCK = 0x0020; + + pub const CREAT = 0x0200; + pub const EXCL = 0x0800; + pub const NOCTTY = 0x8000; + pub const TRUNC = 0x0400; + pub const APPEND = 0x0008; + pub const NONBLOCK = 0x0004; + pub const DSYNC = 0o10000; + pub const SYNC = 0x0080; + pub const RSYNC = 0o4010000; + pub const DIRECTORY = 0x20000; + pub const NOFOLLOW = 0x0100; + pub const CLOEXEC = 0x00100000; + + pub const ASYNC = 0x0040; + pub const DIRECT = 0x00010000; + pub const NOATIME = 0o1000000; + pub const PATH = 0o10000000; + pub const TMPFILE = 0o20200000; + pub const NDELAY = NONBLOCK; +}; + +pub const F = struct { + pub const DUPFD = 0; + pub const GETFD = 1; + pub const SETFD = 2; + pub const GETFL = 3; + pub const SETFL = 4; + + pub const GETOWN = 5; + pub const SETOWN = 6; + + pub const GETLK = 11; + pub const SETLK = 12; + pub const SETLKW = 13; + + pub const RDLCK = 1; + pub const WRLCK = 3; + pub const UNLCK = 2; + + pub const SETOWN_EX = 15; + pub const GETOWN_EX = 16; + + pub const GETOWNER_UIDS = 17; +}; + +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const UN = 8; + pub const NB = 4; +}; + +pub const FD_CLOEXEC = 1; + +pub const SEEK = struct { + pub const SET = 0; + pub const CUR = 1; + pub const END = 2; +}; + +pub const SOCK = struct { + pub const STREAM = 1; + pub const DGRAM = 2; + pub const RAW = 3; + pub const RDM = 4; + pub const SEQPACKET = 5; + + pub const CLOEXEC = 0x10000000; + pub const NONBLOCK = 0x20000000; +}; + +pub const SO = struct { + pub const DEBUG = 0x00000001; + pub const ACCEPTCONN = 0x00000002; + pub const REUSEADDR = 0x00000004; + pub const KEEPALIVE = 0x00000008; + pub const DONTROUTE = 0x00000010; + pub const BROADCAST = 0x00000020; + pub const USELOOPBACK = 0x00000040; + pub const LINGER = 0x00000080; + pub const OOBINLINE = 0x00000100; + pub const REUSEPORT = 0x00000200; + pub const TIMESTAMP = 0x00000400; + pub const NOSIGPIPE = 0x00000800; + pub const ACCEPTFILTER = 0x00001000; + pub const BINTIME = 0x00002000; + pub const NO_OFFLOAD = 0x00004000; + pub const NO_DDP = 0x00008000; + pub const REUSEPORT_LB = 0x00010000; + + pub const SNDBUF = 0x1001; + pub const RCVBUF = 0x1002; + pub const SNDLOWAT = 0x1003; + pub const RCVLOWAT = 0x1004; + pub const SNDTIMEO = 0x1005; + pub const RCVTIMEO = 0x1006; + pub const ERROR = 0x1007; + pub const TYPE = 0x1008; + pub const LABEL = 0x1009; + pub const PEERLABEL = 0x1010; + pub const LISTENQLIMIT = 0x1011; + pub const LISTENQLEN = 0x1012; + pub const LISTENINCQLEN = 0x1013; + pub const SETFIB = 0x1014; + pub const USER_COOKIE = 0x1015; + pub const PROTOCOL = 0x1016; + pub const PROTOTYPE = PROTOCOL; + pub const TS_CLOCK = 0x1017; + pub const MAX_PACING_RATE = 0x1018; + pub const DOMAIN = 0x1019; +}; + +pub const SOL = struct { + pub const SOCKET = 0xffff; +}; + +pub const PF = struct { + pub const UNSPEC = AF.UNSPEC; + pub const LOCAL = AF.LOCAL; + pub const UNIX = PF.LOCAL; + pub const INET = AF.INET; + pub const IMPLINK = AF.IMPLINK; + pub const PUP = AF.PUP; + pub const CHAOS = AF.CHAOS; + pub const NETBIOS = AF.NETBIOS; + pub const ISO = AF.ISO; + pub const OSI = AF.ISO; + pub const ECMA = AF.ECMA; + pub const DATAKIT = AF.DATAKIT; + pub const CCITT = AF.CCITT; + pub const DECnet = AF.DECnet; + pub const DLI = AF.DLI; + pub const LAT = AF.LAT; + pub const HYLINK = AF.HYLINK; + pub const APPLETALK = AF.APPLETALK; + pub const ROUTE = AF.ROUTE; + pub const LINK = AF.LINK; + pub const XTP = AF.pseudo_XTP; + pub const COIP = AF.COIP; + pub const CNT = AF.CNT; + pub const SIP = AF.SIP; + pub const IPX = AF.IPX; + pub const RTIP = AF.pseudo_RTIP; + pub const PIP = AF.pseudo_PIP; + pub const ISDN = AF.ISDN; + pub const KEY = AF.pseudo_KEY; + pub const INET6 = AF.pseudo_INET6; + pub const NATM = AF.NATM; + pub const ATM = AF.ATM; + pub const NETGRAPH = AF.NETGRAPH; + pub const SLOW = AF.SLOW; + pub const SCLUSTER = AF.SCLUSTER; + pub const ARP = AF.ARP; + pub const BLUETOOTH = AF.BLUETOOTH; + pub const IEEE80211 = AF.IEEE80211; + pub const INET_SDP = AF.INET_SDP; + pub const INET6_SDP = AF.INET6_SDP; + pub const MAX = AF.MAX; +}; + +pub const AF = struct { + pub const UNSPEC = 0; + pub const UNIX = 1; + pub const LOCAL = UNIX; + pub const FILE = LOCAL; + pub const INET = 2; + pub const IMPLINK = 3; + pub const PUP = 4; + pub const CHAOS = 5; + pub const NETBIOS = 6; + pub const ISO = 7; + pub const OSI = ISO; + pub const ECMA = 8; + pub const DATAKIT = 9; + pub const CCITT = 10; + pub const SNA = 11; + pub const DECnet = 12; + pub const DLI = 13; + pub const LAT = 14; + pub const HYLINK = 15; + pub const APPLETALK = 16; + pub const ROUTE = 17; + pub const LINK = 18; + pub const pseudo_XTP = 19; + pub const COIP = 20; + pub const CNT = 21; + pub const pseudo_RTIP = 22; + pub const IPX = 23; + pub const SIP = 24; + pub const pseudo_PIP = 25; + pub const ISDN = 26; + pub const E164 = ISDN; + pub const pseudo_KEY = 27; + pub const INET6 = 28; + pub const NATM = 29; + pub const ATM = 30; + pub const pseudo_HDRCMPLT = 31; + pub const NETGRAPH = 32; + pub const SLOW = 33; + pub const SCLUSTER = 34; + pub const ARP = 35; + pub const BLUETOOTH = 36; + pub const IEEE80211 = 37; + pub const INET_SDP = 40; + pub const INET6_SDP = 42; + pub const MAX = 42; +}; + +pub const DT = struct { + pub const UNKNOWN = 0; + pub const FIFO = 1; + pub const CHR = 2; + pub const DIR = 4; + pub const BLK = 6; + pub const REG = 8; + pub const LNK = 10; + pub const SOCK = 12; + pub const WHT = 14; +}; + +/// add event to kq (implies enable) +pub const EV_ADD = 0x0001; + +/// delete event from kq +pub const EV_DELETE = 0x0002; + +/// enable event +pub const EV_ENABLE = 0x0004; + +/// disable event (not reported) +pub const EV_DISABLE = 0x0008; + +/// only report one occurrence +pub const EV_ONESHOT = 0x0010; + +/// clear event state after reporting +pub const EV_CLEAR = 0x0020; + +/// force immediate event output +/// ... with or without EV_ERROR +/// ... use KEVENT_FLAG_ERROR_EVENTS +/// on syscalls supporting flags +pub const EV_RECEIPT = 0x0040; + +/// disable event after reporting +pub const EV_DISPATCH = 0x0080; + +pub const EVFILT_READ = -1; +pub const EVFILT_WRITE = -2; + +/// attached to aio requests +pub const EVFILT_AIO = -3; + +/// attached to vnodes +pub const EVFILT_VNODE = -4; + +/// attached to struct proc +pub const EVFILT_PROC = -5; + +/// attached to struct proc +pub const EVFILT_SIGNAL = -6; + +/// timers +pub const EVFILT_TIMER = -7; + +/// Process descriptors +pub const EVFILT_PROCDESC = -8; + +/// Filesystem events +pub const EVFILT_FS = -9; + +pub const EVFILT_LIO = -10; + +/// User events +pub const EVFILT_USER = -11; + +/// Sendfile events +pub const EVFILT_SENDFILE = -12; + +pub const EVFILT_EMPTY = -13; + +pub const T = struct { + pub const CGETA = 0x8000; + pub const CSETA = 0x8001; + pub const CSETAW = 0x8004; + pub const CSETAF = 0x8003; + pub const CSBRK = 08005; + pub const CXONC = 0x8007; + pub const CFLSH = 0x8006; + + pub const IOCSCTTY = 0x8017; + pub const IOCGPGRP = 0x8015; + pub const IOCSPGRP = 0x8016; + pub const IOCGWINSZ = 0x8012; + pub const IOCSWINSZ = 0x8013; + pub const IOCMGET = 0x8018; + pub const IOCMBIS = 0x8022; + pub const IOCMBIC = 0x8023; + pub const IOCMSET = 0x8019; + pub const FIONREAD = 0xbe000001; + pub const FIONBIO = 0xbe000000; + pub const IOCSBRK = 0x8020; + pub const IOCCBRK = 0x8021; + pub const IOCGSID = 0x8024; +}; + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +const NSIG = 32; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = extern struct { + /// signal handler + __sigaction_u: extern union { + __sa_handler: fn (i32) callconv(.C) void, + }, + + /// see signal options + sa_flags: u32, + + /// signal mask to apply + sa_mask: sigset_t, +}; + +pub const sigset_t = extern struct { + __bits: [SIG.WORDS]u32, +}; + +pub const E = enum(i32) { + /// No error occurred. + SUCCESS = 0, + PERM = -0x7ffffff1, // Operation not permitted + NOENT = -0x7fff9ffd, // No such file or directory + SRCH = -0x7fff8ff3, // No such process + INTR = -0x7ffffff6, // Interrupted system call + IO = -0x7fffffff, // Input/output error + NXIO = -0x7fff8ff5, // Device not configured + @"2BIG" = -0x7fff8fff, // Argument list too long + NOEXEC = -0x7fffecfe, // Exec format error + CHILD = -0x7fff8ffe, // No child processes + DEADLK = -0x7fff8ffd, // Resource deadlock avoided + NOMEM = -0x80000000, // Cannot allocate memory + ACCES = -0x7ffffffe, // Permission denied + FAULT = -0x7fffecff, // Bad address + BUSY = -0x7ffffff2, // Device busy + EXIST = -0x7fff9ffe, // File exists + XDEV = -0x7fff9ff5, // Cross-device link + NODEV = -0x7fff8ff9, // Operation not supported by device + NOTDIR = -0x7fff9ffb, // Not a directory + ISDIR = -0x7fff9ff7, // Is a directory + INVAL = -0x7ffffffb, // Invalid argument + NFILE = -0x7fff8ffa, // Too many open files in system + MFILE = -0x7fff9ff6, // Too many open files + NOTTY = -0x7fff8ff6, // Inappropriate ioctl for device + TXTBSY = -0x7fff8fc5, // Text file busy + FBIG = -0x7fff8ffc, // File too large + NOSPC = -0x7fff9ff9, // No space left on device + SPIPE = -0x7fff8ff4, // Illegal seek + ROFS = -0x7fff9ff8, // Read-only filesystem + MLINK = -0x7fff8ffb, // Too many links + PIPE = -0x7fff9ff3, // Broken pipe + BADF = -0x7fffa000, // Bad file descriptor + + // math software + DOM = 33, // Numerical argument out of domain + RANGE = 34, // Result too large + + // non-blocking and interrupt i/o + + /// Also used for `WOULDBLOCK`. + AGAIN = -0x7ffffff5, + INPROGRESS = -0x7fff8fdc, + ALREADY = -0x7fff8fdb, + + // ipc/network software -- argument errors + NOTSOCK = 38, // Socket operation on non-socket + DESTADDRREQ = 39, // Destination address required + MSGSIZE = 40, // Message too long + PROTOTYPE = 41, // Protocol wrong type for socket + NOPROTOOPT = 42, // Protocol not available + PROTONOSUPPORT = 43, // Protocol not supported + SOCKTNOSUPPORT = 44, // Socket type not supported + /// Also used for `NOTSUP`. + OPNOTSUPP = 45, // Operation not supported + PFNOSUPPORT = 46, // Protocol family not supported + AFNOSUPPORT = 47, // Address family not supported by protocol family + ADDRINUSE = 48, // Address already in use + ADDRNOTAVAIL = 49, // Can't assign requested address + + // ipc/network software -- operational errors + NETDOWN = 50, // Network is down + NETUNREACH = 51, // Network is unreachable + NETRESET = 52, // Network dropped connection on reset + CONNABORTED = 53, // Software caused connection abort + CONNRESET = 54, // Connection reset by peer + NOBUFS = 55, // No buffer space available + ISCONN = 56, // Socket is already connected + NOTCONN = 57, // Socket is not connected + SHUTDOWN = 58, // Can't send after socket shutdown + TOOMANYREFS = 59, // Too many references: can't splice + TIMEDOUT = 60, // Operation timed out + CONNREFUSED = 61, // Connection refused + + LOOP = 62, // Too many levels of symbolic links + NAMETOOLONG = 63, // File name too long + + // should be rearranged + HOSTDOWN = 64, // Host is down + HOSTUNREACH = 65, // No route to host + NOTEMPTY = 66, // Directory not empty + + // quotas & mush + PROCLIM = 67, // Too many processes + USERS = 68, // Too many users + DQUOT = 69, // Disc quota exceeded + + // Network File System + STALE = 70, // Stale NFS file handle + REMOTE = 71, // Too many levels of remote in path + BADRPC = 72, // RPC struct is bad + RPCMISMATCH = 73, // RPC version wrong + PROGUNAVAIL = 74, // RPC prog. not avail + PROGMISMATCH = 75, // Program version wrong + PROCUNAVAIL = 76, // Bad procedure for program + + NOLCK = 77, // No locks available + NOSYS = 78, // Function not implemented + + FTYPE = 79, // Inappropriate file type or format + AUTH = 80, // Authentication error + NEEDAUTH = 81, // Need authenticator + IDRM = 82, // Identifier removed + NOMSG = 83, // No message of desired type + OVERFLOW = 84, // Value too large to be stored in data type + CANCELED = 85, // Operation canceled + ILSEQ = 86, // Illegal byte sequence + NOATTR = 87, // Attribute not found + + DOOFUS = 88, // Programming error + + BADMSG = 89, // Bad message + MULTIHOP = 90, // Multihop attempted + NOLINK = 91, // Link has been severed + PROTO = 92, // Protocol error + + NOTCAPABLE = 93, // Capabilities insufficient + CAPMODE = 94, // Not permitted in capability mode + NOTRECOVERABLE = 95, // State not recoverable + OWNERDEAD = 96, // Previous owner died + + _, +}; + +pub const MINSIGSTKSZ = switch (builtin.cpu.arch) { + .i386, .x86_64 => 2048, + .arm, .aarch64 => 4096, + else => @compileError("MINSIGSTKSZ not defined for this architecture"), +}; +pub const SIGSTKSZ = MINSIGSTKSZ + 32768; + +pub const SS_ONSTACK = 1; +pub const SS_DISABLE = 4; + +pub const stack_t = extern struct { + sp: [*]u8, + size: isize, + flags: i32, +}; + +pub const S = struct { + pub const IFMT = 0o170000; + + pub const IFIFO = 0o010000; + pub const IFCHR = 0o020000; + pub const IFDIR = 0o040000; + pub const IFBLK = 0o060000; + pub const IFREG = 0o100000; + pub const IFLNK = 0o120000; + pub const IFSOCK = 0o140000; + pub const IFWHT = 0o160000; + + pub const ISUID = 0o4000; + pub const ISGID = 0o2000; + pub const ISVTX = 0o1000; + pub const IRWXU = 0o700; + pub const IRUSR = 0o400; + pub const IWUSR = 0o200; + pub const IXUSR = 0o100; + pub const IRWXG = 0o070; + pub const IRGRP = 0o040; + pub const IWGRP = 0o020; + pub const IXGRP = 0o010; + pub const IRWXO = 0o007; + pub const IROTH = 0o004; + pub const IWOTH = 0o002; + pub const IXOTH = 0o001; + + pub fn ISFIFO(m: u32) bool { + return m & IFMT == IFIFO; + } + + pub fn ISCHR(m: u32) bool { + return m & IFMT == IFCHR; + } + + pub fn ISDIR(m: u32) bool { + return m & IFMT == IFDIR; + } + + pub fn ISBLK(m: u32) bool { + return m & IFMT == IFBLK; + } + + pub fn ISREG(m: u32) bool { + return m & IFMT == IFREG; + } + + pub fn ISLNK(m: u32) bool { + return m & IFMT == IFLNK; + } + + pub fn ISSOCK(m: u32) bool { + return m & IFMT == IFSOCK; + } + + pub fn IWHT(m: u32) bool { + return m & IFMT == IFWHT; + } +}; + +pub const HOST_NAME_MAX = 255; + +pub const AT = struct { + /// Magic value that specify the use of the current working directory + /// to determine the target of relative file paths in the openat() and + /// similar syscalls. + pub const FDCWD = -100; + /// Check access using effective user and group ID + pub const EACCESS = 0x0100; + /// Do not follow symbolic links + pub const SYMLINK_NOFOLLOW = 0x0200; + /// Follow symbolic link + pub const SYMLINK_FOLLOW = 0x0400; + /// Remove directory instead of file + pub const REMOVEDIR = 0x0800; + /// Fail if not under dirfd + pub const BENEATH = 0x1000; +}; + +pub const addrinfo = extern struct { + flags: i32, + family: i32, + socktype: i32, + protocol: i32, + addrlen: socklen_t, + canonname: ?[*:0]u8, + addr: ?*sockaddr, + next: ?*addrinfo, +}; + +pub const IPPROTO = struct { + /// dummy for IP + pub const IP = 0; + /// control message protocol + pub const ICMP = 1; + /// tcp + pub const TCP = 6; + /// user datagram protocol + pub const UDP = 17; + /// IP6 header + pub const IPV6 = 41; + /// raw IP packet + pub const RAW = 255; + /// IP6 hop-by-hop options + pub const HOPOPTS = 0; + /// group mgmt protocol + pub const IGMP = 2; + /// gateway^2 (deprecated) + pub const GGP = 3; + /// IPv4 encapsulation + pub const IPV4 = 4; + /// for compatibility + pub const IPIP = IPV4; + /// Stream protocol II + pub const ST = 7; + /// exterior gateway protocol + pub const EGP = 8; + /// private interior gateway + pub const PIGP = 9; + /// BBN RCC Monitoring + pub const RCCMON = 10; + /// network voice protocol + pub const NVPII = 11; + /// pup + pub const PUP = 12; + /// Argus + pub const ARGUS = 13; + /// EMCON + pub const EMCON = 14; + /// Cross Net Debugger + pub const XNET = 15; + /// Chaos + pub const CHAOS = 16; + /// Multiplexing + pub const MUX = 18; + /// DCN Measurement Subsystems + pub const MEAS = 19; + /// Host Monitoring + pub const HMP = 20; + /// Packet Radio Measurement + pub const PRM = 21; + /// xns idp + pub const IDP = 22; + /// Trunk-1 + pub const TRUNK1 = 23; + /// Trunk-2 + pub const TRUNK2 = 24; + /// Leaf-1 + pub const LEAF1 = 25; + /// Leaf-2 + pub const LEAF2 = 26; + /// Reliable Data + pub const RDP = 27; + /// Reliable Transaction + pub const IRTP = 28; + /// tp-4 w/ class negotiation + pub const TP = 29; + /// Bulk Data Transfer + pub const BLT = 30; + /// Network Services + pub const NSP = 31; + /// Merit Internodal + pub const INP = 32; + /// Datagram Congestion Control Protocol + pub const DCCP = 33; + /// Third Party Connect + pub const @"3PC" = 34; + /// InterDomain Policy Routing + pub const IDPR = 35; + /// XTP + pub const XTP = 36; + /// Datagram Delivery + pub const DDP = 37; + /// Control Message Transport + pub const CMTP = 38; + /// TP++ Transport + pub const TPXX = 39; + /// IL transport protocol + pub const IL = 40; + /// Source Demand Routing + pub const SDRP = 42; + /// IP6 routing header + pub const ROUTING = 43; + /// IP6 fragmentation header + pub const FRAGMENT = 44; + /// InterDomain Routing + pub const IDRP = 45; + /// resource reservation + pub const RSVP = 46; + /// General Routing Encap. + pub const GRE = 47; + /// Mobile Host Routing + pub const MHRP = 48; + /// BHA + pub const BHA = 49; + /// IP6 Encap Sec. Payload + pub const ESP = 50; + /// IP6 Auth Header + pub const AH = 51; + /// Integ. Net Layer Security + pub const INLSP = 52; + /// IP with encryption + pub const SWIPE = 53; + /// Next Hop Resolution + pub const NHRP = 54; + /// IP Mobility + pub const MOBILE = 55; + /// Transport Layer Security + pub const TLSP = 56; + /// SKIP + pub const SKIP = 57; + /// ICMP6 + pub const ICMPV6 = 58; + /// IP6 no next header + pub const NONE = 59; + /// IP6 destination option + pub const DSTOPTS = 60; + /// any host internal protocol + pub const AHIP = 61; + /// CFTP + pub const CFTP = 62; + /// "hello" routing protocol + pub const HELLO = 63; + /// SATNET/Backroom EXPAK + pub const SATEXPAK = 64; + /// Kryptolan + pub const KRYPTOLAN = 65; + /// Remote Virtual Disk + pub const RVD = 66; + /// Pluribus Packet Core + pub const IPPC = 67; + /// Any distributed FS + pub const ADFS = 68; + /// Satnet Monitoring + pub const SATMON = 69; + /// VISA Protocol + pub const VISA = 70; + /// Packet Core Utility + pub const IPCV = 71; + /// Comp. Prot. Net. Executive + pub const CPNX = 72; + /// Comp. Prot. HeartBeat + pub const CPHB = 73; + /// Wang Span Network + pub const WSN = 74; + /// Packet Video Protocol + pub const PVP = 75; + /// BackRoom SATNET Monitoring + pub const BRSATMON = 76; + /// Sun net disk proto (temp.) + pub const ND = 77; + /// WIDEBAND Monitoring + pub const WBMON = 78; + /// WIDEBAND EXPAK + pub const WBEXPAK = 79; + /// ISO cnlp + pub const EON = 80; + /// VMTP + pub const VMTP = 81; + /// Secure VMTP + pub const SVMTP = 82; + /// Banyon VINES + pub const VINES = 83; + /// TTP + pub const TTP = 84; + /// NSFNET-IGP + pub const IGP = 85; + /// dissimilar gateway prot. + pub const DGP = 86; + /// TCF + pub const TCF = 87; + /// Cisco/GXS IGRP + pub const IGRP = 88; + /// OSPFIGP + pub const OSPFIGP = 89; + /// Strite RPC protocol + pub const SRPC = 90; + /// Locus Address Resoloution + pub const LARP = 91; + /// Multicast Transport + pub const MTP = 92; + /// AX.25 Frames + pub const AX25 = 93; + /// IP encapsulated in IP + pub const IPEIP = 94; + /// Mobile Int.ing control + pub const MICP = 95; + /// Semaphore Comm. security + pub const SCCSP = 96; + /// Ethernet IP encapsulation + pub const ETHERIP = 97; + /// encapsulation header + pub const ENCAP = 98; + /// any private encr. scheme + pub const APES = 99; + /// GMTP + pub const GMTP = 100; + /// payload compression (IPComp) + pub const IPCOMP = 108; + /// SCTP + pub const SCTP = 132; + /// IPv6 Mobility Header + pub const MH = 135; + /// UDP-Lite + pub const UDPLITE = 136; + /// IP6 Host Identity Protocol + pub const HIP = 139; + /// IP6 Shim6 Protocol + pub const SHIM6 = 140; + /// Protocol Independent Mcast + pub const PIM = 103; + /// CARP + pub const CARP = 112; + /// PGM + pub const PGM = 113; + /// MPLS-in-IP + pub const MPLS = 137; + /// PFSYNC + pub const PFSYNC = 240; + /// Reserved + pub const RESERVED_253 = 253; + /// Reserved + pub const RESERVED_254 = 254; +}; + +pub const rlimit_resource = enum(c_int) { + CPU = 0, + FSIZE = 1, + DATA = 2, + STACK = 3, + CORE = 4, + RSS = 5, + MEMLOCK = 6, + NPROC = 7, + NOFILE = 8, + SBSIZE = 9, + VMEM = 10, + NPTS = 11, + SWAP = 12, + KQUEUES = 13, + UMTXP = 14, + _, + + pub const AS: rlimit_resource = .VMEM; +}; + +pub const rlim_t = i64; + +pub const RLIM = struct { + /// No limit + pub const INFINITY: rlim_t = (1 << 63) - 1; + + pub const SAVED_MAX = INFINITY; + pub const SAVED_CUR = INFINITY; +}; + +pub const rlimit = extern struct { + /// Soft limit + cur: rlim_t, + /// Hard limit + max: rlim_t, +}; + +pub const SHUT = struct { + pub const RD = 0; + pub const WR = 1; + pub const RDWR = 2; +}; + +// TODO fill out if needed +pub const directory_which = enum(c_int) { + B_USER_SETTINGS_DIRECTORY = 0xbbe, + + _, +}; + +pub const cc_t = u8; +pub const speed_t = u8; +pub const tcflag_t = u32; + +pub const NCCS = 32; + +pub const termios = extern struct { + c_iflag: tcflag_t, + c_oflag: tcflag_t, + c_cflag: tcflag_t, + c_lflag: tcflag_t, + c_line: cc_t, + c_ispeed: speed_t, + c_ospeed: speed_t, + cc_t: [NCCS]cc_t, +}; diff --git a/lib/std/c/hermit.zig b/lib/std/c/hermit.zig index ae13840e16..879346ba13 100644 --- a/lib/std/c/hermit.zig +++ b/lib/std/c/hermit.zig @@ -1,3 +1,6 @@ +const std = @import("std"); +const maxInt = std.math.maxInt; + pub const pthread_mutex_t = extern struct { inner: usize = ~@as(usize, 0), }; @@ -5,5 +8,5 @@ pub const pthread_cond_t = extern struct { inner: usize = ~@as(usize, 0), }; pub const pthread_rwlock_t = extern struct { - ptr: usize = std.math.maxInt(usize), + ptr: usize = maxInt(usize), }; diff --git a/lib/std/c/linux.zig b/lib/std/c/linux.zig index 808b05bd6d..b0760e9afe 100644 --- a/lib/std/c/linux.zig +++ b/lib/std/c/linux.zig @@ -1,11 +1,106 @@ const std = @import("../std.zig"); +const builtin = @import("builtin"); const maxInt = std.math.maxInt; -const abi = std.Target.current.abi; -const arch = std.Target.current.cpu.arch; -const os_tag = std.Target.current.os.tag; -usingnamespace std.c; +const native_abi = builtin.abi; +const native_arch = builtin.cpu.arch; +const linux = std.os.linux; +const iovec = std.os.iovec; +const iovec_const = std.os.iovec_const; +const FILE = std.c.FILE; -pub const _errno = switch (abi) { +pub const AF = linux.AF; +pub const ARCH = linux.ARCH; +pub const AT = linux.AT; +pub const CLOCK = linux.CLOCK; +pub const CPU_COUNT = linux.CPU_COUNT; +pub const E = linux.E; +pub const Elf_Symndx = linux.Elf_Symndx; +pub const F = linux.F; +pub const FD_CLOEXEC = linux.FD_CLOEXEC; +pub const F_OK = linux.F_OK; +pub const Flock = linux.Flock; +pub const HOST_NAME_MAX = linux.HOST_NAME_MAX; +pub const IFNAMESIZE = linux.IFNAMESIZE; +pub const IOV_MAX = linux.IOV_MAX; +pub const IPPROTO = linux.IPPROTO; +pub const LOCK = linux.LOCK; +pub const MADV = linux.MADV; +pub const MAP = struct { + pub usingnamespace linux.MAP; + /// Only used by libc to communicate failure. + pub const FAILED = @intToPtr(*c_void, maxInt(usize)); +}; +pub const MMAP2_UNIT = linux.MMAP2_UNIT; +pub const MSG = linux.MSG; +pub const NAME_MAX = linux.NAME_MAX; +pub const O = linux.O; +pub const PATH_MAX = linux.PATH_MAX; +pub const POLL = linux.POLL; +pub const PROT = linux.PROT; +pub const REG = linux.REG; +pub const RLIM = linux.RLIM; +pub const R_OK = linux.R_OK; +pub const S = linux.S; +pub const SA = linux.SA; +pub const SC = linux.SC; +pub const SEEK = linux.SEEK; +pub const SHUT = linux.SHUT; +pub const SIG = linux.SIG; +pub const SIOCGIFINDEX = linux.SIOCGIFINDEX; +pub const SO = linux.SO; +pub const SOCK = linux.SOCK; +pub const SOL = linux.SOL; +pub const STDERR_FILENO = linux.STDERR_FILENO; +pub const STDIN_FILENO = linux.STDIN_FILENO; +pub const STDOUT_FILENO = linux.STDOUT_FILENO; +pub const SYS = linux.SYS; +pub const Sigaction = linux.Sigaction; +pub const TCP = linux.TCP; +pub const VDSO = linux.VDSO; +pub const W = linux.W; +pub const W_OK = linux.W_OK; +pub const X_OK = linux.X_OK; +pub const addrinfo = linux.addrinfo; +pub const blkcnt_t = linux.blkcnt_t; +pub const blksize_t = linux.blksize_t; +pub const clock_t = linux.clock_t; +pub const cpu_set_t = linux.cpu_set_t; +pub const dev_t = linux.dev_t; +pub const dl_phdr_info = linux.dl_phdr_info; +pub const empty_sigset = linux.empty_sigset; +pub const epoll_event = linux.epoll_event; +pub const fd_t = linux.fd_t; +pub const gid_t = linux.gid_t; +pub const ifreq = linux.ifreq; +pub const ino_t = linux.ino_t; +pub const mcontext_t = linux.mcontext_t; +pub const mode_t = linux.mode_t; +pub const msghdr = linux.msghdr; +pub const msghdr_const = linux.msghdr_const; +pub const nfds_t = linux.nfds_t; +pub const nlink_t = linux.nlink_t; +pub const off_t = linux.off_t; +pub const pid_t = linux.pid_t; +pub const pollfd = linux.pollfd; +pub const rlim_t = linux.rlim_t; +pub const rlimit = linux.rlimit; +pub const rlimit_resource = linux.rlimit_resource; +pub const siginfo_t = linux.siginfo_t; +pub const sigset_t = linux.sigset_t; +pub const sockaddr = linux.sockaddr; +pub const socklen_t = linux.socklen_t; +pub const stack_t = linux.stack_t; +pub const time_t = linux.time_t; +pub const timespec = linux.timespec; +pub const timeval = linux.timeval; +pub const timezone = linux.timezone; +pub const ucontext_t = linux.ucontext_t; +pub const uid_t = linux.uid_t; +pub const user_desc = linux.user_desc; +pub const utsname = linux.utsname; +pub const PR = linux.PR; + +pub const _errno = switch (native_abi) { .android => struct { extern "c" var __errno: c_int; fn getErrno() *c_int { @@ -17,22 +112,92 @@ pub const _errno = switch (abi) { }.__errno_location, }; -pub const MAP_FAILED = @intToPtr(*c_void, maxInt(usize)); +pub const Stat = switch (native_arch) { + .sparcv9 => extern struct { + dev: u64, + ino: ino_t, + mode: u32, + nlink: usize, + + uid: u32, + gid: u32, + rdev: u64, + __pad0: u32, + + size: off_t, + blksize: isize, + blocks: i64, + + atim: timespec, + mtim: timespec, + ctim: timespec, + __unused: [2]isize, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } + }, + .mips, .mipsel => extern struct { + dev: dev_t, + __pad0: [2]u32, + ino: ino_t, + mode: mode_t, + nlink: nlink_t, + uid: uid_t, + gid: gid_t, + rdev: dev_t, + __pad1: [2]u32, + size: off_t, + atim: timespec, + mtim: timespec, + ctim: timespec, + blksize: blksize_t, + __pad3: u32, + blocks: blkcnt_t, + __pad4: [14]u32, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } + }, + + else => std.os.linux.Stat, // libc stat is the same as kernel stat. +}; -pub const AI_PASSIVE = 0x01; -pub const AI_CANONNAME = 0x02; -pub const AI_NUMERICHOST = 0x04; -pub const AI_V4MAPPED = 0x08; -pub const AI_ALL = 0x10; -pub const AI_ADDRCONFIG = 0x20; -pub const AI_NUMERICSERV = 0x400; +pub const AI = struct { + pub const PASSIVE = 0x01; + pub const CANONNAME = 0x02; + pub const NUMERICHOST = 0x04; + pub const V4MAPPED = 0x08; + pub const ALL = 0x10; + pub const ADDRCONFIG = 0x20; + pub const NUMERICSERV = 0x400; +}; -pub const NI_NUMERICHOST = 0x01; -pub const NI_NUMERICSERV = 0x02; -pub const NI_NOFQDN = 0x04; -pub const NI_NAMEREQD = 0x08; -pub const NI_DGRAM = 0x10; -pub const NI_NUMERICSCOPE = 0x100; +pub const NI = struct { + pub const NUMERICHOST = 0x01; + pub const NUMERICSERV = 0x02; + pub const NOFQDN = 0x04; + pub const NAMEREQD = 0x08; + pub const DGRAM = 0x10; + pub const NUMERICSCOPE = 0x100; +}; pub const EAI = enum(c_int) { BADFLAGS = -1, @@ -60,8 +225,8 @@ pub const EAI = enum(c_int) { pub extern "c" fn fallocate64(fd: fd_t, mode: c_int, offset: off_t, len: off_t) c_int; pub extern "c" fn fopen64(noalias filename: [*:0]const u8, noalias modes: [*:0]const u8) ?*FILE; -pub extern "c" fn fstat64(fd: fd_t, buf: *libc_stat) c_int; -pub extern "c" fn fstatat64(dirfd: fd_t, path: [*:0]const u8, stat_buf: *libc_stat, flags: u32) c_int; +pub extern "c" fn fstat64(fd: fd_t, buf: *Stat) c_int; +pub extern "c" fn fstatat64(dirfd: fd_t, path: [*:0]const u8, stat_buf: *Stat, flags: u32) c_int; pub extern "c" fn ftruncate64(fd: c_int, length: off_t) c_int; pub extern "c" fn getrlimit64(resource: rlimit_resource, rlim: *rlimit) c_int; pub extern "c" fn lseek64(fd: fd_t, offset: i64, whence: c_int) i64; @@ -137,7 +302,7 @@ pub const pthread_mutex_t = extern struct { pub const pthread_cond_t = extern struct { size: [__SIZEOF_PTHREAD_COND_T]u8 align(@alignOf(usize)) = [_]u8{0} ** __SIZEOF_PTHREAD_COND_T, }; -pub const pthread_rwlock_t = switch (abi) { +pub const pthread_rwlock_t = switch (native_abi) { .android => switch (@sizeOf(usize)) { 4 => extern struct { lock: std.c.pthread_mutex_t = std.c.PTHREAD_MUTEX_INITIALIZER, @@ -168,11 +333,11 @@ pub const sem_t = extern struct { }; const __SIZEOF_PTHREAD_COND_T = 48; -const __SIZEOF_PTHREAD_MUTEX_T = if (os_tag == .fuchsia) 40 else switch (abi) { +const __SIZEOF_PTHREAD_MUTEX_T = switch (native_abi) { .musl, .musleabi, .musleabihf => if (@sizeOf(usize) == 8) 40 else 24, - .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => switch (arch) { + .gnu, .gnuabin32, .gnuabi64, .gnueabi, .gnueabihf, .gnux32 => switch (native_arch) { .aarch64 => 48, - .x86_64 => if (abi == .gnux32) 40 else 32, + .x86_64 => if (native_abi == .gnux32) 40 else 32, .mips64, .powerpc64, .powerpc64le, .sparcv9 => 40, else => if (@sizeOf(usize) == 8) 40 else 24, }, @@ -184,9 +349,11 @@ const __SIZEOF_SEM_T = 4 * @sizeOf(usize); pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8) E; pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E; -pub const RTLD_LAZY = 1; -pub const RTLD_NOW = 2; -pub const RTLD_NOLOAD = 4; -pub const RTLD_NODELETE = 4096; -pub const RTLD_GLOBAL = 256; -pub const RTLD_LOCAL = 0; +pub const RTLD = struct { + pub const LAZY = 1; + pub const NOW = 2; + pub const NOLOAD = 4; + pub const NODELETE = 4096; + pub const GLOBAL = 256; + pub const LOCAL = 0; +}; diff --git a/lib/std/c/netbsd.zig b/lib/std/c/netbsd.zig index e45dc85d34..d76a9ecdf5 100644 --- a/lib/std/c/netbsd.zig +++ b/lib/std/c/netbsd.zig @@ -1,7 +1,10 @@ const std = @import("../std.zig"); -const builtin = std.builtin; - -usingnamespace std.c; +const builtin = @import("builtin"); +const maxInt = std.math.maxInt; +const iovec = std.os.iovec; +const iovec_const = std.os.iovec_const; +const timezone = std.c.timezone; +const rusage = std.c.rusage; extern "c" fn __errno() *c_int; pub const _errno = __errno; @@ -13,56 +16,81 @@ pub extern "c" fn _lwp_self() lwpid_t; pub extern "c" fn pipe2(fds: *[2]fd_t, flags: u32) c_int; pub extern "c" fn arc4random_buf(buf: [*]u8, len: usize) void; -pub extern "c" fn __fstat50(fd: fd_t, buf: *libc_stat) c_int; -pub extern "c" fn __stat50(path: [*:0]const u8, buf: *libc_stat) c_int; + +pub extern "c" fn __fstat50(fd: fd_t, buf: *Stat) c_int; +pub const fstat = __fstat50; + +pub extern "c" fn __stat50(path: [*:0]const u8, buf: *Stat) c_int; +pub const stat = __stat50; + pub extern "c" fn __clock_gettime50(clk_id: c_int, tp: *timespec) c_int; +pub const clock_gettime = __clock_gettime50; + pub extern "c" fn __clock_getres50(clk_id: c_int, tp: *timespec) c_int; +pub const clock_getres = __clock_getres50; + pub extern "c" fn __getdents30(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int; +pub const getdents = __getdents30; + pub extern "c" fn __sigaltstack14(ss: ?*stack_t, old_ss: ?*stack_t) c_int; +pub const sigaltstack = __sigaltstack14; + pub extern "c" fn __nanosleep50(rqtp: *const timespec, rmtp: ?*timespec) c_int; +pub const nanosleep = __nanosleep50; + pub extern "c" fn __sigaction14(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int; +pub const sigaction = __sigaction14; + pub extern "c" fn __sigprocmask14(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int; +pub const sigprocmask = __sigaction14; + pub extern "c" fn __socket30(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int; +pub const socket = __socket30; + pub extern "c" fn __gettimeofday50(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int; +pub const gettimeofday = __gettimeofday50; + pub extern "c" fn __getrusage50(who: c_int, usage: *rusage) c_int; -// libc aliases this as sched_yield +pub const getrusage = __getrusage50; + pub extern "c" fn __libc_thr_yield() c_int; +pub const sched_yield = __libc_thr_yield; pub extern "c" fn posix_memalign(memptr: *?*c_void, alignment: usize, size: usize) c_int; pub const pthread_mutex_t = extern struct { - ptm_magic: u32 = 0x33330003, - ptm_errorcheck: padded_pthread_spin_t = 0, - ptm_ceiling: padded_pthread_spin_t = 0, - ptm_owner: usize = 0, - ptm_waiters: ?*u8 = null, - ptm_recursed: u32 = 0, - ptm_spare2: ?*c_void = null, + magic: u32 = 0x33330003, + errorcheck: padded_pthread_spin_t = 0, + ceiling: padded_pthread_spin_t = 0, + owner: usize = 0, + waiters: ?*u8 = null, + recursed: u32 = 0, + spare2: ?*c_void = null, }; pub const pthread_cond_t = extern struct { - ptc_magic: u32 = 0x55550005, - ptc_lock: pthread_spin_t = 0, - ptc_waiters_first: ?*u8 = null, - ptc_waiters_last: ?*u8 = null, - ptc_mutex: ?*pthread_mutex_t = null, - ptc_private: ?*c_void = null, + magic: u32 = 0x55550005, + lock: pthread_spin_t = 0, + waiters_first: ?*u8 = null, + waiters_last: ?*u8 = null, + mutex: ?*pthread_mutex_t = null, + private: ?*c_void = null, }; pub const pthread_rwlock_t = extern struct { - ptr_magic: c_uint = 0x99990009, - ptr_interlock: switch (std.builtin.cpu.arch) { + magic: c_uint = 0x99990009, + interlock: switch (builtin.cpu.arch) { .aarch64, .sparc, .x86_64, .i386 => u8, .arm, .powerpc => c_int, else => unreachable, } = 0, - ptr_rblocked_first: ?*u8 = null, - ptr_rblocked_last: ?*u8 = null, - ptr_wblocked_first: ?*u8 = null, - ptr_wblocked_last: ?*u8 = null, - ptr_nreaders: c_uint = 0, - ptr_owner: std.c.pthread_t = null, - ptr_private: ?*c_void = null, + rblocked_first: ?*u8 = null, + rblocked_last: ?*u8 = null, + wblocked_first: ?*u8 = null, + wblocked_last: ?*u8 = null, + nreaders: c_uint = 0, + owner: std.c.pthread_t = null, + private: ?*c_void = null, }; const pthread_spin_t = switch (builtin.cpu.arch) { @@ -92,3 +120,1318 @@ pub const sem_t = ?*opaque {}; pub extern "c" fn pthread_setname_np(thread: std.c.pthread_t, name: [*:0]const u8, arg: ?*c_void) E; pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E; + +pub const blkcnt_t = i64; +pub const blksize_t = i32; +pub const clock_t = u32; +pub const dev_t = u64; +pub const fd_t = i32; +pub const gid_t = u32; +pub const ino_t = u64; +pub const mode_t = u32; +pub const nlink_t = u32; +pub const off_t = i64; +pub const pid_t = i32; +pub const socklen_t = u32; +pub const time_t = i64; +pub const uid_t = u32; +pub const lwpid_t = i32; +pub const suseconds_t = c_int; + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: i32, + flags: u32, + fflags: u32, + data: i64, + udata: usize, +}; + +pub const RTLD = struct { + pub const LAZY = 1; + pub const NOW = 2; + pub const GLOBAL = 0x100; + pub const LOCAL = 0x200; + pub const NODELETE = 0x01000; + pub const NOLOAD = 0x02000; + + pub const NEXT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -1))); + pub const DEFAULT = @intToPtr(*c_void, @bitCast(usize, @as(isize, -2))); + pub const SELF = @intToPtr(*c_void, @bitCast(usize, @as(isize, -3))); +}; + +pub const dl_phdr_info = extern struct { + dlpi_addr: usize, + dlpi_name: ?[*:0]const u8, + dlpi_phdr: [*]std.elf.Phdr, + dlpi_phnum: u16, +}; + +pub const Flock = extern struct { + l_start: off_t, + l_len: off_t, + l_pid: pid_t, + l_type: i16, + l_whence: i16, +}; + +pub const addrinfo = extern struct { + flags: i32, + family: i32, + socktype: i32, + protocol: i32, + addrlen: socklen_t, + canonname: ?[*:0]u8, + addr: ?*sockaddr, + next: ?*addrinfo, +}; + +pub const EAI = enum(c_int) { + /// address family for hostname not supported + ADDRFAMILY = 1, + + /// name could not be resolved at this time + AGAIN = 2, + + /// flags parameter had an invalid value + BADFLAGS = 3, + + /// non-recoverable failure in name resolution + FAIL = 4, + + /// address family not recognized + FAMILY = 5, + + /// memory allocation failure + MEMORY = 6, + + /// no address associated with hostname + NODATA = 7, + + /// name does not resolve + NONAME = 8, + + /// service not recognized for socket type + SERVICE = 9, + + /// intended socket type was not recognized + SOCKTYPE = 10, + + /// system error returned in errno + SYSTEM = 11, + + /// invalid value for hints + BADHINTS = 12, + + /// resolved protocol is unknown + PROTOCOL = 13, + + /// argument buffer overflow + OVERFLOW = 14, + + _, +}; + +pub const EAI_MAX = 15; + +pub const msghdr = extern struct { + /// optional address + msg_name: ?*sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +pub const msghdr_const = extern struct { + /// optional address + msg_name: ?*const sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec_const, + + /// # elements in msg_iov + msg_iovlen: i32, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: i32, +}; + +/// The stat structure used by libc. +pub const Stat = extern struct { + dev: dev_t, + mode: mode_t, + ino: ino_t, + nlink: nlink_t, + uid: uid_t, + gid: gid_t, + rdev: dev_t, + atim: timespec, + mtim: timespec, + ctim: timespec, + birthtim: timespec, + size: off_t, + blocks: blkcnt_t, + blksize: blksize_t, + flags: u32, + gen: u32, + __spare: [2]u32, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } +}; + +pub const timespec = extern struct { + tv_sec: i64, + tv_nsec: isize, +}; + +pub const timeval = extern struct { + /// seconds + tv_sec: time_t, + /// microseconds + tv_usec: suseconds_t, +}; + +pub const MAXNAMLEN = 511; + +pub const dirent = extern struct { + d_fileno: ino_t, + d_reclen: u16, + d_namlen: u16, + d_type: u8, + d_name: [MAXNAMLEN:0]u8, + + pub fn reclen(self: dirent) u16 { + return self.d_reclen; + } +}; + +pub const SOCK = struct { + pub const STREAM = 1; + pub const DGRAM = 2; + pub const RAW = 3; + pub const RDM = 4; + pub const SEQPACKET = 5; + pub const CONN_DGRAM = 6; + pub const DCCP = CONN_DGRAM; + + pub const CLOEXEC = 0x10000000; + pub const NONBLOCK = 0x20000000; + pub const NOSIGPIPE = 0x40000000; + pub const FLAGS_MASK = 0xf0000000; +}; + +pub const SO = struct { + pub const DEBUG = 0x0001; + pub const ACCEPTCONN = 0x0002; + pub const REUSEADDR = 0x0004; + pub const KEEPALIVE = 0x0008; + pub const DONTROUTE = 0x0010; + pub const BROADCAST = 0x0020; + pub const USELOOPBACK = 0x0040; + pub const LINGER = 0x0080; + pub const OOBINLINE = 0x0100; + pub const REUSEPORT = 0x0200; + pub const NOSIGPIPE = 0x0800; + pub const ACCEPTFILTER = 0x1000; + pub const TIMESTAMP = 0x2000; + pub const RERROR = 0x4000; + + pub const SNDBUF = 0x1001; + pub const RCVBUF = 0x1002; + pub const SNDLOWAT = 0x1003; + pub const RCVLOWAT = 0x1004; + pub const ERROR = 0x1007; + pub const TYPE = 0x1008; + pub const OVERFLOWED = 0x1009; + + pub const NOHEADER = 0x100a; + pub const SNDTIMEO = 0x100b; + pub const RCVTIMEO = 0x100c; +}; + +pub const SOL = struct { + pub const SOCKET = 0xffff; +}; + +pub const PF = struct { + pub const UNSPEC = AF.UNSPEC; + pub const LOCAL = AF.LOCAL; + pub const UNIX = PF.LOCAL; + pub const INET = AF.INET; + pub const IMPLINK = AF.IMPLINK; + pub const PUP = AF.PUP; + pub const CHAOS = AF.CHAOS; + pub const NS = AF.NS; + pub const ISO = AF.ISO; + pub const OSI = AF.ISO; + pub const ECMA = AF.ECMA; + pub const DATAKIT = AF.DATAKIT; + pub const CCITT = AF.CCITT; + pub const SNA = AF.SNA; + pub const DECnet = AF.DECnet; + pub const DLI = AF.DLI; + pub const LAT = AF.LAT; + pub const HYLINK = AF.HYLINK; + pub const APPLETALK = AF.APPLETALK; + pub const OROUTE = AF.OROUTE; + pub const LINK = AF.LINK; + pub const COIP = AF.COIP; + pub const CNT = AF.CNT; + pub const INET6 = AF.INET6; + pub const IPX = AF.IPX; + pub const ISDN = AF.ISDN; + pub const E164 = AF.E164; + pub const NATM = AF.NATM; + pub const ARP = AF.ARP; + pub const BLUETOOTH = AF.BLUETOOTH; + pub const MPLS = AF.MPLS; + pub const ROUTE = AF.ROUTE; + pub const CAN = AF.CAN; + pub const ETHER = AF.ETHER; + pub const MAX = AF.MAX; +}; + +pub const AF = struct { + pub const UNSPEC = 0; + pub const LOCAL = 1; + pub const UNIX = LOCAL; + pub const INET = 2; + pub const IMPLINK = 3; + pub const PUP = 4; + pub const CHAOS = 5; + pub const NS = 6; + pub const ISO = 7; + pub const OSI = ISO; + pub const ECMA = 8; + pub const DATAKIT = 9; + pub const CCITT = 10; + pub const SNA = 11; + pub const DECnet = 12; + pub const DLI = 13; + pub const LAT = 14; + pub const HYLINK = 15; + pub const APPLETALK = 16; + pub const OROUTE = 17; + pub const LINK = 18; + pub const COIP = 20; + pub const CNT = 21; + pub const IPX = 23; + pub const INET6 = 24; + pub const ISDN = 26; + pub const E164 = ISDN; + pub const NATM = 27; + pub const ARP = 28; + pub const BLUETOOTH = 31; + pub const IEEE80211 = 32; + pub const MPLS = 33; + pub const ROUTE = 34; + pub const CAN = 35; + pub const ETHER = 36; + pub const MAX = 37; +}; + +pub const in_port_t = u16; +pub const sa_family_t = u8; + +pub const sockaddr = extern struct { + /// total length + len: u8, + /// address family + family: sa_family_t, + /// actually longer; address value + data: [14]u8, + + pub const storage = std.x.os.Socket.Address.Native.Storage; + + pub const in = extern struct { + len: u8 = @sizeOf(in), + family: sa_family_t = AF.INET, + port: in_port_t, + addr: u32, + zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, + }; + + pub const in6 = extern struct { + len: u8 = @sizeOf(in6), + family: sa_family_t = AF.INET6, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, + }; + + /// Definitions for UNIX IPC domain. + pub const un = extern struct { + /// total sockaddr length + len: u8 = @sizeOf(un), + + family: sa_family_t = AF.LOCAL, + + /// path name + path: [104]u8, + }; +}; + +pub const AI = struct { + /// get address to use bind() + pub const PASSIVE = 0x00000001; + /// fill ai_canonname + pub const CANONNAME = 0x00000002; + /// prevent host name resolution + pub const NUMERICHOST = 0x00000004; + /// prevent service name resolution + pub const NUMERICSERV = 0x00000008; + /// only if any address is assigned + pub const ADDRCONFIG = 0x00000400; +}; + +pub const CTL = struct { + pub const KERN = 1; + pub const DEBUG = 5; +}; + +pub const KERN = struct { + pub const PROC_ARGS = 48; // struct: process argv/env + pub const PROC_PATHNAME = 5; // path to executable + pub const IOV_MAX = 38; +}; + +pub const PATH_MAX = 1024; +pub const IOV_MAX = KERN.IOV_MAX; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT = struct { + pub const NONE = 0; + pub const READ = 1; + pub const WRITE = 2; + pub const EXEC = 4; +}; + +pub const CLOCK = struct { + pub const REALTIME = 0; + pub const VIRTUAL = 1; + pub const PROF = 2; + pub const MONOTONIC = 3; + pub const THREAD_CPUTIME_ID = 0x20000000; + pub const PROCESS_CPUTIME_ID = 0x40000000; +}; + +pub const MAP = struct { + pub const FAILED = @intToPtr(*c_void, maxInt(usize)); + pub const SHARED = 0x0001; + pub const PRIVATE = 0x0002; + pub const REMAPDUP = 0x0004; + pub const FIXED = 0x0010; + pub const RENAME = 0x0020; + pub const NORESERVE = 0x0040; + pub const INHERIT = 0x0080; + pub const HASSEMAPHORE = 0x0200; + pub const TRYFIXED = 0x0400; + pub const WIRED = 0x0800; + + pub const FILE = 0x0000; + pub const NOSYNC = 0x0800; + pub const ANON = 0x1000; + pub const ANONYMOUS = ANON; + pub const STACK = 0x2000; +}; + +pub const W = struct { + pub const NOHANG = 0x00000001; + pub const UNTRACED = 0x00000002; + pub const STOPPED = UNTRACED; + pub const CONTINUED = 0x00000010; + pub const NOWAIT = 0x00010000; + pub const EXITED = 0x00000020; + pub const TRAPPED = 0x00000040; + + pub fn EXITSTATUS(s: u32) u8 { + return @intCast(u8, (s >> 8) & 0xff); + } + pub fn TERMSIG(s: u32) u32 { + return s & 0x7f; + } + pub fn STOPSIG(s: u32) u32 { + return EXITSTATUS(s); + } + pub fn IFEXITED(s: u32) bool { + return TERMSIG(s) == 0; + } + + pub fn IFCONTINUED(s: u32) bool { + return ((s & 0x7f) == 0xffff); + } + + pub fn IFSTOPPED(s: u32) bool { + return ((s & 0x7f != 0x7f) and !IFCONTINUED(s)); + } + + pub fn IFSIGNALED(s: u32) bool { + return !IFSTOPPED(s) and !IFCONTINUED(s) and !IFEXITED(s); + } +}; + +pub const SA = struct { + pub const ONSTACK = 0x0001; + pub const RESTART = 0x0002; + pub const RESETHAND = 0x0004; + pub const NOCLDSTOP = 0x0008; + pub const NODEFER = 0x0010; + pub const NOCLDWAIT = 0x0020; + pub const SIGINFO = 0x0040; +}; + +// access function +pub const F_OK = 0; // test for existence of file +pub const X_OK = 1; // test for execute or search permission +pub const W_OK = 2; // test for write permission +pub const R_OK = 4; // test for read permission + +pub const O = struct { + /// open for reading only + pub const RDONLY = 0x00000000; + /// open for writing only + pub const WRONLY = 0x00000001; + /// open for reading and writing + pub const RDWR = 0x00000002; + /// mask for above modes + pub const ACCMODE = 0x00000003; + /// no delay + pub const NONBLOCK = 0x00000004; + /// set append mode + pub const APPEND = 0x00000008; + /// open with shared file lock + pub const SHLOCK = 0x00000010; + /// open with exclusive file lock + pub const EXLOCK = 0x00000020; + /// signal pgrp when data ready + pub const ASYNC = 0x00000040; + /// synchronous writes + pub const SYNC = 0x00000080; + /// don't follow symlinks on the last + pub const NOFOLLOW = 0x00000100; + /// create if nonexistent + pub const CREAT = 0x00000200; + /// truncate to zero length + pub const TRUNC = 0x00000400; + /// error if already exists + pub const EXCL = 0x00000800; + /// don't assign controlling terminal + pub const NOCTTY = 0x00008000; + /// write: I/O data completion + pub const DSYNC = 0x00010000; + /// read: I/O completion as for write + pub const RSYNC = 0x00020000; + /// use alternate i/o semantics + pub const ALT_IO = 0x00040000; + /// direct I/O hint + pub const DIRECT = 0x00080000; + /// fail if not a directory + pub const DIRECTORY = 0x00200000; + /// set close on exec + pub const CLOEXEC = 0x00400000; + /// skip search permission checks + pub const SEARCH = 0x00800000; +}; + +pub const F = struct { + pub const DUPFD = 0; + pub const GETFD = 1; + pub const SETFD = 2; + pub const GETFL = 3; + pub const SETFL = 4; + + pub const GETOWN = 5; + pub const SETOWN = 6; + + pub const GETLK = 7; + pub const SETLK = 8; + pub const SETLKW = 9; + + pub const RDLCK = 1; + pub const WRLCK = 3; + pub const UNLCK = 2; +}; + +pub const LOCK = struct { + pub const SH = 1; + pub const EX = 2; + pub const UN = 8; + pub const NB = 4; +}; + +pub const FD_CLOEXEC = 1; + +pub const SEEK = struct { + pub const SET = 0; + pub const CUR = 1; + pub const END = 2; +}; + +pub const DT = struct { + pub const UNKNOWN = 0; + pub const FIFO = 1; + pub const CHR = 2; + pub const DIR = 4; + pub const BLK = 6; + pub const REG = 8; + pub const LNK = 10; + pub const SOCK = 12; + pub const WHT = 14; +}; + +/// add event to kq (implies enable) +pub const EV_ADD = 0x0001; + +/// delete event from kq +pub const EV_DELETE = 0x0002; + +/// enable event +pub const EV_ENABLE = 0x0004; + +/// disable event (not reported) +pub const EV_DISABLE = 0x0008; + +/// only report one occurrence +pub const EV_ONESHOT = 0x0010; + +/// clear event state after reporting +pub const EV_CLEAR = 0x0020; + +/// force immediate event output +/// ... with or without EV_ERROR +/// ... use KEVENT_FLAG_ERROR_EVENTS +/// on syscalls supporting flags +pub const EV_RECEIPT = 0x0040; + +/// disable event after reporting +pub const EV_DISPATCH = 0x0080; + +pub const EVFILT_READ = 0; +pub const EVFILT_WRITE = 1; + +/// attached to aio requests +pub const EVFILT_AIO = 2; + +/// attached to vnodes +pub const EVFILT_VNODE = 3; + +/// attached to struct proc +pub const EVFILT_PROC = 4; + +/// attached to struct proc +pub const EVFILT_SIGNAL = 5; + +/// timers +pub const EVFILT_TIMER = 6; + +/// Filesystem events +pub const EVFILT_FS = 7; + +/// User events +pub const EVFILT_USER = 1; + +/// On input, NOTE_TRIGGER causes the event to be triggered for output. +pub const NOTE_TRIGGER = 0x08000000; + +/// low water mark +pub const NOTE_LOWAT = 0x00000001; + +/// vnode was removed +pub const NOTE_DELETE = 0x00000001; + +/// data contents changed +pub const NOTE_WRITE = 0x00000002; + +/// size increased +pub const NOTE_EXTEND = 0x00000004; + +/// attributes changed +pub const NOTE_ATTRIB = 0x00000008; + +/// link count changed +pub const NOTE_LINK = 0x00000010; + +/// vnode was renamed +pub const NOTE_RENAME = 0x00000020; + +/// vnode access was revoked +pub const NOTE_REVOKE = 0x00000040; + +/// process exited +pub const NOTE_EXIT = 0x80000000; + +/// process forked +pub const NOTE_FORK = 0x40000000; + +/// process exec'd +pub const NOTE_EXEC = 0x20000000; + +/// mask for signal & exit status +pub const NOTE_PDATAMASK = 0x000fffff; +pub const NOTE_PCTRLMASK = 0xf0000000; + +pub const T = struct { + pub const IOCCBRK = 0x2000747a; + pub const IOCCDTR = 0x20007478; + pub const IOCCONS = 0x80047462; + pub const IOCDCDTIMESTAMP = 0x40107458; + pub const IOCDRAIN = 0x2000745e; + pub const IOCEXCL = 0x2000740d; + pub const IOCEXT = 0x80047460; + pub const IOCFLAG_CDTRCTS = 0x10; + pub const IOCFLAG_CLOCAL = 0x2; + pub const IOCFLAG_CRTSCTS = 0x4; + pub const IOCFLAG_MDMBUF = 0x8; + pub const IOCFLAG_SOFTCAR = 0x1; + pub const IOCFLUSH = 0x80047410; + pub const IOCGETA = 0x402c7413; + pub const IOCGETD = 0x4004741a; + pub const IOCGFLAGS = 0x4004745d; + pub const IOCGLINED = 0x40207442; + pub const IOCGPGRP = 0x40047477; + pub const IOCGQSIZE = 0x40047481; + pub const IOCGRANTPT = 0x20007447; + pub const IOCGSID = 0x40047463; + pub const IOCGSIZE = 0x40087468; + pub const IOCGWINSZ = 0x40087468; + pub const IOCMBIC = 0x8004746b; + pub const IOCMBIS = 0x8004746c; + pub const IOCMGET = 0x4004746a; + pub const IOCMSET = 0x8004746d; + pub const IOCM_CAR = 0x40; + pub const IOCM_CD = 0x40; + pub const IOCM_CTS = 0x20; + pub const IOCM_DSR = 0x100; + pub const IOCM_DTR = 0x2; + pub const IOCM_LE = 0x1; + pub const IOCM_RI = 0x80; + pub const IOCM_RNG = 0x80; + pub const IOCM_RTS = 0x4; + pub const IOCM_SR = 0x10; + pub const IOCM_ST = 0x8; + pub const IOCNOTTY = 0x20007471; + pub const IOCNXCL = 0x2000740e; + pub const IOCOUTQ = 0x40047473; + pub const IOCPKT = 0x80047470; + pub const IOCPKT_DATA = 0x0; + pub const IOCPKT_DOSTOP = 0x20; + pub const IOCPKT_FLUSHREAD = 0x1; + pub const IOCPKT_FLUSHWRITE = 0x2; + pub const IOCPKT_IOCTL = 0x40; + pub const IOCPKT_NOSTOP = 0x10; + pub const IOCPKT_START = 0x8; + pub const IOCPKT_STOP = 0x4; + pub const IOCPTMGET = 0x40287446; + pub const IOCPTSNAME = 0x40287448; + pub const IOCRCVFRAME = 0x80087445; + pub const IOCREMOTE = 0x80047469; + pub const IOCSBRK = 0x2000747b; + pub const IOCSCTTY = 0x20007461; + pub const IOCSDTR = 0x20007479; + pub const IOCSETA = 0x802c7414; + pub const IOCSETAF = 0x802c7416; + pub const IOCSETAW = 0x802c7415; + pub const IOCSETD = 0x8004741b; + pub const IOCSFLAGS = 0x8004745c; + pub const IOCSIG = 0x2000745f; + pub const IOCSLINED = 0x80207443; + pub const IOCSPGRP = 0x80047476; + pub const IOCSQSIZE = 0x80047480; + pub const IOCSSIZE = 0x80087467; + pub const IOCSTART = 0x2000746e; + pub const IOCSTAT = 0x80047465; + pub const IOCSTI = 0x80017472; + pub const IOCSTOP = 0x2000746f; + pub const IOCSWINSZ = 0x80087467; + pub const IOCUCNTL = 0x80047466; + pub const IOCXMTFRAME = 0x80087444; +}; + +pub const winsize = extern struct { + ws_row: u16, + ws_col: u16, + ws_xpixel: u16, + ws_ypixel: u16, +}; + +const NSIG = 32; + +pub const SIG = struct { + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + + pub const WORDS = 4; + pub const MAXSIG = 128; + + pub const BLOCK = 1; + pub const UNBLOCK = 2; + pub const SETMASK = 3; + + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const TRAP = 5; + pub const ABRT = 6; + pub const IOT = ABRT; + pub const EMT = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const BUS = 10; + pub const SEGV = 11; + pub const SYS = 12; + pub const PIPE = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const URG = 16; + pub const STOP = 17; + pub const TSTP = 18; + pub const CONT = 19; + pub const CHLD = 20; + pub const TTIN = 21; + pub const TTOU = 22; + pub const IO = 23; + pub const XCPU = 24; + pub const XFSZ = 25; + pub const VTALRM = 26; + pub const PROF = 27; + pub const WINCH = 28; + pub const INFO = 29; + pub const USR1 = 30; + pub const USR2 = 31; + pub const PWR = 32; + + pub const RTMIN = 33; + pub const RTMAX = 63; + + pub inline fn IDX(sig: usize) usize { + return sig - 1; + } + pub inline fn WORD(sig: usize) usize { + return IDX(sig) >> 5; + } + pub inline fn BIT(sig: usize) usize { + return 1 << (IDX(sig) & 31); + } + pub inline fn VALID(sig: usize) usize { + return sig <= MAXSIG and sig > 0; + } +}; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = extern struct { + pub const handler_fn = fn (c_int) callconv(.C) void; + pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; + + /// signal handler + handler: extern union { + handler: ?handler_fn, + sigaction: ?sigaction_fn, + }, + /// signal mask to apply + mask: sigset_t, + /// signal options + flags: c_uint, +}; + +pub const sigval_t = extern union { + int: i32, + ptr: ?*c_void, +}; + +pub const siginfo_t = extern union { + pad: [128]u8, + info: _ksiginfo, +}; + +pub const _ksiginfo = extern struct { + signo: i32, + code: i32, + errno: i32, + // 64bit architectures insert 4bytes of padding here, this is done by + // correctly aligning the reason field + reason: extern union { + rt: extern struct { + pid: pid_t, + uid: uid_t, + value: sigval_t, + }, + child: extern struct { + pid: pid_t, + uid: uid_t, + status: i32, + utime: clock_t, + stime: clock_t, + }, + fault: extern struct { + addr: ?*c_void, + trap: i32, + trap2: i32, + trap3: i32, + }, + poll: extern struct { + band: i32, + fd: i32, + }, + syscall: extern struct { + sysnum: i32, + retval: [2]i32, + @"error": i32, + args: [8]u64, + }, + ptrace_state: extern struct { + pe_report_event: i32, + option: extern union { + pe_other_pid: pid_t, + pe_lwp: lwpid_t, + }, + }, + } align(@sizeOf(usize)), +}; + +pub const sigset_t = extern struct { + __bits: [SIG.WORDS]u32, +}; + +pub const empty_sigset = sigset_t{ .__bits = [_]u32{0} ** SIG.WORDS }; + +// XXX x86_64 specific +pub const mcontext_t = extern struct { + gregs: [26]u64, + mc_tlsbase: u64, + fpregs: [512]u8 align(8), +}; + +pub const REG = struct { + pub const RBP = 12; + pub const RIP = 21; + pub const RSP = 24; +}; + +pub const ucontext_t = extern struct { + flags: u32, + link: ?*ucontext_t, + sigmask: sigset_t, + stack: stack_t, + mcontext: mcontext_t, + __pad: [ + switch (builtin.cpu.arch) { + .i386 => 4, + .mips, .mipsel, .mips64, .mips64el => 14, + .arm, .armeb, .thumb, .thumbeb => 1, + .sparc, .sparcel, .sparcv9 => if (@sizeOf(usize) == 4) 43 else 8, + else => 0, + } + ]u32, +}; + +pub const E = enum(u16) { + /// No error occurred. + SUCCESS = 0, + PERM = 1, // Operation not permitted + NOENT = 2, // No such file or directory + SRCH = 3, // No such process + INTR = 4, // Interrupted system call + IO = 5, // Input/output error + NXIO = 6, // Device not configured + @"2BIG" = 7, // Argument list too long + NOEXEC = 8, // Exec format error + BADF = 9, // Bad file descriptor + CHILD = 10, // No child processes + DEADLK = 11, // Resource deadlock avoided + // 11 was AGAIN + NOMEM = 12, // Cannot allocate memory + ACCES = 13, // Permission denied + FAULT = 14, // Bad address + NOTBLK = 15, // Block device required + BUSY = 16, // Device busy + EXIST = 17, // File exists + XDEV = 18, // Cross-device link + NODEV = 19, // Operation not supported by device + NOTDIR = 20, // Not a directory + ISDIR = 21, // Is a directory + INVAL = 22, // Invalid argument + NFILE = 23, // Too many open files in system + MFILE = 24, // Too many open files + NOTTY = 25, // Inappropriate ioctl for device + TXTBSY = 26, // Text file busy + FBIG = 27, // File too large + NOSPC = 28, // No space left on device + SPIPE = 29, // Illegal seek + ROFS = 30, // Read-only file system + MLINK = 31, // Too many links + PIPE = 32, // Broken pipe + + // math software + DOM = 33, // Numerical argument out of domain + RANGE = 34, // Result too large or too small + + // non-blocking and interrupt i/o + // also: WOULDBLOCK: operation would block + AGAIN = 35, // Resource temporarily unavailable + INPROGRESS = 36, // Operation now in progress + ALREADY = 37, // Operation already in progress + + // ipc/network software -- argument errors + NOTSOCK = 38, // Socket operation on non-socket + DESTADDRREQ = 39, // Destination address required + MSGSIZE = 40, // Message too long + PROTOTYPE = 41, // Protocol wrong type for socket + NOPROTOOPT = 42, // Protocol option not available + PROTONOSUPPORT = 43, // Protocol not supported + SOCKTNOSUPPORT = 44, // Socket type not supported + OPNOTSUPP = 45, // Operation not supported + PFNOSUPPORT = 46, // Protocol family not supported + AFNOSUPPORT = 47, // Address family not supported by protocol family + ADDRINUSE = 48, // Address already in use + ADDRNOTAVAIL = 49, // Can't assign requested address + + // ipc/network software -- operational errors + NETDOWN = 50, // Network is down + NETUNREACH = 51, // Network is unreachable + NETRESET = 52, // Network dropped connection on reset + CONNABORTED = 53, // Software caused connection abort + CONNRESET = 54, // Connection reset by peer + NOBUFS = 55, // No buffer space available + ISCONN = 56, // Socket is already connected + NOTCONN = 57, // Socket is not connected + SHUTDOWN = 58, // Can't send after socket shutdown + TOOMANYREFS = 59, // Too many references: can't splice + TIMEDOUT = 60, // Operation timed out + CONNREFUSED = 61, // Connection refused + + LOOP = 62, // Too many levels of symbolic links + NAMETOOLONG = 63, // File name too long + + // should be rearranged + HOSTDOWN = 64, // Host is down + HOSTUNREACH = 65, // No route to host + NOTEMPTY = 66, // Directory not empty + + // quotas & mush + PROCLIM = 67, // Too many processes + USERS = 68, // Too many users + DQUOT = 69, // Disc quota exceeded + + // Network File System + STALE = 70, // Stale NFS file handle + REMOTE = 71, // Too many levels of remote in path + BADRPC = 72, // RPC struct is bad + RPCMISMATCH = 73, // RPC version wrong + PROGUNAVAIL = 74, // RPC prog. not avail + PROGMISMATCH = 75, // Program version wrong + PROCUNAVAIL = 76, // Bad procedure for program + + NOLCK = 77, // No locks available + NOSYS = 78, // Function not implemented + + FTYPE = 79, // Inappropriate file type or format + AUTH = 80, // Authentication error + NEEDAUTH = 81, // Need authenticator + + // SystemV IPC + IDRM = 82, // Identifier removed + NOMSG = 83, // No message of desired type + OVERFLOW = 84, // Value too large to be stored in data type + + // Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995 + ILSEQ = 85, // Illegal byte sequence + + // From IEEE Std 1003.1-2001 + // Base, Realtime, Threads or Thread Priority Scheduling option errors + NOTSUP = 86, // Not supported + + // Realtime option errors + CANCELED = 87, // Operation canceled + + // Realtime, XSI STREAMS option errors + BADMSG = 88, // Bad or Corrupt message + + // XSI STREAMS option errors + NODATA = 89, // No message available + NOSR = 90, // No STREAM resources + NOSTR = 91, // Not a STREAM + TIME = 92, // STREAM ioctl timeout + + // File system extended attribute errors + NOATTR = 93, // Attribute not found + + // Realtime, XSI STREAMS option errors + MULTIHOP = 94, // Multihop attempted + NOLINK = 95, // Link has been severed + PROTO = 96, // Protocol error + + _, +}; + +pub const MINSIGSTKSZ = 8192; +pub const SIGSTKSZ = MINSIGSTKSZ + 32768; + +pub const SS_ONSTACK = 1; +pub const SS_DISABLE = 4; + +pub const stack_t = extern struct { + sp: [*]u8, + size: isize, + flags: i32, +}; + +pub const S = struct { + pub const IFMT = 0o170000; + + pub const IFIFO = 0o010000; + pub const IFCHR = 0o020000; + pub const IFDIR = 0o040000; + pub const IFBLK = 0o060000; + pub const IFREG = 0o100000; + pub const IFLNK = 0o120000; + pub const IFSOCK = 0o140000; + pub const IFWHT = 0o160000; + + pub const ISUID = 0o4000; + pub const ISGID = 0o2000; + pub const ISVTX = 0o1000; + pub const IRWXU = 0o700; + pub const IRUSR = 0o400; + pub const IWUSR = 0o200; + pub const IXUSR = 0o100; + pub const IRWXG = 0o070; + pub const IRGRP = 0o040; + pub const IWGRP = 0o020; + pub const IXGRP = 0o010; + pub const IRWXO = 0o007; + pub const IROTH = 0o004; + pub const IWOTH = 0o002; + pub const IXOTH = 0o001; + + pub fn ISFIFO(m: u32) bool { + return m & IFMT == IFIFO; + } + + pub fn ISCHR(m: u32) bool { + return m & IFMT == IFCHR; + } + + pub fn ISDIR(m: u32) bool { + return m & IFMT == IFDIR; + } + + pub fn ISBLK(m: u32) bool { + return m & IFMT == IFBLK; + } + + pub fn ISREG(m: u32) bool { + return m & IFMT == IFREG; + } + + pub fn ISLNK(m: u32) bool { + return m & IFMT == IFLNK; + } + + pub fn ISSOCK(m: u32) bool { + return m & IFMT == IFSOCK; + } + + pub fn IWHT(m: u32) bool { + return m & IFMT == IFWHT; + } +}; + +pub const AT = struct { + /// Magic value that specify the use of the current working directory + /// to determine the target of relative file paths in the openat() and + /// similar syscalls. + pub const FDCWD = -100; + /// Check access using effective user and group ID + pub const EACCESS = 0x0100; + /// Do not follow symbolic links + pub const SYMLINK_NOFOLLOW = 0x0200; + /// Follow symbolic link + pub const SYMLINK_FOLLOW = 0x0400; + /// Remove directory instead of file + pub const REMOVEDIR = 0x0800; +}; + +pub const HOST_NAME_MAX = 255; + +pub const IPPROTO = struct { + /// dummy for IP + pub const IP = 0; + /// IP6 hop-by-hop options + pub const HOPOPTS = 0; + /// control message protocol + pub const ICMP = 1; + /// group mgmt protocol + pub const IGMP = 2; + /// gateway^2 (deprecated) + pub const GGP = 3; + /// IP header + pub const IPV4 = 4; + /// IP inside IP + pub const IPIP = 4; + /// tcp + pub const TCP = 6; + /// exterior gateway protocol + pub const EGP = 8; + /// pup + pub const PUP = 12; + /// user datagram protocol + pub const UDP = 17; + /// xns idp + pub const IDP = 22; + /// tp-4 w/ class negotiation + pub const TP = 29; + /// DCCP + pub const DCCP = 33; + /// IP6 header + pub const IPV6 = 41; + /// IP6 routing header + pub const ROUTING = 43; + /// IP6 fragmentation header + pub const FRAGMENT = 44; + /// resource reservation + pub const RSVP = 46; + /// GRE encaps RFC 1701 + pub const GRE = 47; + /// encap. security payload + pub const ESP = 50; + /// authentication header + pub const AH = 51; + /// IP Mobility RFC 2004 + pub const MOBILE = 55; + /// IPv6 ICMP + pub const IPV6_ICMP = 58; + /// ICMP6 + pub const ICMPV6 = 58; + /// IP6 no next header + pub const NONE = 59; + /// IP6 destination option + pub const DSTOPTS = 60; + /// ISO cnlp + pub const EON = 80; + /// Ethernet-in-IP + pub const ETHERIP = 97; + /// encapsulation header + pub const ENCAP = 98; + /// Protocol indep. multicast + pub const PIM = 103; + /// IP Payload Comp. Protocol + pub const IPCOMP = 108; + /// VRRP RFC 2338 + pub const VRRP = 112; + /// Common Address Resolution Protocol + pub const CARP = 112; + /// L2TPv3 + pub const L2TP = 115; + /// SCTP + pub const SCTP = 132; + /// PFSYNC + pub const PFSYNC = 240; + /// raw IP packet + pub const RAW = 255; +}; + +pub const rlimit_resource = enum(c_int) { + CPU = 0, + FSIZE = 1, + DATA = 2, + STACK = 3, + CORE = 4, + RSS = 5, + MEMLOCK = 6, + NPROC = 7, + NOFILE = 8, + SBSIZE = 9, + VMEM = 10, + NTHR = 11, + _, + + pub const AS: rlimit_resource = .VMEM; +}; + +pub const rlim_t = u64; + +pub const RLIM = struct { + /// No limit + pub const INFINITY: rlim_t = (1 << 63) - 1; + + pub const SAVED_MAX = INFINITY; + pub const SAVED_CUR = INFINITY; +}; + +pub const rlimit = extern struct { + /// Soft limit + cur: rlim_t, + /// Hard limit + max: rlim_t, +}; + +pub const SHUT = struct { + pub const RD = 0; + pub const WR = 1; + pub const RDWR = 2; +}; + +pub const nfds_t = u32; + +pub const pollfd = extern struct { + fd: fd_t, + events: i16, + revents: i16, +}; + +pub const POLL = struct { + /// Testable events (may be specified in events field). + pub const IN = 0x0001; + pub const PRI = 0x0002; + pub const OUT = 0x0004; + pub const RDNORM = 0x0040; + pub const WRNORM = OUT; + pub const RDBAND = 0x0080; + pub const WRBAND = 0x0100; + + /// Non-testable events (may not be specified in events field). + pub const ERR = 0x0008; + pub const HUP = 0x0010; + pub const NVAL = 0x0020; +}; diff --git a/lib/std/c/openbsd.zig b/lib/std/c/openbsd.zig index e5ded7f3fb..b3919d4724 100644 --- a/lib/std/c/openbsd.zig +++ b/lib/std/c/openbsd.zig @@ -1,7 +1,8 @@ const std = @import("../std.zig"); -const builtin = std.builtin; - -usingnamespace std.c; +const maxInt = std.math.maxInt; +const builtin = @import("builtin"); +const iovec = std.os.iovec; +const iovec_const = std.os.iovec_const; extern "c" fn __errno() *c_int; pub const _errno = __errno; @@ -43,3 +44,1317 @@ pub extern "c" fn unveil(path: ?[*:0]const u8, permissions: ?[*:0]const u8) c_in pub extern "c" fn pthread_set_name_np(thread: std.c.pthread_t, name: [*:0]const u8) void; pub extern "c" fn pthread_get_name_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) void; + +pub const blkcnt_t = i64; +pub const blksize_t = i32; +pub const clock_t = i64; +pub const dev_t = i32; +pub const fd_t = c_int; +pub const gid_t = u32; +pub const ino_t = u64; +pub const mode_t = u32; +pub const nlink_t = u32; +pub const off_t = i64; +pub const pid_t = i32; +pub const socklen_t = u32; +pub const time_t = i64; +pub const uid_t = u32; + +/// Renamed from `kevent` to `Kevent` to avoid conflict with function name. +pub const Kevent = extern struct { + ident: usize, + filter: c_short, + flags: u16, + fflags: c_uint, + data: i64, + udata: usize, +}; + +// Modes and flags for dlopen() +// include/dlfcn.h + +pub const RTLD = struct { + /// Bind function calls lazily. + pub const LAZY = 1; + /// Bind function calls immediately. + pub const NOW = 2; + /// Make symbols globally available. + pub const GLOBAL = 0x100; + /// Opposite of GLOBAL, and the default. + pub const LOCAL = 0x000; + /// Trace loaded objects and exit. + pub const TRACE = 0x200; +}; + +pub const dl_phdr_info = extern struct { + dlpi_addr: std.elf.Addr, + dlpi_name: ?[*:0]const u8, + dlpi_phdr: [*]std.elf.Phdr, + dlpi_phnum: std.elf.Half, +}; + +pub const Flock = extern struct { + l_start: off_t, + l_len: off_t, + l_pid: pid_t, + l_type: c_short, + l_whence: c_short, +}; + +pub const addrinfo = extern struct { + flags: c_int, + family: c_int, + socktype: c_int, + protocol: c_int, + addrlen: socklen_t, + addr: ?*sockaddr, + canonname: ?[*:0]u8, + next: ?*addrinfo, +}; + +pub const EAI = enum(c_int) { + /// address family for hostname not supported + ADDRFAMILY = -9, + + /// name could not be resolved at this time + AGAIN = -3, + + /// flags parameter had an invalid value + BADFLAGS = -1, + + /// non-recoverable failure in name resolution + FAIL = -4, + + /// address family not recognized + FAMILY = -6, + + /// memory allocation failure + MEMORY = -10, + + /// no address associated with hostname + NODATA = -5, + + /// name does not resolve + NONAME = -2, + + /// service not recognized for socket type + SERVICE = -8, + + /// intended socket type was not recognized + SOCKTYPE = -7, + + /// system error returned in errno + SYSTEM = -11, + + /// invalid value for hints + BADHINTS = -12, + + /// resolved protocol is unknown + PROTOCOL = -13, + + /// argument buffer overflow + OVERFLOW = -14, + + _, +}; + +pub const EAI_MAX = 15; + +pub const msghdr = extern struct { + /// optional address + msg_name: ?*sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec, + + /// # elements in msg_iov + msg_iovlen: c_uint, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: c_int, +}; + +pub const msghdr_const = extern struct { + /// optional address + msg_name: ?*const sockaddr, + + /// size of address + msg_namelen: socklen_t, + + /// scatter/gather array + msg_iov: [*]iovec_const, + + /// # elements in msg_iov + msg_iovlen: c_uint, + + /// ancillary data + msg_control: ?*c_void, + + /// ancillary data buffer len + msg_controllen: socklen_t, + + /// flags on received message + msg_flags: c_int, +}; + +pub const Stat = extern struct { + mode: mode_t, + dev: dev_t, + ino: ino_t, + nlink: nlink_t, + uid: uid_t, + gid: gid_t, + rdev: dev_t, + atim: timespec, + mtim: timespec, + ctim: timespec, + size: off_t, + blocks: blkcnt_t, + blksize: blksize_t, + flags: u32, + gen: u32, + birthtim: timespec, + + pub fn atime(self: @This()) timespec { + return self.atim; + } + + pub fn mtime(self: @This()) timespec { + return self.mtim; + } + + pub fn ctime(self: @This()) timespec { + return self.ctim; + } +}; + +pub const timespec = extern struct { + tv_sec: time_t, + tv_nsec: c_long, +}; + +pub const timeval = extern struct { + tv_sec: time_t, + tv_usec: c_long, +}; + +pub const timezone = extern struct { + tz_minuteswest: c_int, + tz_dsttime: c_int, +}; + +pub const MAXNAMLEN = 255; + +pub const dirent = extern struct { + d_fileno: ino_t, + d_off: off_t, + d_reclen: u16, + d_type: u8, + d_namlen: u8, + __d_padding: [4]u8, + d_name: [MAXNAMLEN + 1]u8, + + pub fn reclen(self: dirent) u16 { + return self.d_reclen; + } +}; + +pub const in_port_t = u16; +pub const sa_family_t = u8; + +pub const sockaddr = extern struct { + /// total length + len: u8, + /// address family + family: sa_family_t, + /// actually longer; address value + data: [14]u8, + + pub const storage = std.x.os.Socket.Address.Native.Storage; + + pub const in = extern struct { + len: u8 = @sizeOf(in), + family: sa_family_t = AF.INET, + port: in_port_t, + addr: u32, + zero: [8]u8 = [8]u8{ 0, 0, 0, 0, 0, 0, 0, 0 }, + }; + + pub const in6 = extern struct { + len: u8 = @sizeOf(in6), + family: sa_family_t = AF.INET6, + port: in_port_t, + flowinfo: u32, + addr: [16]u8, + scope_id: u32, + }; + + /// Definitions for UNIX IPC domain. + pub const un = extern struct { + /// total sockaddr length + len: u8 = @sizeOf(un), + + family: sa_family_t = AF.LOCAL, + + /// path name + path: [104]u8, + }; +}; + +pub const AI = struct { + /// get address to use bind() + pub const PASSIVE = 1; + /// fill ai_canonname + pub const CANONNAME = 2; + /// prevent host name resolution + pub const NUMERICHOST = 4; + /// prevent service name resolution + pub const NUMERICSERV = 16; + /// only if any address is assigned + pub const ADDRCONFIG = 64; +}; + +pub const PATH_MAX = 1024; +pub const IOV_MAX = 1024; + +pub const STDIN_FILENO = 0; +pub const STDOUT_FILENO = 1; +pub const STDERR_FILENO = 2; + +pub const PROT = struct { + pub const NONE = 0; + pub const READ = 1; + pub const WRITE = 2; + pub const EXEC = 4; +}; + +pub const CLOCK = struct { + pub const REALTIME = 0; + pub const PROCESS_CPUTIME_ID = 2; + pub const MONOTONIC = 3; + pub const THREAD_CPUTIME_ID = 4; +}; + +pub const MAP = struct { + pub const FAILED = @intToPtr(*c_void, maxInt(usize)); + pub const SHARED = 0x0001; + pub const PRIVATE = 0x0002; + pub const FIXED = 0x0010; + pub const RENAME = 0; + pub const NORESERVE = 0; + pub const INHERIT = 0; + pub const HASSEMAPHORE = 0; + pub const TRYFIXED = 0; + + pub const FILE = 0; + pub const ANON = 0x1000; + pub const ANONYMOUS = ANON; + pub const STACK = 0x4000; + pub const CONCEAL = 0x8000; +}; + +pub const W = struct { + pub const NOHANG = 1; + pub const UNTRACED = 2; + pub const CONTINUED = 8; + + pub fn EXITSTATUS(s: u32) u8 { + return @intCast(u8, (s >> 8) & 0xff); + } + pub fn TERMSIG(s: u32) u32 { + return (s & 0x7f); + } + pub fn STOPSIG(s: u32) u32 { + return EXITSTATUS(s); + } + pub fn IFEXITED(s: u32) bool { + return TERMSIG(s) == 0; + } + + pub fn IFCONTINUED(s: u32) bool { + return ((s & 0o177777) == 0o177777); + } + + pub fn IFSTOPPED(s: u32) bool { + return (s & 0xff == 0o177); + } + + pub fn IFSIGNALED(s: u32) bool { + return (((s) & 0o177) != 0o177) and (((s) & 0o177) != 0); + } +}; + +pub const SA = struct { + pub const ONSTACK = 0x0001; + pub const RESTART = 0x0002; + pub const RESETHAND = 0x0004; + pub const NOCLDSTOP = 0x0008; + pub const NODEFER = 0x0010; + pub const NOCLDWAIT = 0x0020; + pub const SIGINFO = 0x0040; +}; + +// access function +pub const F_OK = 0; // test for existence of file +pub const X_OK = 1; // test for execute or search permission +pub const W_OK = 2; // test for write permission +pub const R_OK = 4; // test for read permission + +pub const O = struct { + /// open for reading only + pub const RDONLY = 0x00000000; + /// open for writing only + pub const WRONLY = 0x00000001; + /// open for reading and writing + pub const RDWR = 0x00000002; + /// mask for above modes + pub const ACCMODE = 0x00000003; + /// no delay + pub const NONBLOCK = 0x00000004; + /// set append mode + pub const APPEND = 0x00000008; + /// open with shared file lock + pub const SHLOCK = 0x00000010; + /// open with exclusive file lock + pub const EXLOCK = 0x00000020; + /// signal pgrp when data ready + pub const ASYNC = 0x00000040; + /// synchronous writes + pub const SYNC = 0x00000080; + /// don't follow symlinks on the last + pub const NOFOLLOW = 0x00000100; + /// create if nonexistent + pub const CREAT = 0x00000200; + /// truncate to zero length + pub const TRUNC = 0x00000400; + /// error if already exists + pub const EXCL = 0x00000800; + /// don't assign controlling terminal + pub const NOCTTY = 0x00008000; + /// write: I/O data completion + pub const DSYNC = SYNC; + /// read: I/O completion as for write + pub const RSYNC = SYNC; + /// fail if not a directory + pub const DIRECTORY = 0x20000; + /// set close on exec + pub const CLOEXEC = 0x10000; +}; + +pub const F = struct { + pub const DUPFD = 0; + pub const GETFD = 1; + pub const SETFD = 2; + pub const GETFL = 3; + pub const SETFL = 4; + + pub const GETOWN = 5; + pub const SETOWN = 6; + + pub const GETLK = 7; + pub const SETLK = 8; + pub const SETLKW = 9; + + pub const RDLCK = 1; + pub const UNLCK = 2; + pub const WRLCK = 3; +}; + +pub const LOCK = struct { + pub const SH = 0x01; + pub const EX = 0x02; + pub const NB = 0x04; + pub const UN = 0x08; +}; + +pub const FD_CLOEXEC = 1; + +pub const SEEK = struct { + pub const SET = 0; + pub const CUR = 1; + pub const END = 2; +}; + +pub const SOCK = struct { + pub const STREAM = 1; + pub const DGRAM = 2; + pub const RAW = 3; + pub const RDM = 4; + pub const SEQPACKET = 5; + + pub const CLOEXEC = 0x8000; + pub const NONBLOCK = 0x4000; +}; + +pub const SO = struct { + pub const DEBUG = 0x0001; + pub const ACCEPTCONN = 0x0002; + pub const REUSEADDR = 0x0004; + pub const KEEPALIVE = 0x0008; + pub const DONTROUTE = 0x0010; + pub const BROADCAST = 0x0020; + pub const USELOOPBACK = 0x0040; + pub const LINGER = 0x0080; + pub const OOBINLINE = 0x0100; + pub const REUSEPORT = 0x0200; + pub const TIMESTAMP = 0x0800; + pub const BINDANY = 0x1000; + pub const ZEROIZE = 0x2000; + pub const SNDBUF = 0x1001; + pub const RCVBUF = 0x1002; + pub const SNDLOWAT = 0x1003; + pub const RCVLOWAT = 0x1004; + pub const SNDTIMEO = 0x1005; + pub const RCVTIMEO = 0x1006; + pub const ERROR = 0x1007; + pub const TYPE = 0x1008; + pub const NETPROC = 0x1020; + pub const RTABLE = 0x1021; + pub const PEERCRED = 0x1022; + pub const SPLICE = 0x1023; + pub const DOMAIN = 0x1024; + pub const PROTOCOL = 0x1025; +}; + +pub const SOL = struct { + pub const SOCKET = 0xffff; +}; + +pub const PF = struct { + pub const UNSPEC = AF.UNSPEC; + pub const LOCAL = AF.LOCAL; + pub const UNIX = AF.UNIX; + pub const INET = AF.INET; + pub const APPLETALK = AF.APPLETALK; + pub const INET6 = AF.INET6; + pub const DECnet = AF.DECnet; + pub const KEY = AF.KEY; + pub const ROUTE = AF.ROUTE; + pub const SNA = AF.SNA; + pub const MPLS = AF.MPLS; + pub const BLUETOOTH = AF.BLUETOOTH; + pub const ISDN = AF.ISDN; + pub const MAX = AF.MAX; +}; + +pub const AF = struct { + pub const UNSPEC = 0; + pub const UNIX = 1; + pub const LOCAL = UNIX; + pub const INET = 2; + pub const APPLETALK = 16; + pub const INET6 = 24; + pub const KEY = 30; + pub const ROUTE = 17; + pub const SNA = 11; + pub const MPLS = 33; + pub const BLUETOOTH = 32; + pub const ISDN = 26; + pub const MAX = 36; +}; + +pub const DT = struct { + pub const UNKNOWN = 0; + pub const FIFO = 1; + pub const CHR = 2; + pub const DIR = 4; + pub const BLK = 6; + pub const REG = 8; + pub const LNK = 10; + pub const SOCK = 12; + pub const WHT = 14; // XXX +}; + +pub const EV_ADD = 0x0001; +pub const EV_DELETE = 0x0002; +pub const EV_ENABLE = 0x0004; +pub const EV_DISABLE = 0x0008; +pub const EV_ONESHOT = 0x0010; +pub const EV_CLEAR = 0x0020; +pub const EV_RECEIPT = 0x0040; +pub const EV_DISPATCH = 0x0080; +pub const EV_FLAG1 = 0x2000; +pub const EV_ERROR = 0x4000; +pub const EV_EOF = 0x8000; + +pub const EVFILT_READ = -1; +pub const EVFILT_WRITE = -2; +pub const EVFILT_AIO = -3; +pub const EVFILT_VNODE = -4; +pub const EVFILT_PROC = -5; +pub const EVFILT_SIGNAL = -6; +pub const EVFILT_TIMER = -7; +pub const EVFILT_EXCEPT = -9; + +// data/hint flags for EVFILT_{READ|WRITE} +pub const NOTE_LOWAT = 0x0001; +pub const NOTE_EOF = 0x0002; + +// data/hint flags for EVFILT_EXCEPT and EVFILT_{READ|WRITE} +pub const NOTE_OOB = 0x0004; + +// data/hint flags for EVFILT_VNODE +pub const NOTE_DELETE = 0x0001; +pub const NOTE_WRITE = 0x0002; +pub const NOTE_EXTEND = 0x0004; +pub const NOTE_ATTRIB = 0x0008; +pub const NOTE_LINK = 0x0010; +pub const NOTE_RENAME = 0x0020; +pub const NOTE_REVOKE = 0x0040; +pub const NOTE_TRUNCATE = 0x0080; + +// data/hint flags for EVFILT_PROC +pub const NOTE_EXIT = 0x80000000; +pub const NOTE_FORK = 0x40000000; +pub const NOTE_EXEC = 0x20000000; +pub const NOTE_PDATAMASK = 0x000fffff; +pub const NOTE_PCTRLMASK = 0xf0000000; +pub const NOTE_TRACK = 0x00000001; +pub const NOTE_TRACKERR = 0x00000002; +pub const NOTE_CHILD = 0x00000004; + +// data/hint flags for EVFILT_DEVICE +pub const NOTE_CHANGE = 0x00000001; + +pub const T = struct { + pub const IOCCBRK = 0x2000747a; + pub const IOCCDTR = 0x20007478; + pub const IOCCONS = 0x80047462; + pub const IOCDCDTIMESTAMP = 0x40107458; + pub const IOCDRAIN = 0x2000745e; + pub const IOCEXCL = 0x2000740d; + pub const IOCEXT = 0x80047460; + pub const IOCFLAG_CDTRCTS = 0x10; + pub const IOCFLAG_CLOCAL = 0x2; + pub const IOCFLAG_CRTSCTS = 0x4; + pub const IOCFLAG_MDMBUF = 0x8; + pub const IOCFLAG_SOFTCAR = 0x1; + pub const IOCFLUSH = 0x80047410; + pub const IOCGETA = 0x402c7413; + pub const IOCGETD = 0x4004741a; + pub const IOCGFLAGS = 0x4004745d; + pub const IOCGLINED = 0x40207442; + pub const IOCGPGRP = 0x40047477; + pub const IOCGQSIZE = 0x40047481; + pub const IOCGRANTPT = 0x20007447; + pub const IOCGSID = 0x40047463; + pub const IOCGSIZE = 0x40087468; + pub const IOCGWINSZ = 0x40087468; + pub const IOCMBIC = 0x8004746b; + pub const IOCMBIS = 0x8004746c; + pub const IOCMGET = 0x4004746a; + pub const IOCMSET = 0x8004746d; + pub const IOCM_CAR = 0x40; + pub const IOCM_CD = 0x40; + pub const IOCM_CTS = 0x20; + pub const IOCM_DSR = 0x100; + pub const IOCM_DTR = 0x2; + pub const IOCM_LE = 0x1; + pub const IOCM_RI = 0x80; + pub const IOCM_RNG = 0x80; + pub const IOCM_RTS = 0x4; + pub const IOCM_SR = 0x10; + pub const IOCM_ST = 0x8; + pub const IOCNOTTY = 0x20007471; + pub const IOCNXCL = 0x2000740e; + pub const IOCOUTQ = 0x40047473; + pub const IOCPKT = 0x80047470; + pub const IOCPKT_DATA = 0x0; + pub const IOCPKT_DOSTOP = 0x20; + pub const IOCPKT_FLUSHREAD = 0x1; + pub const IOCPKT_FLUSHWRITE = 0x2; + pub const IOCPKT_IOCTL = 0x40; + pub const IOCPKT_NOSTOP = 0x10; + pub const IOCPKT_START = 0x8; + pub const IOCPKT_STOP = 0x4; + pub const IOCPTMGET = 0x40287446; + pub const IOCPTSNAME = 0x40287448; + pub const IOCRCVFRAME = 0x80087445; + pub const IOCREMOTE = 0x80047469; + pub const IOCSBRK = 0x2000747b; + pub const IOCSCTTY = 0x20007461; + pub const IOCSDTR = 0x20007479; + pub const IOCSETA = 0x802c7414; + pub const IOCSETAF = 0x802c7416; + pub const IOCSETAW = 0x802c7415; + pub const IOCSETD = 0x8004741b; + pub const IOCSFLAGS = 0x8004745c; + pub const IOCSIG = 0x2000745f; + pub const IOCSLINED = 0x80207443; + pub const IOCSPGRP = 0x80047476; + pub const IOCSQSIZE = 0x80047480; + pub const IOCSSIZE = 0x80087467; + pub const IOCSTART = 0x2000746e; + pub const IOCSTAT = 0x80047465; + pub const IOCSTI = 0x80017472; + pub const IOCSTOP = 0x2000746f; + pub const IOCSWINSZ = 0x80087467; + pub const IOCUCNTL = 0x80047466; + pub const IOCXMTFRAME = 0x80087444; +}; + +pub const winsize = extern struct { + ws_row: c_ushort, + ws_col: c_ushort, + ws_xpixel: c_ushort, + ws_ypixel: c_ushort, +}; + +const NSIG = 33; + +pub const SIG = struct { + pub const DFL = @intToPtr(?Sigaction.sigaction_fn, 0); + pub const IGN = @intToPtr(?Sigaction.sigaction_fn, 1); + pub const ERR = @intToPtr(?Sigaction.sigaction_fn, maxInt(usize)); + pub const CATCH = @intToPtr(?Sigaction.sigaction_fn, 2); + pub const HOLD = @intToPtr(?Sigaction.sigaction_fn, 3); + + pub const HUP = 1; + pub const INT = 2; + pub const QUIT = 3; + pub const ILL = 4; + pub const TRAP = 5; + pub const ABRT = 6; + pub const IOT = ABRT; + pub const EMT = 7; + pub const FPE = 8; + pub const KILL = 9; + pub const BUS = 10; + pub const SEGV = 11; + pub const SYS = 12; + pub const PIPE = 13; + pub const ALRM = 14; + pub const TERM = 15; + pub const URG = 16; + pub const STOP = 17; + pub const TSTP = 18; + pub const CONT = 19; + pub const CHLD = 20; + pub const TTIN = 21; + pub const TTOU = 22; + pub const IO = 23; + pub const XCPU = 24; + pub const XFSZ = 25; + pub const VTALRM = 26; + pub const PROF = 27; + pub const WINCH = 28; + pub const INFO = 29; + pub const USR1 = 30; + pub const USR2 = 31; + pub const PWR = 32; + + pub const BLOCK = 1; + pub const UNBLOCK = 2; + pub const SETMASK = 3; +}; + +/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall. +pub const Sigaction = extern struct { + pub const handler_fn = fn (c_int) callconv(.C) void; + pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const c_void) callconv(.C) void; + + /// signal handler + handler: extern union { + handler: ?handler_fn, + sigaction: ?sigaction_fn, + }, + /// signal mask to apply + mask: sigset_t, + /// signal options + flags: c_uint, +}; + +pub const sigval = extern union { + int: c_int, + ptr: ?*c_void, +}; + +pub const siginfo_t = extern struct { + signo: c_int, + code: c_int, + errno: c_int, + data: extern union { + proc: extern struct { + pid: pid_t, + pdata: extern union { + kill: extern struct { + uid: uid_t, + value: sigval, + }, + cld: extern struct { + utime: clock_t, + stime: clock_t, + status: c_int, + }, + }, + }, + fault: extern struct { + addr: ?*c_void, + trapno: c_int, + }, + __pad: [128 - 3 * @sizeOf(c_int)]u8, + }, +}; + +comptime { + if (@sizeOf(usize) == 4) + std.debug.assert(@sizeOf(siginfo_t) == 128) + else + // Take into account the padding between errno and data fields. + std.debug.assert(@sizeOf(siginfo_t) == 136); +} + +const arch_bits = switch (builtin.cpu.arch) { + .x86_64 => struct { + pub const ucontext_t = extern struct { + sc_rdi: c_long, + sc_rsi: c_long, + sc_rdx: c_long, + sc_rcx: c_long, + sc_r8: c_long, + sc_r9: c_long, + sc_r10: c_long, + sc_r11: c_long, + sc_r12: c_long, + sc_r13: c_long, + sc_r14: c_long, + sc_r15: c_long, + sc_rbp: c_long, + sc_rbx: c_long, + sc_rax: c_long, + sc_gs: c_long, + sc_fs: c_long, + sc_es: c_long, + sc_ds: c_long, + sc_trapno: c_long, + sc_err: c_long, + sc_rip: c_long, + sc_cs: c_long, + sc_rflags: c_long, + sc_rsp: c_long, + sc_ss: c_long, + + sc_fpstate: arch_bits.fxsave64, + __sc_unused: c_int, + sc_mask: c_int, + sc_cookie: c_long, + }; + + pub const fxsave64 = packed struct { + fx_fcw: u16, + fx_fsw: u16, + fx_ftw: u8, + fx_unused1: u8, + fx_fop: u16, + fx_rip: u64, + fx_rdp: u64, + fx_mxcsr: u32, + fx_mxcsr_mask: u32, + fx_st: [8][2]u64, + fx_xmm: [16][2]u64, + fx_unused3: [96]u8, + }; + }, + else => struct {}, +}; +pub const ucontext_t = arch_bits.ucontext_t; +pub const fxsave64 = arch_bits.fxsave64; + +pub const sigset_t = c_uint; +pub const empty_sigset: sigset_t = 0; + +pub const E = enum(u16) { + /// No error occurred. + SUCCESS = 0, + PERM = 1, // Operation not permitted + NOENT = 2, // No such file or directory + SRCH = 3, // No such process + INTR = 4, // Interrupted system call + IO = 5, // Input/output error + NXIO = 6, // Device not configured + @"2BIG" = 7, // Argument list too long + NOEXEC = 8, // Exec format error + BADF = 9, // Bad file descriptor + CHILD = 10, // No child processes + DEADLK = 11, // Resource deadlock avoided + // 11 was AGAIN + NOMEM = 12, // Cannot allocate memory + ACCES = 13, // Permission denied + FAULT = 14, // Bad address + NOTBLK = 15, // Block device required + BUSY = 16, // Device busy + EXIST = 17, // File exists + XDEV = 18, // Cross-device link + NODEV = 19, // Operation not supported by device + NOTDIR = 20, // Not a directory + ISDIR = 21, // Is a directory + INVAL = 22, // Invalid argument + NFILE = 23, // Too many open files in system + MFILE = 24, // Too many open files + NOTTY = 25, // Inappropriate ioctl for device + TXTBSY = 26, // Text file busy + FBIG = 27, // File too large + NOSPC = 28, // No space left on device + SPIPE = 29, // Illegal seek + ROFS = 30, // Read-only file system + MLINK = 31, // Too many links + PIPE = 32, // Broken pipe + + // math software + DOM = 33, // Numerical argument out of domain + RANGE = 34, // Result too large or too small + + // non-blocking and interrupt i/o + // also: WOULDBLOCK: operation would block + AGAIN = 35, // Resource temporarily unavailable + INPROGRESS = 36, // Operation now in progress + ALREADY = 37, // Operation already in progress + + // ipc/network software -- argument errors + NOTSOCK = 38, // Socket operation on non-socket + DESTADDRREQ = 39, // Destination address required + MSGSIZE = 40, // Message too long + PROTOTYPE = 41, // Protocol wrong type for socket + NOPROTOOPT = 42, // Protocol option not available + PROTONOSUPPORT = 43, // Protocol not supported + SOCKTNOSUPPORT = 44, // Socket type not supported + OPNOTSUPP = 45, // Operation not supported + PFNOSUPPORT = 46, // Protocol family not supported + AFNOSUPPORT = 47, // Address family not supported by protocol family + ADDRINUSE = 48, // Address already in use + ADDRNOTAVAIL = 49, // Can't assign requested address + + // ipc/network software -- operational errors + NETDOWN = 50, // Network is down + NETUNREACH = 51, // Network is unreachable + NETRESET = 52, // Network dropped connection on reset + CONNABORTED = 53, // Software caused connection abort + CONNRESET = 54, // Connection reset by peer + NOBUFS = 55, // No buffer space available + ISCONN = 56, // Socket is already connected + NOTCONN = 57, // Socket is not connected + SHUTDOWN = 58, // Can't send after socket shutdown + TOOMANYREFS = 59, // Too many references: can't splice + TIMEDOUT = 60, // Operation timed out + CONNREFUSED = 61, // Connection refused + + LOOP = 62, // Too many levels of symbolic links + NAMETOOLONG = 63, // File name too long + + // should be rearranged + HOSTDOWN = 64, // Host is down + HOSTUNREACH = 65, // No route to host + NOTEMPTY = 66, // Directory not empty + + // quotas & mush + PROCLIM = 67, // Too many processes + USERS = 68, // Too many users + DQUOT = 69, // Disc quota exceeded + + // Network File System + STALE = 70, // Stale NFS file handle + REMOTE = 71, // Too many levels of remote in path + BADRPC = 72, // RPC struct is bad + RPCMISMATCH = 73, // RPC version wrong + PROGUNAVAIL = 74, // RPC prog. not avail + PROGMISMATCH = 75, // Program version wrong + PROCUNAVAIL = 76, // Bad procedure for program + + NOLCK = 77, // No locks available + NOSYS = 78, // Function not implemented + + FTYPE = 79, // Inappropriate file type or format + AUTH = 80, // Authentication error + NEEDAUTH = 81, // Need authenticator + IPSEC = 82, // IPsec processing failure + NOATTR = 83, // Attribute not found + + // Wide/multibyte-character handling, ISO/IEC 9899/AMD1:1995 + ILSEQ = 84, // Illegal byte sequence + + NOMEDIUM = 85, // No medium found + MEDIUMTYPE = 86, // Wrong medium type + OVERFLOW = 87, // Value too large to be stored in data type + CANCELED = 88, // Operation canceled + IDRM = 89, // Identifier removed + NOMSG = 90, // No message of desired type + NOTSUP = 91, // Not supported + BADMSG = 92, // Bad or Corrupt message + NOTRECOVERABLE = 93, // State not recoverable + OWNERDEAD = 94, // Previous owner died + PROTO = 95, // Protocol error + + _, +}; + +const _MAX_PAGE_SHIFT = switch (builtin.cpu.arch) { + .i386 => 12, + .sparcv9 => 13, +}; +pub const MINSIGSTKSZ = 1 << _MAX_PAGE_SHIFT; +pub const SIGSTKSZ = MINSIGSTKSZ + (1 << _MAX_PAGE_SHIFT) * 4; + +pub const SS_ONSTACK = 0x0001; +pub const SS_DISABLE = 0x0004; + +pub const stack_t = extern struct { + sp: [*]u8, + size: usize, + flags: c_int, +}; + +pub const S = struct { + pub const IFMT = 0o170000; + + pub const IFIFO = 0o010000; + pub const IFCHR = 0o020000; + pub const IFDIR = 0o040000; + pub const IFBLK = 0o060000; + pub const IFREG = 0o100000; + pub const IFLNK = 0o120000; + pub const IFSOCK = 0o140000; + + pub const ISUID = 0o4000; + pub const ISGID = 0o2000; + pub const ISVTX = 0o1000; + pub const IRWXU = 0o700; + pub const IRUSR = 0o400; + pub const IWUSR = 0o200; + pub const IXUSR = 0o100; + pub const IRWXG = 0o070; + pub const IRGRP = 0o040; + pub const IWGRP = 0o020; + pub const IXGRP = 0o010; + pub const IRWXO = 0o007; + pub const IROTH = 0o004; + pub const IWOTH = 0o002; + pub const IXOTH = 0o001; + + pub fn ISFIFO(m: u32) bool { + return m & IFMT == IFIFO; + } + + pub fn ISCHR(m: u32) bool { + return m & IFMT == IFCHR; + } + + pub fn ISDIR(m: u32) bool { + return m & IFMT == IFDIR; + } + + pub fn ISBLK(m: u32) bool { + return m & IFMT == IFBLK; + } + + pub fn ISREG(m: u32) bool { + return m & IFMT == IFREG; + } + + pub fn ISLNK(m: u32) bool { + return m & IFMT == IFLNK; + } + + pub fn ISSOCK(m: u32) bool { + return m & IFMT == IFSOCK; + } +}; + +pub const AT = struct { + /// Magic value that specify the use of the current working directory + /// to determine the target of relative file paths in the openat() and + /// similar syscalls. + pub const FDCWD = -100; + /// Check access using effective user and group ID + pub const EACCESS = 0x01; + /// Do not follow symbolic links + pub const SYMLINK_NOFOLLOW = 0x02; + /// Follow symbolic link + pub const SYMLINK_FOLLOW = 0x04; + /// Remove directory instead of file + pub const REMOVEDIR = 0x08; +}; + +pub const HOST_NAME_MAX = 255; + +pub const IPPROTO = struct { + /// dummy for IP + pub const IP = 0; + /// IP6 hop-by-hop options + pub const HOPOPTS = IP; + /// control message protocol + pub const ICMP = 1; + /// group mgmt protocol + pub const IGMP = 2; + /// gateway^2 (deprecated) + pub const GGP = 3; + /// IP header + pub const IPV4 = IPIP; + /// IP inside IP + pub const IPIP = 4; + /// tcp + pub const TCP = 6; + /// exterior gateway protocol + pub const EGP = 8; + /// pup + pub const PUP = 12; + /// user datagram protocol + pub const UDP = 17; + /// xns idp + pub const IDP = 22; + /// tp-4 w/ class negotiation + pub const TP = 29; + /// IP6 header + pub const IPV6 = 41; + /// IP6 routing header + pub const ROUTING = 43; + /// IP6 fragmentation header + pub const FRAGMENT = 44; + /// resource reservation + pub const RSVP = 46; + /// GRE encaps RFC 1701 + pub const GRE = 47; + /// encap. security payload + pub const ESP = 50; + /// authentication header + pub const AH = 51; + /// IP Mobility RFC 2004 + pub const MOBILE = 55; + /// IPv6 ICMP + pub const IPV6_ICMP = 58; + /// ICMP6 + pub const ICMPV6 = 58; + /// IP6 no next header + pub const NONE = 59; + /// IP6 destination option + pub const DSTOPTS = 60; + /// ISO cnlp + pub const EON = 80; + /// Ethernet-in-IP + pub const ETHERIP = 97; + /// encapsulation header + pub const ENCAP = 98; + /// Protocol indep. multicast + pub const PIM = 103; + /// IP Payload Comp. Protocol + pub const IPCOMP = 108; + /// VRRP RFC 2338 + pub const VRRP = 112; + /// Common Address Resolution Protocol + pub const CARP = 112; + /// PFSYNC + pub const PFSYNC = 240; + /// raw IP packet + pub const RAW = 255; +}; + +pub const rlimit_resource = enum(c_int) { + CPU, + FSIZE, + DATA, + STACK, + CORE, + RSS, + MEMLOCK, + NPROC, + NOFILE, + + _, +}; + +pub const rlim_t = u64; + +pub const RLIM = struct { + /// No limit + pub const INFINITY: rlim_t = (1 << 63) - 1; + + pub const SAVED_MAX = INFINITY; + pub const SAVED_CUR = INFINITY; +}; + +pub const rlimit = extern struct { + /// Soft limit + cur: rlim_t, + /// Hard limit + max: rlim_t, +}; + +pub const SHUT = struct { + pub const RD = 0; + pub const WR = 1; + pub const RDWR = 2; +}; + +pub const nfds_t = c_uint; + +pub const pollfd = extern struct { + fd: fd_t, + events: c_short, + revents: c_short, +}; + +pub const POLL = struct { + pub const IN = 0x0001; + pub const PRI = 0x0002; + pub const OUT = 0x0004; + pub const ERR = 0x0008; + pub const HUP = 0x0010; + pub const NVAL = 0x0020; + pub const RDNORM = 0x0040; + pub const NORM = RDNORM; + pub const WRNORM = OUT; + pub const RDBAND = 0x0080; + pub const WRBAND = 0x0100; +}; + +pub const CTL = struct { + pub const UNSPEC = 0; + pub const KERN = 1; + pub const VM = 2; + pub const FS = 3; + pub const NET = 4; + pub const DEBUG = 5; + pub const HW = 6; + pub const MACHDEP = 7; + + pub const DDB = 9; + pub const VFS = 10; +}; + +pub const KERN = struct { + pub const OSTYPE = 1; + pub const OSRELEASE = 2; + pub const OSREV = 3; + pub const VERSION = 4; + pub const MAXVNODES = 5; + pub const MAXPROC = 6; + pub const MAXFILES = 7; + pub const ARGMAX = 8; + pub const SECURELVL = 9; + pub const HOSTNAME = 10; + pub const HOSTID = 11; + pub const CLOCKRATE = 12; + + pub const PROF = 16; + pub const POSIX1 = 17; + pub const NGROUPS = 18; + pub const JOB_CONTROL = 19; + pub const SAVED_IDS = 20; + pub const BOOTTIME = 21; + pub const DOMAINNAME = 22; + pub const MAXPARTITIONS = 23; + pub const RAWPARTITION = 24; + pub const MAXTHREAD = 25; + pub const NTHREADS = 26; + pub const OSVERSION = 27; + pub const SOMAXCONN = 28; + pub const SOMINCONN = 29; + + pub const NOSUIDCOREDUMP = 32; + pub const FSYNC = 33; + pub const SYSVMSG = 34; + pub const SYSVSEM = 35; + pub const SYSVSHM = 36; + + pub const MSGBUFSIZE = 38; + pub const MALLOCSTATS = 39; + pub const CPTIME = 40; + pub const NCHSTATS = 41; + pub const FORKSTAT = 42; + pub const NSELCOLL = 43; + pub const TTY = 44; + pub const CCPU = 45; + pub const FSCALE = 46; + pub const NPROCS = 47; + pub const MSGBUF = 48; + pub const POOL = 49; + pub const STACKGAPRANDOM = 50; + pub const SYSVIPC_INFO = 51; + pub const ALLOWKMEM = 52; + pub const WITNESSWATCH = 53; + pub const SPLASSERT = 54; + pub const PROC_ARGS = 55; + pub const NFILES = 56; + pub const TTYCOUNT = 57; + pub const NUMVNODES = 58; + pub const MBSTAT = 59; + pub const WITNESS = 60; + pub const SEMINFO = 61; + pub const SHMINFO = 62; + pub const INTRCNT = 63; + pub const WATCHDOG = 64; + pub const ALLOWDT = 65; + pub const PROC = 66; + pub const MAXCLUSTERS = 67; + pub const EVCOUNT = 68; + pub const TIMECOUNTER = 69; + pub const MAXLOCKSPERUID = 70; + pub const CPTIME2 = 71; + pub const CACHEPCT = 72; + pub const FILE = 73; + pub const WXABORT = 74; + pub const CONSDEV = 75; + pub const NETLIVELOCKS = 76; + pub const POOL_DEBUG = 77; + pub const PROC_CWD = 78; + pub const PROC_NOBROADCASTKILL = 79; + pub const PROC_VMMAP = 80; + pub const GLOBAL_PTRACE = 81; + pub const CONSBUFSIZE = 82; + pub const CONSBUF = 83; + pub const AUDIO = 84; + pub const CPUSTATS = 85; + pub const PFSTATUS = 86; + pub const TIMEOUT_STATS = 87; + pub const UTC_OFFSET = 88; + pub const VIDEO = 89; + + pub const PROC_ALL = 0; + pub const PROC_PID = 1; + pub const PROC_PGRP = 2; + pub const PROC_SESSION = 3; + pub const PROC_TTY = 4; + pub const PROC_UID = 5; + pub const PROC_RUID = 6; + pub const PROC_KTHREAD = 7; + pub const PROC_SHOW_THREADS = 0x40000000; + + pub const PROC_ARGV = 1; + pub const PROC_NARGV = 2; + pub const PROC_ENV = 3; + pub const PROC_NENV = 4; +}; + +pub const HW_MACHINE = 1; +pub const HW_MODEL = 2; +pub const HW_NCPU = 3; +pub const HW_BYTEORDER = 4; +pub const HW_PHYSMEM = 5; +pub const HW_USERMEM = 6; +pub const HW_PAGESIZE = 7; +pub const HW_DISKNAMES = 8; +pub const HW_DISKSTATS = 9; +pub const HW_DISKCOUNT = 10; +pub const HW_SENSORS = 11; +pub const HW_CPUSPEED = 12; +pub const HW_SETPERF = 13; +pub const HW_VENDOR = 14; +pub const HW_PRODUCT = 15; +pub const HW_VERSION = 16; +pub const HW_SERIALNO = 17; +pub const HW_UUID = 18; +pub const HW_PHYSMEM64 = 19; +pub const HW_USERMEM64 = 20; +pub const HW_NCPUFOUND = 21; +pub const HW_ALLOWPOWERDOWN = 22; +pub const HW_PERFPOLICY = 23; +pub const HW_SMT = 24; +pub const HW_NCPUONLINE = 25; diff --git a/lib/std/c/wasi.zig b/lib/std/c/wasi.zig index 339bdcd127..d9fbed8e6d 100644 --- a/lib/std/c/wasi.zig +++ b/lib/std/c/wasi.zig @@ -1,4 +1,6 @@ -usingnamespace @import("../os/bits.zig"); +const std = @import("../std.zig"); +const wasi = std.os.wasi; +const FDFLAG = wasi.FDFLAG; extern threadlocal var errno: c_int; @@ -6,12 +8,25 @@ pub fn _errno() *c_int { return &errno; } +pub const fd_t = wasi.fd_t; pub const pid_t = c_int; pub const uid_t = u32; pub const gid_t = u32; pub const off_t = i64; +pub const ino_t = wasi.ino_t; +pub const mode_t = wasi.mode_t; +pub const time_t = wasi.time_t; +pub const timespec = wasi.timespec; +pub const STDERR_FILENO = wasi.STDERR_FILENO; +pub const STDIN_FILENO = wasi.STDIN_FILENO; +pub const STDOUT_FILENO = wasi.STDOUT_FILENO; +pub const E = wasi.E; +pub const CLOCK = wasi.CLOCK; +pub const S = wasi.S; +pub const IOV_MAX = wasi.IOV_MAX; +pub const AT = wasi.AT; -pub const libc_stat = extern struct { +pub const Stat = extern struct { dev: i32, ino: ino_t, nlink: u64, @@ -53,3 +68,33 @@ pub const libc_stat = extern struct { }; } }; + +/// Derived from +/// https://github.com/WebAssembly/wasi-libc/blob/main/expected/wasm32-wasi/predefined-macros.txt +pub const O = struct { + pub const ACCMODE = (EXEC | RDWR | SEARCH); + pub const APPEND = FDFLAG.APPEND; + pub const CLOEXEC = (0); + pub const CREAT = ((1 << 0) << 12); // = __WASI_OFLAGS_CREAT << 12 + pub const DIRECTORY = ((1 << 1) << 12); // = __WASI_OFLAGS_DIRECTORY << 12 + pub const DSYNC = FDFLAG.DSYNC; + pub const EXCL = ((1 << 2) << 12); // = __WASI_OFLAGS_EXCL << 12 + pub const EXEC = (0x02000000); + pub const NOCTTY = (0); + pub const NOFOLLOW = (0x01000000); + pub const NONBLOCK = (1 << FDFLAG.NONBLOCK); + pub const RDONLY = (0x04000000); + pub const RDWR = (RDONLY | WRONLY); + pub const RSYNC = (1 << FDFLAG.RSYNC); + pub const SEARCH = (0x08000000); + pub const SYNC = (1 << FDFLAG.SYNC); + pub const TRUNC = ((1 << 3) << 12); // = __WASI_OFLAGS_TRUNC << 12 + pub const TTY_INIT = (0); + pub const WRONLY = (0x10000000); +}; + +pub const SEEK = struct { + pub const SET: wasi.whence_t = .SET; + pub const CUR: wasi.whence_t = .CUR; + pub const END: wasi.whence_t = .END; +}; diff --git a/lib/std/c/windows.zig b/lib/std/c/windows.zig index a10715f083..1566e820fd 100644 --- a/lib/std/c/windows.zig +++ b/lib/std/c/windows.zig @@ -1,3 +1,247 @@ +//! The reference for these types and values is Microsoft Windows's ucrt (Universal C RunTime). +const std = @import("../std.zig"); +const ws2_32 = std.os.windows.ws2_32; +const windows = std.os.windows; + pub extern "c" fn _errno() *c_int; pub extern "c" fn _msize(memblock: ?*c_void) usize; + +// TODO: copied the else case and removed the socket function (because its in ws2_32) +// need to verify which of these is actually supported on windows +pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int; +pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int; +pub extern "c" fn fstat(fd: fd_t, buf: *Stat) c_int; +pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int; +pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int; +pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int; +pub extern "c" fn sched_yield() c_int; +pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int; +pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int; +pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int; +pub extern "c" fn sigfillset(set: ?*sigset_t) void; +pub extern "c" fn alarm(seconds: c_uint) c_uint; +pub extern "c" fn sigwait(set: ?*sigset_t, sig: ?*c_int) c_int; + +pub const fd_t = windows.HANDLE; +pub const ino_t = windows.LARGE_INTEGER; +pub const pid_t = windows.HANDLE; +pub const mode_t = u0; + +pub const PATH_MAX = 260; + +pub const time_t = c_longlong; + +pub const timespec = extern struct { + tv_sec: time_t, + tv_nsec: c_long, +}; + +pub const timeval = extern struct { + tv_sec: c_long, + tv_usec: c_long, +}; + +pub const Stat = @compileError("TODO windows Stat definition"); + +pub const sig_atomic_t = c_int; + +pub const sigset_t = @compileError("TODO windows sigset_t definition"); +pub const Sigaction = @compileError("TODO windows Sigaction definition"); +pub const timezone = @compileError("TODO windows timezone definition"); +pub const rusage = @compileError("TODO windows rusage definition"); + +/// maximum signal number + 1 +pub const NSIG = 23; + +/// Signal types +pub const SIG = struct { + /// interrupt + pub const INT = 2; + /// illegal instruction - invalid function image + pub const ILL = 4; + /// floating point exception + pub const FPE = 8; + /// segment violation + pub const SEGV = 11; + /// Software termination signal from kill + pub const TERM = 15; + /// Ctrl-Break sequence + pub const BREAK = 21; + /// abnormal termination triggered by abort call + pub const ABRT = 22; + /// SIGABRT compatible with other platforms, same as SIGABRT + pub const ABRT_COMPAT = 6; + + // Signal action codes + /// default signal action + pub const DFL = 0; + /// ignore signal + pub const IGN = 1; + /// return current value + pub const GET = 2; + /// signal gets error + pub const SGE = 3; + /// acknowledge + pub const ACK = 4; + /// Signal error value (returned by signal call on error) + pub const ERR = -1; +}; + +pub const SEEK = struct { + pub const SET = 0; + pub const CUR = 1; + pub const END = 2; +}; + +pub const E = enum(u16) { + /// No error occurred. + SUCCESS = 0, + PERM = 1, + NOENT = 2, + SRCH = 3, + INTR = 4, + IO = 5, + NXIO = 6, + @"2BIG" = 7, + NOEXEC = 8, + BADF = 9, + CHILD = 10, + AGAIN = 11, + NOMEM = 12, + ACCES = 13, + FAULT = 14, + BUSY = 16, + EXIST = 17, + XDEV = 18, + NODEV = 19, + NOTDIR = 20, + ISDIR = 21, + NFILE = 23, + MFILE = 24, + NOTTY = 25, + FBIG = 27, + NOSPC = 28, + SPIPE = 29, + ROFS = 30, + MLINK = 31, + PIPE = 32, + DOM = 33, + /// Also means `DEADLOCK`. + DEADLK = 36, + NAMETOOLONG = 38, + NOLCK = 39, + NOSYS = 40, + NOTEMPTY = 41, + + INVAL = 22, + RANGE = 34, + ILSEQ = 42, + + // POSIX Supplement + ADDRINUSE = 100, + ADDRNOTAVAIL = 101, + AFNOSUPPORT = 102, + ALREADY = 103, + BADMSG = 104, + CANCELED = 105, + CONNABORTED = 106, + CONNREFUSED = 107, + CONNRESET = 108, + DESTADDRREQ = 109, + HOSTUNREACH = 110, + IDRM = 111, + INPROGRESS = 112, + ISCONN = 113, + LOOP = 114, + MSGSIZE = 115, + NETDOWN = 116, + NETRESET = 117, + NETUNREACH = 118, + NOBUFS = 119, + NODATA = 120, + NOLINK = 121, + NOMSG = 122, + NOPROTOOPT = 123, + NOSR = 124, + NOSTR = 125, + NOTCONN = 126, + NOTRECOVERABLE = 127, + NOTSOCK = 128, + NOTSUP = 129, + OPNOTSUPP = 130, + OTHER = 131, + OVERFLOW = 132, + OWNERDEAD = 133, + PROTO = 134, + PROTONOSUPPORT = 135, + PROTOTYPE = 136, + TIME = 137, + TIMEDOUT = 138, + TXTBSY = 139, + WOULDBLOCK = 140, + DQUOT = 10069, + _, +}; + +pub const STRUNCATE = 80; + +pub const F_OK = 0; + +/// Remove directory instead of unlinking file +pub const AT = struct { + pub const REMOVEDIR = 0x200; +}; + +pub const in_port_t = u16; +pub const sa_family_t = ws2_32.ADDRESS_FAMILY; +pub const socklen_t = ws2_32.socklen_t; + +pub const sockaddr = ws2_32.sockaddr; + +pub const in6_addr = [16]u8; +pub const in_addr = u32; + +pub const addrinfo = ws2_32.addrinfo; +pub const AF = ws2_32.AF; +pub const MSG = ws2_32.MSG; +pub const SOCK = ws2_32.SOCK; +pub const TCP = ws2_32.TCP; +pub const IPPROTO = ws2_32.IPPROTO; +pub const BTHPROTO_RFCOMM = ws2_32.BTHPROTO_RFCOMM; + +pub const nfds_t = c_ulong; +pub const pollfd = ws2_32.pollfd; +pub const POLL = ws2_32.POLL; +pub const SOL = ws2_32.SOL; +pub const SO = ws2_32.SO; +pub const PVD_CONFIG = ws2_32.PVD_CONFIG; + +pub const O = struct { + pub const RDONLY = 0o0; + pub const WRONLY = 0o1; + pub const RDWR = 0o2; + + pub const CREAT = 0o100; + pub const EXCL = 0o200; + pub const NOCTTY = 0o400; + pub const TRUNC = 0o1000; + pub const APPEND = 0o2000; + pub const NONBLOCK = 0o4000; + pub const DSYNC = 0o10000; + pub const SYNC = 0o4010000; + pub const RSYNC = 0o4010000; + pub const DIRECTORY = 0o200000; + pub const NOFOLLOW = 0o400000; + pub const CLOEXEC = 0o2000000; + + pub const ASYNC = 0o20000; + pub const DIRECT = 0o40000; + pub const LARGEFILE = 0; + pub const NOATIME = 0o1000000; + pub const PATH = 0o10000000; + pub const TMPFILE = 0o20200000; + pub const NDELAY = NONBLOCK; +}; + +pub const IFNAMESIZE = 30; |
