Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Pipex is a C program that mimics the behavior of Unix pipes. It allows you to connect two commands by redirecting the output of the first command to the input of the second command, reading from an input file and writing to an output file.

triangle-motelti/pipe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Pipex - Unix Pipe Implementation in C

Table of Contents

  1. Introduction
  2. Installation
  3. Usage
  4. How It Works
  5. Features
  6. Project Structure

Introduction

Pipex is a C program that simulates the behavior of Unix pipes (|), allowing you to chain commands together by redirecting the output of one command to the input of another. This implementation mimics the functionality of commands like:

$ < input_file cmd1 | cmd2 > output_file

The program takes exactly four arguments:

  1. Input file
  2. First command
  3. Second command
  4. Output file

Installation

  1. Clone the repository:
git clone https://github.com/<your-username>/pipex.git
cd pipex
  1. Compile the program:
make

The executable pipex will be created in the root directory.

Usage

./pipex input_file "command1" "command2" output_file

Example

./pipex input.txt "grep hello" "wc -l" output.txt

This is equivalent to:

< input.txt grep hello | wc -l > output.txt

How It Works

Program Flow

  1. Validate argument count (exactly 4 arguments + program name)
  2. Open input and output files
  3. Create a pipe and fork two child processes:
    • First child:
      • Redirects STDIN from input file
      • Redirects STDOUT to pipe's write end
      • Executes first command
    • Second child:
      • Redirects STDIN from pipe's read end
      • Redirects STDOUT to output file
      • Executes second command
  4. Parent process waits for both children to complete

Key Components

  • Path Resolution: Searches for commands using the PATH environment variable
  • Error Handling: Comprehensive error messages and proper cleanup
  • Memory Management: Custom allocators with automatic cleanup
  • Process Management: Uses fork(), pipe(), and execve() for command execution

Features

  • 🛠️ Implements Unix-like pipe functionality
  • 🔍 Searches commands in PATH environment variable
  • 🚦 Comprehensive error handling
  • 💾 Memory-safe with proper cleanup
  • 📁 Handles file input/output redirection
  • ⚡ Supports absolute path commands
  • � Graceful exit with proper status codes

Project Structure

pipex/
├── Makefile           # Compilation rules
├── pipex              # Compiled executable
├── pipex.h            # Header file
├── pipex.c            # Main program logic
├── utils/             # Utility functions
│   ├── ft_split.c     # String splitting
│   ├── ft_strjoin.c   # String concatenation
│   ├── ft_strnstr.c   # Substring search
│   ├── path_utils.c   # PATH resolution
│   ├── error_utils.c  # Error handling
│   └── ...            # Other helper functions
└── libft/             # Custom library functions
    ├── ft_calloc.c
    ├── ft_strdup.c
    ├── ft_strncmp.c
    └── ...

About

Pipex is a C program that mimics the behavior of Unix pipes. It allows you to connect two commands by redirecting the output of the first command to the input of the second command, reading from an input file and writing to an output file.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published