github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/imports/wasi_snapshot_preview1/testdata/zig/wasi.zig (about) 1 const std = @import("std"); 2 const os = std.os; 3 const fs = std.fs; 4 const allocator = std.heap.page_allocator; 5 const preopensAlloc = std.fs.wasi.preopensAlloc; 6 const stdout = std.io.getStdOut().writer(); 7 8 pub fn main() !void { 9 // Allocate arguments from the the operating system. 10 const args = try std.process.argsAlloc(allocator); 11 defer std.process.argsFree(allocator, args); 12 13 if (std.mem.eql(u8, args[1], "ls")) { 14 // TODO: This only looks at fd 3. See #14678 15 var dir = std.fs.cwd().openIterableDir(args[2], .{}) catch |err| switch (err) { 16 error.NotDir => { 17 try stdout.print("ENOTDIR\n", .{}); 18 return; 19 }, 20 else => { 21 try stdout.print("./{}\n", .{err}); 22 return; 23 }, 24 }; 25 26 try ls(dir); 27 if (args.len > 3 and std.mem.eql(u8, args[3], "repeat")) { 28 try ls(dir); 29 } 30 } else if (std.mem.eql(u8, args[1], "stat")) { 31 try stdout.print("stdin isatty: {}\n", .{os.isatty(0)}); 32 try stdout.print("stdout isatty: {}\n", .{os.isatty(1)}); 33 try stdout.print("stderr isatty: {}\n", .{os.isatty(2)}); 34 try stdout.print("/ isatty: {}\n", .{os.isatty(3)}); 35 } else if (std.mem.eql(u8, args[1], "preopen")) { 36 var wasi_preopens = try preopensAlloc(allocator); 37 // fs.wasi.Preopens does not have a free function 38 39 for (wasi_preopens.names, 0..) |preopen, i| { 40 try stdout.print("{}: {s}\n", .{ i, preopen }); 41 } 42 } 43 } 44 45 fn ls(dir: std.fs.IterableDir) !void { 46 var iter = dir.iterate(); 47 while (try iter.next()) |entry| { 48 try stdout.print("./{s}\n", .{entry.name}); 49 } 50 }