Execute a program from zig

I’m trying to build a disassembler and I want to call the assembler from the test cases to generate the binary to disassemble, but I can’t find how to actually call an external program.

I can find that there has been an std.os.exec but that seem to be moved or renamed as I can’t find it.

Functions from std.os moved to std.posix.

Probably what you want is not the unix specific exec that replaces the current process, but the spawn, spawnAndWait, or run function of std.process.Child

Test Example:
test.zig

const std = @import("std");

test {
    const argv = [_][]const u8{ "echo", "Hello", "World" };
    var child = std.process.Child.init(&argv, std.testing.allocator);
    try child.spawn();
    const exit_code = child.wait();
    try std.testing.expectEqual(exit_code, std.process.Child.Term{ .Exited = 0 });
}

Run as:

> zig test test.zig
Hello World
All 1 tests passed.
4 Likes

Yes this works. I got a bit confused as it also has a run but that takes a lot of the same arguments as the init, but this works well.

It seems like I can use collectOutput to get the stdout as well.

2 Likes