Skip to content

Commit 9429d9c

Browse files
committed
Add getdents64
1 parent 1e65c59 commit 9429d9c

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ linux system lib by zig namespace [std.os.linux](https://ziglang.org/documentati
1010
| [execve.zig](src/execve.zig) | [execve](https://ziglang.org/documentation/master/std/#std.os.linux.execve) |
1111
| [fork_waitpid.zig](src/fork_waitpid.zig) | [fork](https://ziglang.org/documentation/master/std/#std.os.linux.fork) [waitpid](https://ziglang.org/documentation/master/std/#std.os.linux.waitpid) |
1212
| [getcwd.zig](src/getcwd.zig) | [getcwd](https://ziglang.org/documentation/master/std/#std.os.linux.getcwd) [chdir](https://ziglang.org/documentation/master/std/#std.os.linux.chdir) |
13+
| [getdents64](src/getdents64.zig) | [getdents64](https://ziglang.org/documentation/master/std/#std.os.linux.getdents64) |
1314
| [getgroups.zig](src/getgroups.zig) | [getgroups](https://ziglang.org/documentation/master/std/#std.os.linux.getgroups) |
1415
| [getrandom.zig](src/getrandom.zig) | [getrandom](https://ziglang.org/documentation/master/std/#std.os.linux.getrandom) |
1516
| [lstat.zig](src/lstat.zig) | [lstat](https://ziglang.org/documentation/master/std/#std.os.linux.lstat) |

src/getdents64.zig

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const std = @import("std");
2+
3+
const mem = std.mem;
4+
const stdout = std.io.getStdOut().writer();
5+
const linux = std.os.linux;
6+
const buf_size = 1024;
7+
8+
pub fn main() !void {
9+
const tmp_dir = "/tmp";
10+
var buf: [buf_size]u8 = undefined;
11+
12+
const open_rc = linux.open(tmp_dir, linux.O{}, 0);
13+
const err = linux.E.init(open_rc);
14+
if (err != linux.E.SUCCESS) {
15+
try stdout.print("Error is {}\n", .{err});
16+
}
17+
defer _ = linux.close(@intCast(open_rc));
18+
19+
while (true) {
20+
const nread = linux.getdents64(@intCast(open_rc), &buf, buf.len);
21+
if (nread == 0) {
22+
break;
23+
}
24+
var index: u32 = 0;
25+
while (index < nread) {
26+
const entry = @as(*align(1) linux.dirent64, @ptrCast(&buf[index]));
27+
const name = mem.sliceTo(@as([*:0]u8, @ptrCast(&entry.name)), 0);
28+
try stdout.print("{} {s}\n", .{ entry.ino, name });
29+
index += entry.reclen;
30+
}
31+
try stdout.print("total entries: {}\n", .{nread});
32+
}
33+
}

0 commit comments

Comments
 (0)