authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-10-16 04:04:04-07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-10-16 04:04:04-07:00
log88b83f4860d1c868a2f31f8a36830235ddfcb4b2
treed30a495f7cbf030d6fb7c6eec11538eee52ea684
parente01bc50a47af4b4a8454d56c292e4d5b0780c513
signature Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add openbsd


2 files changed, 1057 insertions(+), 0 deletions(-)

openbsd.zig created+1055
......@@ -0,0 +1,1055 @@
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(@intFromEnum(val) > value);
215 value = @intFromEnum(val);
216 }
217 }
218 comptime {
219 // sanity test
220 std.debug.assert(@intFromEnum(Enum.EPERM) == 1);
221 }
222 const list = blk: {
223 const values = std.enums.values(Enum);
224 const len = @intFromEnum(values[values.len - 1]) + 1;
225 var errors: [len]Error = @splat(error.Unexpected);
226 for (values) |f| errors[@intFromEnum(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, @enumFromInt(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 = @cImport({
416 @cInclude("pthread.h");
417});
418pub const pthread_t = impdef.pthread_t;
419pub const pthread_attr_t = impdef.pthread_attr_t;
420pub const pthread_mutexattr_t = impdef.pthread_mutexattr_t;
421pub const pthread_mutex_t = impdef.pthread_mutex_t;
422pub const pthread_condattr_t = impdef.pthread_condattr_t;
423pub const pthread_cond_t = impdef.pthread_cond_t;
424pub const pthread_rwlockattr_t = impdef.pthread_rwlockattr_t;
425pub const pthread_rwlock_t = impdef.pthread_rwlock_t;
426
427pub const INT_MAX = std.math.maxInt(c_int);
428pub const UINT_MAX = std.math.maxInt(c_uint);
429pub const ARG_MAX = (512 * 1024);
430pub const CHILD_MAX = 80;
431pub const LINK_MAX = 32767;
432pub const MAX_CANON = 255;
433pub const MAX_INPUT = 255;
434pub const NAME_MAX = 255;
435pub const NGROUPS_MAX = 16;
436pub const OPEN_MAX = 64;
437pub const PATH_MAX = 1024;
438pub const PIPE_BUF = 512;
439pub const SYMLINK_MAX = PATH_MAX;
440pub const SYMLOOP_MAX = 32;
441pub const BC_BASE_MAX = INT_MAX;
442pub const BC_DIM_MAX = 65535;
443pub const BC_SCALE_MAX = INT_MAX;
444pub const BC_STRING_MAX = INT_MAX;
445pub const COLL_WEIGHTS_MAX = 2;
446pub const EXPR_NEST_MAX = 32;
447pub const LINE_MAX = 2048;
448pub const RE_DUP_MAX = 255;
449pub const SEM_VALUE_MAX = UINT_MAX;
450
451pub const O = struct {
452 pub const RDONLY = 0x0000;
453 pub const WRONLY = 0x0001;
454 pub const RDWR = 0x0002;
455 pub const ACCMODE = 0x0003;
456 pub const NONBLOCK = 0x0004;
457 pub const APPEND = 0x0008;
458 pub const SHLOCK = 0x0010;
459 pub const EXLOCK = 0x0020;
460 pub const ASYNC = 0x0040;
461 pub const FSYNC = 0x0080;
462 pub const SYNC = 0x0080;
463 pub const DSYNC = SYNC;
464 pub const RSYNC = SYNC;
465 pub const NOFOLLOW = 0x0100;
466 pub const CREAT = 0x0200;
467 pub const TRUNC = 0x0400;
468 pub const EXCL = 0x0800;
469 pub const NOCTTY = 0x8000;
470 pub const CLOEXEC = 0x10000;
471 pub const DIRECTORY = 0x20000;
472 pub const CLOFORK = 0x40000;
473};
474
475pub const AT = struct {
476 pub const FDCWD = -100;
477 pub const EACCESS = 0x01;
478 pub const SYMLINK_NOFOLLOW = 0x02;
479 pub const SYMLINK_FOLLOW = 0x04;
480 pub const REMOVEDIR = 0x08;
481};
482
483pub const SIG = struct {
484 pub const HUP = 1;
485 pub const INT = 2;
486 pub const QUIT = 3;
487 pub const ILL = 4;
488 pub const TRAP = 5;
489 pub const ABRT = 6;
490 pub const IOT = ABRT;
491 pub const EMT = 7;
492 pub const FPE = 8;
493 pub const KILL = 9;
494 pub const BUS = 10;
495 pub const SEGV = 11;
496 pub const SYS = 12;
497 pub const PIPE = 13;
498 pub const ALRM = 14;
499 pub const TERM = 15;
500 pub const URG = 16;
501 pub const STOP = 17;
502 pub const TSTP = 18;
503 pub const CONT = 19;
504 pub const CHLD = 20;
505 pub const TTIN = 21;
506 pub const TTOU = 22;
507 pub const IO = 23;
508 pub const XCPU = 24;
509 pub const XFSZ = 25;
510 pub const VTALRM = 26;
511 pub const PROF = 27;
512 pub const WINCH = 28;
513 pub const INFO = 29;
514 pub const USR1 = 30;
515 pub const USR2 = 31;
516 pub const THR = 32;
517};
518
519pub const POLL = struct {
520 pub const IN = 0x0001;
521 pub const PRI = 0x0002;
522 pub const OUT = 0x0004;
523 pub const ERR = 0x0008;
524 pub const HUP = 0x0010;
525 pub const NVAL = 0x0020;
526 pub const RDNORM = 0x0040;
527 pub const NORM = RDNORM;
528 pub const WRNORM = OUT;
529 pub const RDBAND = 0x0080;
530 pub const WRBAND = 0x0100;
531};
532
533pub const W = struct {
534 pub const NOHANG = 0x01;
535 pub const UNTRACED = 0x02;
536 pub const CONTINUED = 0x08;
537 pub const EXITED = 0x04;
538 pub const STOPPED = UNTRACED;
539 pub const NOWAIT = 0x10;
540 pub const TRAPPED = 0x20;
541 pub const _STOPPED = 0o177;
542 pub const _CONTINUED = 0o177777;
543 pub const COREFLAG = 0o200;
544
545 pub fn _STATUS(x: c_int) u8 {
546 return @intCast(x & 0o177);
547 }
548 pub fn IFSTOPPED(x: c_int) bool {
549 return (x & 0xff) == _STOPPED;
550 }
551 pub fn STOPSIG(x: c_int) u8 {
552 return @truncate(@as(c_uint, @bitCast(x >> 8)));
553 }
554 pub fn IFSIGNALED(x: c_int) bool {
555 return _STATUS(x) != _STOPPED and _STATUS(x) != 0;
556 }
557 pub fn TERMSIG(x: c_int) u8 {
558 return _STATUS(x);
559 }
560 pub fn IFEXITED(x: c_int) bool {
561 return _STATUS(x) == 0;
562 }
563 pub fn EXITSTATUS(x: c_int) u8 {
564 return @truncate(@as(c_uint, @bitCast(x >> 8)));
565 }
566 pub fn IFCONTINUED(x: c_int) bool {
567 return (x & _CONTINUED) == _CONTINUED;
568 }
569 pub fn COREDUMP(x: c_int) u8 {
570 return x & COREFLAG;
571 }
572 pub fn _EXITCODE(r: c_int, s: c_int) c_int {
573 return r << 8 | s;
574 }
575 pub fn _STOPCODE(s: c_int) c_int {
576 return s << 8 | _STOPPED;
577 }
578};
579
580pub const S = struct {
581 pub const ISUID = 0o004000;
582 pub const ISGID = 0o002000;
583 pub const ISTXT = 0o001000;
584 pub const IRWXU = 0o000700;
585 pub const IRUSR = 0o000400;
586 pub const IWUSR = 0o000200;
587 pub const IXUSR = 0o000100;
588 pub const IREAD = IRUSR;
589 pub const IWRITE = IWUSR;
590 pub const IEXEC = IXUSR;
591 pub const IRWXG = 0o000070;
592 pub const IRGRP = 0o000040;
593 pub const IWGRP = 0o000020;
594 pub const IXGRP = 0o000010;
595 pub const IRWXO = 0o000007;
596 pub const IROTH = 0o000004;
597 pub const IWOTH = 0o000002;
598 pub const IXOTH = 0o000001;
599 pub const IFMT = 0o170000;
600 pub const IFIFO = 0o010000;
601 pub const IFCHR = 0o020000;
602 pub const IFDIR = 0o040000;
603 pub const IFBLK = 0o060000;
604 pub const IFREG = 0o100000;
605 pub const IFLNK = 0o120000;
606 pub const IFSOCK = 0o140000;
607 pub const IFWHT = 0o160000; //unused
608 pub const ISVTX = 0o001000;
609};
610
611pub const AF = enum(sa_family_t) {
612 UNSPEC = 0,
613 UNIX = 1,
614 INET = 2,
615 IMPLINK = 3,
616 PUP = 4,
617 CHAOS = 5,
618 NS = 6,
619 ISO = 7,
620 ECMA = 8,
621 DATAKIT = 9,
622 CCITT = 10,
623 SNA = 11,
624 DECnet = 12,
625 DLI = 13,
626 LAT = 14,
627 HYLINK = 15,
628 APPLETALK = 16,
629 ROUTE = 17,
630 LINK = 18,
631 XTP = 19,
632 COIP = 20,
633 CNT = 21,
634 RTIP = 22,
635 IPX = 23,
636 INET6 = 24,
637 PIP = 25,
638 ISDN = 26,
639 NATM = 27,
640 ENCAP = 28,
641 SIP = 29,
642 KEY = 30,
643 HDRCMPLT = 31,
644 BLUETOOTH = 32,
645 MPLS = 33,
646 PFLOW = 34,
647 PIPEX = 35,
648 FRAME = 36,
649
650 pub const LOCAL: AF = .UNIX;
651 pub const OSI: AF = .ISO;
652 pub const E164: AF = .ISDN;
653};
654
655pub const SOCK = struct {
656 pub const STREAM = 1;
657 pub const DGRAM = 2;
658 pub const RAW = 3;
659 pub const RDM = 4;
660 pub const SEQPACKET = 5;
661 pub const CLOEXEC = 0x8000;
662 pub const NONBLOCK = 0x4000;
663 pub const DNS = 0x1000;
664 pub const CLOFORK = 0x0800;
665};
666
667pub const IPPROTO = struct {
668 pub const IP = 0;
669 pub const HOPOPTS = IP;
670 pub const ICMP = 1;
671 pub const IGMP = 2;
672 pub const GGP = 3;
673 pub const IPIP = 4;
674 pub const IPV4 = IPIP;
675 pub const TCP = 6;
676 pub const EGP = 8;
677 pub const PUP = 12;
678 pub const UDP = 17;
679 pub const IDP = 22;
680 pub const TP = 29;
681 pub const IPV6 = 41;
682 pub const ROUTING = 43;
683 pub const FRAGMENT = 44;
684 pub const RSVP = 46;
685 pub const GRE = 47;
686 pub const ESP = 50;
687 pub const AH = 51;
688 pub const MOBILE = 55;
689 pub const ICMPV6 = 58;
690 pub const NONE = 59;
691 pub const DSTOPTS = 60;
692 pub const EON = 80;
693 pub const ETHERIP = 97;
694 pub const ENCAP = 98;
695 pub const PIM = 103;
696 pub const IPCOMP = 108;
697 pub const CARP = 112;
698 pub const SCTP = 132;
699 pub const UDPLITE = 136;
700 pub const MPLS = 137;
701 pub const PFSYNC = 240;
702 pub const RAW = 255;
703};
704
705pub const CLOCK = enum(clockid_t) {
706 REALTIME = 0,
707 PROCESS_CPUTIME_ID = 2,
708 MONOTONIC = 3,
709 THREAD_CPUTIME_ID = 4,
710 UPTIME = 5,
711 BOOTTIME = 6,
712 _,
713};
714
715pub const SOL = struct {
716 pub const SOCKET = 0xffff;
717};
718
719pub const SO = struct {
720 pub const DEBUG = 0x0001;
721 pub const ACCEPTCONN = 0x0002;
722 pub const REUSEADDR = 0x0004;
723 pub const KEEPALIVE = 0x0008;
724 pub const DONTROUTE = 0x0010;
725 pub const BROADCAST = 0x0020;
726 pub const USELOOPBACK = 0x0040;
727 pub const LINGER = 0x0080;
728 pub const OOBINLINE = 0x0100;
729 pub const REUSEPORT = 0x0200;
730 pub const TIMESTAMP = 0x0800;
731 pub const BINDANY = 0x1000;
732 pub const ZEROIZE = 0x2000;
733 pub const SNDBUF = 0x1001;
734 pub const RCVBUF = 0x1002;
735 pub const SNDLOWAT = 0x1003;
736 pub const RCVLOWAT = 0x1004;
737 pub const SNDTIMEO = 0x1005;
738 pub const RCVTIMEO = 0x1006;
739 pub const ERROR = 0x1007;
740 pub const TYPE = 0x1008;
741 pub const RTABLE = 0x1021;
742 pub const PEERCRED = 0x1022;
743 pub const SPLICE = 0x1023;
744 pub const DOMAIN = 0x1024;
745 pub const PROTOCOL = 0x1025;
746};
747
748pub const DT = enum(u8) {
749 UNKNOWN = 0,
750 FIFO = 1,
751 CHR = 2,
752 DIR = 4,
753 BLK = 6,
754 REG = 8,
755 LNK = 10,
756 SOCK = 12,
757 WHT = 14, //unused
758};
759
760pub const SHUT = enum(c_int) {
761 RD = 0,
762 WR = 1,
763 RDWR = 2,
764};
765
766pub const MAP = struct {
767 pub const SHARED = 0x0001;
768 pub const PRIVATE = 0x0002;
769 pub const FIXED = 0x0010;
770 pub const NOREPLACE = 0x0800;
771 pub const ANON = 0x1000;
772 pub const ANONYMOUS = ANON;
773 pub const NOFAULT = 0x2000;
774 pub const STACK = 0x4000;
775 pub const CONCEAL = 0x8000;
776 pub const FAILED = -1;
777};
778
779pub const PROT = struct {
780 pub const NONE = 0x00;
781 pub const READ = 0x01;
782 pub const WRITE = 0x02;
783 pub const EXEC = 0x04;
784};
785
786pub const IPV6 = struct {
787 pub const UNICAST_HOPS = 4;
788 pub const MULTICAST_IF = 9;
789 pub const MULTICAST_HOPS = 10;
790 pub const MULTICAST_LOOP = 11;
791 pub const JOIN_GROUP = 12;
792 pub const LEAVE_GROUP = 13;
793 pub const PORTRANGE = 14;
794 pub const CHECKSUM = 26;
795 pub const V6ONLY = 27;
796 pub const RTHDRDSTOPTS = 35;
797 pub const RECVPKTINFO = 36;
798 pub const RECVHOPLIMIT = 37;
799 pub const RECVRTHDR = 38;
800 pub const RECVHOPOPTS = 39;
801 pub const RECVDSTOPTS = 40;
802 pub const USE_MIN_MTU = 42;
803 pub const RECVPATHMTU = 43;
804 pub const PATHMTU = 44;
805 pub const PKTINFO = 46;
806 pub const HOPLIMIT = 47;
807 pub const NEXTHOP = 48;
808 pub const HOPOPTS = 49;
809 pub const DSTOPTS = 50;
810 pub const RTHDR = 51;
811 pub const AUTH_LEVEL = 53;
812 pub const ESP_TRANS_LEVEL = 54;
813 pub const ESP_NETWORK_LEVEL = 55;
814 pub const RECVTCLASS = 57;
815 pub const AUTOFLOWLABEL = 59;
816 pub const IPCOMP_LEVEL = 60;
817 pub const TCLASS = 61;
818 pub const DONTFRAG = 62;
819 pub const PIPEX = 63;
820 pub const RECVDSTPORT = 64;
821 pub const MINHOPCOUNT = 65;
822};
823
824pub const SEEK = struct {
825 pub const SET = 0;
826 pub const CUR = 1;
827 pub const END = 2;
828};
829
830pub fn pipe2(flag: c_int) ![2]c_int {
831 var fildes: [2]c_int = @splat(-1);
832 const rc = libc.pipe2(&fildes, flag);
833 if (rc == -1) return errno.fromInt(errno.fromLibC());
834 std.debug.assert(rc == 0);
835 return fildes;
836}
837pub fn openat(fd: c_int, file: [*:0]const u8, oflag: c_int) errno.Error!c_int {
838 const rc = libc.openat(fd, file, oflag);
839 if (rc == -1) return errno.fromInt(errno.fromLibC());
840 std.debug.assert(rc >= 0);
841 return rc;
842}
843pub fn openat4(fd: c_int, file: [*:0]const u8, oflag: c_int, mode: mode_t) errno.Error!c_int {
844 const rc = libc.openat(fd, file, oflag, mode);
845 if (rc == -1) return errno.fromInt(errno.fromLibC());
846 return rc;
847}
848pub fn close(fd: c_int) errno.Error!void {
849 const rc = libc.close(fd);
850 if (rc == -1) return errno.fromInt(errno.fromLibC());
851 std.debug.assert(rc == 0);
852}
853pub fn fork() !pid_t {
854 const rc = libc.fork();
855 if (rc == -1) return errno.fromInt(errno.fromLibC());
856 std.debug.assert(rc >= 0);
857 return rc;
858}
859pub fn dup2(fildes: c_int, fildes2: c_int) !void {
860 const rc = libc.dup2(fildes, fildes2);
861 if (rc == -1) return errno.fromInt(errno.fromLibC());
862 std.debug.assert(rc >= 0);
863}
864pub fn fchdir(fildes: c_int) !void {
865 const rc = libc.fchdir(fildes);
866 if (rc == -1) return errno.fromInt(errno.fromLibC());
867 std.debug.assert(rc == 0);
868}
869pub fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) !noreturn {
870 const rc = libc.execvp(file, argv);
871 std.debug.assert(rc == -1);
872 return errno.fromInt(errno.fromLibC());
873}
874pub fn kill(pid: pid_t, sig: c_int) !void {
875 const rc = libc.kill(pid, sig);
876 if (rc == -1) return errno.fromInt(errno.fromLibC());
877 std.debug.assert(rc == 0);
878}
879pub fn poll(fds: []struct_pollfd, timeout_ms: c_int) !c_int {
880 const rc = libc.poll(fds.ptr, @intCast(fds.len), timeout_ms);
881 if (rc == -1) return errno.fromInt(errno.fromLibC());
882 std.debug.assert(rc >= 0);
883 return rc;
884}
885pub fn read(fd: c_int, buf: []u8) errno.Error!usize {
886 const rc = libc.read(fd, buf.ptr, buf.len);
887 if (rc == -1) return errno.fromInt(errno.fromLibC());
888 std.debug.assert(rc >= 0);
889 return @intCast(rc);
890}
891pub fn write(fd: c_int, buf: []const u8) !usize {
892 const rc = libc.write(fd, buf.ptr, buf.len);
893 if (rc == -1) return errno.fromInt(errno.fromLibC());
894 std.debug.assert(rc >= 0);
895 return @intCast(rc);
896}
897pub fn waitpid(pid: pid_t, options: c_int) !struct { pid_t, c_int } {
898 var status: c_int = 0;
899 const rc = libc.waitpid(pid, &status, options);
900 if (rc == -1) return errno.fromInt(errno.fromLibC());
901 std.debug.assert(rc >= 0);
902 return .{ rc, status };
903}
904pub fn getenv(name: [:0]const u8) ?[:0]const u8 {
905 return std.mem.sliceTo(libc.getenv(name.ptr) orelse return null, 0);
906}
907pub fn exit(status: c_int) noreturn {
908 return libc.exit(status);
909}
910pub fn mkdirat(fd: c_int, path: [*:0]const u8, mode: mode_t) errno.Error!void {
911 const rc = libc.mkdirat(fd, path, mode);
912 if (rc == -1) return errno.fromInt(errno.fromLibC());
913 std.debug.assert(rc == 0);
914}
915pub fn fstatat(fd: c_int, path: [*:0]const u8, flag: c_int) errno.Error!struct_stat {
916 var buf: struct_stat = undefined;
917 const rc = libc.fstatat(fd, path, &buf, flag);
918 if (rc == -1) return errno.fromInt(errno.fromLibC());
919 std.debug.assert(rc == 0);
920 return buf;
921}
922pub fn getaddrinfo(noalias nodename: ?[*:0]const u8, noalias servname: ?[*:0]const u8, noalias req: ?*const struct_addrinfo) eai.Error!*const struct_addrinfo {
923 var pai: *const struct_addrinfo = undefined;
924 var hints = std.mem.zeroes(struct_addrinfo);
925 const rc = libc.getaddrinfo(nodename, servname, req orelse &hints, &pai);
926 if (rc < 0) return eai.fromInt(rc);
927 std.debug.assert(rc == 0);
928 return pai;
929}
930pub fn freeaddrinfo(ai: *const struct_addrinfo) void {
931 return libc.freeaddrinfo(ai);
932}
933pub fn socket(domain: AF, ty: c_int, protocol: c_int) errno.Error!c_uint {
934 const rc = libc.socket(@intFromEnum(domain), ty, protocol);
935 if (rc == -1) return errno.fromInt(errno.fromLibC());
936 std.debug.assert(rc > 0);
937 return @intCast(rc);
938}
939pub fn connect(socketfd: c_uint, address: *const struct_sockaddr, address_len: socklen_t) errno.Error!void {
940 const rc = libc.connect(@intCast(socketfd), address, address_len);
941 if (rc == -1) return errno.fromInt(errno.fromLibC());
942 std.debug.assert(rc == 0);
943}
944pub fn clock_gettime(clock_id: CLOCK) !struct_timespec {
945 var tp: struct_timespec = undefined;
946 const rc = libc.clock_gettime(@intFromEnum(clock_id), &tp);
947 if (rc == -1) return errno.fromInt(errno.fromLibC());
948 std.debug.assert(rc == 0);
949 return tp;
950}
951pub fn renameat(oldfd: c_int, old: [*:0]const u8, newfd: c_int, new: [*:0]const u8) !void {
952 const rc = libc.renameat(oldfd, old, newfd, new);
953 if (rc == -1) return errno.fromInt(errno.fromLibC());
954 std.debug.assert(rc == 0);
955}
956pub fn send(socketfd: c_uint, buffer: []const u8, flags: c_int) errno.Error!usize {
957 const rc = libc.send(@intCast(socketfd), buffer.ptr, buffer.len, flags);
958 if (rc == -1) return errno.fromInt(errno.fromLibC());
959 std.debug.assert(rc >= 0);
960 return @intCast(rc);
961}
962pub fn recv(socketfd: c_uint, buffer: []u8, flags: c_int) errno.Error!usize {
963 const rc = libc.recv(@intCast(socketfd), buffer.ptr, buffer.len, flags);
964 if (rc == -1) return errno.fromInt(errno.fromLibC());
965 std.debug.assert(rc >= 0);
966 return @intCast(rc);
967}
968pub fn fstat(fd: c_int) errno.Error!struct_stat {
969 var buf: struct_stat = undefined;
970 const rc = libc.fstat(fd, &buf);
971 if (rc == -1) return errno.fromInt(errno.fromLibC());
972 std.debug.assert(rc == 0);
973 return buf;
974}
975pub fn mmap(addr: ?*anyopaque, len: usize, prot: c_int, flags: c_int, fd: c_int, offset: off_t) errno.Error![]u8 {
976 const rc = libc.mmap(addr, len, prot, flags, fd, offset);
977 if (@intFromPtr(rc) == MAP.FAILED) return errno.fromInt(errno.fromLibC());
978 const ptr: [*]u8 = @ptrCast(rc);
979 return ptr[0..len];
980}
981pub fn writev(fd: c_int, iovec: []const struct_iovec) !usize {
982 const rc = libc.writev(fd, iovec.ptr, @intCast(iovec.len));
983 if (rc == -1) return errno.fromInt(errno.fromLibC());
984 std.debug.assert(rc >= 0);
985 return @intCast(rc);
986}
987pub fn faccessat(fd: c_int, path: [*:0]const u8, amode: c_int, flag: c_int) !void {
988 const rc = libc.faccessat(fd, path, amode, flag);
989 if (rc == -1) return errno.fromInt(errno.fromLibC());
990 std.debug.assert(rc == 0);
991}
992pub fn readlinkat(dirfd: c_int, noalias path: [*:0]const u8, noalias buf: []u8) ![:0]u8 {
993 const rc = libc.readlinkat(dirfd, path, buf.ptr, buf.len);
994 if (rc == -1) return errno.fromInt(errno.fromLibC());
995 std.debug.assert(rc >= 0);
996 buf[@intCast(rc)] = 0;
997 return buf[0..@intCast(rc) :0];
998}
999pub fn futimens(fd: c_int, times: [2]struct_timespec) !void {
1000 const rc = libc.futimens(fd, &times);
1001 if (rc == -1) return errno.fromInt(errno.fromLibC());
1002 std.debug.assert(rc == 0);
1003}
1004pub fn setsockopt(socketfd: c_uint, level: c_int, optname: c_int, opaque_opt: []const u8) errno.Error!void {
1005 const rc = libc.setsockopt(@intCast(socketfd), level, optname, opaque_opt.ptr, @intCast(opaque_opt.len));
1006 if (rc == -1) return errno.fromInt(errno.fromLibC());
1007 std.debug.assert(rc == 0);
1008}
1009pub fn bind(socketfd: c_uint, address: *const struct_sockaddr, address_len: socklen_t) errno.Error!void {
1010 const rc = libc.bind(@intCast(socketfd), address, address_len);
1011 if (rc == -1) return errno.fromInt(errno.fromLibC());
1012 std.debug.assert(rc == 0);
1013}
1014pub fn listen(socketfd: c_uint, backlog: c_int) errno.Error!void {
1015 const rc = libc.listen(@intCast(socketfd), backlog);
1016 if (rc == -1) return errno.fromInt(errno.fromLibC());
1017 std.debug.assert(rc == 0);
1018}
1019pub fn getsockname(socketfd: c_uint, noalias address: *struct_sockaddr, noalias address_len: *socklen_t) errno.Error!void {
1020 const rc = libc.getsockname(@intCast(socketfd), address, address_len);
1021 if (rc == -1) return errno.fromInt(errno.fromLibC());
1022 std.debug.assert(rc == 0);
1023}
1024pub fn munmap(addr: *const anyopaque, len: usize) errno.Error!void {
1025 const rc = libc.munmap(addr, len);
1026 if (rc == -1) return errno.fromInt(errno.fromLibC());
1027 std.debug.assert(rc == 0);
1028}
1029pub fn getdents(dirfd: c_int, buf: []u8) errno.Error!usize {
1030 const rc = libc.getdents(dirfd, buf.ptr, buf.len);
1031 if (rc == -1) return errno.fromInt(errno.fromLibC());
1032 std.debug.assert(rc >= 0);
1033 return @intCast(rc);
1034}
1035pub fn accept4(socketfd: c_uint, noalias address: ?*struct_sockaddr, noalias address_len: *socklen_t, flags: c_int) errno.Error!c_uint {
1036 const rc = libc.accept4(@intCast(socketfd), address, address_len, flags);
1037 if (rc == -1) return errno.fromInt(errno.fromLibC());
1038 std.debug.assert(rc > 0);
1039 return @intCast(rc);
1040}
1041pub fn shutdown(socketfd: c_uint, how: SHUT) errno.Error!void {
1042 const rc = libc.shutdown(@intCast(socketfd), @intFromEnum(how));
1043 if (rc == -1) return errno.fromInt(errno.fromLibC());
1044 std.debug.assert(rc == 0);
1045}
1046pub fn unlinkat(fd: c_int, name: [*:0]const u8, flag: c_int) !void {
1047 const rc = libc.unlinkat(fd, name, flag);
1048 if (rc == -1) return errno.fromInt(errno.fromLibC());
1049 std.debug.assert(rc == 0);
1050}
1051pub fn lseek(fd: c_int, offset: off_t, whence: c_int) !void {
1052 const rc = libc.lseek(fd, offset, whence);
1053 if (rc == -1) return errno.fromInt(errno.fromLibC());
1054 std.debug.assert(rc >= 0);
1055}
sys.zig+2
......@@ -6,6 +6,7 @@ pub const impl = switch (builtin.target.os.tag) {
66 .macos => darwin,
77 .freebsd => freebsd,
88 .netbsd => netbsd,
9 .openbsd => openbsd,
910 else => unreachable,
1011};
1112
......@@ -13,3 +14,4 @@ pub const linux = @import("./linux.zig");
1314pub const darwin = @import("./darwin.zig");
1415pub const freebsd = @import("./freebsd.zig");
1516pub const netbsd = @import("./netbsd.zig");
17pub const openbsd = @import("./openbsd.zig");