diff --git a/5-ShellP3/.debug/launch.json b/5-ShellP3/.debug/launch.json new file mode 100644 index 0000000000000000000000000000000000000000..72e404dc24f1893e3fe252a15d6e112e4807ad4c --- /dev/null +++ b/5-ShellP3/.debug/launch.json @@ -0,0 +1,29 @@ +{ + "configurations": [ + { + "name": "(gdb) 5-ShellP3", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/5-ShellP3/starter/dsh", + "args": [""], + "stopAtEntry": false, + "cwd": "${workspaceFolder}/5-ShellP3/starter", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + }, + { + "description": "Set Disassembly Flavor to Intel", + "text": "-gdb-set disassembly-flavor intel", + "ignoreFailures": true + } + ], + "preLaunchTask": "Build 5-ShellP3" + } + ] +} \ No newline at end of file diff --git a/5-ShellP3/.debug/tasks.json b/5-ShellP3/.debug/tasks.json new file mode 100644 index 0000000000000000000000000000000000000000..3fb660801e6eae56c837af79541c9eadffc14a0a --- /dev/null +++ b/5-ShellP3/.debug/tasks.json @@ -0,0 +1,20 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build 5-ShellP3", + "type": "shell", + "command": "make", + "group": { + "kind": "build", + "isDefault": true + }, + "options": { + "cwd": "${workspaceFolder}/5-ShellP3/starter" + }, + "problemMatcher": ["$gcc"], + "detail": "Runs the 'make' command to build the project." + } + ] + } + \ No newline at end of file diff --git a/5-ShellP3/bats/assignment_tests.sh b/5-ShellP3/bats/assignment_tests.sh new file mode 100644 index 0000000000000000000000000000000000000000..ac345604db4e15a8253e8d88c595717e28172fa5 --- /dev/null +++ b/5-ShellP3/bats/assignment_tests.sh @@ -0,0 +1,36 @@ +#!/usr/bin/env bats + +############################ DO NOT EDIT THIS FILE ##################################### +# File: assignement_tests.sh +# +# DO NOT EDIT THIS FILE +# +# Add/Edit Student tests in student_tests.sh +# +# All tests in this file must pass - it is used as part of grading! +######################################################################################## + +@test "Pipes" { + run "./dsh" <<EOF +ls | grep dshlib.c +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="dshlib.cdsh3>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 ] +} diff --git a/5-ShellP3/bats/student_tests.sh b/5-ShellP3/bats/student_tests.sh new file mode 100644 index 0000000000000000000000000000000000000000..d47fa7248614bb93f12fb6709683f58f8ed8232f --- /dev/null +++ b/5-ShellP3/bats/student_tests.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bats + +# File: student_tests.sh +# +# Create your unit tests suit in this file + +@test "Example: check ls runs without errors" { + run ./dsh <<EOF +ls +EOF + + # Assertions + [ "$status" -eq 0 ] +} + + +# Same test suite from previous assignment to ensure nothing broke between then and now +@test "Check exit runs without errors" { + run ./dsh <<< "exit" + [ "$status" -eq 0 ] +} + +@test "Check cd with no argument does nothing" { + run ./dsh <<< "cd; pwd" + [[ "$output" == "$pwd"* ]] +} + +@test "Check cd to a non-existent directory fails" { + run ./dsh <<< "cd /nonexistent" + [[ "$output" == *"cd failed"* ]] +} + +@test "Check echo prints correct message" { + run ./dsh <<< 'echo "Hello, world!"' + [[ "$output" == "Hello, world!"* ]] +} + +@test "Check empty input does not crash shell" { + run ./dsh <<< "" + [ "$status" -eq 0 ] +} + +@test "Check pwd command runs without errors" { + run ./dsh <<< "pwd" + [ "$status" -eq 0 ] +} + +@test "Invalid does not run without errors" { + run ./dsh <<< "somerandomcommand" + [[ "$output" == *"execvp failed"* ]] +} + +# New test suite + +@test "Piping ls and grep commands" { + result="$(echo 'ls | grep .c' | ./dsh)" + [[ "$result" == *".c"* ]] +} + +@test "Piping cat and grep commands" { + echo "test line" > testfile.txt + result="$(echo 'cat testfile.txt | grep line' | ./dsh)" + rm testfile.txt + [[ "$result" == *"test line"* ]] +} + +@test "Piping with quoted arguments" { + result="$(echo "echo \"hello world\" | grep \"hello\"" | ./dsh)" + [[ "$result" == *"hello world"* ]] +} + +@test "Piping multiple times" { + result="$(echo 'ls | grep .c | wc -l' | ./dsh)" + [[ "$result" =~ [0-9]+ ]] +} + +@test "Pipe error handling" { + result="$(echo 'ls | nonexistentcommand' | ./dsh 2>&1)" + [[ "$result" == *"No such file or directory"* ]] +} + diff --git a/5-ShellP3/code.md b/5-ShellP3/code.md new file mode 100644 index 0000000000000000000000000000000000000000..532ae5fc740da2ab89e44a66262abd391ae538fe --- /dev/null +++ b/5-ShellP3/code.md @@ -0,0 +1,31 @@ +# Code Grading Instructions + +- dshlib.c must implement a exec_local_cmd_loop() func that: + - runs a loop collecting user input + - parses commands and arguments from each line of input + - parsing must handle pipes - all commands must be split by pipes into multiple commands + - determines if the parsed command is an "external command" +- the parsed commands must: + - have trailing and leading whitespace removed + - must preserve whitepsace between double quotes (i.e. perserve echo "hello world") + - must treat double-quoted string as a single argument +- the code must implement "cd" as an internal command + - if cd is provided with arguments, attmpt to change the current process working directory to the provided argument + - if no argument is provided to cd, it should do nothing +- the code must implement "exit" as an internal command, exiting with a return code of 0 +- for all other commands, they should be treated as external and executed with fork/exec + - execution must be pipelined if there is more than one command + - the solution should use the pipe() syscall to create pipes and connect the commands + - the first command in the pipeline takes input from stdin, and the last command in the pipeline writes to stdout +- students are not required to implement redirection or pipes in this assignment +- extra credit, these are optional requirements: + - extra credit 1 - implement < and > for redirection bewteen commands + - extra credit 2 - implement >> append redirection + +# Possible Grade + +- 50 points: Correct implementation of required functionality +- 5 points: Code quality (how easy is your solution to follow) +- 10 points: Quality and breadth of BATS unit tests +- 10 points: [EXTRA CREDIT 1] handle < and > redirection +- 5 points: [EXTRA CREDIT 2] handle >> append redirection \ No newline at end of file diff --git a/5-ShellP3/dsh b/5-ShellP3/dsh new file mode 100755 index 0000000000000000000000000000000000000000..81124377739b5bb73ec439059a317595e9344355 Binary files /dev/null and b/5-ShellP3/dsh differ diff --git a/5-ShellP3/dsh_cli.c b/5-ShellP3/dsh_cli.c new file mode 100644 index 0000000000000000000000000000000000000000..9262cf457e6b235b19999efa04153e1de0962255 --- /dev/null +++ b/5-ShellP3/dsh_cli.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "dshlib.h" + +/* DO NOT EDIT + * main() logic moved to exec_local_cmd_loop() in dshlib.c +*/ +int main(){ + int rc = exec_local_cmd_loop(); + printf("cmd loop returned %d\n", rc); +} \ No newline at end of file diff --git a/5-ShellP3/dshlib.c b/5-ShellP3/dshlib.c new file mode 100644 index 0000000000000000000000000000000000000000..19c655033fd85118edce1b60d185674715731121 --- /dev/null +++ b/5-ShellP3/dshlib.c @@ -0,0 +1,274 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <ctype.h> +#include <stdbool.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/wait.h> + +#include "dshlib.h" + +/* + * Implement your exec_local_cmd_loop function by building a loop that prompts the + * user for input. Use the SH_PROMPT constant from dshlib.h and then + * use fgets to accept user input. + * + * while(1){ + * printf("%s", SH_PROMPT); + * if (fgets(cmd_buff, ARG_MAX, stdin) == NULL){ + * printf("\n"); + * break; + * } + * //remove the trailing \n from cmd_buff + * cmd_buff[strcspn(cmd_buff,"\n")] = '\0'; + * + * //IMPLEMENT THE REST OF THE REQUIREMENTS + * } + * + * Also, use the constants in the dshlib.h in this code. + * SH_CMD_MAX maximum buffer size for user input + * EXIT_CMD constant that terminates the dsh program + * SH_PROMPT the shell prompt + * OK the command was parsed properly + * WARN_NO_CMDS the user command was empty + * ERR_TOO_MANY_COMMANDS too many pipes used + * ERR_MEMORY dynamic memory management failure + * + * errors returned + * OK No error + * ERR_MEMORY Dynamic memory management failure + * WARN_NO_CMDS No commands parsed + * ERR_TOO_MANY_COMMANDS too many pipes used + * + * console messages + * CMD_WARN_NO_CMD print on WARN_NO_CMDS + * CMD_ERR_PIPE_LIMIT print on ERR_TOO_MANY_COMMANDS + * CMD_ERR_EXECUTE print on execution failure of external command + * + * Standard Library Functions You Might Want To Consider Using (assignment 1+) + * malloc(), free(), strlen(), fgets(), strcspn(), printf() + * + * Standard Library Functions You Might Want To Consider Using (assignment 2+) + * fork(), execvp(), exit(), chdir() + */ + +int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff) { + if (cmd_line == NULL || cmd_buff == NULL) { + return ERR_MEMORY; + } + + memset(cmd_buff, 0, sizeof(cmd_buff_t)); + + cmd_buff->_cmd_buffer = strdup(cmd_line); + if (cmd_buff->_cmd_buffer == NULL) { + return ERR_MEMORY; + } + + char *p = cmd_buff->_cmd_buffer; + int argc = 0; + bool in_quotes = false; + + while (*p && isspace(*p)) p++; + + while (*p && argc < CMD_ARGV_MAX - 1) { + cmd_buff->argv[argc] = p; + + while (*p) { + if (*p == '"') { + in_quotes = !in_quotes; + + memmove(p, p + 1, strlen(p)); + continue; + } + + if (!in_quotes && isspace(*p)) { + break; + } + + p++; + } + + if (*p) { + *p++ = '\0'; + while (*p && isspace(*p)) p++; + } + + argc++; + + if (!*p) break; + } + + cmd_buff->argv[argc] = NULL; + cmd_buff->argc = argc; + + return (argc == 0) ? WARN_NO_CMDS : OK; +} + +int exec_local_cmd_loop() { + char *cmd_buff = malloc(SH_CMD_MAX); + if (cmd_buff == NULL) { + fprintf(stderr, "Failed to allocate memory\n"); + return ERR_MEMORY; + } + + int status; + + while (1) { + printf("%s", SH_PROMPT); + + if (fgets(cmd_buff, SH_CMD_MAX, stdin) == NULL) { + printf("\n"); + break; + } + + cmd_buff[strcspn(cmd_buff, "\n")] = '\0'; + + if (strlen(cmd_buff) == 0) { + printf(CMD_WARN_NO_CMD); + continue; + } + + char *cmd_copy = strdup(cmd_buff); + if (cmd_copy == NULL) { + fprintf(stderr, "Failed to allocate memory\n"); + continue; + } + + command_list_t cmd_list; + memset(&cmd_list, 0, sizeof(command_list_t)); + + char *token; + char *saveptr; + int cmd_count = 0; + + token = strtok_r(cmd_copy, PIPE_STRING, &saveptr); + + while (token != NULL && cmd_count < CMD_MAX) { + if (build_cmd_buff(token, &cmd_list.commands[cmd_count]) != OK) { + for (int i = 0; i < cmd_count; i++) { + free(cmd_list.commands[i]._cmd_buffer); + } + free(cmd_copy); + fprintf(stderr, "Failed to parse command\n"); + goto next_command; + } + + cmd_count++; + token = strtok_r(NULL, PIPE_STRING, &saveptr); + } + + if (token != NULL) { + printf(CMD_ERR_PIPE_LIMIT, CMD_MAX); + for (int i = 0; i < cmd_count; i++) { + free(cmd_list.commands[i]._cmd_buffer); + } + free(cmd_copy); + goto next_command; + } + + free(cmd_copy); + cmd_list.num = cmd_count; + + if (cmd_count == 0) { + printf(CMD_WARN_NO_CMD); + goto next_command; + } + + if (strcmp(cmd_list.commands[0].argv[0], EXIT_CMD) == 0) { + printf("exiting...\n"); + for (int i = 0; i < cmd_count; i++) { + free(cmd_list.commands[i]._cmd_buffer); + } + free(cmd_buff); + return OK; + } + else if (strcmp(cmd_list.commands[0].argv[0], "cd") == 0) { + if (cmd_list.commands[0].argc > 1) { + if (chdir(cmd_list.commands[0].argv[1]) != 0) { + perror("CD failed"); + } + } + for (int i = 0; i < cmd_count; i++) { + free(cmd_list.commands[i]._cmd_buffer); + } + goto next_command; + } + + if (cmd_count > 0) { + int pipes[CMD_MAX-1][2]; + pid_t pids[CMD_MAX]; + + for (int i = 0; i < cmd_count - 1; i++) { + if (pipe(pipes[i]) < 0) { + perror("Pipe creation failed"); + for (int i = 0; i < cmd_count; i++) { + free(cmd_list.commands[i]._cmd_buffer); + } + goto next_command; + } + } + + for (int i = 0; i < cmd_count; i++) { + pids[i] = fork(); + + if (pids[i] < 0) { + perror("Fork failed"); + for (int j = 0; j < cmd_count - 1; j++) { + close(pipes[j][0]); + close(pipes[j][1]); + } + for (int i = 0; i < cmd_count; i++) { + free(cmd_list.commands[i]._cmd_buffer); + } + goto next_command; + } + + if (pids[i] == 0) { + if (i > 0) { + if (dup2(pipes[i-1][0], STDIN_FILENO) < 0) { + perror("dup2 input redirection failed"); + exit(1); + } + } + + if (i < cmd_count - 1) { + if (dup2(pipes[i][1], STDOUT_FILENO) < 0) { + perror("dup2 output redirection failed"); + exit(1); + } + } + + for (int j = 0; j < cmd_count - 1; j++) { + close(pipes[j][0]); + close(pipes[j][1]); + } + + if (execvp(cmd_list.commands[i].argv[0], cmd_list.commands[i].argv) < 0) { + perror("execvp failed"); + exit(1); + } + } + } + + for (int i = 0; i < cmd_count - 1; i++) { + close(pipes[i][0]); + close(pipes[i][1]); + } + + for (int i = 0; i < cmd_count; i++) { + waitpid(pids[i], &status, 0); + } + } + + for (int i = 0; i < cmd_count; i++) { + free(cmd_list.commands[i]._cmd_buffer); + } + + next_command: + continue; + } + + free(cmd_buff); + return OK; +} \ No newline at end of file diff --git a/5-ShellP3/dshlib.h b/5-ShellP3/dshlib.h new file mode 100644 index 0000000000000000000000000000000000000000..fe3b24680bed23840340d37a89310125ae517306 --- /dev/null +++ b/5-ShellP3/dshlib.h @@ -0,0 +1,93 @@ +#ifndef __DSHLIB_H__ + #define __DSHLIB_H__ + + +//Constants for command structure sizes +#define EXE_MAX 64 +#define ARG_MAX 256 +#define CMD_MAX 8 +#define CMD_ARGV_MAX (CMD_MAX + 1) +// Longest command that can be read from the shell +#define SH_CMD_MAX EXE_MAX + ARG_MAX + +typedef struct command +{ + char exe[EXE_MAX]; + char args[ARG_MAX]; +} command_t; + +typedef struct cmd_buff +{ + int argc; + char *argv[CMD_ARGV_MAX]; + char *_cmd_buffer; +} cmd_buff_t; + +/* WIP - Move to next assignment +#define N_ARG_MAX 15 //MAX number of args for a command +typedef struct command{ + char exe [EXE_MAX]; + char args[ARG_MAX]; + int argc; + char *argv[N_ARG_MAX + 1]; //last argv[LAST] must be \0 +}command_t; +*/ + +typedef struct command_list{ + int num; + cmd_buff_t commands[CMD_MAX]; +}command_list_t; + +//Special character #defines +#define SPACE_CHAR ' ' +#define PIPE_CHAR '|' +#define PIPE_STRING "|" + +#define SH_PROMPT "dsh3> " +#define EXIT_CMD "exit" +#define EXIT_SC 99 + +//Standard Return Codes +#define OK 0 +#define WARN_NO_CMDS -1 +#define ERR_TOO_MANY_COMMANDS -2 +#define ERR_CMD_OR_ARGS_TOO_BIG -3 +#define ERR_CMD_ARGS_BAD -4 //for extra credit +#define ERR_MEMORY -5 +#define ERR_EXEC_CMD -6 +#define OK_EXIT -7 + +//prototypes +int alloc_cmd_buff(cmd_buff_t *cmd_buff); +int free_cmd_buff(cmd_buff_t *cmd_buff); +int clear_cmd_buff(cmd_buff_t *cmd_buff); +int build_cmd_buff(char *cmd_line, cmd_buff_t *cmd_buff); +int close_cmd_buff(cmd_buff_t *cmd_buff); +int build_cmd_list(char *cmd_line, command_list_t *clist); +int free_cmd_list(command_list_t *cmd_lst); + +//built in command stuff +typedef enum { + BI_CMD_EXIT, + BI_CMD_DRAGON, + BI_CMD_CD, + BI_NOT_BI, + BI_EXECUTED, +} Built_In_Cmds; +Built_In_Cmds match_command(const char *input); +Built_In_Cmds exec_built_in_cmd(cmd_buff_t *cmd); + +//main execution context +int exec_local_cmd_loop(); +int exec_cmd(cmd_buff_t *cmd); +int execute_pipeline(command_list_t *clist); + + + + +//output constants +#define CMD_OK_HEADER "PARSED COMMAND LINE - TOTAL COMMANDS %d\n" +#define CMD_WARN_NO_CMD "warning: no commands provided\n" +#define CMD_ERR_PIPE_LIMIT "error: piping limited to %d commands\n" + +#endif \ No newline at end of file diff --git a/5-ShellP3/makefile b/5-ShellP3/makefile new file mode 100644 index 0000000000000000000000000000000000000000..b14b0723e23ea2036f0220406b40a93e6a0cd283 --- /dev/null +++ b/5-ShellP3/makefile @@ -0,0 +1,31 @@ +# Compiler settings +CC = gcc +CFLAGS = -Wall -Wextra -g + +# Target executable name +TARGET = dsh + +# Find all source and header files +SRCS = $(wildcard *.c) +HDRS = $(wildcard *.h) + +# Default target +all: $(TARGET) + +# Compile source to executable +$(TARGET): $(SRCS) $(HDRS) + $(CC) $(CFLAGS) -o $(TARGET) $(SRCS) + +# Clean up build files +clean: + rm -f $(TARGET) + +test: + bats $(wildcard ./bats/*.sh) + +valgrind: + echo "pwd\nexit" | valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1 ./$(TARGET) + echo "pwd\nexit" | valgrind --tool=helgrind --error-exitcode=1 ./$(TARGET) + +# Phony targets +.PHONY: all clean test \ No newline at end of file diff --git a/5-ShellP3/questions.md b/5-ShellP3/questions.md new file mode 100644 index 0000000000000000000000000000000000000000..baea96fd7973cb46c4aca6847956337f3fb4cc21 --- /dev/null +++ b/5-ShellP3/questions.md @@ -0,0 +1,15 @@ +1. Your shell forks multiple child processes when executing piped commands. How does your implementation ensure that all child processes complete before the shell continues accepting user input? What would happen if you forgot to call waitpid() on all child processes? + +My implementation ensures that all child processes are completed before the shell continues accepting input by storing each child's process ID in an array and then making use of the waitpid() command to ensure that the parent waits for each child to finish. Forgetting to do so would mean the child would be terminated byt still exist in the process table (zombie). Additionally, system resources such as memory will continue to be consumed which could potentially lead to leaks and the shell becoming unstable. + +2. The dup2() function is used to redirect input and output file descriptors. Explain why it is necessary to close unused pipe ends after calling dup2(). What could go wrong if you leave pipes open? + +If we were to leave pipes open, system resources will be wasted, eventually leading to no new pipes or files being able to be opened. Additionally, there will be no EOF signal sent which means the read portion will continue waiting for more input that will never get there. This could lead to the program freezing. + +3. Your shell recognizes built-in commands (cd, exit, dragon). Unlike external commands, built-in commands do not require execvp(). Why is cd implemented as a built-in rather than an external command? What challenges would arise if cd were implemented as an external process? + +CD us implemented like this since the working directory that the shell process is running in needs to be changed. If we were to change this and attempt to implement it externally, the directory change action would not actually persist in the shell after the process exits. This means that to the user, it would seemingly work but the following commands ran would be executed in the original direcrory, not the seemingly new one. + +4. Currently, your shell supports a fixed number of piped commands (CMD_MAX). How would you modify your implementation to allow an arbitrary number of piped commands while still handling memory allocation efficiently? What trade-offs would you need to consider? + +In order to make this change, the fixed-array would have to replaced with dynamically allocated arrays (linked list or a resizeable array for example). This pattern would have to continue for the rest of the program (allocating pids for example) where everything is managed dynamically. Some trade-offs to consider include error handling to manage dynamic memory allocation (resource limit reached for example), overall performance since dynamic memory allocation is slower than a fixed implementation, and reliability of the program since dynamic memory allocation is more likely to fail than a fixed implementation. diff --git a/5-ShellP3/readme.md b/5-ShellP3/readme.md new file mode 100644 index 0000000000000000000000000000000000000000..7dc0e4772ea13de4f82efd065f73eed19fe03b0d --- /dev/null +++ b/5-ShellP3/readme.md @@ -0,0 +1,135 @@ +# Assignment: Custom Shell Part 3 - Pipes + +This week we will build on our `dsh` Drexel Shell by implementing pipes. In the first shell assignment we approached this subject by splitting lines of input by the pipe `|` and printing out each separate command parsed between the pipes. In the second assignment we set the pipe splitting logic aside and implemented _execution_ of single commands. + +This week we'll bring these concepts together by implementing pipelined execution of multiple commands! + +# Reuse Prior Work! + +The `dsh` assignments are meant to be additive. This week you'll need to use code you wrote in the first two shell assignments. Be sure to re-use your existing code, and refactor it to meet the requirements of this assignment. + +# Pipes + +This week you'll need to use a couple new important syscalls - `pipe()` and `dup2()`. + +The `pipe()` command creates a 1-way communication path that is implemented with two file descriptions. Conceptually it looks like this: + +```txt + process-1-output --write--> pipe[fd1]:pipe[fd0] --read--> process-2-input +``` + +How do the pipes get "connected" between the processes? That's where `dup2()` comes in. + +> Tip: pipes should be created and connected inside a `fork()`; you've already used `fork/execvp` to implement a single command. This time you'll need to do that in a loop, because each command should get it's own `fork/execvp`. So, you'll need to handle `pipe()` and `dup2()` _inside_ each child's fork. + +Think of `dup2()` as sort of patching or redirection. The function signature is: + + `dup2(source_fd, target_fd)` + +Remember that each forked child has it's own process space and set of file descriptors, to include `STDIN_FILENO` and `STDOUT_FILENO`. So, inside each child process, you could use dup2 to _replace_ the child's STDIN and STDOUT with pipe file descriptors instead. + +**Preventing Descriptor Leaks** + +When you use `dup2()`, it _copies_ the source FD onto the target, effectively replacing the target. The source is no longer used, and can be closed. If you don't close the original pipes after copying with `dup2()`, you have introduced a **descriptor leak**; this could cause your program to run out of available file descriptors. + +# Multiple Children / Waitpid + +Since you must create multiple forks to handle piped commands, be sure to wait on all of them with `waitpid()`, which can be used to wait for a specific process id. _(Hint: you'll need to save the pid of each process you fork)._ + +# Assignment Details + +### Step 1 - Review [./starter/dshlib.h](./starter/dshlib.h) + +The file [./starter/dshlib.h](./starter/dshlib.h) contains some useful definitions and types. Review the available resources in this file before you start coding - these are intended to make your work easier and more robust! + +### Step 2 - Re-implement Your Main Loop and Parsing Code in exec_local_cmd_loop() to parse multiple commands by pipe [./starter/dshlib.c](./starter/dshlib.c) + +This should mostly be a refactoring, copy/paste exercise to merge your solutions from the first two shell assignments. You need to combine your pipe tokenizing solution with loading up `cmd_buff_t` ultimately into a `command_list_t`. + +### Step 3 - Implement pipelined execution of the parsed commands [./starter/dshlib.c](./starter/dshlib.c) + +The first part of this is a refactoring problem - instead of just one `fork/exec`, you'll need to do that in a loop and keep track of the child pids and child exit statuses. + +The second part is implementing pipe logic. The section above named "Pipes" contains everything you really need to know! Also, check out demo code from this week's lecture [5-pipe-handling](https://github.com/drexel-systems/SysProg-Class/tree/main/demos/process-thread/5-pipe-handling). + + +### Step 4 - Create BATS Tests + +You should now be familiar with authoring your own [bash-based BATS unit tests](https://bats-core.readthedocs.io/en/stable/tutorial.html#your-first-test) from the prior assignment. + +Same as last week, you have an "assignment-tests.sh" file that we provide and you cannot change, and a "student_tests.sh" file that **you need to add your own tests to**: + +- your-workspace-folder/ + - bats/assignement_tests.sh + - bats/student_tests.sh + +**This week we expect YOU to come up with the majority of the test cases! If you have trouble, look at the tests we provided from prior weeks** + +**Be sure to run `make test` and verify your tests pass before submitting assignments.** + +### Step 5 - Answer Questions + +Answer the questions located in [./questions.md](./questions.md). + +### Sample Run with Sample Output +The below shows a sample run executing multiple commands and the expected program output: + +```bash +./dsh +dsh3> ls | grep ".c" +dragon.c +dsh_cli.c +dshlib.c +dsh3> exit +exiting... +cmd loop returned 0 +``` + +### Extra Credit: +10 + +Last week you should have learned about redirection (it was part of the assignment's research question). Once you've implemented pipes for this assignment, redirection is a natural next step. + +**Requirements** + +- parse `<` and `>` during command buffer creation; you'll need to parse and track these on `cmd_buff_t` +- modify command execution to use `dup2()` in a similar way as you did for pipes; you can copy the in and out fd's specified by the user on to STDIN or STDOUT of the child's forked process + +Example run: + +```bash +./dsh +dsh3> echo "hello, class" > out.txt +dsh3> cat out.txt +hello, class +``` + +### Extra Credit++: +5 + +Extend the first extra credit to implement `>>`. This is the same as `>`, except the target fd is in append mode. + +Example run: + +```bash +./dsh +dsh3> echo "hello, class" > out.txt +dsh3> cat out.txt +hello, class +dsh3> echo "this is line 2" >> out.txt +dsh3> cat out.txt +hello, class +this is line 2 +dsh3> +``` + +#### Grading Rubric + +- 50 points: Correct implementation of required functionality +- 5 points: Code quality (how easy is your solution to follow) +- 10 points: Answering the written questions: [questions.md](./questions.md) +- 10 points: Quality and breadth of BATS unit tests +- 10 points: [EXTRA CREDIT] handle < and > redirection +- 5 points: [EXTRA CREDIT++] handle >> append redirection + +Total points achievable is 90/75. + +