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