Skip to content
Snippets Groups Projects
Commit 34f5ebfb authored by dt686's avatar dt686
Browse files

small fix

parent 91e3e9a0
Branches
No related tags found
No related merge requests found
...@@ -12,3 +12,66 @@ EOF ...@@ -12,3 +12,66 @@ EOF
# Assertions # Assertions
[ "$status" -eq 0 ] [ "$status" -eq 0 ]
} }
@test "Change directory then do ls" {
current=$(pwd)
cd /tmp
mkdir -p student-test
cd student-test
touch test_file
cd ..
run "${current}/dsh" <<EOF
cd student-test | ls
EOF
# Strip all whitespace (spaces, tabs, newlines) from the output
stripped_output=$(echo "$output" | tr -d '[:space:]')
# Expected output with all whitespace removed for easier matching
expected_output="test_filedsh3>dsh3>cmdloopreturned0"
# These echo commands will help with debugging and will only print
#if the test fails
echo "Captured stdout:"
echo "Output: $output"
echo "Exit Status: $status"
echo "${stripped_output} -> ${expected_output}"
# Check exact match
[ "$stripped_output" = "$expected_output" ]
# Assertions
[ "$status" -eq 0 ]
}
@test "Change directory" {
current=$(pwd)
cd /tmp
mkdir -p student-test
run "${current}/dsh" <<EOF
cd student-test
pwd
EOF
# Strip all whitespace (spaces, tabs, newlines) from the output
stripped_output=$(echo "$output" | tr -d '[:space:]')
# Expected output with all whitespace removed for easier matching
expected_output="/tmp/student-testdsh3>dsh3>dsh3>cmdloopreturned0"
# These echo commands will help with debugging and will only print
#if the test fails
echo "Captured stdout:"
echo "Output: $output"
echo "Exit Status: $status"
echo "${stripped_output} -> ${expected_output}"
# Check exact match
[ "$stripped_output" = "$expected_output" ]
# Assertions
[ "$status" -eq 0 ]
}
File added
...@@ -189,10 +189,16 @@ int execute_pipeline(command_list_t *clist){ ...@@ -189,10 +189,16 @@ int execute_pipeline(command_list_t *clist){
} }
// Execute command // Execute command
if (strcmp(clist->commands[i].argv[0], "cd") == 0){
continue;
}else{
execvp(clist->commands[i].argv[0], clist->commands[i].argv); execvp(clist->commands[i].argv[0], clist->commands[i].argv);
perror("execvp"); perror("execvp");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
}
} }
// Parent process: close all pipe ends // Parent process: close all pipe ends
...@@ -206,9 +212,9 @@ int execute_pipeline(command_list_t *clist){ ...@@ -206,9 +212,9 @@ int execute_pipeline(command_list_t *clist){
waitpid(pids[i], NULL, 0); waitpid(pids[i], NULL, 0);
} }
exit(OK); // exit(OK);
// return OK; return OK;
} }
...@@ -222,12 +228,12 @@ int exec_local_cmd_loop() ...@@ -222,12 +228,12 @@ int exec_local_cmd_loop()
// TODO IMPLEMENT MAIN LOOP // TODO IMPLEMENT MAIN LOOP
while(1){ while(1){
printf("%s", SH_PROMPT);
if (fgets(cmd_buff, ARG_MAX, stdin) == NULL){ if (fgets(cmd_buff, ARG_MAX, stdin) == NULL){
printf("\n"); printf("\n");
break; break;
} }
printf("%s", SH_PROMPT);
//remove the trailing \n from cmd_buff //remove the trailing \n from cmd_buff
cmd_buff[strcspn(cmd_buff,"\n")] = '\0'; cmd_buff[strcspn(cmd_buff,"\n")] = '\0';
...@@ -241,16 +247,22 @@ int exec_local_cmd_loop() ...@@ -241,16 +247,22 @@ int exec_local_cmd_loop()
} }
build_cmd_list(cmd_buff, &clist); build_cmd_list(cmd_buff, &clist);
int supervisor = fork(); if (strcmp(clist.commands[0].argv[0], "cd") == 0){
if (supervisor == 0) { if (clist.commands[0].argc == 2){
execute_pipeline(&clist); // Supervisor process
chdir(clist.commands[0].argv[1]);
} else {
printf("cd command requires 1 argument\n");
}
for (int i=0; i<clist.num - 1; i++){
clist.commands[i] = clist.commands[i+1];
} }
if (supervisor == -1) { clist.num--;
perror("fork supervisor");
exit(EXIT_FAILURE);
} }
waitpid(supervisor, NULL, 0);
execute_pipeline(&clist);
} }
... ...
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment