diff options
-rw-r--r-- | examples/exec.c | 30 |
1 files changed, 9 insertions, 21 deletions
diff --git a/examples/exec.c b/examples/exec.c index 077b94f..eed7586 100644 --- a/examples/exec.c +++ b/examples/exec.c @@ -7,7 +7,7 @@ int main(void) { ssh_session session; ssh_channel channel; - ssh_buffer buf; + char buffer[256]; int rc; session = connect_ssh("localhost", NULL, 0); @@ -18,7 +18,6 @@ int main(void) { channel = channel_new(session);; if (channel == NULL) { ssh_disconnect(session); - ssh_finalize(); return 1; } @@ -26,7 +25,6 @@ int main(void) { if (rc < 0) { channel_close(channel); ssh_disconnect(session); - ssh_finalize(); return 1; } @@ -34,34 +32,24 @@ int main(void) { if (rc < 0) { channel_close(channel); ssh_disconnect(session); - ssh_finalize(); return 1; } - if (channel_is_open(channel)) { - while (channel_poll(channel, 0) >= 0) { - buf = buffer_new(); - rc = channel_read_buffer(channel, buf, 0, 0); - if (rc < 0) { - buffer_free(buf); - channel_close(channel); - ssh_disconnect(session); - ssh_finalize(); - return 1; - } - - printf("%s\n", (char *) buffer_get(buf)); - - buffer_free(buf); - } + while ((rc = channel_read(channel, buffer, sizeof(buffer), 0)) >= 0) { + fwrite(buffer, 1, rc, stdout); + } + + if (rc < 0) { + channel_close(channel); + ssh_disconnect(session); + return 1; } channel_send_eof(channel); channel_close(channel); ssh_disconnect(session); - ssh_finalize(); return 0; } |