summaryrefslogtreecommitdiffstats
path: root/02_exercise/shell.c
diff options
context:
space:
mode:
Diffstat (limited to '02_exercise/shell.c')
-rw-r--r--02_exercise/shell.c49
1 files changed, 11 insertions, 38 deletions
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; }