diff options
Diffstat (limited to '03_exercise/srv/shell.c')
-rw-r--r-- | 03_exercise/srv/shell.c | 53 |
1 files changed, 48 insertions, 5 deletions
diff --git a/03_exercise/srv/shell.c b/03_exercise/srv/shell.c index 74586c7..792a26c 100644 --- a/03_exercise/srv/shell.c +++ b/03_exercise/srv/shell.c @@ -2,6 +2,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <fcntl.h> #include <stdbool.h> #include <wait.h> #include <errno.h> @@ -15,7 +16,49 @@ process *processes; -const char * const get_current_dir_name(); +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 receive_file(int sock, char *path, int size) { + char buffer[512]; + int file_fd; + int nread, received; + + if ((file_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { perror("Open"); } + + while (true) { + received = 0; + + /* Read all data */ + while (received < size) { + if ((nread = read(sock, buffer + received, size - received)) < 0) { + perror("Read"); + //pthread_exit(NULL); + exit(1); + } + + received += nread; + } + + if (strncmp(buffer, "end", 4) == 0) { break; } + + write_all(file_fd, buffer, strlen(buffer) + 1); + } +} + +const char *const get_current_dir_name(); void signal_handler(int signal) { if (signal == SIGINT) { @@ -54,7 +97,7 @@ int shell(int in_fd) { bool done = false; while (!done) { - char line[BUF_SIZE]; + char line[BUF_SIZE]; __ssize_t length; if ((length = read(in_fd, line, BUF_SIZE)) < 0) { @@ -107,9 +150,9 @@ int shell(int in_fd) { signal(SIGINT, signal_handler); } for (size_t i = 0; i < arrayLen(processes); ++i) { - int ret = exec_command(processes[i]); - if (ret) - printf("[%i] ", ret); + /*int ret = */exec_command(processes[i]); + /*if (ret) + printf("[%i] ", ret);*/ } signal(SIGINT, SIG_IGN); } |