From e61c71b269ce28c988ecdcf2ab4ac76bcbe5afef Mon Sep 17 00:00:00 2001 From: Alexander Avery Date: Tue, 19 Apr 2022 19:31:18 -0400 Subject: [PATCH] attempting writing to buffer --- popcorn.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/popcorn.c b/popcorn.c index 83605ef..42f2d93 100644 --- a/popcorn.c +++ b/popcorn.c @@ -2,6 +2,7 @@ #include #include #include +#include int verify_host(ssh_session session) { enum ssh_known_hosts_e state; @@ -19,11 +20,13 @@ int verify_host(ssh_session session) { return 0; } -int get_movies(ssh_session session) { +int get_movies(ssh_session session, char** buffer, size_t size) { ssh_channel channel; int rc; char read_buffer[256]; int nbytes; + size_t len = 0; + char* temp; channel = ssh_channel_new(session); if (channel == NULL) @@ -36,7 +39,7 @@ int get_movies(ssh_session session) { return rc; } - rc = ssh_channel_request_exec(channel, "ls -l /"); + rc = ssh_channel_request_exec(channel, "find /var/www/public/media -type f -name \"*mp4\""); if (rc != SSH_OK) { fprintf(stderr, "Failed to request exec"); ssh_channel_close(channel); @@ -46,12 +49,21 @@ int get_movies(ssh_session session) { nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0); while (nbytes > 0) { - if (fwrite(read_buffer, 1, nbytes, stdout) != nbytes) { - fprintf(stderr, "Failed to write to stdout"); - ssh_channel_close(channel); - ssh_channel_free(channel); - return SSH_ERROR; - } + fprintf(stderr, "size: %li\tlen: %li\tnbytes: %d\n", size, len, nbytes); + if ((len+=nbytes) > size) { + fprintf(stderr, "new len: %li\n", len); + char* temp = realloc(*buffer, len); + size = len; + if (!temp) { + fprintf(stderr, "Failed to reallocate char buffer"); + ssh_channel_close(channel); + ssh_channel_free(channel); + return -1; + } + *buffer = temp; + } + + *buffer = strcat(*buffer, read_buffer); nbytes = ssh_channel_read(channel, read_buffer, sizeof(read_buffer), 0); @@ -114,11 +126,15 @@ int main(int argc, char *argv[]) { exit(-1); } - if (get_movies(session) != SSH_OK) { + int size = 16; + char* b = malloc(size); + memset(b, 0, size); + if (get_movies(session, &b, size) != SSH_OK) { fprintf(stderr, "Unable to list movies: %s\n", ssh_get_error(session)); exit(-1); } + fprintf(stdout, "%s", b); ssh_disconnect(session); ssh_free(session);