Getting started with Zig and trying to write a simple “print working directory” type of command. I’ve read the docs for std.Io.Dir and seen the realPath function and it’s siblings and the advice not to use it. Tried anyway and got an error.FileNotFound.
What is the idiomatic way to get a directory’s path as a string? Here’s the method I tried…
@lalinsky that was my original question. The docs mention the lack of OS support, but don’t offer an alternative means of getting a path from a File or Dir object, nor a way to test for OS support.
With the limitation that it thinks that you can’t have a longer path than MAX_PATH, which isn’t actually the case (at least on Unix-likes).
No, really, try it out. Here a small shell snippet which creates such a path and moves you inside of it:
for i in {1..10000}
do
mkdir directory
cd directory
done
pwd
But if you think about it, you CAN’T actually have a maximum path length.
And it’s very simple to explain why:
Let’s assume there is such a maximum length.
Mount a disk under /mnt.
Create a path which fully uses the maximum length up.
Unmount the disk.
Create another maximum path which uses the maximum path up fully or even just slightly less.
Mount the disk again but now under that newly created path.
You now have a path which is in total longer than the maximum path.
MAX_PATH is only actually used as the maximum length you can use in a single syscall (on Linux) for some syscalls but besides that is just an arbitrary value (and explicitly mentioned in Linux’ documentation to be arbitrary).