1 /* Tests that stdin can be redirected from a normal file.  */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 int
main(void)7 main (void)
8 {
9    const char* fname = "freopen.dat";
10    const char tsttxt[]
11        = "A random line of text, used to test correct freopen etc.\n";
12    FILE* instream;
13    FILE *old_stderr;
14    char c1;
15 
16    /* Like the freopen call in flex.  */
17    old_stderr = freopen (fname, "w+", stderr);
18    if (old_stderr == NULL
19       || fwrite (tsttxt, 1, strlen (tsttxt), stderr) != strlen (tsttxt)
20       || fclose (stderr) != 0)
21    {
22       printf ("fail\n");
23       exit (1);
24    }
25 
26    instream = freopen(fname, "r", stdin);
27    if (instream == NULL) {
28       printf("fail\n");
29       exit(1);
30    }
31 
32    c1 = getc(instream);
33    if (c1 != 'A') {
34       printf("fail\n");
35       exit(1);
36    }
37 
38    printf ("pass\n");
39    exit(0);
40 }
41