The command "cat > test.txt" will echo terminal input into a text file instead of stdout.
I/O redirection is implemented using the dup2 function. dup2 copies one file descriptor to another in the file descriptor table. To realize redirection in the cat command above, first open test.txt which result in the creation of a new file descriptor in the table. Then use dup2 to copy this new entry to file descriptor 2 (stdout). After the call, stdout is now pointing to test.txt. When cat writes to stdout, the data will be written to test.txt.
No comments:
Post a Comment