Skip to content

akdovlet/pipex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by akdovlet.

pipex


Description

pipex is a C program that reproduces the behaviour of the Unix shell pipe operator (|). It chains commands together so that the standard output of one becomes the standard input of the next, while reading from an input file and writing the final result to an output file — exactly as a shell would when you type:

< infile cmd1 | cmd2 > outfile

The project deepens understanding of core Unix concepts: file descriptors, process creation with fork, inter-process communication with pipe, program execution with execve, and proper resource management.

Features

  • Chains an arbitrary number of commands (bonus) via Unix pipes
  • Resolves command paths from the PATH environment variable, with a hardcoded fallback when the environment is empty
  • Supports commands given as absolute paths (e.g. /usr/bin/cat)
  • here_doc mode (bonus): reads input interactively from stdin until a delimiter is found, mimicking << DELIMITER shell syntax
  • Mirrors bash's error handling and exit codes (missing files, invalid commands, permission errors, etc.)
  • Catches and propagates child process signals (e.g. SIGSEGV)
  • No memory leaks — full cleanup on every exit path

Instructions

Requirements

  • A C compiler (cc)
  • GNU make
  • A POSIX-compatible system (Linux / macOS)

Compilation

# Standard binary (exactly two commands)
make

# Bonus binary (multiple commands + here_doc support)
make bonus

# Build both
make both

The make command also builds the bundled libft library automatically.

Cleaning up

make clean    # remove object files
make fclean   # remove object files and binaries
make re       # fclean + rebuild

Execution

Standard mode — mirrors < infile cmd1 | cmd2 > outfile:

./pipex infile "cmd1" "cmd2" outfile

Bonus mode — supports multiple commands:

./pipex_bonus infile "cmd1" "cmd2" ... "cmdN" outfile

here_doc mode (bonus) — replaces the input file with stdin, appends to outfile:

./pipex_bonus here_doc DELIMITER "cmd1" "cmd2" ... "cmdN" outfile

Examples

# Equivalent to: < /etc/passwd grep root | wc -l > out.txt
./pipex /etc/passwd "grep root" "wc -l" out.txt

# Multiple pipes (bonus)
./pipex_bonus /etc/passwd "grep root" "tr ':' '\n'" "head -5" out.txt

# here_doc (bonus) — type lines, end with EOF
./pipex_bonus here_doc EOF "cat" "wc -l" out.txt

Resources

Documentation & References

  • pipe(2) man page — creating inter-process communication channels
  • fork(2) man page — process duplication
  • execve(2) man page — replacing a process image with a new program
  • dup2(2) man page — redirecting file descriptors
  • waitpid(2) man page — waiting for child processes and retrieving exit codes
  • access(2) man page — checking file permissions
  • The Linux Programming Interface — Michael Kerrisk, chapters on processes, pipes, and file I/O
  • Advanced Programming in the UNIX Environment — W. Richard Stevens & Stephen Rago

Use of AI

AI (Claude) was used exclusively for the redaction of this README. It was not used at any stage of the development, debugging, or research process. All code, design decisions, and problem-solving were done independently.

About

Inter-process communication excercise

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors