summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Zabka <zabkaste@hu-berlin.de>2020-05-22 00:24:51 +0200
committerStefan Zabka <zabkaste@hu-berlin.de>2020-05-22 00:24:51 +0200
commit40d578c09654ba6720ffd6e365e0d411cfa38613 (patch)
tree154cb0c40b37a3a9e985ce4fdd5ffd8f2ce123e1
parentf959165cdece3c1bfae8abce834a2bb78db96190 (diff)
downloadbetriebssysteme-40d578c09654ba6720ffd6e365e0d411cfa38613.tar.gz
betriebssysteme-40d578c09654ba6720ffd6e365e0d411cfa38613.zip
Somewhat working pipes
-rw-r--r--02_exercise/process.c26
-rw-r--r--02_exercise/process_test.c13
-rw-r--r--02_exercise/shell.c2
3 files changed, 26 insertions, 15 deletions
diff --git a/02_exercise/process.c b/02_exercise/process.c
index 9bc85be..00f8762 100644
--- a/02_exercise/process.c
+++ b/02_exercise/process.c
@@ -44,17 +44,15 @@ int parse_command(char const *line, char const *end, process *p) {
arrayPush(local_parts) = part;
}
- arrayInit(part);
- arrayPush(part) = '\0';
- arrayPush(local_parts) = part;
- p->argc = arrayLen(local_parts);
+ arrayPush(local_parts) = NULL;
+ p->argc = arrayLen(local_parts) - 1;
p->argv = local_parts;
return 0;
}
int parse_line(char const *const line, process **processes) {
- //Splits the line at | and then parses the commands
+ // Splits the line at | and then parses the commands
int ret_code = 0;
if (arrayInit(*processes) != 0) {
@@ -78,24 +76,24 @@ int parse_line(char const *const line, process **processes) {
cursor = end + 1;
}
size_t p_len = arrayLen(*processes);
- process * last = *processes + (p_len -1);
+ process *last = *processes + (p_len - 1);
// Linking up all processes as we currently only have pipes for multiple commands
- for (size_t i = 0; i < p_len -1 ; ++i) {
+ for (size_t i = 0; i < p_len - 1; ++i) {
int fds[2];
- if(pipe(fds) != 0) {
+ if (pipe(fds) != 0) {
perror("Failed to create pipe");
return -1;
}
- (*processes)[i].out_fd=fds[1];
- (*processes)[i+1].in_fd=fds[0];
-
+ (*processes)[i].out_fd = fds[1];
+ (*processes)[i + 1].in_fd = fds[0];
}
// Setting all processes to non blocking when
- if(strcmp(last->argv[last->argc-2], "&")== 0) {
+ if (strcmp(last->argv[last->argc - 1], "&") == 0) {
arrayPop(last->argv);
last->argc = last->argc - 1;
+ last->argv[last->argc] = NULL;
for (size_t i = 0; i < p_len; ++i) {
(*processes)[i].blocking = false;
@@ -132,8 +130,8 @@ int exec_command(process p, unsigned timeout) {
return WEXITSTATUS(status);
}
-int free_processes(process ** pr) {
- process * processes = * pr;
+int free_processes(process **pr) {
+ process *processes = *pr;
while (!arrayIsEmpty(processes)) {
process p = arrayPop(processes);
while (!arrayIsEmpty(p.argv)) {
diff --git a/02_exercise/process_test.c b/02_exercise/process_test.c
index 89eb2fb..dc3a291 100644
--- a/02_exercise/process_test.c
+++ b/02_exercise/process_test.c
@@ -43,7 +43,20 @@ void test_pipe() {
assert(processes[1].in_fd != 0);
free_processes(&processes);
}
+
+void test_ls(){
+ process * processes= NULL;
+ parse_line("ls", &processes);
+ assert(arrayLen(processes) == 1);
+ process p = processes[0];
+ assert(arrayLen(p.argv)==2);
+ assert(p.argc == 1);
+ assert(strcmp(p.argv[0], "ls") == 0);
+ assert(p.argc[p.argv] == NULL);
+ free_processes(&processes);
+}
int main() {
+ test_ls();
test_simple_case();
test_detatched();
test_pipe();
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index 9653189..eed8e64 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -57,7 +57,7 @@ int main(void) {
char const *current_wd = get_current_dir_name();
prompt = relative_path(original_wd, current_wd);
free((void *)current_wd);
- } else if (strcmp(processes[0].argv[1], "exit") == 0) {
+ } else if (strcmp(processes[0].argv[0], "exit") == 0) {
done = true;
} else {
int ret;