Recreate shell pipe functionality in C
Features β’ Installation β’ Usage β’ Architecture β’ Documentation
- Overview
- Features
- Installation
- Usage
- Architecture
- How It Works
- Project Structure
- Documentation
- Testing
- Technologies
pipex is a 42 School project that recreates the shell pipe functionality (|) in C. It demonstrates deep understanding of:
- Process management (fork, execve, waitpid)
- Inter-process communication (pipes)
- File descriptor manipulation (dup2, open, close)
- Environment variable parsing (PATH resolution)
- Memory management and error handling
# Instead of:
$ cmd1 < file1 | cmd2 > file2
# You can use:
$ ./pipex file1 cmd1 cmd2 file2- β Basic Pipe Functionality: Execute two commands with pipe between them
- β File Redirection: Read from input file, write to output file
- β PATH Resolution: Automatically finds commands in PATH environment variable
- β Error Handling: Comprehensive error messages matching shell behavior
- β Memory Management: Proper allocation and deallocation
- π Heredoc Support: Handle
here_docdelimiter for interactive input - π Multiple Pipes: Support for 3+ commands in a pipeline
- π Robust Parsing: Handles command arguments properly
- GCC compiler
- Make
- Linux or macOS
# Clone the repository
git clone https://github.com/odeij/pipex.git
cd pipex
# Build mandatory version
make
# Build bonus version
make bonus
# Clean object files
make clean
# Clean everything
make fclean
# Rebuild
make re# Basic pipe: cmd1 < file1 | cmd2 > file2
./pipex input.txt "grep hello" "wc -l" output.txt
# With absolute paths
./pipex /etc/passwd "cat" "head -5" result.txt
# Multiple commands (bonus)
./pipex input.txt "cat" "grep test" "wc -w" output.txt
8000
# Interactive input until delimiter
./pipex_bonus here_doc END "cat" "grep hello" output.txt
# Type lines, then type "END" to finish# Count lines in a file
./pipex file.txt "cat" "wc -l" result.txt
# Search and filter
./pipex log.txt "grep ERROR" "sort" errors.txt
# Process and format
./pipex data.txt "cut -d: -f1" "sort -u" unique.txt| System Call | Purpose | Usage |
|---|---|---|
fork() |
Create child process | Execute commands in separate processes |
execve() |
Replace process image | Execute the actual command |
pipe() |
Create IPC channel | Connect commands |
dup2() |
Redirect file descriptors | Redirect stdin/stdout |
open() |
Open files | Access input/output files |
waitpid() |
Wait for child process | Prevent zombie processes |
access() |
Check file permissions | Verify command executability |
βββββββββββ
β Parent β
β Process β
ββββββ¬βββββ
β
ββββ fork() ββββ
β β
β ββββββΌβββββ
β β Child 1 β
β β cmd1 β
β ββββββ¬βββββ
β β
β [pipe]
β β
ββββ fork() ββββ
β β
β ββββββΌβββββ
β β Child 2 β
β β cmd2 β
β βββββββββββ
β
ββββ waitpid() ββββ Wait for children
infile (fd) ββdup2ββ> STDIN_FILENO (Child 1)
β
β cmd1 executes
β
pipe[1] (fd) ββdup2ββ> STDOUT_FILENO (Child 1)
β
β [Data flows through pipe]
β
pipe[0] (fd) ββdup2ββ> STDIN_FILENO (Child 2)
β
β cmd2 executes
β
outfile (fd) ββdup2ββ> STDOUT_FILENO (Child 2)
-
Argument Validation
- Check for exactly 5 arguments (file1, cmd1, cmd2, file2)
- Verify input file exists and is readable
- Verify output file can be created/written
-
File Opening
- Open input file in read-only mode
- Open/create output file in write mode
-
Pipe Creation
- Create pipe using
pipe()system call - Get two file descriptors: read end and write end
- Create pipe using
-
First Child Process
- Fork to create child process
- Redirect stdin to input file
- Redirect stdout to pipe write end
- Execute first command using
execve()
-
Second Child Process
- Fork to create second child process
- Redirect stdin to pipe read end
- Redirect stdout to output file
- Execute second command using
execve()
-
Parent Cleanup
- Close all file descriptors
- Wait for both children to finish
- Exit with appropriate status code
The program searches for commands in the PATH environment variable:
PATH="/usr/bin:/bin:/usr/local/bin"
Command: "ls"
1. Test: /usr/bin/ls β Found! β
2. Return: "/usr/bin/ls"
If command contains /, it's treated as an absolute path and tested directly.
For detailed architecture diagrams and system call flow, see ARCHITECTURE.md.
pipex/
βββ includes/
β βββ pipex.h # Header file with all declarations
βββ src/
β βββ utils/ # String utility functions
β β βββ ft_strlen.c
β β βββ ft_strncmp.c
β β βββ ft_strchr.c
β β βββ ft_strdup.c
β β βββ ft_substr.c
β β βββ ft_strjoin.c
β β βββ ft_split.c
β βββ bonus/ # Bonus features
β β βββ bonus_main.c
β β βββ heredoc.c
β β βββ multi_pipe.c
β βββ main.c # Main program logic
β βββ args_check.c # Argument validation
β βββ path_utils.c # PATH resolution
β βββ pipe_utils.c # Pipe operations
β βββ exec_utils.c # Command execution
β βββ error_utils.c # Error handling
βββ Makefile # Build configuration
βββ CODE_EXPLANATION.md # Detailed code explanations
βββ README.md # This file
-
CODE_EXPLANATION.md - Comprehensive line-by-line code explanations
- Function-by-function breakdown
- System call explanations
- Memory management details
- Common evaluation questions
-
ARCHITECTURE.md - System architecture and design
- Process flow diagrams
- File descriptor management
- Memory flow visualization
- Error handling flow
Finds the full path to a command executable by searching PATH directories.
Parses command string, finds executable path, and executes using execve().
Creates a pipe for inter-process communication.
Redirects standard input/output using dup2().
Run the test suite:
make
./test.sh# Test 1: Basic functionality
echo "hello world" > test.txt
./pipex test.txt "cat" "wc -w" result.txt
cat result.txt # Should output: 2
# Test 2: Error handling
./pipex nonexistent.txt "cat" "wc" output.txt
# Should show error message
# Test 3: Command not found
./pipex test.txt "nonexistentcmd" "wc" output.txt
# Should show: command not found
# Test 4: Multiple pipes (bonus)
./pipex_bonus test.txt "cat" "grep hello" "wc -l" result.txt
# Test 5: Heredoc (bonus)
./pipex_bonus here_doc END "cat" "wc -l" result.txt
# Type lines, then "END"# Shell command
$ cat file.txt | grep test | wc -l
# Equivalent pipex (bonus)
$ ./pipex_bonus file.txt "cat" "grep test" "wc -l" output.txt- Language: C (C99 standard)
- System Calls: fork, execve, pipe, dup2, open, close, waitpid, access
- Build System: Make
- Standards: 42 School Norm (norminette compliant)
This project demonstrates understanding of:
- β Process Management: Creating and managing child processes
- β IPC: Inter-process communication using pipes
- β File I/O: File descriptor manipulation and redirection
- β System Programming: Direct use of Unix system calls
- β Memory Management: Proper allocation and deallocation
- β Error Handling: Comprehensive error checking and reporting
- β Environment Variables: Parsing and using PATH variable
- This project follows 42 School Norminette coding standards
- All code is commented and documented for evaluation purposes
- Memory leaks are prevented through proper cleanup
- Error handling matches shell behavior
This is a 42 School project. If you're a 42 student working on this project:
- Don't copy this code directly - You need to understand and write it yourself
- Use this as a reference - Study the concepts and implement your own solution
- Learn from the explanations - The documentation helps understand the concepts
This project is part of the 42 School curriculum. Educational use only.
Odeij Amaleddine
β If you found this project helpful, consider giving it a star! β
Made with β€οΈ at 42 School