authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-26 20:11:54-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-05-26 20:11:54-04:00
loge0a0df5a8a815ce99b40fd97a74b51d3fa9ab35d
tree44f0f08f3c7ef1378a0c42501cefc784637fd45b
parentee1a95b555464d5a309992b06e02efd07770026c
parent552ef5f2e4ecc4b788476be6836d262aaf3216d3
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11598 from aiotter/master

Add functions from `dirent.h` to std.c

3 files changed, 47 insertions(+), 19 deletions(-)

lib/std/c.zig+28-6
...@@ -59,23 +59,45 @@ pub usingnamespace switch (builtin.os.tag) {...@@ -59,23 +59,45 @@ pub usingnamespace switch (builtin.os.tag) {
5959
60pub const whence_t = if (builtin.os.tag == .wasi) std.os.wasi.whence_t else c_int;60pub const whence_t = if (builtin.os.tag == .wasi) std.os.wasi.whence_t else c_int;
6161
62// Unix-like systems
62pub usingnamespace switch (builtin.os.tag) {63pub usingnamespace switch (builtin.os.tag) {
63 .netbsd, .macos, .ios, .watchos, .tvos, .windows => struct {},64 .netbsd, .windows => struct {},
64 else => struct {65 else => struct {
65 pub extern "c" fn clock_getres(clk_id: c_int, tp: *c.timespec) c_int;66 pub const DIR = opaque {};
67 pub extern "c" fn opendir(pathname: [*:0]const u8) ?*DIR;
68 pub extern "c" fn fdopendir(fd: c_int) ?*DIR;
69 pub extern "c" fn rewinddir(dp: *DIR) void;
70 pub extern "c" fn closedir(dp: *DIR) c_int;
71 pub extern "c" fn telldir(dp: *DIR) c_long;
72 pub extern "c" fn seekdir(dp: *DIR, loc: c_long) void;
73
66 pub extern "c" fn clock_gettime(clk_id: c_int, tp: *c.timespec) c_int;74 pub extern "c" fn clock_gettime(clk_id: c_int, tp: *c.timespec) c_int;
67 pub extern "c" fn fstat(fd: c.fd_t, buf: *c.Stat) c_int;75 pub extern "c" fn clock_getres(clk_id: c_int, tp: *c.timespec) c_int;
68 pub extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
69 pub extern "c" fn gettimeofday(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;76 pub extern "c" fn gettimeofday(noalias tv: ?*c.timeval, noalias tz: ?*c.timezone) c_int;
70 pub extern "c" fn nanosleep(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;77 pub extern "c" fn nanosleep(rqtp: *const c.timespec, rmtp: ?*c.timespec) c_int;
78
79 pub extern "c" fn getrusage(who: c_int, usage: *c.rusage) c_int;
80
71 pub extern "c" fn sched_yield() c_int;81 pub extern "c" fn sched_yield() c_int;
82
72 pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;83 pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const c.Sigaction, noalias oact: ?*c.Sigaction) c_int;
73 pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const c.sigset_t, noalias oset: ?*c.sigset_t) c_int;84 pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const c.sigset_t, noalias oset: ?*c.sigset_t) c_int;
85 pub extern "c" fn sigfillset(set: ?*c.sigset_t) void;
86 pub extern "c" fn sigwait(set: ?*c.sigset_t, sig: ?*c_int) c_int;
87
74 pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;88 pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
89
75 pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *c.Stat) c_int;90 pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *c.Stat) c_int;
76 pub extern "c" fn sigfillset(set: ?*c.sigset_t) void;91
77 pub extern "c" fn alarm(seconds: c_uint) c_uint;92 pub extern "c" fn alarm(seconds: c_uint) c_uint;
78 pub extern "c" fn sigwait(set: ?*c.sigset_t, sig: ?*c_int) c_int;93 },
94};
95
96pub usingnamespace switch (builtin.os.tag) {
97 .netbsd, .macos, .ios, .watchos, .tvos, .windows => struct {},
98 else => struct {
99 pub extern "c" fn fstat(fd: c.fd_t, buf: *c.Stat) c_int;
100 pub extern "c" fn readdir(dp: *c.DIR) ?*c.dirent;
79 },101 },
80};102};
81103
lib/std/c/darwin.zig+4-13
...@@ -62,9 +62,13 @@ const private = struct {...@@ -62,9 +62,13 @@ const private = struct {
62 /// force 64bit version.62 /// force 64bit version.
63 /// Note that this is fixed on aarch64 and no longer necessary.63 /// Note that this is fixed on aarch64 and no longer necessary.
64 extern "c" fn @"fstatat$INODE64"(dirfd: fd_t, path_name: [*:0]const u8, buf: *Stat, flags: u32) c_int;64 extern "c" fn @"fstatat$INODE64"(dirfd: fd_t, path_name: [*:0]const u8, buf: *Stat, flags: u32) c_int;
65
66 extern "c" fn readdir(dir: *std.c.DIR) ?*dirent;
67 extern "c" fn @"readdir$INODE64"(dir: *std.c.DIR) ?*dirent;
65};68};
66pub const fstat = if (native_arch == .aarch64) private.fstat else private.@"fstat$INODE64";69pub const fstat = if (native_arch == .aarch64) private.fstat else private.@"fstat$INODE64";
67pub const fstatat = if (native_arch == .aarch64) private.fstatat else private.@"fstatat$INODE64";70pub const fstatat = if (native_arch == .aarch64) private.fstatat else private.@"fstatat$INODE64";
71pub const readdir = if (native_arch == .aarch64) private.readdir else private.@"readdir$INODE64";
6872
69pub extern "c" fn mach_absolute_time() u64;73pub extern "c" fn mach_absolute_time() u64;
70pub extern "c" fn mach_continuous_time() u64;74pub extern "c" fn mach_continuous_time() u64;
...@@ -696,19 +700,6 @@ pub extern "c" fn os_unfair_lock_assert_not_owner(o: os_unfair_lock_t) void;...@@ -696,19 +700,6 @@ pub extern "c" fn os_unfair_lock_assert_not_owner(o: os_unfair_lock_t) void;
696700
697// XXX: close -> close$NOCANCEL701// XXX: close -> close$NOCANCEL
698// XXX: getdirentries -> _getdirentries64702// XXX: getdirentries -> _getdirentries64
699pub extern "c" fn clock_getres(clk_id: c_int, tp: *timespec) c_int;
700pub extern "c" fn clock_gettime(clk_id: c_int, tp: *timespec) c_int;
701pub extern "c" fn getrusage(who: c_int, usage: *rusage) c_int;
702pub extern "c" fn gettimeofday(noalias tv: ?*timeval, noalias tz: ?*timezone) c_int;
703pub extern "c" fn nanosleep(rqtp: *const timespec, rmtp: ?*timespec) c_int;
704pub extern "c" fn sched_yield() c_int;
705pub extern "c" fn sigaction(sig: c_int, noalias act: ?*const Sigaction, noalias oact: ?*Sigaction) c_int;
706pub extern "c" fn sigprocmask(how: c_int, noalias set: ?*const sigset_t, noalias oset: ?*sigset_t) c_int;
707pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
708pub extern "c" fn stat(noalias path: [*:0]const u8, noalias buf: *Stat) c_int;
709pub extern "c" fn sigfillset(set: ?*sigset_t) void;
710pub extern "c" fn alarm(seconds: c_uint) c_uint;
711pub extern "c" fn sigwait(set: ?*sigset_t, sig: ?*c_int) c_int;
712703
713// See: https://opensource.apple.com/source/xnu/xnu-6153.141.1/bsd/sys/_types.h.auto.html704// See: https://opensource.apple.com/source/xnu/xnu-6153.141.1/bsd/sys/_types.h.auto.html
714// TODO: audit mode_t/pid_t, should likely be u16/i32705// TODO: audit mode_t/pid_t, should likely be u16/i32
lib/std/c/linux.zig+15
...@@ -364,3 +364,18 @@ pub const RTLD = struct {...@@ -364,3 +364,18 @@ pub const RTLD = struct {
364 pub const GLOBAL = 256;364 pub const GLOBAL = 256;
365 pub const LOCAL = 0;365 pub const LOCAL = 0;
366};366};
367
368pub const dirent = struct {
369 d_ino: c_uint,
370 d_off: c_uint,
371 d_reclen: c_ushort,
372 d_type: u8,
373 d_name: [256]u8,
374};
375pub const dirent64 = struct {
376 d_ino: c_ulong,
377 d_off: c_ulong,
378 d_reclen: c_ushort,
379 d_type: u8,
380 d_name: [256]u8,
381};