2005-02-25 Roland McGrath <roland@redhat.com>
[glibc.git] / libio / tst-ext.c
blobf69fbe6f21cd91ca311de5000b199be642646b4f
1 #include <stdio_ext.h>
2 #include <stdlib.h>
3 #include <string.h>
6 int
7 main (void)
9 FILE *fp;
10 const char teststring[] = "hello world";
11 char buf[3072];
12 int result = 0;
13 char readbuf[256];
15 /* Open a file. */
16 fp = tmpfile ();
18 /* Set a buffer. */
19 if (setvbuf (fp, buf, _IOFBF, sizeof buf) == EOF)
21 printf ("setvbuf failed: %m\n");
22 exit (1);
25 /* Get the buffer size. */
26 if (__fbufsize (fp) != sizeof buf)
28 printf ("__fbusize() reported a buffer size of %Zd bytes;"
29 " we installed a buffer with %Zd bytes\n",
30 __fbufsize (fp), sizeof buf);
31 result = 1;
34 /* Write something and read it back. */
35 if (fputs (teststring, fp) == EOF)
37 printf ("writing to new stream failed: %m\n");
38 exit (1);
40 rewind (fp);
41 if (fgets (readbuf, sizeof readbuf, fp) == NULL)
43 printf ("reading from new stream failed: %m\n");
44 exit (1);
46 if (strcmp (readbuf, teststring) != 0)
48 puts ("not the correct string read");
49 exit (1);
52 /* The file must be opened for reading and writing. */
53 if (__freading (fp) == 0)
55 puts ("__freading() reported stream is not last read from");
56 result = 1;
58 if (__fwriting (fp) != 0)
60 puts ("__fwriting() reported stream is write-only or last written to");
61 result = 1;
63 rewind (fp);
64 if (fputs (teststring, fp) == EOF)
66 printf ("writing(2) to new stream failed: %m\n");
67 exit (1);
69 if (__fwriting (fp) == 0)
71 puts ("__fwriting() doe snot reported stream is last written to");
72 result = 1;
74 if (__freading (fp) != 0)
76 puts ("__freading() reported stream is last read from");
77 result = 1;
80 if (__freadable (fp) == 0)
82 puts ("__freading() reported stream is last readable");
83 result = 1;
85 if (__fwritable (fp) == 0)
87 puts ("__freading() reported stream is last writable");
88 result = 1;
91 /* The string we wrote above should still be in the buffer. */
92 if (__fpending (fp) != strlen (teststring))
94 printf ("__fpending() returned %Zd; expected %Zd\n",
95 __fpending (fp), strlen (teststring));
96 result = 1;
98 /* Discard all the output. */
99 __fpurge (fp);
100 /* And check again. */
101 if (__fpending (fp) != 0)
103 printf ("__fpending() returned %Zd; expected 0\n",
104 __fpending (fp));
105 result = 1;
109 /* Find out whether buffer is line buffered. */
110 if (__flbf (fp) != 0)
112 puts ("__flbf() reports line buffered but it is fully buffered");
113 result = 1;
116 if (setvbuf (fp, buf, _IOLBF, sizeof buf) == EOF)
118 printf ("setvbuf(2) failed: %m\n");
119 exit (1);
121 if (__flbf (fp) == 0)
123 puts ("__flbf() reports file is not line buffered");
124 result = 1;
127 if (setvbuf (fp, NULL, _IONBF, 0) == EOF)
129 printf ("setvbuf(3) failed: %m\n");
130 exit (1);
132 if (__flbf (fp) != 0)
134 puts ("__flbf() reports line buffered but it is not buffered");
135 result = 1;
138 fclose (fp);
140 return result;