8000 GitHub - odeij/pipex: 42 project to handle pipes Β· GitHub
[go: up one dir, main page]

Skip to content
odeijPublic

About

42 project to handle pipes

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
< 8000 div class="react-directory-truncate">out.txt
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Pipex - 42 Project

42 C Linux macOS

Recreate shell pipe functionality in C

Features β€’ Installation β€’ Usage β€’ Architecture β€’ Documentation


πŸ“‹ Table of Contents


🎯 Overview

pipex is a 42 School project that recreates the shell pipe functionality (|) in C. It demonstrates deep understanding of:

  • Process management (fork, execve, waitpid)
  • Inter-process communication (pipes)
  • File descriptor manipulation (dup2, open, close)
  • Environment variable parsing (PATH resolution)
  • Memory management and error handling

What It Does

# Instead of:
$ cmd1 < file1 | cmd2 > file2

# You can use:
$ ./pipex file1 cmd1 cmd2 file2

✨ Features

Mandatory Features

  • βœ… Basic Pipe Functionality: Execute two commands with pipe between them
  • βœ… File Redirection: Read from input file, write to output file
  • βœ… PATH Resolution: Automatically finds commands in PATH environment variable
  • βœ… Error Handling: Comprehensive error messages matching shell behavior
  • βœ… Memory Management: Proper allocation and deallocation

Bonus Features

  • 🎁 Heredoc Support: Handle here_doc delimiter for interactive input
  • 🎁 Multiple Pipes: Support for 3+ commands in a pipeline
  • 🎁 Robust Parsing: Handles command arguments properly

πŸ› οΈ Installation

Prerequisites

  • GCC compiler
  • Make
  • Linux or macOS

Build

# Clone the repository
git clone https://github.com/odeij/pipex.git
cd pipex

# Build mandatory version
make

# Build bonus version
make bonus

# Clean object files
make clean

# Clean everything
make fclean

# Rebuild
make re

πŸ“– Usage

Basic Usage

# Basic pipe: cmd1 < file1 | cmd2 > file2
./pipex input.txt "grep hello" "wc -l" output.txt

# With absolute paths
./pipex /etc/passwd "cat" "head -5" result.txt

# Multiple commands (bonus)
./pipex input.txt "cat" "grep test" "wc -w" output.txt

Heredoc (Bonus)

8000
# Interactive input until delimiter
./pipex_bonus here_doc END "cat" "grep hello" output.txt
# Type lines, then type "END" to finish

Examples

# Count lines in a file
./pipex file.txt "cat" "wc -l" result.txt

# Search and filter
./pipex log.txt "grep ERROR" "sort" errors.txt

# Process and format
./pipex data.txt "cut -d: -f1" "sort -u" unique.txt

πŸ—οΈ Architecture

System Calls Used

System Call Purpose Usage
fork() Create child process Execute commands in separate processes
execve() Replace process image Execute the actual command
pipe() Create IPC channel Connect commands
dup2() Redirect file descriptors Redirect stdin/stdout
open() Open files Access input/output files
waitpid() Wait for child process Prevent zombie processes
access() Check file permissions Verify command executability

Process Flow

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Parent β”‚
β”‚ Process β”‚
β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
     β”‚
     β”œβ”€β”€β”€ fork() ───┐
     β”‚               β”‚
     β”‚          β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”
     β”‚          β”‚ Child 1 β”‚
     β”‚          β”‚  cmd1   β”‚
     β”‚          β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”˜
     β”‚               β”‚
     β”‚          [pipe]
     β”‚               β”‚
     β”œβ”€β”€β”€ fork() ───┐
     β”‚               β”‚
     β”‚          β”Œβ”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”
     β”‚          β”‚ Child 2 β”‚
     β”‚          β”‚  cmd2   β”‚
     β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
     β”‚
     └─── waitpid() ──── Wait for children

File Descriptor Flow

infile (fd) ──dup2──> STDIN_FILENO (Child 1)
                              β”‚
                              β”‚ cmd1 executes
                              β”‚
pipe[1] (fd) ──dup2──> STDOUT_FILENO (Child 1)
     β”‚
     β”‚ [Data flows through pipe]
     β”‚
pipe[0] (fd) ──dup2──> STDIN_FILENO (Child 2)
                              β”‚
                              β”‚ cmd2 executes
                              β”‚
outfile (fd) ──dup2──> STDOUT_FILENO (Child 2)

πŸ” How It Works

Step-by-Step Execution

  1. Argument Validation

    • Check for exactly 5 arguments (file1, cmd1, cmd2, file2)
    • Verify input file exists and is readable
    • Verify output file can be created/written
  2. File Opening

    • Open input file in read-only mode
    • Open/create output file in write mode
  3. Pipe Creation

    • Create pipe using pipe() system call
    • Get two file descriptors: read end and write end
  4. First Child Process

    • Fork to create child process
    • Redirect stdin to input file
    • Redirect stdout to pipe write end
    • Execute first command using execve()
  5. Second Child Process

    • Fork to create second child process
    • Redirect stdin to pipe read end
    • Redirect stdout to output file
    • Execute second command using execve()
  6. Parent Cleanup

    • Close all file descriptors
    • Wait for both children to finish
    • Exit with appropriate status code

PATH Resolution

The program searches for commands in the PATH environment variable:

PATH="/usr/bin:/bin:/usr/local/bin"
Command: "ls"

1. Test: /usr/bin/ls β†’ Found! βœ…
2. Return: "/usr/bin/ls"

If command contains /, it's treated as an absolute path and tested directly.


πŸ“Š Visual Architecture

For detailed architecture diagrams and system call flow, see ARCHITECTURE.md.

πŸ“ Project Structure

pipex/
β”œβ”€β”€ includes/
β”‚   └── pipex.h              # Header file with all declarations
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ utils/               # String utility functions
β”‚   β”‚   β”œβ”€β”€ ft_strlen.c
β”‚   β”‚   β”œβ”€β”€ ft_strncmp.c
β”‚   β”‚   β”œβ”€β”€ ft_strchr.c
β”‚   β”‚   β”œβ”€β”€ ft_strdup.c
β”‚   β”‚   β”œβ”€β”€ ft_substr.c
β”‚   β”‚   β”œβ”€β”€ ft_strjoin.c
β”‚   β”‚   └── ft_split.c
β”‚   β”œβ”€β”€ bonus/               # Bonus features
β”‚   β”‚   β”œβ”€β”€ bonus_main.c
β”‚   β”‚   β”œβ”€β”€ heredoc.c
β”‚   β”‚   └── multi_pipe.c
β”‚   β”œβ”€β”€ main.c               # Main program logic
β”‚   β”œβ”€β”€ args_check.c         # Argument validation
β”‚   β”œβ”€β”€ path_utils.c         # PATH resolution
β”‚   β”œβ”€β”€ pipe_utils.c         # Pipe operations
β”‚   β”œβ”€β”€ exec_utils.c         # Command execution
β”‚   └── error_utils.c        # Error handling
β”œβ”€β”€ Makefile                 # Build configuration
β”œβ”€β”€ CODE_EXPLANATION.md      # Detailed code explanations
└── README.md               # This file

πŸ“š Documentation

Available Documentation

  • CODE_EXPLANATION.md - Comprehensive line-by-line code explanations

    • Function-by-function breakdown
    • System call explanations
    • Memory management details
    • Common evaluation questions
  • ARCHITECTURE.md - System architecture and design

    • Process flow diagrams
    • File descriptor management
    • Memory flow visualization
    • Error handling flow

Key Functions

get_cmd_path()

Finds the full path to a command executable by searching PATH directories.

execute_cmd()

Parses command string, finds executable path, and executes using execve().

create_pipe()

Creates a pipe for inter-process communication.

redirect_stdin() / redirect_stdout()

Redirects standard input/output using dup2().


πŸ§ͺ Testing

Automated Testing

Run the test suite:

make
./test.sh

Manual Test Cases

# Test 1: Basic functionality
echo "hello world" > test.txt
./pipex test.txt "cat" "wc -w" result.txt
cat result.txt  # Should output: 2

# Test 2: Error handling
./pipex nonexistent.txt "cat" "wc" output.txt
# Should show error message

# Test 3: Command not found
./pipex test.txt "nonexistentcmd" "wc" output.txt
# Should show: command not found

# Test 4: Multiple pipes (bonus)
./pipex_bonus test.txt "cat" "grep hello" "wc -l" result.txt

# Test 5: Heredoc (bonus)
./pipex_bonus here_doc END "cat" "wc -l" result.txt
# Type lines, then "END"

Comparison with Shell

# Shell command
$ cat file.txt | grep test | wc -l

# Equivalent pipex (bonus)
$ ./pipex_bonus file.txt "cat" "grep test" "wc -l" output.txt

πŸ’» Technologies

  • Language: C (C99 standard)
  • System Calls: fork, execve, pipe, dup2, open, close, waitpid, access
  • Build System: Make
  • Standards: 42 School Norm (norminette compliant)

πŸŽ“ Learning Outcomes

This project demonstrates understanding of:

  • βœ… Process Management: Creating and managing child processes
  • βœ… IPC: Inter-process communication using pipes
  • βœ… File I/O: File descriptor manipulation and redirection
  • βœ… System Programming: Direct use of Unix system calls
  • βœ… Memory Management: Proper allocation and deallocation
  • βœ… Error Handling: Comprehensive error checking and reporting
  • βœ… Environment Variables: Parsing and using PATH variable

πŸ“ Notes

  • This project follows 42 School Norminette coding standards
  • All code is commented and documented for evaluation purposes
  • Memory leaks are prevented through proper cleanup
  • Error handling matches shell behavior

🀝 Contributing

This is a 42 School project. If you're a 42 student working on this project:

  1. Don't copy this code directly - You need to understand and write it yourself
  2. Use this as a reference - Study the concepts and implement your own solution
  3. Learn from the explanations - The documentation helps understand the concepts

πŸ“„ License

This project is part of the 42 School curriculum. Educational use only.


πŸ‘€ Author

Odeij Amaleddine


⭐ If you found this project helpful, consider giving it a star! ⭐

Made with ❀️ at 42 School

About

42 project to handle pipes

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

0