This source file includes following definitions.
- main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20
21
22
23 #define _GL_NO_LARGE_FILES
24 #include <stdio.h>
25
26 #include "signature.h"
27 SIGNATURE_CHECK (fflush, int, (FILE *));
28
29 #include <errno.h>
30 #include <unistd.h>
31
32 #include "macros.h"
33
34 int
35 main (void)
36 {
37 FILE *f;
38 char buffer[10];
39 int fd;
40
41
42 f = fopen ("test-fflush.txt", "w");
43 if (!f || fwrite ("1234567890ABCDEFG", 1, 17, f) != 17 || fclose (f) != 0)
44 {
45 fputs ("Failed to create sample file.\n", stderr);
46 unlink ("test-fflush.txt");
47 return 1;
48 }
49
50
51 f = fopen ("test-fflush.txt", "r");
52 ASSERT (f != NULL);
53 fd = fileno (f);
54 if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
55 {
56 fputs ("Failed initial read of sample file.\n", stderr);
57 if (f)
58 fclose (f);
59 unlink ("test-fflush.txt");
60 return 1;
61 }
62
63
64 #if !(defined __BEOS__ || defined __UCLIBC__)
65 if (lseek (fd, 0, SEEK_CUR) == 5)
66 {
67 fputs ("Sample file was not buffered after fread.\n", stderr);
68 fclose (f);
69 unlink ("test-fflush.txt");
70 return 1;
71 }
72 #endif
73
74 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
75 {
76 fputs ("Failed to flush-fseek sample file.\n", stderr);
77 fclose (f);
78 unlink ("test-fflush.txt");
79 return 1;
80 }
81
82 if (lseek (fd, 0, SEEK_CUR) != 5)
83 {
84 fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
85 (long) lseek (fd, 0, SEEK_CUR));
86 fclose (f);
87 unlink ("test-fflush.txt");
88 return 1;
89 }
90 if (ftell (f) != 5)
91 {
92 fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
93 (long) ftell (f));
94 fclose (f);
95 unlink ("test-fflush.txt");
96 return 1;
97 }
98
99 if (fgetc (f) != '6')
100 {
101 fputs ("Failed to read next byte after fseek.\n", stderr);
102 fclose (f);
103 unlink ("test-fflush.txt");
104 return 1;
105 }
106
107 if (lseek (fd, 0, SEEK_CUR) == 6)
108 {
109 fputs ("Sample file was not buffered after fgetc.\n", stderr);
110 fclose (f);
111 unlink ("test-fflush.txt");
112 return 1;
113 }
114
115 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
116 {
117 fputs ("Failed to flush-fseeko sample file.\n", stderr);
118 fclose (f);
119 unlink ("test-fflush.txt");
120 return 1;
121 }
122
123 if (lseek (fd, 0, SEEK_CUR) != 6)
124 {
125 fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
126 (long) lseek (fd, 0, SEEK_CUR));
127 fclose (f);
128 unlink ("test-fflush.txt");
129 return 1;
130 }
131 if (ftell (f) != 6)
132 {
133 fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
134 (long) ftell (f));
135 fclose (f);
136 unlink ("test-fflush.txt");
137 return 1;
138 }
139
140 if (fgetc (f) != '7')
141 {
142 fputs ("Failed to read next byte after fseeko.\n", stderr);
143 fclose (f);
144 unlink ("test-fflush.txt");
145 return 1;
146 }
147 fclose (f);
148
149
150
151 {
152 FILE *fp = fopen ("test-fflush.txt", "w");
153 ASSERT (fp != NULL);
154 fputc ('x', fp);
155 ASSERT (close (fileno (fp)) == 0);
156 errno = 0;
157 ASSERT (fflush (fp) == EOF);
158 ASSERT (errno == EBADF);
159 fclose (fp);
160 }
161
162
163
164 {
165 FILE *fp = fdopen (-1, "w");
166 if (fp != NULL)
167 {
168 fputc ('x', fp);
169 errno = 0;
170 ASSERT (fflush (fp) == EOF);
171 ASSERT (errno == EBADF);
172 }
173 }
174 {
175 FILE *fp;
176 close (99);
177 fp = fdopen (99, "w");
178 if (fp != NULL)
179 {
180 fputc ('x', fp);
181 errno = 0;
182 ASSERT (fflush (fp) == EOF);
183 ASSERT (errno == EBADF);
184 }
185 }
186
187
188 unlink ("test-fflush.txt");
189
190 return 0;
191 }