Shell built-in commands, unlike legacy executables such as /bin/ls or /usr/bin/vim, are internal features of a shell that the user may invoke. For example, alias (to create a shortcut such as ‘l’ for ‘ls’), cd, history are among common built-in commands supported by most shells. When a user enters a command, a shell must first check if it is a built-in command in which case forking a child to load and run a legacy executable is not required. If the entered command is not built-in, then a shell assumes that it is a binary executable that needs to found and executed by a worker process. Extending built-in commands into a programming language, called shell scripts, is a popular way to manage and automate tasks on UNIX based operating systems such as Linux and MacOS.
Use the code of main4.c as a starting point to extend its capability to support the following built-in commands:
$ cprompt <x>
where <x> is 0, 1, 2 or 3. The notation <x> means that x is a variable. When <x> is 0, the default prompt ‘$’ is output. If <x> is 1, the prompt is changed to ‘>’. <x> equals to 2 changes the prompt to the PID of the process. Implement your own prompt for the case where <x> equals 3. Annotate your code to describe what option 3 outputs.
$ mem <s>
where <s> is a string that is stored in a memo note. Implement the memo note as a 1-D array of maximum size 10, char *memnote, where each element is a pointer to a string. Use malloc() to allocate just enough memory to store the string <s>. When memnote is full, cycle to the beginning and overwrite memnote with the new string. Hence memnote is a circular array that overwrites the oldest element if space is full. Keep track of the newest and oldest memo note so that when the user enters the command ‘mem’ without a string <s> then the memo note strings are output by recency, with the newest first and the oldest last, by the order the user has added them to the memnote string array.
$ d <sec> <binexec>
The command d asks that the shell delay running the legacy executable <binexec> by <sec> seconds. Perform this task by calling library function sleep() with <sec> as argument. Look up section 3 of the man pages (run % man 3 sleep) to check how to use sleep() including any header files. First fork a child process who checks if a delay is needed by inspecting a variable, int delayflag, set by the parent. If 1, sleep() is called with argument <sec> followed by execlp() of <binexec>. Otherwise, execlp() is called without delay. In the case delayed execution is requested by the user, the parent does not call waitpid() to wait until the child terminates. Instead, it returns to the beginning of the while loop to print a fresh prompt since the delay could be substantial.
The command x without arguments means to terminate the shell. The shell prints a suitable message to stdout, then calls exit(0) to terminate.
Although main4.c serves as the main skeleton code, implement the built-in features supported by the shell, call it mysh, in a modular fashion making calls to functions that are put in their own file in a folder v12. Create a README text file that describes what each file in a folder v12 contains and its role. Use a Makefile to compile your code. 20 points out of 150 will be assigned for modularity, clarity, and style. Although not necessary, you may use string processing library functions. The library function atoi() which converts a number represented as a string (e.g., “3075”) into an integer (3075) may be useful. Look up its man page if you decide to use it. Test and verify that mysh works correctly.