Skip to content

tigran-sargsyan-w/pipex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pipex

Completed: Mandatory + Bonus
🏅 Score: 125/100

42 Logo

pipex is a project from 42 School that replicates the behavior of Unix pipes and redirections. The project simulates the execution of a command line such as:

< infile "command1" | "command2" > outfile  

and also includes a bonus feature for here_doc functionality.

Table of Contents

Description

The pipex project simulates the piping mechanism found in Unix-like systems. It reads data from an input file, processes it through a series of commands connected by pipes, and writes the final output to an output file. Additionally, the bonus here_doc feature allows the program to accept input from the terminal until a specified delimiter is reached.

Features

  • Pipe Simulation: Emulates Unix pipe behavior to connect multiple commands.
  • Input/Output Redirection: Reads data from a file and writes the processed output to another file.
  • Bonus - Here_doc: Supports here_doc functionality for interactive input until a delimiter is met.
  • Process Management: Uses system calls like fork(), pipe(), and dup2() to manage multiple processes.
  • Robust Error Handling: Displays clear error messages and ensures proper resource cleanup.
  • Libft Integration: Utilizes custom library functions from libft to aid in various operations.

Requirements

  • C Compiler (e.g., gcc)
  • Make for building the project
  • Unix-based Operating System (Linux, macOS)

Installation

  1. Clone the repository:
git clone https://github.com/tigran-sargsyan-w/pipex.git  
  1. Navigate to the project directory:
cd pipex  
  1. Build the project using Make:
make  

This command compiles the project and produces the pipex executable.

Usage

Standard Mode

Run the program with an input file, two (or more) commands, and an output file:

./pipex infile "command1" "command2" outfile  

Example:

./pipex infile "cat" "grep test" "wc -l" outfile  

Bonus Mode - Here_doc

To use the here_doc bonus feature, replace the input file with here_doc and specify a limiter:

./pipex here_doc LIMITER "command1" "command2" outfile  

Example:

./pipex here_doc END "cat" "wc -l" outfile  

In this mode, the program reads from the terminal until the LIMITER word (e.g., END) is encountered.

How It Works

  1. Input Parsing: Validates and parses command-line arguments.
  2. Pipe Creation: Sets up pipes to channel data between commands.
  3. Process Forking: Creates child processes for each command execution.
  4. Command Execution: Executes commands using functions like execve() with proper redirection of file descriptors.
  5. Bonus Handling: Implements here_doc functionality for interactive input.
  6. Error Management: Detects errors, outputs descriptive messages, and cleans up resources appropriately.

Testing

To test pipex, run the provided test script:

./tester.sh  

This script checks:

  • Functional Correctness: Ensures that the pipe simulation and here_doc bonus work as expected.
  • Error Handling: Verifies the program's response to invalid or missing input.
  • Memory Management: Optionally, run with Valgrind to check for memory leaks:
valgrind --leak-check=full ./pipex infile "command1" "command2" outfile  

Custom Testing

If you want to run your own tests, here are some example commands that you can use:

Functional Tests

./pipex infile "cat" "wc -l" outfile
./pipex infile "cat" "grep test" "sort" "uniq" "wc -l" outfile
./pipex here_doc END "cat" "wc -l" outfile  
./pipex here_doc END "cat" "grep a" "wc -l" outfile
./pipex infile "cat" "nonexist test" "wc -l" outfile  

Memory Leak & File Descriptor Tests Using Valgrind

valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes -s -q ./pipex infile "cat" "grep test" "wc -l" outfile
valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes -s -q ./pipex infile "cat" "grep test" "sort" "uniq" "wc -l" outfile
valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes -s -q ./pipex here_doc END "cat" "wc -l" outfile
valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes -s -q ./pipex here_doc END "cat" "grep a" "wc -l" outfile
valgrind --leak-check=full --show-leak-kinds=all --track-fds=yes --trace-children=yes -s -q ./pipex infile "cat" "nonexist test" "wc -l" outfile  

These examples cover both regular functional testing and memory/descriptor checks with Valgrind.

About

This project will let you discover in detail a UNIX mechanism that you already know by using it in your program.

Topics

Resources

Stars

Watchers

Forks

Contributors