questions.md
-
In this assignment I suggested you use
fgets()
to get user input in the main while loop. Why isfgets()
a good choice for this application?Answer: fgets() is a good choice for this application since it is ideal for reading input line by line. It will read up to size - 1 characters from the stream and stops when it encounters either an EOF or a newline. If a newline is encountered, it will store it into the buffer then add a terminating null byte after the last character in the buffer.
-
You needed to use
malloc()
to allocte memory forcmd_buff
indsh_cli.c
. Can you explain why you needed to do that, instead of allocating a fixed-size array?Answer: Using malloc() instead of just having a fixed size for the array will give us memory management since we aren't sure exactly how much memory to allocate except the max. However, if we only allocate the max, then it will become inefficient since we are allocating more memory than we may need. Also, a fixed size array may lead to a buffer overflow if the input exceeds the array's capacity. If we needed more memory in the future, then we already have malloc coded instead of going back to recode it.
-
In
dshlib.c
, the functionbuild_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: Incase the commands have trailing spaces inputted by user error, then this will negate those variables and make the program more consistent.
-
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: start here
-
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: start here
-
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: start here
-
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: start here