From 506c6f458b9fabc1dd60cf58cee6f15637957b55 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Mon, 16 Mar 2026 00:04:54 -0700 Subject: [PATCH] add getdents --- mod.zig | 20 ++++++++++++++++++++ test.zig | 1 + 2 files changed, 21 insertions(+) diff --git a/mod.zig b/mod.zig index 9c4c5309fab236d4115bcb928821d9793a991e71..3e8b278968addfac172e8bf62c1f64860c5f818c 100644 --- a/mod.zig +++ b/mod.zig @@ -2650,6 +2650,7 @@ pub const libc = struct { pub extern fn accept4(socket: c_int, noalias address: ?*struct_sockaddr, noalias address_len: *socklen_t, flags: c_int) c_int; pub extern fn memfd_create(name: [*:0]const u8, flags: c_uint) c_int; pub extern fn sendfile(out_fd: c_int, in_fd: c_int, offset: ?*const off_t, count: usize) isize; + pub extern fn getdents(dirfd: c_int, buf: [*]u8, nbytes: usize) c_int; }; pub const clock_t = c_long; @@ -2694,6 +2695,7 @@ pub const struct_sockaddr_in = extern struct { family: AF = .INET, port: in_port pub const struct_sockaddr_in6 = extern struct { family: AF = .INET6, port: in_port_t, flowinfo: u32, addr: struct_in6_addr, scope_id: u32 }; pub const struct_sockaddr_un = extern struct { family: AF = .UNIX, path: [108]u8 }; pub 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 }; +pub const struct_dirent = extern struct { ino: ino_t, off: off_t, reclen: c_ushort, type: DT, name: [NAME_MAX + 1]u8 }; pub const AT = struct { pub const FDCWD = -100; @@ -3028,6 +3030,18 @@ pub const SO = struct { pub const DOMAIN = 39; }; +pub const DT = enum(u8) { + UNKNOWN = 0, + FIFO = 1, + CHR = 2, + DIR = 4, + BLK = 6, + REG = 8, + LNK = 10, + SOCK = 12, + WHT = 14, +}; + pub fn getpid() pid_t { return libc.getpid(); } @@ -3188,3 +3202,9 @@ pub fn sendfile(out_fd: c_int, in_fd: c_int, offset: ?*const off_t, count: usize std.debug.assert(rc >= 0); return @intCast(rc); } +pub fn getdents(dirfd: c_int, buf: []u8) errno.Error!usize { + const rc = libc.getdents(dirfd, buf.ptr, buf.len); + if (rc == -1) return errno.fromInt(errno.fromLibC()); + std.debug.assert(rc >= 0); + return @intCast(rc); +} diff --git a/test.zig b/test.zig index 82ab24657f3fb3fd58ef037959d0760027e0653d..8457737a32330e84179f577738807d69d921978b 100644 --- a/test.zig +++ b/test.zig @@ -34,4 +34,5 @@ test { _ = &linux.writev; _ = &linux.getsockname; _ = &linux.sendfile; + _ = &linux.getdents; } -- 2.54.0