]>
Commit | Line | Data |
---|---|---|
0a04075e UD |
1 | #include <stdio_ext.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | ||
5 | ||
6 | int | |
7 | main (void) | |
8 | { | |
9 | FILE *fp; | |
10 | const char teststring[] = "hello world"; | |
11 | char buf[3072]; | |
12 | int result = 0; | |
13 | char readbuf[256]; | |
14 | ||
15 | /* Open a file. */ | |
16 | fp = tmpfile (); | |
17 | ||
18 | /* Set a buffer. */ | |
19 | if (setvbuf (fp, buf, _IOFBF, sizeof buf) == EOF) | |
20 | { | |
21 | printf ("setvbuf failed: %m\n"); | |
22 | exit (1); | |
23 | } | |
24 | ||
25 | /* Get the buffer size. */ | |
26 | if (__fbufsize (fp) != sizeof buf) | |
27 | { | |
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; | |
32 | } | |
33 | ||
34 | /* Write something and read it back. */ | |
35 | if (fputs (teststring, fp) == EOF) | |
36 | { | |
37 | printf ("writing to new stream failed: %m\n"); | |
38 | exit (1); | |
39 | } | |
40 | rewind (fp); | |
41 | if (fgets (readbuf, sizeof readbuf, fp) == NULL) | |
42 | { | |
43 | printf ("reading from new stream failed: %m\n"); | |
44 | exit (1); | |
45 | } | |
46 | if (strcmp (readbuf, teststring) != 0) | |
47 | { | |
48 | puts ("not the correct string read"); | |
49 | exit (1); | |
50 | } | |
51 | ||
52 | /* The file must be opened for reading and writing. */ | |
53 | if (__freading (fp) == 0) | |
54 | { | |
55 | puts ("__freading() reported stream is not last read from"); | |
56 | result = 1; | |
57 | } | |
58 | if (__fwriting (fp) != 0) | |
59 | { | |
60 | puts ("__fwriting() reported stream is write-only or last written to"); | |
61 | result = 1; | |
62 | } | |
63 | rewind (fp); | |
64 | if (fputs (teststring, fp) == EOF) | |
65 | { | |
66 | printf ("writing(2) to new stream failed: %m\n"); | |
67 | exit (1); | |
68 | } | |
69 | if (__fwriting (fp) == 0) | |
70 | { | |
71 | puts ("__fwriting() doe snot reported stream is last written to"); | |
72 | result = 1; | |
73 | } | |
74 | if (__freading (fp) != 0) | |
75 | { | |
76 | puts ("__freading() reported stream is last read from"); | |
77 | result = 1; | |
78 | } | |
79 | ||
80 | if (__freadable (fp) == 0) | |
81 | { | |
82 | puts ("__freading() reported stream is last readable"); | |
83 | result = 1; | |
84 | } | |
85 | if (__fwritable (fp) == 0) | |
86 | { | |
87 | puts ("__freading() reported stream is last writable"); | |
88 | result = 1; | |
89 | } | |
90 | ||
91 | /* The string we wrote above should still be in the buffer. */ | |
92 | if (__fpending (fp) != strlen (teststring)) | |
93 | { | |
94 | printf ("__fpending() returned %Zd; expected %Zd\n", | |
95 | __fpending (fp), strlen (teststring)); | |
96 | result = 1; | |
97 | } | |
98 | /* Discard all the output. */ | |
99 | __fpurge (fp); | |
100 | /* And check again. */ | |
101 | if (__fpending (fp) != 0) | |
102 | { | |
103 | printf ("__fpending() returned %Zd; expected 0\n", | |
104 | __fpending (fp)); | |
105 | result = 1; | |
106 | } | |
107 | ||
108 | ||
109 | /* Find out whether buffer is line buffered. */ | |
110 | if (__flbf (fp) != 0) | |
111 | { | |
112 | puts ("__flbf() reports line buffered but it is fully buffered"); | |
113 | result = 1; | |
114 | } | |
115 | ||
116 | if (setvbuf (fp, buf, _IOLBF, sizeof buf) == EOF) | |
117 | { | |
118 | printf ("setvbuf(2) failed: %m\n"); | |
119 | exit (1); | |
120 | } | |
121 | if (__flbf (fp) == 0) | |
122 | { | |
123 | puts ("__flbf() reports file is not line buffered"); | |
124 | result = 1; | |
125 | } | |
126 | ||
127 | if (setvbuf (fp, NULL, _IONBF, 0) == EOF) | |
128 | { | |
129 | printf ("setvbuf(3) failed: %m\n"); | |
130 | exit (1); | |
131 | } | |
132 | if (__flbf (fp) != 0) | |
133 | { | |
134 | puts ("__flbf() reports line buffered but it is not buffered"); | |
135 | result = 1; | |
136 | } | |
137 | ||
138 | fclose (fp); | |
139 | ||
140 | return result; | |
141 | } |