diff --git a/example/scp_write.c b/example/scp_write.c index b3fe330b..667663ad 100644 --- a/example/scp_write.c +++ b/example/scp_write.c @@ -47,7 +47,7 @@ int main(int argc, char *argv[]) FILE *local; int rc; char mem[1024]; - size_t nread, sent; + size_t nread; char *ptr; struct stat fileinfo; @@ -168,20 +168,21 @@ int main(int argc, char *argv[]) break; } ptr = mem; - sent = 0; do { /* write the same data over and over, until error or completion */ rc = libssh2_channel_write(channel, ptr, nread); if (rc < 0) { fprintf(stderr, "ERROR %d\n", rc); - } else { - /* rc indicates how many bytes were written this time */ - sent += rc; + break; } - } while (rc > 0 && sent < nread); + else { + /* rc indicates how many bytes were written this time */ + ptr += rc; + nread -= rc; + } + } while (nread); - nread -= sent; } while (1); fprintf(stderr, "Sending EOF\n"); diff --git a/example/scp_write_nonblock.c b/example/scp_write_nonblock.c index df243841..3b7bda87 100644 --- a/example/scp_write_nonblock.c +++ b/example/scp_write_nonblock.c @@ -51,7 +51,7 @@ int main(int argc, char *argv[]) long flag = 1; #endif char mem[1024]; - size_t nread, sent; + size_t nread; char *ptr; struct stat fileinfo; @@ -182,7 +182,6 @@ int main(int argc, char *argv[]) break; } ptr = mem; - sent = 0; do { /* write the same data over and over, until error or completion */ @@ -191,12 +190,14 @@ int main(int argc, char *argv[]) continue; } else if (rc < 0) { fprintf(stderr, "ERROR %d\n", rc); + break; } else { /* rc indicates how many bytes were written this time */ - sent += rc; + nread -= rc; + ptr += rc; } - } while (rc > 0 && sent < nread); - } while (1); + } while (nread); + } while (!nread); /* only continue if nread was drained */ fprintf(stderr, "Sending EOF\n"); while (libssh2_channel_send_eof(channel) == LIBSSH2_ERROR_EAGAIN); diff --git a/example/sftp_RW_nonblock.c b/example/sftp_RW_nonblock.c index 01d3f93d..f0055704 100644 --- a/example/sftp_RW_nonblock.c +++ b/example/sftp_RW_nonblock.c @@ -246,7 +246,7 @@ int main(int argc, char *argv[]) nread); ptr += rc; nread -= nread; - } while (rc > 0); + } while (rc >= 0); if(rc != LIBSSH2_ERROR_EAGAIN) { /* error or end of file */ diff --git a/example/sftp_write.c b/example/sftp_write.c index ba40226d..7c1de34e 100644 --- a/example/sftp_write.c +++ b/example/sftp_write.c @@ -185,10 +185,13 @@ int main(int argc, char *argv[]) do { /* write data in a loop until we block */ rc = libssh2_sftp_write(sftp_handle, ptr, nread); + if(rc < 0) + break; ptr += rc; - nread -= nread; - } while (rc > 0); - } while (1); + nread -= rc; + } while (nread); + + } while (rc > 0); libssh2_sftp_close(sftp_handle); libssh2_sftp_shutdown(sftp_session); diff --git a/example/sftp_write_nonblock.c b/example/sftp_write_nonblock.c index a249852d..69fca144 100644 --- a/example/sftp_write_nonblock.c +++ b/example/sftp_write_nonblock.c @@ -199,9 +199,9 @@ int main(int argc, char *argv[]) ; } ptr += rc; - nread -= nread; - } while (rc > 0); - } while (1); + nread -= rc; + } while (nread); + } while (rc > 0); fclose(local); libssh2_sftp_close(sftp_handle);