CS计算机代考程序代写 CSC209H Worksheet: Redirecting file descriptors

CSC209H Worksheet: Redirecting file descriptors
We most often see dup2 used together with pipes or when we are about to call exec and we want to specify where the new program will read from or write to. In this worksheet, we will focus on a simple example of the latter.
The program takes two arguments. The first argument is a program to execute that will read from stdin. The second argument is a file from which to redirect input.
Example: ./redir wc redir.c
This will execute the wc program by redirecting input from redir.c.
int main(int argc, char **argv) {
// Expecting 2 argments:
// The first is a program to execute
// The second is the name of a file to redirect input from
// Reset stdin so that it is coming from the open file.
// Use exec to run the program specified in argv[1]
return 1; // If we get here it is an error
}