Way to get current working directory in `Io`

I’m learning the Io interface and be trying to find some C function equivalents, for example, the getcwd(3) C function, to make a tiny program behaves like the pwd(1) utility. I tried two approaches, first std.c.getcwd(), and std.process.currentPath() secondly:

const std = @import("std");
const Io = std.Io;

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var stdout_buffer: [512]u8 = undefined;
    var stdout_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
    const bw = &stdout_writer.interface;

    const cwd: []const u8 = @ptrCast(std.c.getcwd(&stdout_buffer, stdout_buffer.len));
    // const cwd = std.process.currentPath(io, &stdout_buffer);
    try bw.print("{s}\n", .{cwd.?}); // `cwd` if process.currentPath used
    try bw.flush();
}

(1) For std.c.getcwd, Since print expects a slice, I tried @ptrCast but compiler cannot infer the complete result type:

cwd_libc.zig:11:29: error: cannot infer length of slice from many pointer

(2) I expect currentPath has result type ?[]const u8 but in fact !usize, and I wonder why this and what API should I use instead.

Edit: I know many-item pointer has unknown length but then how can we print it :frowning:

const std = @import("std");
const Io = std.Io;

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var stdout_buffer: [512]u8 = undefined;
    var stdout_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
    const bw = &stdout_writer.interface;

    var cwd_buffer: [std.c.PATH_MAX]u8 = undefined;
    const cwd: ?[]const u8 = std.mem.sliceTo(std.c.getcwd(&cwd_buffer, cwd_buffer.len), 0);
    try bw.print("{s}\n", .{cwd.?});
    try bw.flush();
}
const std = @import("std");
const Io = std.Io;

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var stdout_buffer: [512]u8 = undefined;
    var stdout_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
    const bw = &stdout_writer.interface;
    var cwd_buffer: [std.Io.Dir.max_path_bytes]u8 = undefined;
    const cwd: []const u8 = cwd_buffer[0..try std.process.currentPath(io, &cwd_buffer)];
    try bw.print("{s}\n", .{cwd});
    try bw.flush();
}

Another choice

const std = @import("std");
const Io = std.Io;

pub fn main(init: std.process.Init) !void {
    const io = init.io;

    var stdout_buffer: [512]u8 = undefined;
    var stdout_writer: Io.File.Writer = .init(.stdout(), io, &stdout_buffer);
    const bw = &stdout_writer.interface;
    var cwd_buffer: [std.Io.Dir.max_path_bytes]u8 = undefined;
    var cwd_fba: std.heap.FixedBufferAllocator = .init(&cwd_buffer);
    const cwd: []const u8 = try std.process.currentPathAlloc(io, cwd_fba.allocator());
    try bw.print("{s}\n", .{cwd});
    try bw.flush();
}

Why not use the std.Io.Dir.cwd()?

In general, file operations in Zig operate relative to a target directory, and std.Io.Dir.cwd() gets you the working directory. Most of the operations you’d do on paths in a lot of other languages operate on Io.Dir or Io.Files exclusively in Zig.

I’d probably use

const cwd = std.Io.Dir.cwd();
var path_buffer: [std.fs.max_path_bytes]u8 = undefined;
const path = try cwd.realPath(io, &path_buffer);

I’ll admit, I’m unsure how this is different from std.process.currentPath, though.

I learned lots of new stuffs here: 1) how to convert a many-item pointer to a slice 2) use process.currentPath as the end: usize 3) use FixBufferallocator API instead of buffer 4) Io.Dir.cwd is where operations on File/Dir we usually need. You guys are true talents.

This function has limited platform support, and using it can lead to unnecessary failures and race conditions. It is generally advisable to avoid this function entirely.

Using std.process.currentPathand std.fs.path.resolveAlloc/resolveAppend together to replace realpath is encouraged.

Oh, sorry, my example using fixedBufferAllocator might have been a bit misleading. I only used it to make currentPathAlloc behave as similarly to currentPath as possible. Generally, if we want to use currentPathAlloc, we can just manage it with a regular heap allocator. If you want to manage it on the stack, you usually use currentPath without going around.

Although many people prefer to avoid using the heap, I think managing it on the heap is appropriate here, because essentially, from a cross-platform perspective, there isn’t really a maximum path length. std.c.MAX_PATH isn’t a true limit; it’s just a commonly enforced buffer size limit and isn’t really usable across platforms.

std.process.currentPath or std.process.currentPathAlloc.

The currently accepted answer of using std.c.getcwd introduces an unnecessary dependency on libc and bypasses the std.Io interface which is okay to do in an application where you control everything, but in a reusable package meant to be consumed by third parties is entirely unacceptable.

And just to clarify since there was some confusion around it, usage of std.process.currentPath would look something like:

var buffer: [std.Io.Dir.max_path_bytes]u8 = undefined;
const len = try std.process.currentPath(io, &buffer);
const current_path = buffer[0..len];
// ...