1. In this assignment I suggested you use `fgets()` to get user input in the main while loop. Why is `fgets()` a good choice for this application?
> **Answer**: `fgets()` is a good choice for this application since it takes an argument for max length, avoiding buffer overflow. It is also convenient since it reads the whole line, so it matches the behavior of a command shell (input finishes when user hits enter)
2. You needed to use `malloc()` to allocte memory for `cmd_buff` in `dsh_cli.c`. Can you explain why you needed to do that, instead of allocating a fixed-size array?
> **Answer**: Using malloc() for cmd_buff allows dynamic memory allocation based on ARG_MAX, which helps prevent excessive stack usage and potential overflow. Heap allocation also ensures persistence when the array (pointer) is passed in different functions and enables future scalability, unlike a fixed-size array.
3. In `dshlib.c`, the function `build_cmd_list(`)` must trim leading and trailing spaces from each command before storing it. Why is this necessary? If we didn't trim spaces, what kind of issues might arise when executing commands in our shell?
> **Answer**: Trimming leading and trailing spaces from commands in build_cmd_list() is necessary to ensure accurate validation and execution of inputs. If spaces were not removed:
- Extra spaces might cause commands to be not recognized, which leads to problems like difficulty in string comparison (eg: " exit" vs "exit")
- Empty commands cannot be or takes more time to be detected
4. For this question you need to do some research on STDIN, STDOUT, and STDERR in Linux. We've learned this week that shells are "robust brokers of input and output". Google _"linux shell stdin stdout stderr explained"_ to get started.
- One topic you should have found information on is "redirection". Please provide at least 3 redirection examples that we should implement in our custom shell, and explain what challenges we might have implementing them.
> **Answer**:
- Output Redirection (> and >>) – Saves command output to a file (ls > fileout). Challenge: Handling file permissions and overwriting existing files
- Input Redirection (<) – Reads input from a file instead of the keyboard (wc < filein). Challenge: Detecting missing or unreadable files
- Error Redirection (2>) – Captures error messages (gcc script.c 2> fileerror). Challenge: Ensuring errors are correctly separated from standard output
- You should have also learned about "pipes". Redirection and piping both involve controlling input and output in the shell, but they serve different purposes. Explain the key differences between redirection and piping.
> **Answer**: Redirection writes/reads output/intput to/from files, while piping passes output of one command as input to another. So the key difference is that redirection interacts with files, while piping connects processes dynamically
- STDERR is often used for error messages, while STDOUT is for regular output. Why is it important to keep these separate in a shell?
> **Answer**: Keeping them separate makes debugging more clear and prevents errors from mixing with regular output. This allows logging errors while still processing successful command results.
- How should our custom shell handle errors from commands that fail? Consider cases where a command outputs both STDOUT and STDERR. Should we provide a way to merge them, and if so, how?
> **Answer**: Our shell should detect failed commands and display specific error messages via STDERR. We should allow merging STDERR and STDOUT (2>&1) when needed for logging or processing combined output.