summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Zabka <zabkaste@hu-berlin.de>2020-05-20 18:07:43 +0200
committerStefan Zabka <zabkaste@hu-berlin.de>2020-05-20 18:07:43 +0200
commit9f7bf8ddf255f06a3c27c13dad87e34a70356d58 (patch)
treea09f18cbf96bd8f983ad1b67de22d57561ce4cf1
parent08d6167988a0bbec72d84ff12f8857f0b4d0b42a (diff)
downloadbetriebssysteme-9f7bf8ddf255f06a3c27c13dad87e34a70356d58.tar.gz
betriebssysteme-9f7bf8ddf255f06a3c27c13dad87e34a70356d58.zip
First implementation of prompt
-rw-r--r--02_exercise/prompt_utils.c90
-rw-r--r--02_exercise/prompt_utils.h11
-rw-r--r--02_exercise/prompt_utils_test.c36
-rw-r--r--02_exercise/prompt_utils_test.h6
-rw-r--r--02_exercise/shell.c49
5 files changed, 154 insertions, 38 deletions
diff --git a/02_exercise/prompt_utils.c b/02_exercise/prompt_utils.c
new file mode 100644
index 0000000..819509d
--- /dev/null
+++ b/02_exercise/prompt_utils.c
@@ -0,0 +1,90 @@
+#include "prompt_utils.h"
+
+#include "array.h"
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+char const *const relative_path(char const *const from_dir, char const *const to_dir) {
+ // easiest cases first
+ {
+ int result = strcmp(from_dir, to_dir);
+ if (result == 0) {
+ char *return_value = malloc(2);
+ strcpy(return_value, ".");
+ return return_value;
+ }
+ }
+ {
+ if (strcmp("/", from_dir) == 0) {
+ char *return_value = malloc(strlen(to_dir) + 2);
+ return_value[0] = '.';
+ memcpy(return_value + 1, to_dir, strlen(to_dir) + 1);
+ return return_value;
+ }
+ }
+ // Splitting path into pieces so we can do strcmp on each of them
+ size_t const *const from_dir_indeces = get_seperator_indeces(from_dir, '/');
+ size_t const *const to_dir_indeces = get_seperator_indeces(to_dir, '/');
+
+ size_t from_dir_len = arrayLen(from_dir_indeces);
+ size_t to_dir_len = arrayLen(to_dir_indeces);
+
+ // finding the longest common substring
+ size_t array_len = from_dir_len < to_dir_len ? from_dir_len : to_dir_len;
+ size_t i = 0;
+ size_t common_position = 0;
+ for (; i < array_len - 1; i++) {
+ if (from_dir_indeces[i + 1] != to_dir_indeces[i + 1]) {
+ break;
+ }
+ size_t index = from_dir_indeces[i];
+ size_t count = from_dir_indeces[i + 1] - from_dir_indeces[i];
+ if (strncmp(from_dir + index, to_dir + index, count) != 0) {
+ break;
+ }
+ common_position = from_dir_indeces[i + 1];
+ }
+
+ size_t levels_up = from_dir_len - i - 1;
+ char *return_value;
+ if (levels_up == 0) {
+ // Equal dirs for whole length of path and not equal => subdir
+ size_t length = strlen(to_dir + common_position) + strlen("./") + 1;
+ return_value = malloc(length * sizeof(char));
+ strcpy(return_value, ".");
+ strcat(return_value, to_dir + common_position);
+ } else {
+ char const *const go_up = "/..";
+ size_t length = strlen(to_dir + common_position) + strlen("..") +
+ strlen(go_up) * (levels_up - 1);
+ return_value = malloc(length * sizeof(char));
+ strcpy(return_value, "..");
+ for (size_t j = 0; j < levels_up - 1; j++) {
+ strcat(return_value, go_up);
+ }
+ strcat(return_value, to_dir + common_position);
+ }
+
+ arrayRelease((void *)from_dir_indeces);
+ arrayRelease((void *)to_dir_indeces);
+
+ return return_value;
+}
+
+size_t *get_seperator_indeces(char const *const text, char seperator) {
+ size_t *indeces;
+ arrayInit(indeces);
+ char const *current = text;
+ if (strchr(current, seperator) == NULL) {
+ arrayRelease(indeces);
+ return NULL;
+ }
+ while ((current = strchr(current, seperator)) != NULL) {
+ arrayPush(indeces) = current - text;
+ ++current;
+ }
+ arrayPush(indeces) = strlen(text);
+ return indeces;
+}
diff --git a/02_exercise/prompt_utils.h b/02_exercise/prompt_utils.h
new file mode 100644
index 0000000..93de502
--- /dev/null
+++ b/02_exercise/prompt_utils.h
@@ -0,0 +1,11 @@
+#ifndef PROMPT_UTILS_H_INCLUDED
+#define PROMPT_UTILS_H_INCLUDED
+
+#include <stddef.h>
+
+// Returns the relative path to get from the `from_dir` to the `to_dir`
+char const *const relative_path(char const *const from_dir, char const *const to_dir);
+
+// Returns the position of each element one after the seperator
+size_t *get_seperator_indeces(char const *const text, char seperator);
+#endif // PROMPT_UTILS_H_INCLUDED \ No newline at end of file
diff --git a/02_exercise/prompt_utils_test.c b/02_exercise/prompt_utils_test.c
new file mode 100644
index 0000000..b26ce6f
--- /dev/null
+++ b/02_exercise/prompt_utils_test.c
@@ -0,0 +1,36 @@
+#include "array.h"
+#include "prompt_utils.h"
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+void test_get_seperator_indeces() {
+ char const *const test_str = "/This/is/a/test";
+ size_t const *const result = get_seperator_indeces(test_str, '/');
+ for (size_t i = 0; i < arrayLen(result); i++) {
+ printf("%ld : %s \n", result[i], test_str + result[i]);
+ }
+}
+void test_paths(char const *const from, char const *const to,
+ char const *const expected) {
+ size_t const *const result = get_seperator_indeces(from, '/');
+ for (size_t i = 0; i < arrayLen(result); i++) {
+ printf("%ld : %s \n", result[i], from + result[i]);
+ }
+ size_t const *const result2 = get_seperator_indeces(to, '/');
+ for (size_t i = 0; i < arrayLen(result2); i++) {
+ printf("%ld : %s \n", result2[i], to + result2[i]);
+ }
+ char const *const path = relative_path(from, to);
+ printf("The relative path of %s to %s is %s \n", from, to, path);
+
+ assert(strcmp(path, expected) == 0);
+}
+void test_relative_path() {
+ test_paths("/Test", "/Test/a", "./a");
+ test_paths("/abc/def/ghi", "/abc/def/ghi", ".");
+ test_paths("/Test", "/Test/a/asd", "./a/asd");
+ test_paths("/Test/b/c", "/Test/b", "..");
+ test_paths("/", "/Test/a/asd", "./Test/a/asd");
+ test_paths("/Test/c", "/Test/b", "../b");
+} \ No newline at end of file
diff --git a/02_exercise/prompt_utils_test.h b/02_exercise/prompt_utils_test.h
new file mode 100644
index 0000000..5c637b2
--- /dev/null
+++ b/02_exercise/prompt_utils_test.h
@@ -0,0 +1,6 @@
+#ifndef PROMPT_UTILS_H_INCLUDED
+#define PROMPT_UTILS_H_INCLUDED
+void test_get_seperator_indeces();
+void test_relative_path();
+
+#endif \ No newline at end of file
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index 2594d6b..c0c51e1 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -2,16 +2,17 @@
#include <stdlib.h>
#include <string.h>
+#include <assert.h>
#include <fcntl.h>
#include <signal.h>
+#include <stdbool.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "array.h"
-#include "errno.h"
-#include "stdbool.h"
+#include "prompt_utils.h"
void print_prompt(char const *const original_wd, char const *current_wd);
@@ -19,16 +20,15 @@ void print_prompt(char const *const original_wd, char const *current_wd);
int exec_command(char *command);
int main(void) {
- // the directory is specified without a / at the end
char const *const original_wd = get_current_dir_name();
- char const *current_wd = get_current_dir_name();
-
+ char const *prompt = relative_path(original_wd, original_wd);
+ printf("%s \n", original_wd);
while (true) {
char *command = NULL;
size_t cap = 0;
size_t length = 0;
- print_prompt(original_wd, current_wd);
+ printf("%s > ", prompt);
if ((length = getline(&command, &cap, stdin)) < 0) {
fprintf(stderr, "Failed to read from STDIN");
exit(-1);
@@ -36,8 +36,10 @@ int main(void) {
if (strstr(command, "cd") != NULL) {
printf("Changing dirs \n");
chdir("beispiele");
- free((void *) current_wd);
- current_wd = get_current_dir_name();
+ free((void *)prompt);
+ char const *current_wd = get_current_dir_name();
+ prompt = relative_path(original_wd, current_wd);
+ free((void *)current_wd);
} else if (strcmp(command, "exit\n") == 0) {
exit(0);
} else {
@@ -47,36 +49,7 @@ int main(void) {
}
// Gotta cast to void otherwise we discard qualifiers
free((void *)original_wd);
- free((void *)current_wd);
-}
-
-void print_prompt(char const *const original_wd, char const *const current_wd) {
- // easiest case first
- {
- int result = strcmp(original_wd, current_wd);
- if (result == 0) {
- printf(".> ");
- return;
- }
- }
- printf("%s > ", current_wd);
- // Now checking for substrings aka are we in a subdir
- {
- size_t dirlen = strlen(original_wd);
- char *const orig_dir_with_slash = malloc(dirlen + 2);
- if (orig_dir_with_slash == NULL) {
- fprintf(stderr, "Failed to allocate for prompt");
- exit(ENOMEM);
- }
- memcpy(orig_dir_with_slash, original_wd, dirlen);
- orig_dir_with_slash[dirlen] = '/';
- orig_dir_with_slash[dirlen + 1] = '\0';
- char const *const result = strstr(current_wd, original_wd);
- if (result != NULL) {
- printf(".%s > ", current_wd + dirlen);
- return;
- }
- }
+ free((void *)prompt);
}
int exec_command(char *command) { return 0; }