A custom implementation of Unix pipes in C, recreating the functionality of shell pipe operator (
|).
- Overview
- Features
- Installation
- Usage
- Examples
- Technical Details
- Project Structure
- Requirements
- Testing
- Contributing
- License
Pipex is a project that recreates the behavior of the Unix pipe operator (|) in C. It demonstrates deep understanding of system programming concepts including:
- Process creation and management with
fork() - Inter-process communication using pipes
- File descriptor manipulation with
dup2() - Command execution with
execve() - Environment variable handling
- Error management
This project is part of the 42 curriculum and serves as an excellent learning tool for understanding Unix system calls and process management.
- Basic piping:
file1 cmd1 cmd2 file2 - Multiple pipes:
file1 cmd1 cmd2 cmd3 ... cmdn file2 - Here document:
here_doc LIMITER cmd1 cmd2 file2 - Robust error handling
- Path resolution for command execution
- Custom libft library implementation
- GCC compiler
- Make build system
- Unix-like operating system (Linux/macOS)
# Clone the repository
git clone https://github.com/migclay12/pipex.git
cd pipex
# Build the project
make
# The executable 'pipex' will be created in the root directory# Remove object files
make clean
# Remove executable and object files
make fclean
# Rebuild everything
make re./pipex file1 cmd1 cmd2 file2This is equivalent to: < file1 cmd1 | cmd2 > file2
./pipex file1 cmd1 cmd2 cmd3 cmd4 file2This is equivalent to: < file1 cmd1 | cmd2 | cmd3 | cmd4 > file2
./pipex here_doc LIMITER cmd1 cmd2 file2This is equivalent to: cmd1 << LIMITER | cmd2 > file2
# Instead of: cat file.txt | grep "error" | wc -l > count.txt
./pipex file.txt "grep error" "wc -l" count.txt# Instead of: cat access.log | grep "GET" | awk '{print $7}' | sort | uniq -c | sort -nr > top_pages.txt
./pipex access.log "grep GET" "awk '{print \$7}'" "sort" "uniq -c" "sort -nr" top_pages.txt# Instead of: echo -e "line1\nline2\nEOF" | cat | wc -l > count.txt
./pipex here_doc EOF "cat" "wc -l" count.txt
# Then type: line1
# line2
# EOF- Parent Process: Manages pipe creation and child process coordination
- Child Processes: Each executes a specific command in the pipeline
- Pipe Communication: Data flows through pipes between processes
ft_pipex(): Handles basic two-command pipelinesft_pipex_bonus(): Supports multiple command pipelinesft_here_doc(): Implements here document functionalityft_join_path(): Resolves command paths using PATH environment variableft_execve(): Safely executes commands with proper error handling
- Proper cleanup of allocated memory
- File descriptor management
- Process cleanup and exit handling
pipex/
βββ source/ # Source code
β βββ main.c # Program entry point
β βββ pipex.c # Core pipex functionality
β βββ path.c # Path resolution logic
β βββ child.c # Child process management
β βββ pipex.h # Header file with declarations
βββ libft/ # Custom C library
β βββ ft_*.c # Library functions
β βββ libft.h # Library header
β βββ Makefile # Library build system
βββ Makefile # Main project Makefile
βββ README.md # This file
βββ en.subject.pdf # Project specification
This project implements the following requirements:
- β
Reproduce the behavior of
< file1 cmd1 | cmd2 > file2 - β
Handle multiple pipes:
< file1 cmd1 | cmd2 | ... | cmdn > file2 - β
Support here document:
cmd1 << LIMITER | cmd2 > file2 - β Proper error handling and usage messages
- β No memory leaks
- β Follow coding standards and norms
# Test basic functionality
echo "Hello World" > input.txt
./pipex input.txt "cat" "wc -c" output.txt
cat output.txt # Should show character count
# Compare with shell pipe
echo "Hello World" | cat | wc -c# Test multiple pipes
echo -e "apple\nbanana\nApple\ncherry" > fruits.txt
./pipex fruits.txt "grep Apple" "sort" "uniq -c" result.txt
# Equivalent shell command
cat fruits.txt | grep Apple | sort | uniq -c# Test here document
./pipex here_doc EOF "cat" "tr 'a-z' 'A-Z'" uppercase.txt
# Type: hello world
# EOF
cat uppercase.txt # Should show "HELLO WORLD"Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project demonstrates mastery of:
- System Programming: Unix system calls, process management
- File I/O: File descriptors, redirection, pipes
- Process Management: Fork, exec, wait system calls
- Error Handling: Robust error detection and reporting
- Memory Management: Dynamic allocation and cleanup
- Shell Internals: Understanding of how shells work internally
This project is licensed under the MIT License - see the LICENSE file for details.
Miguel Gonzalez Clayton
- 42 School Student
"The best way to learn system programming is by implementing system tools yourself."
Made with β€οΈ for the 42 curriculum