From e41cae7db9a405e4facb7d9ad1f8d90a72cb71aa Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Tue, 25 Feb 2020 21:12:43 -0800 Subject: [PATCH] Fix the WASI-tutorial to handle short writes properly. (#991) 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`. --- docs/WASI-tutorial.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/WASI-tutorial.md b/docs/WASI-tutorial.md index 874a88928e..2b6249b242 100644 --- a/docs/WASI-tutorial.md +++ b/docs/WASI-tutorial.md @@ -30,7 +30,7 @@ any knowledge of WASI, WebAssembly, or sandboxing. #include int main(int argc, char **argv) { - int n, m; + ssize_t n, m; char buf[BUFSIZ]; if (argc != 3) { @@ -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; } }