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.c36
1 files changed, 25 insertions, 11 deletions
diff --git a/02_exercise/shell.c b/02_exercise/shell.c
index 1c03961..ed53897 100644
--- a/02_exercise/shell.c
+++ b/02_exercise/shell.c
@@ -1,28 +1,31 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-
-#include <sys/wait.h>
#include <unistd.h>
-
-#include "array.h"
#include <stdbool.h>
+#include <sys/wait.h>
-void print_prompt();
+#include "array.h"
+#include "prompt_utils.h"
int parse_line(char const *line, char ***parts, size_t *part_count);
// returns the return code of the executed program
int exec_command(const char *path, char *const argv[], unsigned timeout);
-int main(int argc, char* argv[]) {
+int main(void) {
+ chdir(".");
+ setvbuf(stdout, NULL, _IONBF, 0);
+
+ char const *const original_wd = get_current_dir_name();
+ char const *prompt = relative_path(original_wd, original_wd);
bool done = false;
while (!done) {
char *line = NULL;
size_t cap = 0;
- size_t length = 0;
+ __ssize_t length = 0;
- print_prompt();
+ printf("%s > ", prompt);
if ((length = getline(&line, &cap, stdin)) < 0) {
fprintf(stderr, "Failed to read from STDIN");
exit(-1);
@@ -39,9 +42,15 @@ int main(int argc, char* argv[]) {
fprintf(stderr, "usage: cd <path>");
goto clean;
}
+
int ret = chdir(arguments[1]);
if (ret)
printf("[%i] ", ret);
+
+ 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(arguments[0], "exit") == 0) {
done = true;
} else {
@@ -59,6 +68,9 @@ int main(int argc, char* argv[]) {
}
arrayRelease(arguments);
}
+
+ free((void *)original_wd);
+ free((void *)prompt);
}
int parse_line(char const *line, char ***parts, size_t *part_count) {
@@ -106,6 +118,7 @@ void print_prompt() {
}
int exec_command(const char *path, char *const argv[], unsigned timeout) {
+ timeout = timeout ^ timeout;
int pid;
int pipefd[2];
int status;
@@ -128,10 +141,11 @@ int exec_command(const char *path, char *const argv[], unsigned timeout) {
close(pipefd[1]);
- size_t length = 0;
+ __ssize_t length = 0;
while ((length = read(pipefd[0], buf, 10)) > 0) {
buf[length] = '\0';
- printf("%s", buf);
+ fprintf(stdout, "%s", buf);
+ fflush(stdout);
}
waitpid(pid, &status, 0);
@@ -139,4 +153,4 @@ int exec_command(const char *path, char *const argv[], unsigned timeout) {
close(pipefd[0]);
return WEXITSTATUS(status);
-} \ No newline at end of file
+}