authorgravatar for shritesh@shritesh.comShritesh Bhattarai <shritesh@shritesh.com> 2019-05-03 13:04:27-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-03 17:35:42-04:00
logf4798297ded2d05f4e1919fb27a9cdb9981a5e11
tree31115b1bf380d3d73aa6b736fa2b25288b0e998a
parent2f3b46170340cfe20be4539c4d1eeea9a1dd9cb2

wasi: Implement read and write with err checking


1 files changed, 24 insertions(+), 5 deletions(-)

std/os/wasi.zig+24-5
......@@ -4,7 +4,6 @@ pub const STDIN_FILENO = 0;
44pub const STDOUT_FILENO = 1;
55pub const STDERR_FILENO = 2;
66
7// TODO: implement this like darwin does
87pub fn getErrno(r: usize) usize {
98 const signed_r = @bitCast(isize, r);
109 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 {
1312pub fn write(fd: i32, buf: [*]const u8, count: usize) usize {
1413 var nwritten: usize = undefined;
1514
16 const iovs = []ciovec_t{ciovec_t{
15 const ciovs = ciovec_t{
1716 .buf = buf,
1817 .buf_len = count,
19 }};
18 };
2019
21 _ = fd_write(@bitCast(fd_t, isize(fd)), &iovs[0], iovs.len, &nwritten);
22 return nwritten;
20 const err = fd_write(@bitCast(fd_t, isize(fd)), &ciovs, 1, &nwritten);
21 if (err == ESUCCESS) {
22 return nwritten;
23 } else {
24 return @bitCast(usize, -isize(err));
25 }
26}
27
28pub fn read(fd: i32, buf: [*]u8, nbyte: usize) usize {
29 var nread: usize = undefined;
30
31 const iovs = iovec_t{
32 .buf = buf,
33 .buf_len = nbyte,
34 };
35
36 const err = fd_read(@bitCast(fd_t, isize(fd)), &iovs, 1, &nread);
37 if (err == ESUCCESS) {
38 return nread;
39 } else {
40 return @bitCast(usize, -isize(err));
41 }
2342}