From bfdc48e320143183927933c23f43ff87def798f3 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Thu, 24 Dec 2009 10:46:46 +0100 Subject: Simplification of the "exec" sample --- examples/exec.c | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'examples') diff --git a/examples/exec.c b/examples/exec.c index 077b94fd..eed75863 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; } -- cgit v1.2.3