diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-01-27 10:04:53 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-01-27 10:04:53 +0100 |
commit | c4c475b49d98c8ae330a57a2ece95f4e90ef9575 (patch) | |
tree | 72f6532b11a80e250695736aa00f0dec0651da2e | |
parent | 32755aeb605a1b46383af4e3cd1674c75ae0585d (diff) | |
download | serenity-c4c475b49d98c8ae330a57a2ece95f4e90ef9575.zip |
/bin/cp: Handle partial write() case.
-rw-r--r-- | Userland/cp.cpp | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/Userland/cp.cpp b/Userland/cp.cpp index db5c08b2d9..d8d9fdb419 100644 --- a/Userland/cp.cpp +++ b/Userland/cp.cpp @@ -29,12 +29,18 @@ int main(int argc, char** argv) } if (nread == 0) break; - ssize_t nwritten = write(dst_fd, buffer, nread); - if (nwritten < 0) { - perror("write dst"); - return 1; + ssize_t remaining_to_write = nread; + char* bufptr = buffer; + while (remaining_to_write) { + ssize_t nwritten = write(dst_fd, bufptr, remaining_to_write); + if (nwritten < 0) { + perror("write dst"); + return 1; + } + assert(nwritten > 0); + remaining_to_write -= nwritten; + bufptr += nwritten; } - assert(nwritten != 0); } close(src_fd); close(dst_fd); |