1 /* Check that basic freopen functionality works.  */
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 
7 int
main(void)8 main (void)
9 {
10   FILE *old_stderr;
11   FILE *f;
12   const char fname[] = "sk1test.dat";
13   const char tsttxt[]
14     = "A random line of text, used to test correct freopen etc.\n";
15   char buf[sizeof tsttxt] = "";
16 
17   /* Like the freopen call in flex.  */
18   old_stderr = freopen (fname, "w+", stderr);
19   if (old_stderr == NULL
20       || fwrite (tsttxt, 1, strlen (tsttxt), stderr) != strlen (tsttxt)
21       || fclose (stderr) != 0)
22     {
23       printf ("fail\n");
24       exit (1);
25     }
26 
27   /* Using "rb" to make this test similar to the use in genconf.c in
28      GhostScript.  */
29   f = fopen (fname, "rb");
30   if (f == NULL
31       || fseek (f, 0L, SEEK_END) != 0
32       || ftell (f) != strlen (tsttxt))
33     {
34       printf ("fail\n");
35       exit (1);
36     }
37 
38   rewind (f);
39   if (fread (buf, 1, strlen (tsttxt), f) != strlen (tsttxt)
40       || strcmp (buf, tsttxt) != 0
41       || fclose (f) != 0)
42     {
43       printf ("fail\n");
44       exit (1);
45     }
46 
47   printf ("pass\n");
48   exit (0);
49 }
50