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`.
This commit is contained in:
Dan Gohman
2020-02-25 21:12:43 -08:00
committed by GitHub
parent 4b2c56e655
commit e41cae7db9

View File

@@ -30,7 +30,7 @@ any knowledge of WASI, WebAssembly, or sandboxing.
#include <errno.h> #include <errno.h>
int main(int argc, char **argv) { int main(int argc, char **argv) {
int n, m; ssize_t n, m;
char buf[BUFSIZ]; char buf[BUFSIZ];
if (argc != 3) { if (argc != 3) {
@@ -51,13 +51,15 @@ int main(int argc, char **argv) {
} }
while ((n = read(in, buf, BUFSIZ)) > 0) { while ((n = read(in, buf, BUFSIZ)) > 0) {
char *ptr = buf;
while (n > 0) { while (n > 0) {
m = write(out, buf, n); m = write(out, ptr, (size_t)n);
if (m < 0) { if (m < 0) {
fprintf(stderr, "write error: %s\n", strerror(errno)); fprintf(stderr, "write error: %s\n", strerror(errno));
exit(1); exit(1);
} }
n -= m; n -= m;
ptr += m;
} }
} }