From 9629104f494a441681fd28295f98531682875fc7 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sun, 7 Jun 2020 12:51:27 +0200 Subject: put works --- 03_exercise/cli/client | Bin 28384 -> 28360 bytes 03_exercise/cli/client.c | 41 ++++++++++++++++++++++------------------- 2 files changed, 22 insertions(+), 19 deletions(-) (limited to '03_exercise/cli') diff --git a/03_exercise/cli/client b/03_exercise/cli/client index 0fcf5c1..34d1d01 100755 Binary files a/03_exercise/cli/client and b/03_exercise/cli/client differ diff --git a/03_exercise/cli/client.c b/03_exercise/cli/client.c index c1bc5d1..052dabd 100644 --- a/03_exercise/cli/client.c +++ b/03_exercise/cli/client.c @@ -50,36 +50,38 @@ int write_all(int sock, char *buffer, int size) { } void send_file(int server_fd, char *path) { - char buffer[512]; - int file_fd; - - if ((file_fd = open(path, O_RDONLY)) == -1) { perror("Open"); } - - FILE *file = fdopen(file_fd, "r"); + printf("Trying to send \"%s\" to remote...\n", path); + char buffer[BUF_SIZE]; + memset(buffer, 0, BUF_SIZE); + int tmp_fd, file_fd; + if ((tmp_fd = open(path, O_RDONLY)) == -1) { perror("Open"); } + FILE *file = fdopen(tmp_fd, "r"); fseek(file, 0L, SEEK_END); long int sz = ftell(file); fseek(file, 0L, SEEK_SET); + fclose(file); + close(tmp_fd); + if ((file_fd = open(path, O_RDONLY)) == -1) { perror("Open"); } int length = snprintf(NULL, 0, "%ld", sz); - char msg[length + 1 + 3]; - sprintf(msg, "< 0) { printf("Sent %i bytes\n", write_all(server_fd, buffer, strlen(buffer) + 1)); } - fclose(file); + if (errno) + perror("wad"); + close(file_fd); + + printf("done.\n"); } #define BREAK 1 @@ -143,7 +145,7 @@ int main() { pid_t pid = fork(); int done = 0; - if (pid == 0) { + if (pid != 0) { read_stdin(&done); } else { read_server_sock(&done); @@ -175,6 +177,7 @@ void read_stdin(int *done) { ssize_t length; while (!*done) { + memset(stdinp_buffer, 0, BUF_SIZE); if ((length = read(STDIN_FILENO, stdinp_buffer, BUF_SIZE)) > 0) { switch (parse(sock, stdinp_buffer, length)) { case CONTINUE: -- cgit v1.2.3-54-g00ecf