Subject: | Net::Cmd assumes that read/write work on sockets w/patch |
On *nix systems read and write on sockets work the same as recv and
send. However this is not true of all operating systems e.g. BeOS.
This patch fixes this and makes Net::Cmd more portable:
*** OCmd.pm Wed Jan 20 17:39:58 2010
--- Cmd.pm Wed Jan 20 17:42:01 2010
***************
*** 230,236 ****
my $swlen;
$cmd->close
! unless (defined($swlen = syswrite($cmd, $str, $len)) && $swlen
== $len);
$cmd->debug_print(1, $str)
if ($cmd->debug);
--- 230,238 ----
my $swlen;
$cmd->close
! unless (defined($swlen = send($cmd, $str, 0)) && $swlen == $len);
!
! print ">>>>>>>$swlen, $len\n";
$cmd->debug_print(1, $str)
if ($cmd->debug);
***************
*** 285,291 ****
my $select_ret = select($rout = $rin, undef, undef, $timeout);
if ($select_ret > 0) {
! unless (sysread($cmd, $buf = "", 1024)) {
carp(ref($cmd) . ": Unexpected EOF on command channel")
if $cmd->debug;
$cmd->close;
--- 287,295 ----
my $select_ret = select($rout = $rin, undef, undef, $timeout);
if ($select_ret > 0) {
! #NJH
! # unless (sysread($cmd, $buf = "", 1024)) {
! unless (recv($cmd, $buf = "", 1024, 0)) {
carp(ref($cmd) . ": Unexpected EOF on command channel")
if $cmd->debug;
$cmd->close;
***************
*** 441,464 ****
local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
! while ($len) {
! my $wout;
! my $s = select(undef, $wout = $win, undef, $timeout);
! if ((defined $s and $s > 0) or -f $cmd) # -f for testing on win32
! {
! my $w = syswrite($cmd, $line, $len, $offset);
! unless (defined($w)) {
! carp("$cmd: $!") if $cmd->debug;
! return undef;
! }
! $len -= $w;
! $offset += $w;
! }
! else {
! carp("$cmd: Timeout") if ($cmd->debug);
! return undef;
! }
! }
1;
}
--- 445,475 ----
local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
! if($^O eq 'beos') {
! unless (send($cmd, $line, 0)) {
! carp("$cmd: $!") if $cmd->debug;
! return undef;
! }
! } else {
! while ($len) {
! my $wout;
! my $s = select(undef, $wout = $win, undef, $timeout);
! if ((defined $s and $s > 0) or -f $cmd) # -f for testing on win32
! {
! my $w = syswrite($cmd, $line, $len, $offset);
! unless (defined($w)) {
! carp("$cmd: $!") if $cmd->debug;
! return undef;
! }
! $len -= $w;
! $offset += $w;
! }
! else {
! carp("$cmd: Timeout") if ($cmd->debug);
! return undef;
! }
! }
! }
1;
}
***************
*** 486,507 ****
my $timeout = $cmd->timeout || undef;
local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
! while ($len) {
! my $wout;
! if (select(undef, $wout = $win, undef, $timeout) > 0) {
! my $w = syswrite($cmd, $line, $len, $offset);
! unless (defined($w)) {
carp("$cmd: $!") if $cmd->debug;
return undef;
}
! $len -= $w;
! $offset += $w;
! }
! else {
! carp("$cmd: Timeout") if ($cmd->debug);
! return undef;
! }
! }
1;
}
--- 497,525 ----
my $timeout = $cmd->timeout || undef;
local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
! if ($^O eq 'beos') {
! unless(send($cmd, $line, 0)) {
carp("$cmd: $!") if $cmd->debug;
return undef;
}
! } else {
! while ($len) {
! my $wout;
! if (select(undef, $wout = $win, undef, $timeout) > 0) {
! my $w = syswrite($cmd, $line, $len, $offset);
! unless (defined($w)) {
! carp("$cmd: $!") if $cmd->debug;
! return undef;
! }
! $len -= $w;
! $offset += $w;
! }
! else {
! carp("$cmd: Timeout") if ($cmd->debug);
! return undef;
! }
! }
! }
1;
}
***************
*** 529,535 ****
$cmd->debug_print(1, ".\n")
if ($cmd->debug);
! syswrite($cmd, $tosend, length $tosend);
delete ${*$cmd}{'net_cmd_last_ch'};
--- 547,553 ----
$cmd->debug_print(1, ".\n")
if ($cmd->debug);
! send($cmd, $tosend, 0);
delete ${*$cmd}{'net_cmd_last_ch'};