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:
@@ -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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user