Pipex is a C program that simulates the behavior of Unix pipes (|), allowing you to chain commands together by redirecting the output of one command to the input of another. This implementation mimics the functionality of commands like:
$ < input_file cmd1 | cmd2 > output_fileThe program takes exactly four arguments:
- Input file
- First command
- Second command
- Output file
- Clone the repository:
git clone https://github.com/<your-username>/pipex.git
cd pipex- Compile the program:
makeThe executable pipex will be created in the root directory.
./pipex input_file "command1" "command2" output_file./pipex input.txt "grep hello" "wc -l" output.txtThis is equivalent to:
< input.txt grep hello | wc -l > output.txt- Validate argument count (exactly 4 arguments + program name)
- Open input and output files
- Create a pipe and fork two child processes:
- First child:
- Redirects STDIN from input file
- Redirects STDOUT to pipe's write end
- Executes first command
- Second child:
- Redirects STDIN from pipe's read end
- Redirects STDOUT to output file
- Executes second command
- First child:
- Parent process waits for both children to complete
- Path Resolution: Searches for commands using the
PATHenvironment variable - Error Handling: Comprehensive error messages and proper cleanup
- Memory Management: Custom allocators with automatic cleanup
- Process Management: Uses
fork(),pipe(), andexecve()for command execution
- 🛠️ Implements Unix-like pipe functionality
- 🔍 Searches commands in
PATHenvironment variable - 🚦 Comprehensive error handling
- 💾 Memory-safe with proper cleanup
- 📁 Handles file input/output redirection
- ⚡ Supports absolute path commands
- � Graceful exit with proper status codes
pipex/
├── Makefile # Compilation rules
├── pipex # Compiled executable
├── pipex.h # Header file
├── pipex.c # Main program logic
├── utils/ # Utility functions
│ ├── ft_split.c # String splitting
│ ├── ft_strjoin.c # String concatenation
│ ├── ft_strnstr.c # Substring search
│ ├── path_utils.c # PATH resolution
│ ├── error_utils.c # Error handling
│ └── ... # Other helper functions
└── libft/ # Custom library functions
├── ft_calloc.c
├── ft_strdup.c
├── ft_strncmp.c
└── ...