1

I've been test running zig recently, and have been able to pick up most of it by simply reading the standard library. However, I can't find anything similar to how we do perror/strerror in C. Is there a way to get a string representation for operating system errors (errno) in zig?

To be clear, I'm looking for a zig way to do this:

const char* path = "filethatdoesntexist.txt";
FILE* f = fopen(path,"rb");
if (!f) {
    perror(path);
}
Jason
  • 2,493
  • 2
  • 27
  • 27

1 Answers1

2

The closest thing is @errorName for Zig errors, but it only returns a simple string representation of the error. E.g.

std.debug.assert(std.mem.eql(u8, @errorName(error.FileNotFound), "FileNotFound"))

If you're linking libc and using an API that returns errno, then you have the option of using perror and strerror directly.

sigod
  • 3,514
  • 2
  • 21
  • 44
  • 1
    This works for me. I think using `@errorName` is ideal here. Using raw system calls (on Linux) can return `-errno`, but wouldn't set libc's global `errno` so this is probably most flexible. Thank you! – Jason May 04 '23 at 13:05