Skip to content
Snippets Groups Projects
Select Git revision
1 result Searching

questions.md

questions.md 3.39 KiB

Assignment Questions

1. fgets() for User Input

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?

'fgets()' is good for this assignment, rather than getline(), because it only allocates enough memory up to the size given as a argument, meaning it's much safer than just grabbing random bits of memory and sprting through them.

2. Memory Allocation with malloc()

You needed to use malloc() to allocate memory for cmd_buff in dsh_cli.c. Can you explain why you needed to do that instead of allocating a fixed-size array?

You need to use 'malloc' because you are dynamically allocating memory and it allows you to not waste space when allocating memory for 'cmd_buff'.

3. Trimming Spaces in build_cmd_list()

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?

The main issue that arises is that even if logically your code would split the buffer into executable and arguments, in practice it actually doesn't because it treats each arguement as an additional executable.