Zig equivalent to mkstemp?

Is there an equivalent to glibc’s mkstemp(3) tucked away somewhere in Zig’s standard library? I searched in std.fs to no avail, but that might have been the wrong place to look.

If not mkstemp, is there an alternative for creating guaranteed-unique file names in Zig?

Did you try searching the standard documentation for temp or tmp?

std.Build.makeTempPath could be a source of inspiration perhaps?

Looks like makeTempPath relies on std.crypto.random for uniqueness. That might work.

Generally speaking, I am looking for the most idiomatic approach for Zig in this use case, and I am also trying to learn about the content and deliberate limitations of std.

Usually that sort of thing can be accessed under std.c but it doesn’t look like they have incorporated that function into the standard library. They have only added what the compiler uses.

If you are linking libc, then you can declare the function to use it.

pub extern "c" fn mkstemp(template: ?[*:0]const u8) c_int
4 Likes

Thanks, that’s something to keep in mind for future reference. I don’t currently link against glibc though; trying to keep things Zig-only if I can.

1 Like