Skip to content
/ pipex Public

Unix pipe simulation in C — replicates < infile cmd1 | cmd2 > outfile with bash-like argument parsing supporting single/double quotes, backslash escaping, and robust error handling. A 42 project.

Notifications You must be signed in to change notification settings

MrMsnawi/pipex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pipex

A 42 project that replicates the behavior of the shell pipe operator (|) in C.

./pipex infile "cmd1" "cmd2" outfile

Behaves like:

< infile cmd1 | cmd2 > outfile

How It Works

Pipex creates a pipeline between two commands using Unix system calls:

  1. Creates a pipe with pipe()
  2. Forks two child processes with fork()
  3. Child 1 — Redirects infile to stdin and pipe write-end to stdout, then executes cmd1
  4. Child 2 — Redirects pipe read-end to stdin and outfile to stdout, then executes cmd2
  5. Parent — Closes pipe ends, waits for both children, and returns the exit status of cmd2

Bash-Like Parsing

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

Compilation

make        # Build
make clean  # Remove object files
make fclean # Remove object files and binary
make re     # Rebuild from scratch

Usage

./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

Exit Codes

Code Meaning
0 Success
1 Bad arguments / general error
2 Syntax error (unclosed quote)
127 Command not found

Project Structure

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

About

Unix pipe simulation in C — replicates < infile cmd1 | cmd2 > outfile with bash-like argument parsing supporting single/double quotes, backslash escaping, and robust error handling. A 42 project.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published