Minishell is a comprehensive Unix shell implementation developed as a team project for 42 School. This project recreates the core functionality of bash, demonstrating advanced systems programming concepts including process management, signal handling, parsing, and inter-process communication.
Built entirely in C with only low-level system calls and the GNU Readline library, Minishell showcases our ability to understand and implement complex shell behaviors from the ground up.
- Complete Shell Environment: Full-featured interactive shell with command history
- Advanced Parsing Engine: Sophisticated tokenizer handling quotes, pipes, and redirections
- Process Management: Multi-process pipeline execution with proper signal propagation
- Memory Management: Zero memory leaks with comprehensive cleanup systems
- POSIX Compliance: Standards-compliant implementation of shell behaviors
cd- Directory navigation with relative/absolute paths andOLDPWDsupportecho- Text output with-nflag supportenv- Environment variable displayexit- Shell termination with custom exit codesexport- Environment variable creation and modificationpwd- Current working directory displayunset- Environment variable removal
- Command Execution: Support for absolute paths, relative paths, and
$PATHlookup - Pipeline Processing: Multi-command pipes (
cmd1 | cmd2 | cmd3) - I/O Redirection:
- Output redirection (
>,>>) - Input redirection (
<) - Here-documents (
<<) with delimiter processing
- Output redirection (
- Quote Processing: Single and double quote handling with proper escaping
- Variable Expansion: Environment variables (
$HOME,$USER) and exit codes ($?)
- Readline Integration: Command-line editing with history navigation
- Signal Handling: Proper
Ctrl+C,Ctrl+D, andCtrl+\behavior - Custom Prompt: "GLaDOS>" prompt (Portal reference!)
- TTY Detection: Automatic behavior adjustment for interactive vs. non-interactive mode
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Tokenizer βββββΆβ Parser βββββΆβ Executor β
β β’ Quote handlingβ β β’ Command groups β β β’ Process mgmt β
β β’ Operators β β β’ Redirections β β β’ Pipeline exec β
β β’ Variables β β β’ Pipe sequences β β β’ Signal prop β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β Lexical Analysisβ β Syntax Analysis β β Runtime Exec β
β β’ Token streams β β β’ AST building β β β’ Fork/exec β
β β’ Error handlingβ β β’ Heredoc prep β β β’ Wait/status β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
Core Shell State (t_msh)
typedef struct s_msh {
char pwd[MAXPATHLEN]; // Current working directory
const char *arg0; // Shell name
char **env; // Environment variables
int last_code; // Last command exit status
int should_exit; // Exit flag
int exit_code; // Shell exit code
t_pipeline *pipeline; // Current executing pipeline
} t_msh;Command Pipeline (t_pipeline)
- Linked list of commands connected by pipes
- Environment variable resolution
- Process ID tracking for signal management
- Built-in command detection and execution
Token Processing (t_token)
- Type classification (TEXT, PIPE, REDIR_IN/OUT, HEREDOC)
- Length-bounded string processing
- Quote-aware tokenization
- Input Processing: Readline captures user input with history support
- Lexical Analysis: Tokenizer breaks input into semantic tokens
- Parsing: Command groups and redirections are identified and structured
- Variable Resolution: Environment variables and special parameters expanded
- Heredoc Processing: Here-documents processed and temporary files created
- Pipeline Execution: Commands executed in child processes with proper pipe setup
- Signal Management: Parent process manages child signals and terminal control
- Cleanup: Memory freed, file descriptors closed, temporary files removed
- Custom Memory Functions: Implementation of
ft_calloc,ft_strdup, etc. - Leak Prevention: Comprehensive cleanup in all execution paths
- String Management: Safe string operations with bounds checking
- Fork/Exec Model: Proper child process creation and management
- Pipeline Setup: Inter-process communication via pipes
- Signal Propagation: Signals correctly forwarded to child processes
- Exit Status Handling: Proper collection and reporting of child exit statuses
// Interactive mode: Ctrl+C resets prompt
void interactive_sig_handler(int sig) {
printf("\n");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}
// Execution mode: Signals propagated to pipeline
void exec_sig_handler(int sig) {
if (sig == SIGQUIT && g_minishell.pipeline)
pipeline_propagate_signal(g_minishell.pipeline, SIGQUIT);
}- SHLVL Tracking: Proper shell nesting level management
- PATH Resolution: Command lookup in PATH directories
- PWD/OLDPWD: Working directory state maintenance
- Variable Export: Environment variable inheritance to child processes
| Metric | Value |
|---|---|
| Lines of Code | 3,244 |
| Source Files | 47 C files |
| Header Files | 1 comprehensive header |
| Built-in Commands | 7 complete implementations |
| Memory Leaks | 0 (Valgrind validated) |
- GCC compiler with C99 support
- GNU Readline library (
libreadline-dev) - Termcap library (
libtermcap-dev) - Make utility
# Clone the repository
git clone https://github.com/lgeniaux/minishell.git
cd minishell
# Build the project
make
# Run the shell
./minishellGLaDOS> echo "Welcome to Minishell!"
Welcome to Minishell!
GLaDOS> export GREETING="Hello World"
GLaDOS> echo $GREETING | wc -w
2
GLaDOS> ls | grep .c | head -5
GLaDOS> cd srcs && pwd && cd .. && echo "Back to: $PWD"
GLaDOS> cat << EOF > test.txt
> This is a heredoc
> Multiple lines supported
> EOF
GLaDOS> exit 42This project demonstrates mastery of:
- Low-level I/O: File descriptor management and redirection
- Process Control: Fork, exec, wait system calls
- Signal Handling: Asynchronous event management
- Memory Management: Manual allocation and deallocation
- Modular Design: Clean separation of parsing, execution, and I/O
- Error Handling: Robust error recovery and user feedback
- Testing Strategy: Comprehensive validation against bash behavior
- Collaborative Development: Git workflow with multiple contributors
- Lexical Analysis: Tokenization and parsing theory
- Process Communication: Inter-process communication via pipes
- State Machines: Shell state management and mode transitions
- System Interface: Understanding Unix system call interface
This project is part of the 42 School curriculum. Code is provided for educational purposes.
