diff options
| author | Shritesh Bhattarai <shritesh@shritesh.com> | 2019-05-03 13:04:27 -0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2019-05-03 17:35:42 -0400 |
| commit | f4798297ded2d05f4e1919fb27a9cdb9981a5e11 (patch) | |
| tree | 31115b1bf380d3d73aa6b736fa2b25288b0e998a | |
| parent | 2f3b46170340cfe20be4539c4d1eeea9a1dd9cb2 (diff) | |
| download | zig-f4798297ded2d05f4e1919fb27a9cdb9981a5e11.tar.gz zig-f4798297ded2d05f4e1919fb27a9cdb9981a5e11.zip | |
wasi: Implement read and write with err checking
| -rw-r--r-- | std/os/wasi.zig | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/std/os/wasi.zig b/std/os/wasi.zig index e598cd6257..2118db9a2a 100644 --- a/std/os/wasi.zig +++ b/std/os/wasi.zig @@ -4,7 +4,6 @@ pub const STDIN_FILENO = 0; pub const STDOUT_FILENO = 1; pub const STDERR_FILENO = 2; -// TODO: implement this like darwin does pub fn getErrno(r: usize) usize { const signed_r = @bitCast(isize, r); return if (signed_r > -4096 and signed_r < 0) @intCast(usize, -signed_r) else 0; @@ -13,11 +12,31 @@ pub fn getErrno(r: usize) usize { pub fn write(fd: i32, buf: [*]const u8, count: usize) usize { var nwritten: usize = undefined; - const iovs = []ciovec_t{ciovec_t{ + const ciovs = ciovec_t{ .buf = buf, .buf_len = count, - }}; + }; - _ = fd_write(@bitCast(fd_t, isize(fd)), &iovs[0], iovs.len, &nwritten); - return nwritten; + const err = fd_write(@bitCast(fd_t, isize(fd)), &ciovs, 1, &nwritten); + if (err == ESUCCESS) { + return nwritten; + } else { + return @bitCast(usize, -isize(err)); + } +} + +pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize { + var nread: usize = undefined; + + const iovs = iovec_t{ + .buf = buf, + .buf_len = nbyte, + }; + + const err = fd_read(@bitCast(fd_t, isize(fd)), &iovs, 1, &nread); + if (err == ESUCCESS) { + return nread; + } else { + return @bitCast(usize, -isize(err)); + } } |
