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 (stream: fp, buf: buf, _IOFBF, n: sizeof buf) == EOF) |
20 | { |
21 | printf (format: "setvbuf failed: %m\n" ); |
22 | exit (1); |
23 | } |
24 | |
25 | /* Get the buffer size. */ |
26 | if (__fbufsize (fp: fp) != sizeof buf) |
27 | { |
28 | printf (format: "__fbusize() reported a buffer size of %Zd bytes;" |
29 | " we installed a buffer with %Zd bytes\n" , |
30 | __fbufsize (fp: fp), sizeof buf); |
31 | result = 1; |
32 | } |
33 | |
34 | /* Write something and read it back. */ |
35 | if (fputs (teststring, fp) == EOF) |
36 | { |
37 | printf (format: "writing to new stream failed: %m\n" ); |
38 | exit (1); |
39 | } |
40 | rewind (fp); |
41 | if (fgets (s: readbuf, n: sizeof readbuf, stream: fp) == NULL) |
42 | { |
43 | printf (format: "reading from new stream failed: %m\n" ); |
44 | exit (1); |
45 | } |
46 | if (strcmp (readbuf, teststring) != 0) |
47 | { |
48 | puts (s: "not the correct string read" ); |
49 | exit (1); |
50 | } |
51 | |
52 | /* The file must be opened for reading and writing. */ |
53 | if (__freading (fp: fp) == 0) |
54 | { |
55 | puts (s: "__freading() reported stream is not last read from" ); |
56 | result = 1; |
57 | } |
58 | if (__fwriting (fp: fp) != 0) |
59 | { |
60 | puts (s: "__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 (format: "writing(2) to new stream failed: %m\n" ); |
67 | exit (1); |
68 | } |
69 | if (__fwriting (fp: fp) == 0) |
70 | { |
71 | puts (s: "__fwriting() doe snot reported stream is last written to" ); |
72 | result = 1; |
73 | } |
74 | if (__freading (fp: fp) != 0) |
75 | { |
76 | puts (s: "__freading() reported stream is last read from" ); |
77 | result = 1; |
78 | } |
79 | |
80 | if (__freadable (fp: fp) == 0) |
81 | { |
82 | puts (s: "__freading() reported stream is last readable" ); |
83 | result = 1; |
84 | } |
85 | if (__fwritable (fp: fp) == 0) |
86 | { |
87 | puts (s: "__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: fp) != strlen (teststring)) |
93 | { |
94 | printf (format: "__fpending() returned %Zd; expected %Zd\n" , |
95 | __fpending (fp: fp), strlen (teststring)); |
96 | result = 1; |
97 | } |
98 | /* Discard all the output. */ |
99 | __fpurge (fp: fp); |
100 | /* And check again. */ |
101 | if (__fpending (fp: fp) != 0) |
102 | { |
103 | printf (format: "__fpending() returned %Zd; expected 0\n" , |
104 | __fpending (fp: fp)); |
105 | result = 1; |
106 | } |
107 | |
108 | |
109 | /* Find out whether buffer is line buffered. */ |
110 | if (__flbf (fp: fp) != 0) |
111 | { |
112 | puts (s: "__flbf() reports line buffered but it is fully buffered" ); |
113 | result = 1; |
114 | } |
115 | |
116 | if (setvbuf (stream: fp, buf: buf, _IOLBF, n: sizeof buf) == EOF) |
117 | { |
118 | printf (format: "setvbuf(2) failed: %m\n" ); |
119 | exit (1); |
120 | } |
121 | if (__flbf (fp: fp) == 0) |
122 | { |
123 | puts (s: "__flbf() reports file is not line buffered" ); |
124 | result = 1; |
125 | } |
126 | |
127 | if (setvbuf (stream: fp, NULL, _IONBF, n: 0) == EOF) |
128 | { |
129 | printf (format: "setvbuf(3) failed: %m\n" ); |
130 | exit (1); |
131 | } |
132 | if (__flbf (fp: fp) != 0) |
133 | { |
134 | puts (s: "__flbf() reports line buffered but it is not buffered" ); |
135 | result = 1; |
136 | } |
137 | |
138 | fclose (fp); |
139 | |
140 | return result; |
141 | } |
142 | |