C/C++: As we introduced in class, the four functional characters (<, >, |, &) ar
ID: 3855525 • Letter: C
Question
C/C++:
As we introduced in class, the four functional characters (<, >, |, &) are for input redirection, output redirection, pipe and background jobs respectively. By default, they can be interpreted correctly by a UNIX terminal, but not by a C/C++ program. In a UNIX terminal if you type in “echo hello > file1”, you will find a file named “file1” created which has string “hello” stored inside, and that’s because the terminal interpreted “>” correctly and redirected the output of string “hello” from screen to “file1”.
In our Project 1, we want you to write a C/C++ program to interpret all these four functional characters as a UNIX terminal does. So when your program is running, we expect it to allow the user to type in a command line, and if the user types in “echo hello > file1”, the same thing as in a UNIX terminal should happen. This is only the test for output redirection, and we expect the other three functional characters to be interpreted correctly as well.
Note that function “system” cannot be used in your program because it will do everything for you. For example, “system(“echo hello > file1”);” can give the right result without needing you to do anything. Instead, we want you to use system calls for process management and file systems, such as fork, waitpid, execvp, exit, pipe, dup, open, close, etc. First, your program should read in the user’s command line and store it in an array. Then it checks if any of the four functional characters is in the array and will do corresponding work if so.
Use output redirection as an example, if I input “echo hello > file1”, my C/C++ program should read them in, and store them in an array whose name is “args”, with args[0] having “echo”, args[1] having “hello”, args[2] having “>” and args[3] having “file1”. Then my program checks if any of the four functional characters exits, and yes, “>” is found in args[2]. After this my program can do corresponding work: since “>” is in args[2], the following argument—args[3]—should be the output redirection destination, thus my program creates a file called “file1” (using system call “create”) and redirects standard output to “file1” (using system call “dup2”, there can be multiple solutions). Finally, my program executes the command by calling “execvp(args[0], args);”.
Explanation / Answer
Redirections
$ cmd1 > stdout.txt
The above command redirects stdout to file stdout.txt
For implementing the above operation, we should be able to link stdout of cmd1 with file descriptor of stdout.txt opened with write mode. Let us look at the code.
$ cmd1 2> stdout.txt
The above command redirects stderr to file stdout.txt
$ cmd2 > stdout_stderr.txt 2>&1
The above command redirects both stdout and stderr to file stdout_stderr.txt
$ cmd3 < input.txt
The above command redirects data from input.txt to stdin for cmd3.
Pipe
$ cmd1 | cmd2
This command says that cmd2 will receive stdin from stdout of cmd1.
1 2 3 4 5 6 7 8 9
main(){ close(1); // Release fd no - 1 open("stdout.txt", "w"); // Open a file with fd no = 1 // Child process if (fork() == 0) { exec("cmd1"); // By default, the program writes to stdout (fd no - 1). ie, in this case, the file } } Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.