summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/redir.c
blob: 5d0440423fd8d63bc02de73c4fb883db772a4388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Program: REDIR.C
Author:  F. PIETTE (2:293/2201.135)
Object:  Demonstration of the output redirection
Creation: Augustus 2, 1991
Updates:


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

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);
}