summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNiklas Halle <niklas@niklashalle.net>2020-06-06 15:24:59 +0200
committerNiklas Halle <niklas@niklashalle.net>2020-06-06 15:24:59 +0200
commitd5aa7933f42de1ef374feefe063dae724d059817 (patch)
treee8c7447d8d42d007066cb736294f3bb86d04a667
parente1520589de17d08c4bde37f1198f18d012a1f377 (diff)
downloadbetriebssysteme-d5aa7933f42de1ef374feefe063dae724d059817.tar.gz
betriebssysteme-d5aa7933f42de1ef374feefe063dae724d059817.zip
broken shit
-rw-r--r--03_exercise/.srv_pid2
-rwxr-xr-x03_exercise/cli/clientbin23640 -> 27408 bytes
-rw-r--r--03_exercise/cli/client.c73
-rw-r--r--03_exercise/srv/process.c42
-rwxr-xr-x03_exercise/srv/serverbin58856 -> 60872 bytes
-rw-r--r--03_exercise/srv/shell.c53
6 files changed, 141 insertions, 29 deletions
diff --git a/03_exercise/.srv_pid b/03_exercise/.srv_pid
index ed33336..f376adb 100644
--- a/03_exercise/.srv_pid
+++ b/03_exercise/.srv_pid
@@ -1 +1 @@
-618929
+892256
diff --git a/03_exercise/cli/client b/03_exercise/cli/client
index c57cade..8da5d5a 100755
--- a/03_exercise/cli/client
+++ b/03_exercise/cli/client
Binary files 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, "<<!%ld", sz);
+
+ write(server_fd, msg, strlen(msg));
+
+ int size;
+ while (read(file_fd, buffer, sizeof(buffer)) != 0) {
+ size = strlen(buffer) + 1;
+ if (write(server_fd, &size, sizeof(size)) < 0) {
+ perror("Write");
+ exit(1);
+ }
+
+ printf("Sent %i bytes\n", write_all(server_fd, buffer, strlen(buffer) + 1));
+ }
+
+ fclose(file);
+ close(file_fd);
+}
+
int main() {
struct sockaddr_in addr = {
.sin_family = AF_INET,
@@ -54,10 +102,11 @@ int main() {
if (read(cfd, buf, BUF_SIZE) < 0)
die("Could not receive message");
- printf("%s\n$> ", 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;
diff --git a/03_exercise/srv/process.c b/03_exercise/srv/process.c
index c5be8c3..fe66a66 100644
--- a/03_exercise/srv/process.c
+++ b/03_exercise/srv/process.c
@@ -20,25 +20,25 @@ int parse_command(char const *line, char const *end, process *p) {
for (size_t i = 0; line + i < end; ++i) {
char c;
switch (c = line[i]) {
- default:
- arrayPush(part) = c;
- break;
- case ' ':
- case '\t':
- case '\0':
- if (arrayLen(part) == 0)
- continue;
-
- arrayPush(part) = '\0';
- arrayPush(local_parts) = part;
- arrayInit(part);
+ default:
+ arrayPush(part) = c;
+ break;
+ case ' ':
+ case '\t':
+ case '\0':
+ if (arrayLen(part) == 0)
+ continue;
+
+ arrayPush(part) = '\0';
+ arrayPush(local_parts) = part;
+ arrayInit(part);
}
}
if (arrayLen(part) == 0) {
arrayRelease(part);
} else {
- arrayPush(part) = '\0';
+ arrayPush(part) = '\0';
arrayPush(local_parts) = part;
}
@@ -51,22 +51,22 @@ int parse_command(char const *line, char const *end, process *p) {
int parse_line(char const *const line, process **processes) {
// splits the line at | and then parses the commands
- int ret_code;
+ int ret_code;
if (arrayInit(*processes) != 0) {
perror("Failed to initialize processes array");
return -1;
}
- bool done = false;
+ bool done = false;
char const *cursor = line;
while (!done) {
- process p = {.argv = NULL, .argc = 0, .in_fd = 0, .out_fd = 0, .pid = 0, .blocking = true};
- char const *end = strchr(cursor, '|');
+ process p = {.argv = NULL, .argc = 0, .in_fd = 0, .out_fd = 0, .pid = 0, .blocking = true};
+ char const *end = strchr(cursor, '|');
if (end == NULL) {
done = true;
- end = line + strlen(line);
+ end = line + strlen(line);
}
if ((ret_code = parse_command(cursor, end, &p)) != 0) {
@@ -87,7 +87,7 @@ int parse_line(char const *const line, process **processes) {
perror("Failed to create pipe");
return -1;
}
- (*processes)[i].out_fd = fds[1];
+ (*processes)[i].out_fd = fds[1];
(*processes)[i + 1].in_fd = fds[0];
}
@@ -113,7 +113,7 @@ int exec_command(process p) {
dup2(p.in_fd, 0);
close(p.in_fd);
}
- if(p.in_fd == 0 && !p.blocking) {
+ if (p.in_fd == 0 && !p.blocking) {
close(0);
}
@@ -156,7 +156,7 @@ void free_processes(process **pr) {
while (!arrayIsEmpty(processes)) {
process p = arrayPop(processes);
while (!arrayIsEmpty(p.argv)) {
- char *tmp = arrayTop(p.argv);
+ char *tmp = arrayTop(p.argv);
if (tmp) {
arrayRelease(tmp);
}
diff --git a/03_exercise/srv/server b/03_exercise/srv/server
index efe2660..69edeea 100755
--- a/03_exercise/srv/server
+++ b/03_exercise/srv/server
Binary files differ
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);
}