I guess it's save to say most of us used the pipe | character in the termial
to perform an operation between two command like cat and wc
cat /etc/passwd | wc -lwhich is a very famous duo we all used many time, but have you ever wondered how
it works, I mean basically the pipe | makes the output of cat to be the input
of wc, we can also redirect the output of wc to file by using > character then
the name of the file like this:
cat /etc/passwd | wc -l > output.txtI asked myself the same question and I decided to make a program to mimic the pipe |
and the redirection > operation.
I found out it's not that simple to implement such a feature to a program, it involves
pipes obviously and I had to work with processes on a system level using fork(), waitpid()
and execve(), but it was a fun project and I have learnt a lot from it.
| Program name | pipex |
|---|---|
| Arguments | file1 cmd1 cmd2 file2 |
| External functs | • open, close, read, write, malloc, free, perror, strerror, access, dup, dup2, execve, exit, fork, pipe, unlink, wait, waitpid • My ft_printf library |
| Libft | My own library |
| Description | This project is about handling pipes. |
Clone the repository in the desired directory.
git clone https://github.com/SolarisCode/pipex.gitThen use make inside the pervious directory either with all or bonus rule if you want to use
as many pipes as you want and also gain the functionality of here_docs combained with append >>
character.
make allor
make bonusThe program will be executed as follows:
./pipex infile "cat -e" "wc -l" outfilejust like the it does in the termial:
< infile cat -e | wc -l outfileAnd if you complied the bonus part you would be able to run it like this:
./pipex file1 cmd1 cmd2 cmd3 ... cmdn file2It will support << and >> when the first parameter is here_doc:
/pipex here_doc LIMITER cmd cmd1 fileJust like in the termial:
cmd << LIMITER | cmd1 >> fileWhile the
LIMITERis the word of your choice to end the input for the standard input.
Feel free to contact me if you have a better way to implement any of these functionalities :)