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