test-framework-sh: Don't leave temporary directories on NetBSD.
[gnulib.git] / tests / test-fflush.c
blob1a7190896371be65fa8003054e324f878a0b603c
1 /* Test of POSIX compatible fflush() function.
2 Copyright (C) 2007, 2009-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Eric Blake, 2007. */
19 #include <config.h>
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
24 #include <stdio.h>
26 #include "signature.h"
27 SIGNATURE_CHECK (fflush, int, (FILE *));
29 #include <errno.h>
30 #include <unistd.h>
32 #include "macros.h"
34 int
35 main (void)
37 FILE *f;
38 char buffer[10];
39 int fd;
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");
47 return 1;
50 /* Test fflush. */
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)
56 fputs ("Failed initial read of sample file.\n", stderr);
57 if (f)
58 fclose (f);
59 unlink ("test-fflush.txt");
60 return 1;
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);
68 fclose (f);
69 unlink ("test-fflush.txt");
70 return 1;
72 #endif
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);
77 fclose (f);
78 unlink ("test-fflush.txt");
79 return 1;
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));
86 fclose (f);
87 unlink ("test-fflush.txt");
88 return 1;
90 if (ftell (f) != 5)
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;
98 /* Check that file reading resumes at correct location. */
99 if (fgetc (f) != '6')
101 fputs ("Failed to read next byte after fseek.\n", stderr);
102 fclose (f);
103 unlink ("test-fflush.txt");
104 return 1;
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);
110 fclose (f);
111 unlink ("test-fflush.txt");
112 return 1;
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);
118 fclose (f);
119 unlink ("test-fflush.txt");
120 return 1;
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));
127 fclose (f);
128 unlink ("test-fflush.txt");
129 return 1;
131 if (ftell (f) != 6)
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;
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);
143 fclose (f);
144 unlink ("test-fflush.txt");
145 return 1;
147 fclose (f);
149 /* Test that fflush() sets errno if someone else closes the stream
150 fd behind the back of stdio. */
151 #if !defined __ANDROID__ /* fdsan */
153 FILE *fp = fopen ("test-fflush.txt", "w");
154 ASSERT (fp != NULL);
155 fputc ('x', fp);
156 ASSERT (close (fileno (fp)) == 0);
157 errno = 0;
158 ASSERT (fflush (fp) == EOF);
159 ASSERT (errno == EBADF);
160 fclose (fp);
162 #endif
164 /* Test that fflush() sets errno if the stream was constructed with
165 an invalid file descriptor. */
167 FILE *fp = fdopen (-1, "w");
168 if (fp != NULL)
170 fputc ('x', fp);
171 errno = 0;
172 ASSERT (fflush (fp) == EOF);
173 ASSERT (errno == EBADF);
177 FILE *fp;
178 close (99);
179 fp = fdopen (99, "w");
180 if (fp != NULL)
182 fputc ('x', fp);
183 errno = 0;
184 ASSERT (fflush (fp) == EOF);
185 ASSERT (errno == EBADF);
189 /* Clean up. */
190 unlink ("test-fflush.txt");
192 return test_exit_status;