Question: Write a C program that takes the name of the file and a url and downloads content from the url into the specified file. But
Write a C program that takes the name of the file and a url and downloads content from the url into the specified file. But you will only use system call I/O instead of C File I/O for this program. Name your program redirect.c. However you have to use fork, exec, open and dup system calls to do this. If your program is run like this: ./redirect file1 URL, then
You have to first open the file file1 using open system call. You have to open it for writing or you should create this file if such a file doesnt already exist. In the latter case, the permissions needed to create this file are Read and Write for the user, Read for the group and Read for others.
Now you have to fork a child process(only one process).
In the child, dup the file1 file descriptor to stdout and then exec wget as follows: execl("/usr/bin/wget", "wget", "-qO-", argv[2], (char*) NULL); Now all the content, that the execd program (wget in this case) prints to stdout, will be redirected to the file file1 (because you duped file1 file descriptor to stdout before calling exec).
In your parent process, you just have to wait (using wait or waitpid) and collect your child. Once you do that, you just have to print the exit status that youve received from the collected child. Just like in question 1, make sure to use the necessary macros.
Also you are expected to do error checking for all system calls and other parts where ever necessary. You can use dup or dup2 for redirection. However, please check the order of arguments for dup2.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
