1 /* Test of POSIX compatible fflush() function.
2 Copyright (C) 2007, 2009-2016 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake, 2007. */
21 /* None of the files accessed by this test are large, so disable the
22 ftell link warning if we are not using the gnulib ftell module. */
23 #define _GL_NO_LARGE_FILES
26 #include "signature.h"
27 SIGNATURE_CHECK (fflush, int, (FILE *));
41 /* Create test file. */
42 f = fopen ("test-fflush.txt", "w");
43 if (!f || fwrite ("1234567890ABCDEFG", 1, 17, f) != 17 || fclose (f) != 0)
45 fputs ("Failed to create sample file.\n", stderr);
46 unlink ("test-fflush.txt");
51 f = fopen ("test-fflush.txt", "r");
54 if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
56 fputs ("Failed initial read of sample file.\n", stderr);
59 unlink ("test-fflush.txt");
62 /* For deterministic results, ensure f read a bigger buffer.
63 This is not the case on BeOS, nor on uClibc. */
64 #if !(defined __BEOS__ || defined __UCLIBC__)
65 if (lseek (fd, 0, SEEK_CUR) == 5)
67 fputs ("Sample file was not buffered after fread.\n", stderr);
69 unlink ("test-fflush.txt");
73 /* POSIX requires fflush-fseek to set file offset of fd. */
74 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
76 fputs ("Failed to flush-fseek sample file.\n", stderr);
78 unlink ("test-fflush.txt");
81 /* Check that offset is correct. */
82 if (lseek (fd, 0, SEEK_CUR) != 5)
84 fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
85 (long) lseek (fd, 0, SEEK_CUR));
87 unlink ("test-fflush.txt");
92 fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
95 unlink ("test-fflush.txt");
98 /* Check that file reading resumes at correct location. */
101 fputs ("Failed to read next byte after fseek.\n", stderr);
103 unlink ("test-fflush.txt");
106 /* For deterministic results, ensure f read a bigger buffer. */
107 if (lseek (fd, 0, SEEK_CUR) == 6)
109 fputs ("Sample file was not buffered after fgetc.\n", stderr);
111 unlink ("test-fflush.txt");
114 /* POSIX requires fflush-fseeko to set file offset of fd. */
115 if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
117 fputs ("Failed to flush-fseeko sample file.\n", stderr);
119 unlink ("test-fflush.txt");
122 /* Check that offset is correct. */
123 if (lseek (fd, 0, SEEK_CUR) != 6)
125 fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
126 (long) lseek (fd, 0, SEEK_CUR));
128 unlink ("test-fflush.txt");
133 fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
136 unlink ("test-fflush.txt");
139 /* Check that file reading resumes at correct location. */
140 if (fgetc (f) != '7')
142 fputs ("Failed to read next byte after fseeko.\n", stderr);
144 unlink ("test-fflush.txt");
149 /* Test that fflush() sets errno if someone else closes the stream
150 fd behind the back of stdio. */
152 FILE *fp = fopen ("test-fflush.txt", "w");
155 ASSERT (close (fileno (fp)) == 0);
157 ASSERT (fflush (fp) == EOF);
158 ASSERT (errno == EBADF);
162 /* Test that fflush() sets errno if the stream was constructed with
163 an invalid file descriptor. */
165 FILE *fp = fdopen (-1, "w");
170 ASSERT (fflush (fp) == EOF);
171 ASSERT (errno == EBADF);
177 fp = fdopen (99, "w");
182 ASSERT (fflush (fp) == EOF);
183 ASSERT (errno == EBADF);
188 unlink ("test-fflush.txt");