From d5aa7933f42de1ef374feefe063dae724d059817 Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sat, 6 Jun 2020 15:24:59 +0200 Subject: broken shit --- 03_exercise/cli/client | Bin 23640 -> 27408 bytes 03_exercise/cli/client.c | 73 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) (limited to '03_exercise/cli') diff --git a/03_exercise/cli/client b/03_exercise/cli/client index c57cade..8da5d5a 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 83b4dec..d64dd62 100644 --- a/03_exercise/cli/client.c +++ b/03_exercise/cli/client.c @@ -33,6 +33,54 @@ static inline void die(const char *msg) { exit(-1); } +int write_all(int sock, char *buffer, int size) { + int nwrite, sent = 0; + + while (sent < size) { + if ((nwrite = write(sock, buffer + sent, size - sent)) < 0) { + perror("Write"); + exit(1); + } + + sent += nwrite; + } + + return sent; +} + +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"); + + fseek(file, 0L, SEEK_END); + long int sz = ftell(file); + fseek(file, 0L, SEEK_SET); + + int length = snprintf(NULL, 0, "%ld", sz); + char msg[length + 1 + 3]; + sprintf(msg, "< ", buf); + printf("%s\n", buf); memset(buf, 0, BUF_SIZE); while (1) { + printf("$> "); //printf("[C] awaiting command\n"); if ((length = getline(&line, &cap, stdin)) < 0) { fprintf(stderr, "Failed to read from STDIN"); @@ -70,6 +119,26 @@ int main() { continue; } + char cmd[4]; + char path[BUF_SIZE - 3]; + memcpy(cmd, line, 3); + cmd[3] = 0; + memcpy(path, line + 4, BUF_SIZE - 4); + path[strlen(path) - 1] = 0; + printf("cmd: \"%s\"\n", cmd); + printf("path: \"%s\"\n", path); + if (strcmp(cmd, "put") == 0) { + if (strlen(path) == 0) { + fprintf(stderr, "path missing\n"); + } else { + printf("got put with %s\n", path); + send_file(cfd, path); + } + free(line); + line = NULL; + continue; + } + strncpy(buf, line, strlen(line) + 1); //printf("[C] pre send: \"%s\"\n", buf); @@ -86,7 +155,7 @@ int main() { if (read(cfd, buf, BUF_SIZE) < 0) die("Could not receive message"); - printf("%s$> ", buf); + printf("%s", buf); memset(buf, 0, BUF_SIZE); free(line); line = NULL; -- cgit v1.2.3-54-g00ecf