From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/redir.c | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/redir.c (limited to 'reference/C/CONTRIB/SNIP/redir.c') diff --git a/reference/C/CONTRIB/SNIP/redir.c b/reference/C/CONTRIB/SNIP/redir.c new file mode 100755 index 0000000..5d04404 --- /dev/null +++ b/reference/C/CONTRIB/SNIP/redir.c @@ -0,0 +1,59 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + + +Program: REDIR.C +Author: F. PIETTE (2:293/2201.135) +Object: Demonstration of the output redirection +Creation: Augustus 2, 1991 +Updates: + + + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ +#include +#include +#include +#include +#include +#include + +void main(void) +{ + int old_fh; + int new_fh; + + fprintf(stdout, "This goes to the original standard output\n"); + + /* Duplicate the stdout file handle to restore it later */ + old_fh = dup(fileno(stdout)); + if (old_fh == -1) { + fprintf(stderr, "dup error\n"); + exit(1); + } + + /* Open the new file for output */ + if ((new_fh = open("redir.txt", O_CREAT | O_TRUNC | O_WRONLY, + S_IREAD | S_IWRITE)) == -1) { + fprintf(stderr, "Unable to open redir.txt\n"); + exit(1); + } + /* Duplicate the new handle to stdout */ + dup2(new_fh, fileno(stdout)); + /* We don't need new_fh any more, so close it */ + close(new_fh); + + /* stdout is now redirected, let's try it */ + fprintf(stdout, "This goes to redir.txt file !\n"); + + /* If you run a program using spawn(), the child program will have */ + /* its output redirected to REDIR.TXT file ! */ + + /* Now let's restore stdout to its original state */ + fflush(stdout); /* First flush the outut buffer */ + /* Then duplicate the original file handle to stdout */ + dup2(old_fh, fileno(stdout)); + + /* Let's try if we canceled the redirection */ + fprintf(stdout, "Back to original stdout\n"); + + exit(0); +} -- cgit v1.2.3-54-g00ecf