Skip to content

Repository files navigation

Pipex (42 Project)

A 42 school project that recreates the behavior of Unix shell pipes using system calls. Pipex demonstrates inter-process communication by chaining commands through pipes, mimicking the shell's pipe operator (|).

What Pipex Demonstrates

This project showcases understanding of:

  • Unix Process Management: Creating and managing child processes with fork()
  • Inter-Process Communication: Using pipe() to connect process input/output streams
  • Program Execution: Replacing process images with execve()
  • File Descriptor Manipulation: Redirecting stdin/stdout with dup2()
  • Error Handling: Managing system call failures and process exit codes
  • PATH Resolution: Searching executable paths in the environment

Program Usage

Mandatory Part

./pipex infile "cmd1" "cmd2" outfile

This behaves like the shell command:

< infile cmd1 | cmd2 > outfile

Example:

./pipex infile "grep hello" "wc -l" outfile
# Equivalent to: < infile grep hello | wc -l > outfile

Bonus Part

Multiple Commands

./pipex infile "cmd1" "cmd2" "cmd3" ... "cmdN" outfile

This behaves like:

< infile cmd1 | cmd2 | cmd3 | ... | cmdN > outfile

Example:

./pipex infile "cat" "grep pattern" "sort" "uniq" outfile
# Equivalent to: < infile cat | grep pattern | sort | uniq > outfile

Here_doc Mode

./pipex_bonus here_doc LIMITER "cmd1" "cmd2" outfile

This behaves like:

cmd1 << LIMITER | cmd2 >> outfile

Example:

./pipex_bonus here_doc EOF "grep hello" "wc -l" outfile
# Reads input until "EOF" is typed
# Pipes through grep and wc, appends to outfile

Mandatory Part Explanation

The mandatory implementation handles a two-command pipeline:

  1. Process Creation: Forks two child processes
  2. First Child: Opens infile, redirects stdin from it, redirects stdout to pipe write end, executes cmd1
  3. Second Child: Redirects stdin from pipe read end, opens/creates outfile, redirects stdout to it, executes cmd2
  4. Parent Process: Closes pipe ends and waits for both children to complete
  5. Exit Code: Returns the exit code of the last command in the pipeline

Bonus Part Explanation

Multiple Pipes

Supports chaining an arbitrary number of commands:

  • Creates N-1 pipes for N commands
  • Each command runs in its own child process
  • First command reads from infile
  • Last command writes to outfile
  • Middle commands read from previous pipe and write to next pipe
  • All pipes are closed appropriately to prevent resource leaks

Here_doc Mode

Implements heredoc functionality:

  • Reads input from stdin until LIMITER is encountered
  • Stores input in a pipe (not a temporary file)
  • Pipes through specified commands
  • Appends output to outfile (instead of truncating)
  • Validates that exactly 6 arguments are provided

Error Handling

The program handles various error scenarios:

  • Invalid Arguments: Displays usage message and exits
  • File Errors: Reports when infile cannot be opened, outfile cannot be created
  • Command Not Found: Exits with code 127 when command doesn't exist in PATH
  • System Call Failures: Uses perror() to report pipe, fork, dup2, or execve failures
  • Exit Codes: Preserves child process exit codes and signals

Compilation

The project includes a Makefile with the following targets:

make              # Compiles mandatory part -> pipex
make bonus        # Compiles bonus part -> pipex_bonus
make clean        # Removes object files
make fclean       # Removes object files and executables
make re           # Recompiles everything from scratch

Compilation flags: -Wall -Wextra -Werror (strict error checking)

Allowed Functions

The following standard C and Unix system functions are permitted:

  • File Operations: open, close, read, write, unlink, access
  • Memory Management: malloc, free
  • Process Management: fork, wait, waitpid, wait3, wait4
  • Program Execution: execve and its variants (execvp, execlp, etc.)
  • Inter-Process Communication: pipe, dup, dup2
  • Error Handling: perror, strerror
  • Standard I/O: exit
  • libft: Custom library functions from previous 42 projects

Project Structure

pipex/
├── pipex.c              # Mandatory entry point and main logic
├── pipex_bonus.c        # Bonus entry point (here_doc + multi-pipe)
├── proccess.c           # Process handling and command execution
├── process_bonus.c      # Multi-pipe process management
├── here_doc.c           # Heredoc implementation
├── pipex_utils.c        # PATH parsing and error utilities
├── path_utils.c         # Environment variable extraction
├── utils.c              # String manipulation helpers
├── utils_bonus.c        # Bonus utility functions
├── pipex.h              # Header file with function prototypes
├── Makefile             # Build automation
└── libft/               # Custom C library (from previous projects)

Technical Implementation Details

Process Flow (Mandatory)

  1. Validate argument count (must be 5)
  2. Create pipe with pipe()
  3. Fork first child:
    • Open infile for reading
    • Redirect stdin from infile
    • Redirect stdout to pipe write end
    • Execute cmd1
  4. Fork second child:
    • Redirect stdin from pipe read end
    • Create/truncate outfile
    • Redirect stdout to outfile
    • Execute cmd2
  5. Parent closes pipe and waits for children

Command Resolution

  • Extracts PATH from environment variables
  • Splits PATH by ':' delimiter
  • Tests each directory with access() for executable permission
  • Returns full path to command or NULL if not found

File Descriptor Management

  • All unused file descriptors are closed in each process
  • Prevents descriptor leaks and blocking
  • Ensures proper cleanup on error paths

Note: This project is part of the 42 school curriculum and implements Unix pipe behavior from scratch using only allowed system calls. It does not use shell execution or high-level piping functions.

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages