Use select() in dd(1)
Avoid blocking in a call to splice() when no input is available. We can now break out of dd using ^C even if the input is coming from /dev/stdin. Use tabs instead of spaces.
This commit is contained in:
parent
c94dfdc99d
commit
0ca8e52bc1
14
dd.c
14
dd.c
|
@ -17,6 +17,8 @@
|
|||
#include <sys/ioctl.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/vfs.h>
|
||||
#include <time.h>
|
||||
|
@ -138,6 +140,8 @@ copy_splice(struct dd_config *ddc)
|
|||
int ifd, ofd, p[2] = {-1, -1};
|
||||
ssize_t r = 0;
|
||||
size_t n = 0;
|
||||
fd_set rfd, wfd;
|
||||
|
||||
if (prepare_copy(ddc, &ifd, &ofd) < 0)
|
||||
return -1;
|
||||
if (pipe(p) < 0) {
|
||||
|
@ -156,6 +160,15 @@ copy_splice(struct dd_config *ddc)
|
|||
for (;ddc->b_out != ddc->count && !sigint;) {
|
||||
if (n > ddc->count - ddc->b_out)
|
||||
n = ddc->count - ddc->b_out;
|
||||
FD_ZERO(&rfd);
|
||||
FD_ZERO(&wfd);
|
||||
FD_SET(ifd, &rfd);
|
||||
FD_SET(ofd, &wfd);
|
||||
if (select(ifd > ofd ? ifd + 1 : ofd + 1, &rfd, &wfd, NULL, NULL) < 0) {
|
||||
ddc->saved_errno = errno;
|
||||
break;
|
||||
}
|
||||
if (FD_ISSET(ifd, &rfd) == 1 && FD_ISSET(ofd, &wfd) == 1) {
|
||||
r = splice(ifd, NULL, p[1], NULL, n, SPLICE_F_MORE);
|
||||
if (r <= 0) {
|
||||
ddc->saved_errno = errno;
|
||||
|
@ -170,6 +183,7 @@ copy_splice(struct dd_config *ddc)
|
|||
ddc->b_out += r;
|
||||
++ddc->rec_out;
|
||||
}
|
||||
}
|
||||
close(ifd);
|
||||
close(ofd);
|
||||
close(p[0]);
|
||||
|
|
Loading…
Reference in New Issue
Block a user