Fd in os.linux.open do not align with os.linux.read

I am trying some systems programming for the first time following the book Linux Systems Programming using Zig 0.13. But so far I find the C examples in the book easier than the Zig versions. One example I dont understand is how os.linux.open return a usize as fd but os.linux.read takes a i32, in C this is just a C int in both cases. How can I possible convert the usize of open which is 64bit to a i32 that read need and why do they not align in std? Looking at the impl of read it looks like it converts the i32 to usize before use anyways so I really dot get what is going on here.

Those functions in std.os.linux are direct syscall wrappers (see, for example, the Source Code of std.os.linux.open for what I mean). Some relevant links:

The closest equivalent to what you’re looking for would be in std.posix, e.g. std.posix.open

Also worth noting that when working with the filesystem and writing cross-platform code, the APIs in std.fs are generally preferred over std.posix. That would look something like:

const file = try std.fs.cwd().openFile("some/path/file.txt", .{});
defer file.close();

// ...
2 Likes