Commit 83d8378139

aiotter <git@aiotter.com>
2022-05-06 14:58:20
std.c: Add C APIs to read directories
1 parent ac1aaec
Changed files (2)
lib
lib/std/c/darwin.zig
@@ -62,9 +62,13 @@ const private = struct {
     /// force 64bit version.
     /// Note that this is fixed on aarch64 and no longer necessary.
     extern "c" fn @"fstatat$INODE64"(dirfd: fd_t, path_name: [*:0]const u8, buf: *Stat, flags: u32) c_int;
+
+    extern "c" fn readdir(dir: *std.c.DIR) ?*dirent;
+    extern "c" fn @"readdir$INODE64"(dir: *std.c.DIR) ?*dirent;
 };
 pub const fstat = if (native_arch == .aarch64) private.fstat else private.@"fstat$INODE64";
 pub const fstatat = if (native_arch == .aarch64) private.fstatat else private.@"fstatat$INODE64";
+pub const readdir = if (native_arch == .aarch64) private.readdir else private.@"readdir$INODE64";
 
 pub extern "c" fn mach_absolute_time() u64;
 pub extern "c" fn mach_continuous_time() u64;
lib/std/c.zig
@@ -59,6 +59,20 @@ pub usingnamespace switch (builtin.os.tag) {
 
 pub const whence_t = if (builtin.os.tag == .wasi) std.os.wasi.whence_t else c_int;
 
+// Unix-like systems
+pub usingnamespace switch (builtin.os.tag) {
+    .netbsd, .windows => struct {},
+    else => struct {
+        pub const DIR = opaque {};
+        pub extern "c" fn opendir(pathname: [*:0]const u8) ?*DIR;
+        pub extern "c" fn fdopendir(fd: c_int) ?*DIR;
+        pub extern "c" fn rewinddir(dp: *DIR) void;
+        pub extern "c" fn closedir(dp: *DIR) c_int;
+        pub extern "c" fn telldir(dp: *DIR) c_long;
+        pub extern "c" fn seekdir(dp: *DIR, loc: c_long) void;
+    },
+};
+
 pub usingnamespace switch (builtin.os.tag) {
     .netbsd, .macos, .ios, .watchos, .tvos, .windows => struct {},
     else => struct {
@@ -76,6 +90,7 @@ pub usingnamespace switch (builtin.os.tag) {
         pub extern "c" fn sigfillset(set: ?*c.sigset_t) void;
         pub extern "c" fn alarm(seconds: c_uint) c_uint;
         pub extern "c" fn sigwait(set: ?*c.sigset_t, sig: ?*c_int) c_int;
+        pub extern "c" fn readdir(dp: *c.DIR) ?*c.dirent;
     },
 };