Skip to content
Snippets Groups Projects
Commit 34e22d42 authored by luishernandez's avatar luishernandez
Browse files

3-shellp1

parent 67161e1b
Branches
No related tags found
No related merge requests found
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**: _start here_ Because it specifies a limit amount of characters to read, fgets() is a solid option for safely reading a complete line of input. By doing this, buffer overflow problems that might arise with functions like as gets() are avoided. Furthermore, in order to enable the shell to gracefully terminate in headless or automated testing environments, fgets() appropriately handles the newline character and returns NULL when end-of-file (EOF) is reached.
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**: _start here_ Memory allocation for cmd_buff is done via malloc(), which offers flexibility and control over memory consumption. If you wish to avoid utilizing a lot of stack space (which is restricted) or if the maximum input size may change, it enables you to dynamically allocate the precise amount of memory required during runtime. It may also be simpler to change buffer sizes later on without requiring major changes to the code structure thanks to this dynamic allocation.
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**: _start here_ To guarantee that the command and its parameters are interpreted appropriately, leading and trailing spaces must be trimmed. Unintentional arguments or empty tokens may result from using too many spaces. When comparing command names or trying to find the executable, for instance, a command like " ls " can be interpreted with additional whitespace surrounding "ls," which could lead to mismatches. Errors or improper shell execution of the desired command might arise from not trimming spaces.
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**: _start here_ Output Redirection (>): For instance, ls > output.txt. The challenge is for the shell to correctly interpret the > operator, open (or create) the target file in write mode, then use system calls like open() and dup2() to redirect the STDOUT file descriptor to this file. Errors (such as when the file cannot be opened) must be handled carefully.
Input Redirection (<): Sort < input.txt is an example. Difficulty: To make the command read from the file rather from the keyboard, the shell must identify the < operator, open the input file, and reroute STDIN. In the event that the file is not there or cannot be read, it must additionally handle error checks.
Error Redirection (2>): Example: grep foo file.txt 2> errors.txtIn order to redirect STDERR, the error output stream must be handled independently by the shell. To point to a defined file, the implementation has to duplicate the STDERR file descriptor. It should also support situations in which STDOUT and STDERR are combined (using operators like 2>&1), which makes parsing and file descriptor handling much more difficult.
In order to implement redirection in a shell, the command line must be carefully parsed, different token spacing and ordering must be handled, and file descriptors must be properly set up and recovered for ensuing operations.
- 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_ In redirection, files or devices are linked to a command's standard streams (STDIN, STDOUT, or STDERR) rather than the default terminal. Command > file.txt, for instance, reroutes STDOUT to a file. Redirection is typically used to read from or write to persistent storage and deals with static file I/O.The STDOUT of one command is directly connected to the STDIN of another command via piping. For instance, ls | grep "txt" feeds grep the result of ls. Pipes enable the chaining of commands in a pipeline and establish a dynamic, in-memory communication channel between processes.
- 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_ It's crucial to keep STDERR and STDOUT distinct so that error messages may be identified from normal output. Debugging and error management are made simpler by this division; for instance, a user can divert the standard output to a file while still viewing error messages on the terminal. Additionally, it keeps error messages separate from the output data, which is very important when piping output to other applications or in scripts. This distinct division improves usability and guarantees that each stream may be managed according to its content.
- 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_ In order to gracefully manage problems, our custom shell should: Verify each command's exit status and notify the user of any issues.
Clearly displaying STDERR error messages, perhaps with a distinct color or format to set them out from normal output.
STDOUT and STDERR are kept apart by default to prevent error messages from mingling with regular output. We should, however, also provide a way to combine them as needed (for example, by employing a redirection syntax such as 2>&1).
System commands such as dup2() can be used to duplicate the STDERR file descriptor to STDOUT in order to merge the streams. For logging or instructions where the user wishes to view a combined output, this enables both streams to be forwarded to the same location (such as a file or pipe). In the end, the shell should provide flexibility without allowing errors to be lost in silence.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment