the concept of the project is to mimic the behavior of the pipe in the shell, the project consist that this output “ ./pipex file1 cmd1 cmd2 file2 ” behave as “< file1 cmd1 | cmd2 > file2 ”.
So how do we achieve this : first we must understand whats happening in this command " < file1 cmd1 | cmd2 > file2 " : briefly, we read from the file1 run the first command , then run the second command on the output of the first one and write this final output in the file2 . To achieve this :
it will open the file1 then we'll use the dup2() function that change the file descriptor for a file to a new one we specify , so we change the file descriptor of the stdin to the fd of the file1 ( we we'll use the file1 as our stdin) and we will change the fd of stdout to the fd[1] (write side of the pipe) , then we run our command by first splitting our cmnd1 by spaces so we could get the command without its options then we find the path of binary files in env variable and we check if there's a binary file for that command in each path we have if we found it we run it with the function execve(), here we have the output of the command1 and the child process is finished bcs execve() does so.
For the parent process which is the running of the command2 we do the same thing with some differneces as we open the file2 we use dup2 to make it as stdout and we make fd[0] as stdin.