1const std = @import("std");
2const builtin = @import("builtin");
3const arch = builtin.target.cpu.arch;
4
5comptime {
6 std.debug.assert(builtin.target.os.tag == .openbsd);
7 std.debug.assert(@bitSizeOf(usize) == 64);
8}
9
10pub const errno = struct {
11 pub const Error = error{
12 Unexpected,
13 EPERM,
14 ENOENT,
15 ESRCH,
16 EINTR,
17 EIO,
18 ENXIO,
19 E2BIG,
20 ENOEXEC,
21 EBADF,
22 ECHILD,
23 EDEADLK,
24 ENOMEM,
25 EACCES,
26 EFAULT,
27 ENOTBLK,
28 EBUSY,
29 EEXIST,
30 EXDEV,
31 ENODEV,
32 ENOTDIR,
33 EISDIR,
34 EINVAL,
35 ENFILE,
36 EMFILE,
37 ENOTTY,
38 ETXTBSY,
39 EFBIG,
40 ENOSPC,
41 ESPIPE,
42 EROFS,
43 EMLINK,
44 EPIPE,
45 EDOM,
46 ERANGE,
47 EAGAIN,
48 EINPROGRESS,
49 EALREADY,
50 ENOTSOCK,
51 EDESTADDRREQ,
52 EMSGSIZE,
53 EPROTOTYPE,
54 ENOPROTOOPT,
55 EPROTONOSUPPORT,
56 ESOCKTNOSUPPORT,
57 EOPNOTSUPP,
58 EPFNOSUPPORT,
59 EAFNOSUPPORT,
60 EADDRINUSE,
61 EADDRNOTAVAIL,
62 ENETDOWN,
63 ENETUNREACH,
64 ENETRESET,
65 ECONNABORTED,
66 ECONNRESET,
67 ENOBUFS,
68 EISCONN,
69 ENOTCONN,
70 ESHUTDOWN,
71 ETOOMANYREFS,
72 ETIMEDOUT,
73 ECONNREFUSED,
74 ELOOP,
75 ENAMETOOLONG,
76 EHOSTDOWN,
77 EHOSTUNREACH,
78 ENOTEMPTY,
79 EPROCLIM,
80 EUSERS,
81 EDQUOT,
82 ESTALE,
83 EREMOTE,
84 EBADRPC,
85 ERPCMISMATCH,
86 EPROGUNAVAIL,
87 EPROGMISMATCH,
88 EPROCUNAVAIL,
89 ENOLCK,
90 ENOSYS,
91 EFTYPE,
92 EAUTH,
93 ENEEDAUTH,
94 EIPSEC,
95 ENOATTR,
96 EILSEQ,
97 ENOMEDIUM,
98 EMEDIUMTYPE,
99 EOVERFLOW,
100 ECANCELED,
101 EIDRM,
102 ENOMSG,
103 ENOTSUP,
104 EBADMSG,
105 ENOTRECOVERABLE,
106 EOWNERDEAD,
107 EPROTO,
108 };
109
110 pub const Enum = enum(c_ushort) {
111 EPERM = 1,
112 ENOENT = 2,
113 ESRCH = 3,
114 EINTR = 4,
115 EIO = 5,
116 ENXIO = 6,
117 E2BIG = 7,
118 ENOEXEC = 8,
119 EBADF = 9,
120 ECHILD = 10,
121 EDEADLK = 11,
122 ENOMEM = 12,
123 EACCES = 13,
124 EFAULT = 14,
125 ENOTBLK = 15,
126 EBUSY = 16,
127 EEXIST = 17,
128 EXDEV = 18,
129 ENODEV = 19,
130 ENOTDIR = 20,
131 EISDIR = 21,
132 EINVAL = 22,
133 ENFILE = 23,
134 EMFILE = 24,
135 ENOTTY = 25,
136 ETXTBSY = 26,
137 EFBIG = 27,
138 ENOSPC = 28,
139 ESPIPE = 29,
140 EROFS = 30,
141 EMLINK = 31,
142 EPIPE = 32,
143 EDOM = 33,
144 ERANGE = 34,
145 EAGAIN = 35,
146 EINPROGRESS = 36,
147 EALREADY = 37,
148 ENOTSOCK = 38,
149 EDESTADDRREQ = 39,
150 EMSGSIZE = 40,
151 EPROTOTYPE = 41,
152 ENOPROTOOPT = 42,
153 EPROTONOSUPPORT = 43,
154 ESOCKTNOSUPPORT = 44,
155 EOPNOTSUPP = 45,
156 EPFNOSUPPORT = 46,
157 EAFNOSUPPORT = 47,
158 EADDRINUSE = 48,
159 EADDRNOTAVAIL = 49,
160 ENETDOWN = 50,
161 ENETUNREACH = 51,
162 ENETRESET = 52,
163 ECONNABORTED = 53,
164 ECONNRESET = 54,
165 ENOBUFS = 55,
166 EISCONN = 56,
167 ENOTCONN = 57,
168 ESHUTDOWN = 58,
169 ETOOMANYREFS = 59,
170 ETIMEDOUT = 60,
171 ECONNREFUSED = 61,
172 ELOOP = 62,
173 ENAMETOOLONG = 63,
174 EHOSTDOWN = 64,
175 EHOSTUNREACH = 65,
176 ENOTEMPTY = 66,
177 EPROCLIM = 67,
178 EUSERS = 68,
179 EDQUOT = 69,
180 ESTALE = 70,
181 EREMOTE = 71,
182 EBADRPC = 72,
183 ERPCMISMATCH = 73,
184 EPROGUNAVAIL = 74,
185 EPROGMISMATCH = 75,
186 EPROCUNAVAIL = 76,
187 ENOLCK = 77,
188 ENOSYS = 78,
189 EFTYPE = 79,
190 EAUTH = 80,
191 ENEEDAUTH = 81,
192 EIPSEC = 82,
193 ENOATTR = 83,
194 EILSEQ = 84,
195 ENOMEDIUM = 85,
196 EMEDIUMTYPE = 86,
197 EOVERFLOW = 87,
198 ECANCELED = 88,
199 EIDRM = 89,
200 ENOMSG = 90,
201 ENOTSUP = 91,
202 EBADMSG = 92,
203 ENOTRECOVERABLE = 93,
204 EOWNERDEAD = 94,
205 EPROTO = 95,
206
207 pub const EWOULDBLOCK: Enum = .EAGAIN;
208 };
209
210 comptime {
211 // assert Enum is sequential
212 var value: c_ushort = 0; // SUCCESS
213 for (std.enums.values(Enum)) |val| {
214 std.debug.assert(@backingInt(val) > value);
215 value = @backingInt(val);
216 }
217 }
218 comptime {
219 // sanity test
220 std.debug.assert(@backingInt(Enum.EPERM) == 1);
221 }
222 const list = blk: {
223 const values = std.enums.values(Enum);
224 const len = @backingInt(values[values.len - 1]) + 1;
225 var errors: [len]Error = @splat(error.Unexpected);
226 for (values) |f| errors[@backingInt(f)] = @field(Error, @tagName(f));
227 const final = errors;
228 break :blk final;
229 };
230 pub fn fromInt(code: c_int) Error {
231 @setRuntimeSafety(false);
232 if (code >= list.len) return error.Unexpected;
233 if (code <= 0) return error.Unexpected;
234 return list[@intCast(code)];
235 }
236 pub fn fromLibC() c_int {
237 return libc.__errno().*;
238 }
239};
240
241pub const eai = struct {
242 pub const Error = error{
243 EAI_BADFLAGS,
244 EAI_NONAME,
245 EAI_AGAIN,
246 EAI_FAIL,
247 EAI_NODATA,
248 EAI_FAMILY,
249 EAI_SOCKTYPE,
250 EAI_SERVICE,
251 EAI_ADDRFAMILY,
252 EAI_MEMORY,
253 EAI_SYSTEM,
254 EAI_BADHINTS,
255 EAI_PROTOCOL,
256 EAI_OVERFLOW,
257 };
258
259 pub const Enum = enum(c_int) {
260 EAI_BADFLAGS = -1,
261 EAI_NONAME = -2,
262 EAI_AGAIN = -3,
263 EAI_FAIL = -4,
264 EAI_NODATA = -5,
265 EAI_FAMILY = -6,
266 EAI_SOCKTYPE = -7,
267 EAI_SERVICE = -8,
268 EAI_ADDRFAMILY = -9,
269 EAI_MEMORY = -10,
270 EAI_SYSTEM = -11,
271 EAI_BADHINTS = -12,
272 EAI_PROTOCOL = -13,
273 EAI_OVERFLOW = -14,
274 };
275
276 pub fn fromInt(code: c_int) Error {
277 return switch (@as(Enum, @fromBackingInt(@intCast(code)))) {
278 .EAI_BADFLAGS => error.EAI_BADFLAGS,
279 .EAI_NONAME => error.EAI_NONAME,
280 .EAI_AGAIN => error.EAI_AGAIN,
281 .EAI_FAIL => error.EAI_FAIL,
282 .EAI_NODATA => error.EAI_NODATA,
283 .EAI_FAMILY => error.EAI_FAMILY,
284 .EAI_SOCKTYPE => error.EAI_SOCKTYPE,
285 .EAI_SERVICE => error.EAI_SERVICE,
286 .EAI_ADDRFAMILY => error.EAI_ADDRFAMILY,
287 .EAI_MEMORY => error.EAI_MEMORY,
288 .EAI_SYSTEM => error.EAI_SYSTEM,
289 .EAI_BADHINTS => error.EAI_BADHINTS,
290 .EAI_PROTOCOL => error.EAI_PROTOCOL,
291 .EAI_OVERFLOW => error.EAI_OVERFLOW,
292 };
293 }
294};
295
296pub const libc = struct {
297 pub extern fn __errno() *c_int;
298 pub extern fn pipe2(pipefd: *[2]c_int, flag: c_int) c_int;
299 pub extern fn openat(fd: c_int, file: [*:0]const u8, oflag: c_int, ...) c_int;
300 pub extern fn close(fildes: c_int) c_int;
301 pub extern fn fork() pid_t;
302 pub extern fn dup2(fildes: c_int, fildes2: c_int) c_int;
303 pub extern fn _exit(status: c_int) noreturn;
304 pub extern fn fchdir(fildes: c_int) c_int;
305 pub extern fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
306 pub extern fn kill(pid: pid_t, sig: c_int) c_int;
307 pub extern fn poll(fds: [*]struct_pollfd, nfds: nfds_t, timeout: c_int) c_int;
308 pub extern fn read(fd: c_int, buf: [*]u8, count: usize) isize;
309 pub extern fn write(fd: c_int, buf: *const anyopaque, n: usize) isize;
310 pub extern fn waitpid(pid: pid_t, stat_loc: ?*c_int, options: c_int) pid_t;
311 pub extern fn getenv(name: [*:0]const u8) ?[*:0]const u8;
312 pub extern fn exit(status: c_int) noreturn;
313 pub extern fn mkdirat(fd: c_int, path: [*:0]const u8, mode: mode_t) c_int;
314 pub extern fn fstatat(fd: c_int, noalias path: [*:0]const u8, noalias buf: *struct_stat, flag: c_int) c_int;
315 pub extern fn getaddrinfo(noalias nodename: ?[*:0]const u8, noalias servname: ?[*:0]const u8, noalias hints: ?*const struct_addrinfo, noalias res: **const struct_addrinfo) c_int;
316 pub extern fn freeaddrinfo(ai: *const struct_addrinfo) void;
317 pub extern fn socket(domain: c_int, type: c_int, protocol: c_int) c_int;
318 pub extern fn connect(socket: c_int, address: *const struct_sockaddr, address_len: socklen_t) c_int;
319 pub extern fn arc4random_buf(buf: [*]u8, n: usize) void;
320 pub extern fn pthread_mutex_init(mutex: *pthread_mutex_t, attr: ?*const pthread_mutexattr_t) c_int;
321 pub extern fn pthread_mutex_destroy(mutex: *pthread_mutex_t) c_int;
322 pub extern fn pthread_mutex_lock(mutex: *pthread_mutex_t) c_int;
323 pub extern fn pthread_mutex_trylock(mutex: *pthread_mutex_t) c_int;
324 pub extern fn pthread_mutex_unlock(mutex: *pthread_mutex_t) c_int;
325 pub extern fn pthread_cond_init(noalias cond: *pthread_cond_t, noalias attr: ?*const pthread_condattr_t) c_int;
326 pub extern fn pthread_cond_destroy(cond: *pthread_cond_t) c_int;
327 pub extern fn pthread_cond_wait(noalias cond: *pthread_cond_t, noalias mutex: *pthread_mutex_t) c_int;
328 pub extern fn pthread_cond_timedwait(noalias cond: *pthread_cond_t, noalias mutex: *pthread_mutex_t, noalias abstime: *const struct_timespec) c_int;
329 pub extern fn pthread_cond_signal(cond: *pthread_cond_t) c_int;
330 pub extern fn pthread_cond_broadcast(cond: *pthread_cond_t) c_int;
331 pub extern fn pthread_rwlock_init(noalias rwlock: *pthread_rwlock_t, noalias attr: ?*const pthread_rwlockattr_t) c_int;
332 pub extern fn pthread_rwlock_destroy(rwlock: *pthread_rwlock_t) c_int;
333 pub extern fn pthread_rwlock_rdlock(rwlock: *pthread_rwlock_t) c_int;
334 pub extern fn pthread_rwlock_tryrdlock(rwlock: *pthread_rwlock_t) c_int;
335 pub extern fn pthread_rwlock_wrlock(rwlock: *pthread_rwlock_t) c_int;
336 pub extern fn pthread_rwlock_trywrlock(rwlock: *pthread_rwlock_t) c_int;
337 pub extern fn pthread_rwlock_unlock(rwlock: *pthread_rwlock_t) c_int;
338 pub extern fn clock_gettime(clock_id: clockid_t, tp: *struct_timespec) c_int;
339 pub extern fn renameat(oldfd: c_int, old: [*:0]const u8, newfd: c_int, new: [*:0]const u8) c_int;
340 pub extern fn send(fd: c_int, buffer: [*]const u8, length: usize, flags: c_int) isize;
341 pub extern fn recv(fd: c_int, buffer: [*]u8, length: usize, flags: c_int) isize;
342 pub extern fn fstat(fd: c_int, buf: *struct_stat) c_int;
343 pub extern fn mmap(addr: ?*anyopaque, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: off_t) *allowzero anyopaque;
344 pub extern fn writev(fd: c_int, iovec: [*]const struct_iovec, count: c_int) isize;
345 pub extern fn faccessat(fd: c_int, path: [*:0]const u8, amode: c_int, flag: c_int) c_int;
346 pub extern fn readlinkat(fd: c_int, noalias path: [*:0]const u8, noalias buf: [*]u8, len: usize) isize;
347 pub extern fn futimens(fd: c_int, times: *const [2]struct_timespec) c_int;
348 pub extern fn setsockopt(fd: c_int, level: c_int, optname: c_int, optval: *const anyopaque, optlen: socklen_t) c_int;
349 pub extern fn bind(socket: c_int, address: *const struct_sockaddr, address_len: socklen_t) c_int;
350 pub extern fn listen(socket: c_int, backlog: c_int) c_int;
351 pub extern fn getsockname(fd: c_int, noalias addr: *struct_sockaddr, noalias len: *socklen_t) c_int;
352 pub extern fn pthread_attr_init(attr: *pthread_attr_t) c_int;
353 pub extern fn pthread_attr_destroy(attr: *pthread_attr_t) c_int;
354 pub extern fn pthread_attr_setguardsize(attr: *pthread_attr_t, guardsize: usize) c_int;
355 pub extern fn pthread_attr_setstacksize(attr: *pthread_attr_t, stacksize: usize) c_int;
356 pub extern fn pthread_create(noalias newthread: *pthread_t, noalias attr: *const pthread_attr_t, start_routine: *const fn (*anyopaque) callconv(.c) ?*anyopaque, noalias arg: *anyopaque) c_int;
357 pub extern fn pthread_detach(thread: pthread_t) c_int;
358 pub extern fn pthread_join(thread: pthread_t, retval: ?*?*anyopaque) c_int;
359 pub extern fn munmap(addr: *const anyopaque, len: usize) c_int;
360 pub extern fn getdents(dirfd: c_int, buf: [*]u8, nbytes: usize) c_int;
361 pub extern fn accept4(socket: c_int, noalias address: ?*struct_sockaddr, noalias address_len: *socklen_t, flags: c_int) c_int;
362 pub extern fn shutdown(socket: c_int, how: c_int) c_int;
363 pub extern fn unlinkat(fd: c_int, name: [*:0]const u8, flag: c_int) c_int;
364 pub extern fn lseek(fd: c_int, offset: off_t, whence: c_int) off_t;
365};
366
367pub const blkcnt_t = i64;
368pub const blksize_t = i32;
369pub const clock_t = i64;
370pub const clockid_t = i32;
371pub const cpuid_t = c_ulong;
372pub const dev_t = i32;
373pub const fixpt_t = u32;
374pub const fsblkcnt_t = u64;
375pub const fsfilcnt_t = u64;
376pub const gid_t = u32;
377pub const id_t = u32;
378pub const in_addr_t = u32;
379pub const in_port_t = u16;
380pub const ino_t = u64;
381pub const key_t = c_long;
382pub const mode_t = u32;
383pub const nlink_t = u32;
384pub const off_t = i64;
385pub const pid_t = i32;
386pub const rlim_t = u64;
387pub const sa_family_t = u8;
388pub const segsz_t = i32;
389pub const socklen_t = u32;
390pub const suseconds_t = c_long;
391pub const time_t = i64;
392pub const timer_t = i32;
393pub const uid_t = u32;
394pub const useconds_t = u32;
395pub const struct_pollfd = extern struct { fd: c_int, events: c_short, revents: c_short };
396pub const nfds_t = c_uint;
397pub const struct_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: struct_timespec, mtim: struct_timespec, ctim: struct_timespec, size: off_t, blocks: blkcnt_t, blksize: blksize_t, flags: u32, gen: u32, birthtim: struct_timespec };
398pub const struct_timespec = extern struct { sec: time_t, nsec: c_long };
399pub const struct_iovec = extern struct { base: [*]u8, len: usize };
400pub const struct_in_addr = extern struct { addr: in_addr_t };
401pub const struct_in6_addr = extern struct { addr: extern union { a8: [16]u8, a16: [8]u16, a32: [4]u32 } };
402pub const struct_sockaddr = extern struct { len: u8, family: AF, data: [14]u8 };
403pub const struct_sockaddr_in = extern struct { len: u8 = 16, family: AF = .INET, port: in_port_t, addr: struct_in_addr, zero: [8]u8 = @splat(0) };
404pub const struct_sockaddr_in6 = extern struct { len: u8 = 28, family: AF = .INET6, port: in_port_t, flowinfo: u32, addr: struct_in6_addr, scope_id: u32 };
405pub const struct_sockaddr_un = extern struct { len: u8 = 106, family: AF = .UNIX, path: [104]u8 };
406comptime {
407 std.debug.assert(@sizeOf(struct_sockaddr_in) == 16);
408 std.debug.assert(@sizeOf(struct_sockaddr_in6) == 28);
409 std.debug.assert(@sizeOf(struct_sockaddr_un) == 106);
410}
411pub const struct_addrinfo = extern struct { flags: c_int, family: c_int, socktype: c_int, protocol: c_int, addrlen: socklen_t, addr: ?*struct_sockaddr, canonname: ?[*:0]u8, next: ?*struct_addrinfo };
412pub const struct_dirent = extern struct { fileno: ino_t, off: off_t, reclen: u16, type: DT, namlen: u8, padding: [4]u8, name: [NAME_MAX + 1]u8 };
413pub const struct_timeval = extern struct { sec: time_t, usec: suseconds_t };
414
415const impdef = @import("c");
416pub const pthread_t = impdef.pthread_t;
417pub const pthread_attr_t = impdef.pthread_attr_t;
418pub const pthread_mutexattr_t = impdef.pthread_mutexattr_t;
419pub const pthread_mutex_t = impdef.pthread_mutex_t;
420pub const pthread_condattr_t = impdef.pthread_condattr_t;
421pub const pthread_cond_t = impdef.pthread_cond_t;
422pub const pthread_rwlockattr_t = impdef.pthread_rwlockattr_t;
423pub const pthread_rwlock_t = impdef.pthread_rwlock_t;
424
425pub const INT_MAX = std.math.maxInt(c_int);
426pub const UINT_MAX = std.math.maxInt(c_uint);
427pub const ARG_MAX = (512 * 1024);
428pub const CHILD_MAX = 80;
429pub const LINK_MAX = 32767;
430pub const MAX_CANON = 255;
431pub const MAX_INPUT = 255;
432pub const NAME_MAX = 255;
433pub const NGROUPS_MAX = 16;
434pub const OPEN_MAX = 64;
435pub const PATH_MAX = 1024;
436pub const PIPE_BUF = 512;
437pub const SYMLINK_MAX = PATH_MAX;
438pub const SYMLOOP_MAX = 32;
439pub const BC_BASE_MAX = INT_MAX;
440pub const BC_DIM_MAX = 65535;
441pub const BC_SCALE_MAX = INT_MAX;
442pub const BC_STRING_MAX = INT_MAX;
443pub const COLL_WEIGHTS_MAX = 2;
444pub const EXPR_NEST_MAX = 32;
445pub const LINE_MAX = 2048;
446pub const RE_DUP_MAX = 255;
447pub const SEM_VALUE_MAX = UINT_MAX;
448
449pub const O = struct {
450 pub const RDONLY = 0x0000;
451 pub const WRONLY = 0x0001;
452 pub const RDWR = 0x0002;
453 pub const ACCMODE = 0x0003;
454 pub const NONBLOCK = 0x0004;
455 pub const APPEND = 0x0008;
456 pub const SHLOCK = 0x0010;
457 pub const EXLOCK = 0x0020;
458 pub const ASYNC = 0x0040;
459 pub const FSYNC = 0x0080;
460 pub const SYNC = 0x0080;
461 pub const DSYNC = SYNC;
462 pub const RSYNC = SYNC;
463 pub const NOFOLLOW = 0x0100;
464 pub const CREAT = 0x0200;
465 pub const TRUNC = 0x0400;
466 pub const EXCL = 0x0800;
467 pub const NOCTTY = 0x8000;
468 pub const CLOEXEC = 0x10000;
469 pub const DIRECTORY = 0x20000;
470 pub const CLOFORK = 0x40000;
471};
472
473pub const AT = struct {
474 pub const FDCWD = -100;
475 pub const EACCESS = 0x01;
476 pub const SYMLINK_NOFOLLOW = 0x02;
477 pub const SYMLINK_FOLLOW = 0x04;
478 pub const REMOVEDIR = 0x08;
479};
480
481pub const SIG = struct {
482 pub const HUP = 1;
483 pub const INT = 2;
484 pub const QUIT = 3;
485 pub const ILL = 4;
486 pub const TRAP = 5;
487 pub const ABRT = 6;
488 pub const IOT = ABRT;
489 pub const EMT = 7;
490 pub const FPE = 8;
491 pub const KILL = 9;
492 pub const BUS = 10;
493 pub const SEGV = 11;
494 pub const SYS = 12;
495 pub const PIPE = 13;
496 pub const ALRM = 14;
497 pub const TERM = 15;
498 pub const URG = 16;
499 pub const STOP = 17;
500 pub const TSTP = 18;
501 pub const CONT = 19;
502 pub const CHLD = 20;
503 pub const TTIN = 21;
504 pub const TTOU = 22;
505 pub const IO = 23;
506 pub const XCPU = 24;
507 pub const XFSZ = 25;
508 pub const VTALRM = 26;
509 pub const PROF = 27;
510 pub const WINCH = 28;
511 pub const INFO = 29;
512 pub const USR1 = 30;
513 pub const USR2 = 31;
514 pub const THR = 32;
515};
516
517pub const POLL = struct {
518 pub const IN = 0x0001;
519 pub const PRI = 0x0002;
520 pub const OUT = 0x0004;
521 pub const ERR = 0x0008;
522 pub const HUP = 0x0010;
523 pub const NVAL = 0x0020;
524 pub const RDNORM = 0x0040;
525 pub const NORM = RDNORM;
526 pub const WRNORM = OUT;
527 pub const RDBAND = 0x0080;
528 pub const WRBAND = 0x0100;
529};
530
531pub const W = struct {
532 pub const NOHANG = 0x01;
533 pub const UNTRACED = 0x02;
534 pub const CONTINUED = 0x08;
535 pub const EXITED = 0x04;
536 pub const STOPPED = UNTRACED;
537 pub const NOWAIT = 0x10;
538 pub const TRAPPED = 0x20;
539 pub const _STOPPED = 0o177;
540 pub const _CONTINUED = 0o177777;
541 pub const COREFLAG = 0o200;
542
543 pub fn _STATUS(x: c_int) u8 {
544 return @intCast(x & 0o177);
545 }
546 pub fn IFSTOPPED(x: c_int) bool {
547 return (x & 0xff) == _STOPPED;
548 }
549 pub fn STOPSIG(x: c_int) u8 {
550 return @truncate(@as(c_uint, @bitCast(x >> 8)));
551 }
552 pub fn IFSIGNALED(x: c_int) bool {
553 return _STATUS(x) != _STOPPED and _STATUS(x) != 0;
554 }
555 pub fn TERMSIG(x: c_int) u8 {
556 return _STATUS(x);
557 }
558 pub fn IFEXITED(x: c_int) bool {
559 return _STATUS(x) == 0;
560 }
561 pub fn EXITSTATUS(x: c_int) u8 {
562 return @truncate(@as(c_uint, @bitCast(x >> 8)));
563 }
564 pub fn IFCONTINUED(x: c_int) bool {
565 return (x & _CONTINUED) == _CONTINUED;
566 }
567 pub fn COREDUMP(x: c_int) u8 {
568 return x & COREFLAG;
569 }
570 pub fn _EXITCODE(r: c_int, s: c_int) c_int {
571 return r << 8 | s;
572 }
573 pub fn _STOPCODE(s: c_int) c_int {
574 return s << 8 | _STOPPED;
575 }
576};
577
578pub const S = struct {
579 pub const ISUID = 0o004000;
580 pub const ISGID = 0o002000;
581 pub const ISTXT = 0o001000;
582 pub const IRWXU = 0o000700;
583 pub const IRUSR = 0o000400;
584 pub const IWUSR = 0o000200;
585 pub const IXUSR = 0o000100;
586 pub const IREAD = IRUSR;
587 pub const IWRITE = IWUSR;
588 pub const IEXEC = IXUSR;
589 pub const IRWXG = 0o000070;
590 pub const IRGRP = 0o000040;
591 pub const IWGRP = 0o000020;
592 pub const IXGRP = 0o000010;
593 pub const IRWXO = 0o000007;
594 pub const IROTH = 0o000004;
595 pub const IWOTH = 0o000002;
596 pub const IXOTH = 0o000001;
597 pub const IFMT = 0o170000;
598 pub const IFIFO = 0o010000;
599 pub const IFCHR = 0o020000;
600 pub const IFDIR = 0o040000;
601 pub const IFBLK = 0o060000;
602 pub const IFREG = 0o100000;
603 pub const IFLNK = 0o120000;
604 pub const IFSOCK = 0o140000;
605 pub const IFWHT = 0o160000; //unused
606 pub const ISVTX = 0o001000;
607};
608
609pub const AF = enum(sa_family_t) {
610 UNSPEC = 0,
611 UNIX = 1,
612 INET = 2,
613 IMPLINK = 3,
614 PUP = 4,
615 CHAOS = 5,
616 NS = 6,
617 ISO = 7,
618 ECMA = 8,
619 DATAKIT = 9,
620 CCITT = 10,
621 SNA = 11,
622 DECnet = 12,
623 DLI = 13,
624 LAT = 14,
625 HYLINK = 15,
626 APPLETALK = 16,
627 ROUTE = 17,
628 LINK = 18,
629 XTP = 19,
630 COIP = 20,
631 CNT = 21,
632 RTIP = 22,
633 IPX = 23,
634 INET6 = 24,
635 PIP = 25,
636 ISDN = 26,
637 NATM = 27,
638 ENCAP = 28,
639 SIP = 29,
640 KEY = 30,
641 HDRCMPLT = 31,
642 BLUETOOTH = 32,
643 MPLS = 33,
644 PFLOW = 34,
645 PIPEX = 35,
646 FRAME = 36,
647
648 pub const LOCAL: AF = .UNIX;
649 pub const OSI: AF = .ISO;
650 pub const E164: AF = .ISDN;
651};
652
653pub const SOCK = struct {
654 pub const STREAM = 1;
655 pub const DGRAM = 2;
656 pub const RAW = 3;
657 pub const RDM = 4;
658 pub const SEQPACKET = 5;
659 pub const CLOEXEC = 0x8000;
660 pub const NONBLOCK = 0x4000;
661 pub const DNS = 0x1000;
662 pub const CLOFORK = 0x0800;
663};
664
665pub const IPPROTO = struct {
666 pub const IP = 0;
667 pub const HOPOPTS = IP;
668 pub const ICMP = 1;
669 pub const IGMP = 2;
670 pub const GGP = 3;
671 pub const IPIP = 4;
672 pub const IPV4 = IPIP;
673 pub const TCP = 6;
674 pub const EGP = 8;
675 pub const PUP = 12;
676 pub const UDP = 17;
677 pub const IDP = 22;
678 pub const TP = 29;
679 pub const IPV6 = 41;
680 pub const ROUTING = 43;
681 pub const FRAGMENT = 44;
682 pub const RSVP = 46;
683 pub const GRE = 47;
684 pub const ESP = 50;
685 pub const AH = 51;
686 pub const MOBILE = 55;
687 pub const ICMPV6 = 58;
688 pub const NONE = 59;
689 pub const DSTOPTS = 60;
690 pub const EON = 80;
691 pub const ETHERIP = 97;
692 pub const ENCAP = 98;
693 pub const PIM = 103;
694 pub const IPCOMP = 108;
695 pub const CARP = 112;
696 pub const SCTP = 132;
697 pub const UDPLITE = 136;
698 pub const MPLS = 137;
699 pub const PFSYNC = 240;
700 pub const RAW = 255;
701};
702
703pub const CLOCK = enum(clockid_t) {
704 REALTIME = 0,
705 PROCESS_CPUTIME_ID = 2,
706 MONOTONIC = 3,
707 THREAD_CPUTIME_ID = 4,
708 UPTIME = 5,
709 BOOTTIME = 6,
710 _,
711};
712
713pub const SOL = struct {
714 pub const SOCKET = 0xffff;
715};
716
717pub const SO = struct {
718 pub const DEBUG = 0x0001;
719 pub const ACCEPTCONN = 0x0002;
720 pub const REUSEADDR = 0x0004;
721 pub const KEEPALIVE = 0x0008;
722 pub const DONTROUTE = 0x0010;
723 pub const BROADCAST = 0x0020;
724 pub const USELOOPBACK = 0x0040;
725 pub const LINGER = 0x0080;
726 pub const OOBINLINE = 0x0100;
727 pub const REUSEPORT = 0x0200;
728 pub const TIMESTAMP = 0x0800;
729 pub const BINDANY = 0x1000;
730 pub const ZEROIZE = 0x2000;
731 pub const SNDBUF = 0x1001;
732 pub const RCVBUF = 0x1002;
733 pub const SNDLOWAT = 0x1003;
734 pub const RCVLOWAT = 0x1004;
735 pub const SNDTIMEO = 0x1005;
736 pub const RCVTIMEO = 0x1006;
737 pub const ERROR = 0x1007;
738 pub const TYPE = 0x1008;
739 pub const RTABLE = 0x1021;
740 pub const PEERCRED = 0x1022;
741 pub const SPLICE = 0x1023;
742 pub const DOMAIN = 0x1024;
743 pub const PROTOCOL = 0x1025;
744};
745
746pub const DT = enum(u8) {
747 UNKNOWN = 0,
748 FIFO = 1,
749 CHR = 2,
750 DIR = 4,
751 BLK = 6,
752 REG = 8,
753 LNK = 10,
754 SOCK = 12,
755 WHT = 14, //unused
756};
757
758pub const SHUT = enum(c_int) {
759 RD = 0,
760 WR = 1,
761 RDWR = 2,
762};
763
764pub const MAP = struct {
765 pub const SHARED = 0x0001;
766 pub const PRIVATE = 0x0002;
767 pub const FIXED = 0x0010;
768 pub const NOREPLACE = 0x0800;
769 pub const ANON = 0x1000;
770 pub const ANONYMOUS = ANON;
771 pub const NOFAULT = 0x2000;
772 pub const STACK = 0x4000;
773 pub const CONCEAL = 0x8000;
774 pub const FAILED = -1;
775};
776
777pub const PROT = struct {
778 pub const NONE = 0x00;
779 pub const READ = 0x01;
780 pub const WRITE = 0x02;
781 pub const EXEC = 0x04;
782};
783
784pub const IPV6 = struct {
785 pub const UNICAST_HOPS = 4;
786 pub const MULTICAST_IF = 9;
787 pub const MULTICAST_HOPS = 10;
788 pub const MULTICAST_LOOP = 11;
789 pub const JOIN_GROUP = 12;
790 pub const LEAVE_GROUP = 13;
791 pub const PORTRANGE = 14;
792 pub const CHECKSUM = 26;
793 pub const V6ONLY = 27;
794 pub const RTHDRDSTOPTS = 35;
795 pub const RECVPKTINFO = 36;
796 pub const RECVHOPLIMIT = 37;
797 pub const RECVRTHDR = 38;
798 pub const RECVHOPOPTS = 39;
799 pub const RECVDSTOPTS = 40;
800 pub const USE_MIN_MTU = 42;
801 pub const RECVPATHMTU = 43;
802 pub const PATHMTU = 44;
803 pub const PKTINFO = 46;
804 pub const HOPLIMIT = 47;
805 pub const NEXTHOP = 48;
806 pub const HOPOPTS = 49;
807 pub const DSTOPTS = 50;
808 pub const RTHDR = 51;
809 pub const AUTH_LEVEL = 53;
810 pub const ESP_TRANS_LEVEL = 54;
811 pub const ESP_NETWORK_LEVEL = 55;
812 pub const RECVTCLASS = 57;
813 pub const AUTOFLOWLABEL = 59;
814 pub const IPCOMP_LEVEL = 60;
815 pub const TCLASS = 61;
816 pub const DONTFRAG = 62;
817 pub const PIPEX = 63;
818 pub const RECVDSTPORT = 64;
819 pub const MINHOPCOUNT = 65;
820};
821
822pub const SEEK = struct {
823 pub const SET = 0;
824 pub const CUR = 1;
825 pub const END = 2;
826};
827
828pub fn pipe2(flag: c_int) ![2]c_int {
829 var fildes: [2]c_int = @splat(-1);
830 const rc = libc.pipe2(&fildes, flag);
831 if (rc == -1) return errno.fromInt(errno.fromLibC());
832 std.debug.assert(rc == 0);
833 return fildes;
834}
835pub fn openat(fd: c_int, file: [*:0]const u8, oflag: c_int) errno.Error!c_int {
836 const rc = libc.openat(fd, file, oflag);
837 if (rc == -1) return errno.fromInt(errno.fromLibC());
838 std.debug.assert(rc >= 0);
839 return rc;
840}
841pub fn openat4(fd: c_int, file: [*:0]const u8, oflag: c_int, mode: mode_t) errno.Error!c_int {
842 const rc = libc.openat(fd, file, oflag, mode);
843 if (rc == -1) return errno.fromInt(errno.fromLibC());
844 return rc;
845}
846pub fn close(fd: c_int) errno.Error!void {
847 const rc = libc.close(fd);
848 if (rc == -1) return errno.fromInt(errno.fromLibC());
849 std.debug.assert(rc == 0);
850}
851pub fn fork() !pid_t {
852 const rc = libc.fork();
853 if (rc == -1) return errno.fromInt(errno.fromLibC());
854 std.debug.assert(rc >= 0);
855 return rc;
856}
857pub fn dup2(fildes: c_int, fildes2: c_int) !void {
858 const rc = libc.dup2(fildes, fildes2);
859 if (rc == -1) return errno.fromInt(errno.fromLibC());
860 std.debug.assert(rc >= 0);
861}
862pub fn fchdir(fildes: c_int) !void {
863 const rc = libc.fchdir(fildes);
864 if (rc == -1) return errno.fromInt(errno.fromLibC());
865 std.debug.assert(rc == 0);
866}
867pub fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) !noreturn {
868 const rc = libc.execvp(file, argv);
869 std.debug.assert(rc == -1);
870 return errno.fromInt(errno.fromLibC());
871}
872pub fn kill(pid: pid_t, sig: c_int) !void {
873 const rc = libc.kill(pid, sig);
874 if (rc == -1) return errno.fromInt(errno.fromLibC());
875 std.debug.assert(rc == 0);
876}
877pub fn poll(fds: []struct_pollfd, timeout_ms: c_int) !c_int {
878 const rc = libc.poll(fds.ptr, @intCast(fds.len), timeout_ms);
879 if (rc == -1) return errno.fromInt(errno.fromLibC());
880 std.debug.assert(rc >= 0);
881 return rc;
882}
883pub fn read(fd: c_int, buf: []u8) errno.Error!usize {
884 const rc = libc.read(fd, buf.ptr, buf.len);
885 if (rc == -1) return errno.fromInt(errno.fromLibC());
886 std.debug.assert(rc >= 0);
887 return @intCast(rc);
888}
889pub fn write(fd: c_int, buf: []const u8) !usize {
890 const rc = libc.write(fd, buf.ptr, buf.len);
891 if (rc == -1) return errno.fromInt(errno.fromLibC());
892 std.debug.assert(rc >= 0);
893 return @intCast(rc);
894}
895pub fn waitpid(pid: pid_t, options: c_int) !struct { pid_t, c_int } {
896 var status: c_int = 0;
897 const rc = libc.waitpid(pid, &status, options);
898 if (rc == -1) return errno.fromInt(errno.fromLibC());
899 std.debug.assert(rc >= 0);
900 return .{ rc, status };
901}
902pub fn getenv(name: [:0]const u8) ?[:0]const u8 {
903 return std.mem.sliceTo(libc.getenv(name.ptr) orelse return null, 0);
904}
905pub fn exit(status: c_int) noreturn {
906 return libc.exit(status);
907}
908pub fn mkdirat(fd: c_int, path: [*:0]const u8, mode: mode_t) errno.Error!void {
909 const rc = libc.mkdirat(fd, path, mode);
910 if (rc == -1) return errno.fromInt(errno.fromLibC());
911 std.debug.assert(rc == 0);
912}
913pub fn fstatat(fd: c_int, path: [*:0]const u8, flag: c_int) errno.Error!struct_stat {
914 var buf: struct_stat = undefined;
915 const rc = libc.fstatat(fd, path, &buf, flag);
916 if (rc == -1) return errno.fromInt(errno.fromLibC());
917 std.debug.assert(rc == 0);
918 return buf;
919}
920pub fn getaddrinfo(noalias nodename: ?[*:0]const u8, noalias servname: ?[*:0]const u8, noalias req: ?*const struct_addrinfo) eai.Error!*const struct_addrinfo {
921 var pai: *const struct_addrinfo = undefined;
922 var hints = std.mem.zeroes(struct_addrinfo);
923 const rc = libc.getaddrinfo(nodename, servname, req orelse &hints, &pai);
924 if (rc < 0) return eai.fromInt(rc);
925 std.debug.assert(rc == 0);
926 return pai;
927}
928pub fn freeaddrinfo(ai: *const struct_addrinfo) void {
929 return libc.freeaddrinfo(ai);
930}
931pub fn socket(domain: AF, ty: c_int, protocol: c_int) errno.Error!c_uint {
932 const rc = libc.socket(@backingInt(domain), ty, protocol);
933 if (rc == -1) return errno.fromInt(errno.fromLibC());
934 std.debug.assert(rc > 0);
935 return @intCast(rc);
936}
937pub fn connect(socketfd: c_uint, address: *const struct_sockaddr, address_len: socklen_t) errno.Error!void {
938 const rc = libc.connect(@intCast(socketfd), address, address_len);
939 if (rc == -1) return errno.fromInt(errno.fromLibC());
940 std.debug.assert(rc == 0);
941}
942pub fn clock_gettime(clock_id: CLOCK) !struct_timespec {
943 var tp: struct_timespec = undefined;
944 const rc = libc.clock_gettime(@backingInt(clock_id), &tp);
945 if (rc == -1) return errno.fromInt(errno.fromLibC());
946 std.debug.assert(rc == 0);
947 return tp;
948}
949pub fn renameat(oldfd: c_int, old: [*:0]const u8, newfd: c_int, new: [*:0]const u8) !void {
950 const rc = libc.renameat(oldfd, old, newfd, new);
951 if (rc == -1) return errno.fromInt(errno.fromLibC());
952 std.debug.assert(rc == 0);
953}
954pub fn send(socketfd: c_uint, buffer: []const u8, flags: c_int) errno.Error!usize {
955 const rc = libc.send(@intCast(socketfd), buffer.ptr, buffer.len, flags);
956 if (rc == -1) return errno.fromInt(errno.fromLibC());
957 std.debug.assert(rc >= 0);
958 return @intCast(rc);
959}
960pub fn recv(socketfd: c_uint, buffer: []u8, flags: c_int) errno.Error!usize {
961 const rc = libc.recv(@intCast(socketfd), buffer.ptr, buffer.len, flags);
962 if (rc == -1) return errno.fromInt(errno.fromLibC());
963 std.debug.assert(rc >= 0);
964 return @intCast(rc);
965}
966pub fn fstat(fd: c_int) errno.Error!struct_stat {
967 var buf: struct_stat = undefined;
968 const rc = libc.fstat(fd, &buf);
969 if (rc == -1) return errno.fromInt(errno.fromLibC());
970 std.debug.assert(rc == 0);
971 return buf;
972}
973pub fn mmap(addr: ?*anyopaque, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: off_t) errno.Error![]u8 {
974 const rc = libc.mmap(addr, len, prot, flags, fd, offset);
975 if (@intFromPtr(rc) == MAP.FAILED) return errno.fromInt(errno.fromLibC());
976 const ptr: [*]u8 = @ptrCast(rc);
977 return ptr[0..len];
978}
979pub fn writev(fd: c_int, iovec: []const struct_iovec) !usize {
980 const rc = libc.writev(fd, iovec.ptr, @intCast(iovec.len));
981 if (rc == -1) return errno.fromInt(errno.fromLibC());
982 std.debug.assert(rc >= 0);
983 return @intCast(rc);
984}
985pub fn faccessat(fd: c_int, path: [*:0]const u8, amode: c_int, flag: c_int) !void {
986 const rc = libc.faccessat(fd, path, amode, flag);
987 if (rc == -1) return errno.fromInt(errno.fromLibC());
988 std.debug.assert(rc == 0);
989}
990pub fn readlinkat(dirfd: c_int, noalias path: [*:0]const u8, noalias buf: []u8) ![:0]u8 {
991 const rc = libc.readlinkat(dirfd, path, buf.ptr, buf.len);
992 if (rc == -1) return errno.fromInt(errno.fromLibC());
993 std.debug.assert(rc >= 0);
994 buf[@intCast(rc)] = 0;
995 return buf[0..@intCast(rc) :0];
996}
997pub fn futimens(fd: c_int, times: [2]struct_timespec) !void {
998 const rc = libc.futimens(fd, &times);
999 if (rc == -1) return errno.fromInt(errno.fromLibC());
1000 std.debug.assert(rc == 0);
1001}
1002pub fn setsockopt(socketfd: c_uint, level: c_int, optname: c_int, opaque_opt: []const u8) errno.Error!void {
1003 const rc = libc.setsockopt(@intCast(socketfd), level, optname, opaque_opt.ptr, @intCast(opaque_opt.len));
1004 if (rc == -1) return errno.fromInt(errno.fromLibC());
1005 std.debug.assert(rc == 0);
1006}
1007pub fn bind(socketfd: c_uint, address: *const struct_sockaddr, address_len: socklen_t) errno.Error!void {
1008 const rc = libc.bind(@intCast(socketfd), address, address_len);
1009 if (rc == -1) return errno.fromInt(errno.fromLibC());
1010 std.debug.assert(rc == 0);
1011}
1012pub fn listen(socketfd: c_uint, backlog: c_int) errno.Error!void {
1013 const rc = libc.listen(@intCast(socketfd), backlog);
1014 if (rc == -1) return errno.fromInt(errno.fromLibC());
1015 std.debug.assert(rc == 0);
1016}
1017pub fn getsockname(socketfd: c_uint, noalias address: *struct_sockaddr, noalias address_len: *socklen_t) errno.Error!void {
1018 const rc = libc.getsockname(@intCast(socketfd), address, address_len);
1019 if (rc == -1) return errno.fromInt(errno.fromLibC());
1020 std.debug.assert(rc == 0);
1021}
1022pub fn munmap(addr: *const anyopaque, len: usize) errno.Error!void {
1023 const rc = libc.munmap(addr, len);
1024 if (rc == -1) return errno.fromInt(errno.fromLibC());
1025 std.debug.assert(rc == 0);
1026}
1027pub fn getdents(dirfd: c_int, buf: []u8) errno.Error!usize {
1028 const rc = libc.getdents(dirfd, buf.ptr, buf.len);
1029 if (rc == -1) return errno.fromInt(errno.fromLibC());
1030 std.debug.assert(rc >= 0);
1031 return @intCast(rc);
1032}
1033pub fn accept4(socketfd: c_uint, noalias address: ?*struct_sockaddr, noalias address_len: *socklen_t, flags: c_int) errno.Error!c_uint {
1034 const rc = libc.accept4(@intCast(socketfd), address, address_len, flags);
1035 if (rc == -1) return errno.fromInt(errno.fromLibC());
1036 std.debug.assert(rc > 0);
1037 return @intCast(rc);
1038}
1039pub fn shutdown(socketfd: c_uint, how: SHUT) errno.Error!void {
1040 const rc = libc.shutdown(@intCast(socketfd), @backingInt(how));
1041 if (rc == -1) return errno.fromInt(errno.fromLibC());
1042 std.debug.assert(rc == 0);
1043}
1044pub fn unlinkat(fd: c_int, name: [*:0]const u8, flag: c_int) !void {
1045 const rc = libc.unlinkat(fd, name, flag);
1046 if (rc == -1) return errno.fromInt(errno.fromLibC());
1047 std.debug.assert(rc == 0);
1048}
1049pub fn lseek(fd: c_int, offset: off_t, whence: c_int) !void {
1050 const rc = libc.lseek(fd, offset, whence);
1051 if (rc == -1) return errno.fromInt(errno.fromLibC());
1052 std.debug.assert(rc >= 0);
1053}