A 42 project that replicates the behavior of the shell pipe operator (|) in C.
./pipex infile "cmd1" "cmd2" outfile
Behaves like:
< infile cmd1 | cmd2 > outfilePipex creates a pipeline between two commands using Unix system calls:
- Creates a pipe with
pipe() - Forks two child processes with
fork() - Child 1 — Redirects
infileto stdin and pipe write-end to stdout, then executescmd1 - Child 2 — Redirects pipe read-end to stdin and
outfileto stdout, then executescmd2 - Parent — Closes pipe ends, waits for both children, and returns the exit status of
cmd2
The parser handles command arguments exactly like a shell:
| Feature | Example | Parsed As |
|---|---|---|
| Single quotes | grep 'hello world' |
grep, hello world |
| Double quotes | awk "{print NR}" |
awk, {print NR} |
| Backslash escape | echo hello\ world |
echo, hello world |
| Mixed quotes | echo "it's" |
echo, it's |
| Adjacent quotes | "hello"'world' |
helloworld |
| Quote removal | grep 'pattern' |
grep, pattern |
| Unclosed quote detection | grep 'missing |
Syntax error (exit 2) |
| Leading/trailing whitespace | " ls -la " |
ls, -la |
make # Build
make clean # Remove object files
make fclean # Remove object files and binary
make re # Rebuild from scratch./pipex infile "ls -la" "grep pipex" outfile
./pipex infile "cat" "wc -l" outfile
./pipex infile "grep 'hello world'" "wc -w" outfile
./pipex infile "cat" "awk '{print \$2}'" outfile| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Bad arguments / general error |
| 2 | Syntax error (unclosed quote) |
| 127 | Command not found |
pipex/
├── main.c # Entry point, fork logic, execute, child processes
├── pipex.h # Header with prototypes and includes
├── Makefile # Build system
└── utils/
├── parse.c # Bash-like command parser (quotes, escaping)
├── ft_split.c # String splitting by delimiter
├── ft_putstr_fd.c # Write string to file descriptor
├── ft_strdup.c # String duplication
├── ft_strjoin.c # String concatenation
├── ft_strlen.c # String length
├── ft_strncmp.c # String comparison
└── utils.c # Error handling, free, PATH resolution