/bin/cp: Handle partial write() case.

This commit is contained in:
Andreas Kling 2019-01-27 10:04:53 +01:00
parent 32755aeb60
commit c4c475b49d
Notes: sideshowbarker 2024-07-19 15:55:59 +09:00

View File

@ -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);