Commit f4798297de

Shritesh Bhattarai <shritesh@shritesh.com>
2019-05-03 20:04:27
wasi: Implement read and write with err checking
1 parent 2f3b461
Changed files (1)
std
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));
+    }
 }