Sitemap

Project Overview

Minishell is a 42 project that requires you to create a simplified version of bash shell. The goal is to understand how shells work, process management, and system calls.

Learning Objectives

  • Understand shell mechanics and command execution
  • Learn about process creation and management
  • Master file descriptor manipulation
  • Implement signal handling
  • Work with environment variables
  • Parse and interpret command line input

Mandatory Requirements

General Rules

  • Write in C language
  • Follow the Norm (42 coding standard)
  • No memory leaks, segfaults, or undefined behavior
  • Create a Makefile with rules: all, clean, fclean, re
  • Handle errors gracefully without crashing
  • Use only allowed functions (see allowed functions list)

Allowed Functions

readline, rl_clear_history, rl_on_new_line, rl_replace_line, rl_redisplay
add_history, printf, malloc, free, write, access, open, read, close
fork, wait, waitpid, wait3, wait4, signal, sigaction, sigemptyset
sigaddset, kill, exit, getcwd, chdir, stat, lstat, fstat, unlink
execve, dup, dup2, pipe, opendir, readdir, closedir, strerror, perror
isatty, ttyname, ttyslot, ioctl, getenv, tcsetattr, tcgetattr
tgetent, tgetflag, tgetnum, tgetstr, tgoto, tputs

Technical Specifications

Core Features

  1. Display a prompt when waiting for new commands
  2. Command history functionality
  3. Search and launch executables (relative/absolute path or using PATH)
  4. Handle single quotes (') - prevent interpretation of metacharacters
  5. Handle double quotes (") - prevent interpretation except for $
  6. Implement redirections:
  • < redirect input
  • > redirect output
  • << heredoc
  • >> append output
  1. Implement pipes (|)
  2. Handle environment variables ($ followed by characters)
  3. Handle $? (exit status of last executed command)
  4. Handle ctrl-C, ctrl-D, ctrl-\ signals
  5. Implement built-in commands

Program Structure

  • Program name: minishell
  • Handle one or more options/arguments (if needed)
  • Interactive mode: display prompt and wait for commands
  • Non-interactive mode: not required but can be implemented

Built-in Commands

1. echo with option -n

  • Print arguments to stdout
  • -n option: don't print trailing newline
  • Handle multiple arguments and spaces

2. cd with relative or absolute path

  • Change current directory
  • Handle ~ (home directory)
  • Handle - (previous directory)
  • Handle no arguments (go to HOME)
  • Update PWD and OLDPWD environment variables

3. pwd with no options

  • Print current working directory
  • Should work even if PWD is unset or modified

4. export with no options

  • Set environment variables
  • Display all exported variables when no arguments
  • Handle var=value format
  • Handle invalid variable names

Get Crouns’s stories in your inbox

Join Medium for free to get updates from this writer.

5. unset with no options

  • Remove environment variables
  • Handle multiple variables
  • Don’t unset read-only variables

6. env with no options or arguments

  • Display all environment variables
  • Each variable on a new line in key=value format

7. exit with no options

  • Exit the shell
  • Handle numeric arguments
  • Handle multiple arguments (error case)
  • Exit with appropriate status code

Signal Handling

Interactive Mode

  • ctrl-C: Display new prompt on new line
  • ctrl-D: Exit shell
  • ctrl-\: Do nothing (ignore)

Non-Interactive Mode (commands running)

  • ctrl-C: Stop current command, display new prompt
  • ctrl-D: EOF behavior
  • ctrl-\: Quit current command

Implementation Notes

  • Use sigaction or signal functions
  • Handle signals appropriately in parent and child processes
  • Don’t let signals kill the shell unexpectedly

Environment Variables

Expansion Rules

  • $VAR or ${VAR}: expand to value of VAR
  • $?: expand to exit status of last command
  • $$: expand to process ID (optional)
  • Variables in double quotes are expanded
  • Variables in single quotes are NOT expanded

Special Cases

  • Undefined variables expand to empty string
  • Handle $ followed by invalid characters
  • Handle $ at end of string

Parsing and Execution

Command Parsing

  1. Tokenization: Split input into tokens
  2. Quote handling: Process single and double quotes
  3. Variable expansion: Expand $ variables
  4. Redirection parsing: Identify and parse <, >, <<, >>
  5. Pipeline parsing: Split on | operators

Execution Order

  1. Parse command line
  2. Handle redirections
  3. Check for built-in commands
  4. Search for external commands
  5. Execute command
  6. Handle return status

Edge Cases

  • Empty commands
  • Commands with only spaces
  • Multiple pipes
  • Redirections with pipes
  • Invalid file permissions
  • Non-existent commands

Error Handling

Common Errors

  • Command not found: Display error message
  • Permission denied: Handle file permissions
  • No such file or directory: Handle missing files
  • Syntax errors: Handle malformed commands
  • Memory allocation failures: Handle malloc failures

Error Messages

  • Should be similar to bash error messages
  • Use perror or strerror for system errors
  • Display errors to stderr
  • Set appropriate exit status

--

--