Skip to content

Commit

Permalink
Fix the WASI-tutorial to handle short writes properly. (#991)
Browse files Browse the repository at this point in the history
If write doesn't write the full buffer, start the next write at the
point where the write left off.

Also, usize `ssize_t` for the return types of `read` and `write`.
  • Loading branch information
sunfishcode authored Feb 26, 2020
1 parent 4b2c56e commit e41cae7
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions docs/WASI-tutorial.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ any knowledge of WASI, WebAssembly, or sandboxing.
#include <errno.h>

int main(int argc, char **argv) {
int n, m;
ssize_t n, m;
char buf[BUFSIZ];

if (argc != 3) {
Expand All @@ -51,13 +51,15 @@ int main(int argc, char **argv) {
}

while ((n = read(in, buf, BUFSIZ)) > 0) {
char *ptr = buf;
while (n > 0) {
m = write(out, buf, n);
m = write(out, ptr, (size_t)n);
if (m < 0) {
fprintf(stderr, "write error: %s\n", strerror(errno));
exit(1);
}
n -= m;
ptr += m;
}
}

Expand Down

0 comments on commit e41cae7

Please sign in to comment.