+ my $len = length($line);
+ my $offset = 0;
+ my $win = "";
+ vec($win, fileno($cmd), 1) = 1;
+ my $timeout = $cmd->timeout || undef;
+ my $initial = time;
+ my $pending = $timeout;
+
+ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
+
+ while ($len) {
+ my $wout;
+ my $nfound = select(undef, $wout = $win, undef, $pending);
+ if ((defined $nfound and $nfound > 0) or -f $cmd) # -f for testing on win32
+ {
+ my $w = syswrite($cmd, $line, $len, $offset);
+ if (! defined($w) ) {
+ my $err = $!;
+ $cmd->close;
+ $cmd->_set_status_closed($err);
+ return;
+ }
+ $len -= $w;
+ $offset += $w;
+ }
+ elsif ($nfound == -1) {
+ if ( $! == EINTR ) {
+ if ( defined($timeout) ) {
+ redo if ($pending = $timeout - ( time - $initial ) ) > 0;
+ $cmd->_set_status_timeout;
+ return;
+ }
+ redo;
+ }
+ my $err = $!;
+ $cmd->close;
+ $cmd->_set_status_closed($err);
+ return;
+ }
+ else {
+ $cmd->_set_status_timeout;
+ return;
+ }
+ }
+
+ return 1;
+}