In the bonus part our objective is to handle multiple pipes and support redirection with here_doc:
Handling Multiple pipes:
Supporting Here Document redirection:
How to achieve this (code side):
Handling Multiple Pipes:
process
function is responsible for creating a child process, setting up a pipe, and executing a command in that child process. This function is called for each command (cmd1
, cmd2
, ..., cmdn
) except the last one.process
:
pipe
function.fork
function. If the process is the child (pid == 0
), it sets up the pipe for output (STDOUT_FILENO
) and executes the command using the execute
function.STDIN_FILENO
) and waits for the child process to finish executing.main
) iterates through the command arguments and calls process
for each command except the last one. The output of each command is piped to the input of the next command.Supporting Here Document Redirection:
here_doc
function is responsible for reading input until a limiter word is encountered and then writing this input to a pipe.here_doc
function:
pipe
function.fork
function. If the process is the child, it reads input until the limiter word is encountered using get_next_line
and writes this input to the write side of pipe.STDIN_FILENO
).main
), if the first argument is "here_doc", it calls the here_doc
function to handle the input redirection.then after finishing we’ll see that the last command doesnt need to pipe so we dont include it for the previous process and we first redirect the output_file to stdout and run our last command with the output in file_out.