The Shell
Bash-like Shell. From Scratch. In C. Demo Below
The minishell project is part of the common core curriculum at 42Vienna and is seen as a milestone and one of the biggest jumps in complexity on the way to completing the cursus.
The rules are simple: Create a program that will replicate basic shell behaviour like typically found on POSIX systems.
Some of the requirements are:
- Custom readline function
- Command line parsing
- I/O loop to accept continuous commands
- Command execution
- File Descriptor redirection
- Pipe Operators
- Environment Variables
- Builtin functions (cd, export, exit etc.)
- Heredoc
- Logical AND ( && )
- Logical OR ( || )
- and more...
The program has to be written in C, we are working on GNU/Linux machines.
We are also required to follow a very strict norm on which you can read more here.
What makes the 42 projects stand out in my opinion is that the tools we use are very restricted — essentially only syscalls to interface with the kernel, some utility functions and memory functions, and in this case we were allowed to use printf, which is normally forbidden. This means that all utility has to be implemented by hand, and all relevant code will be written by the project's authors.
We were allowed to use the readline function used in bash to handle user input. Since memory leaks of any kind are considered an unrecoverable mistake when validating a project, and the readline function would inevitably leak memory, those leaks were explicitly tolerated by the project's requirements.
But, out of curiosity, I decided to recreate the readline function, removing the dependency and leaks from our project, and learning a lot along the way. In my opinion, this is one of the more interesting parts of our implementation, and I very much enjoyed it!
This project was a team effort, and big thanks go out to my team partner and good friend,
Andrej Arama!
The source code for the project can be seen on my
git server and
I have also provided a live version of the shell below.
Many thanks to
ttyd
for the web integration!
Have fun!