Debian Codegraph @

imports/wasi_snapshot_preview1/testdata/zig/wasi.zig

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