Change copyright notice from GPLv2+ to GPLv3+.
[gnulib.git] / tests / test-fflush.c
1 /* Test of POSIX compatible fflush() function.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Eric Blake, 2007.  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <unistd.h>
23
24 int
25 main (int argc, char *argv[])
26 {
27   FILE *f;
28   char buffer[10];
29   int fd;
30
31   /* Create test file.  */
32   f = fopen ("test-fflush.txt", "w");
33   if (!f || fwrite ("1234567890ABCDEFG", 1, 17, f) != 17 || fclose (f) != 0)
34     {
35       fputs ("Failed to create sample file.\n", stderr);
36       unlink ("test-fflush.txt");
37       return 1;
38     }
39
40   /* Test fflush.  */
41   f = fopen ("test-fflush.txt", "r");
42   fd = fileno (f);
43   if (!f || 0 > fd || fread (buffer, 1, 5, f) != 5)
44     {
45       fputs ("Failed initial read of sample file.\n", stderr);
46       fclose (f);
47       unlink ("test-fflush.txt");
48       return 1;
49     }
50   /* For deterministic results, ensure f read a bigger buffer.
51      This is not the case on BeOS, nor on uClibc.  */
52 #if !(defined __BEOS__ || defined __UCLIBC__)
53   if (lseek (fd, 0, SEEK_CUR) == 5)
54     {
55       fputs ("Sample file was not buffered after fread.\n", stderr);
56       fclose (f);
57       unlink ("test-fflush.txt");
58       return 1;
59     }
60 #endif
61   /* POSIX requires fflush-fseek to set file offset of fd.  */
62   if (fflush (f) != 0 || fseek (f, 0, SEEK_CUR) != 0)
63     {
64       fputs ("Failed to flush-fseek sample file.\n", stderr);
65       fclose (f);
66       unlink ("test-fflush.txt");
67       return 1;
68     }
69   /* Check that offset is correct.  */
70   if (lseek (fd, 0, SEEK_CUR) != 5)
71     {
72       fprintf (stderr, "File offset is wrong after fseek: %ld.\n",
73                (long) lseek (fd, 0, SEEK_CUR));
74       fclose (f);
75       unlink ("test-fflush.txt");
76       return 1;
77     }
78   if (ftell (f) != 5)
79     {
80       fprintf (stderr, "ftell result is wrong after fseek: %ld.\n",
81                (long) ftell (f));
82       fclose (f);
83       unlink ("test-fflush.txt");
84       return 1;
85     }
86   /* Check that file reading resumes at correct location.  */
87   if (fgetc (f) != '6')
88     {
89       fputs ("Failed to read next byte after fseek.\n", stderr);
90       fclose (f);
91       unlink ("test-fflush.txt");
92       return 1;
93     }
94   /* For deterministic results, ensure f read a bigger buffer.  */
95   if (lseek (fd, 0, SEEK_CUR) == 6)
96     {
97       fputs ("Sample file was not buffered after fgetc.\n", stderr);
98       fclose (f);
99       unlink ("test-fflush.txt");
100       return 1;
101     }
102   /* POSIX requires fflush-fseeko to set file offset of fd.  */
103   if (fflush (f) != 0 || fseeko (f, 0, SEEK_CUR) != 0)
104     {
105       fputs ("Failed to flush-fseeko sample file.\n", stderr);
106       fclose (f);
107       unlink ("test-fflush.txt");
108       return 1;
109     }
110   /* Check that offset is correct.  */
111   if (lseek (fd, 0, SEEK_CUR) != 6)
112     {
113       fprintf (stderr, "File offset is wrong after fseeko: %ld.\n",
114                (long) lseek (fd, 0, SEEK_CUR));
115       fclose (f);
116       unlink ("test-fflush.txt");
117       return 1;
118     }
119   if (ftell (f) != 6)
120     {
121       fprintf (stderr, "ftell result is wrong after fseeko: %ld.\n",
122                (long) ftell (f));
123       fclose (f);
124       unlink ("test-fflush.txt");
125       return 1;
126     }
127   /* Check that file reading resumes at correct location.  */
128   if (fgetc (f) != '7')
129     {
130       fputs ("Failed to read next byte after fseeko.\n", stderr);
131       fclose (f);
132       unlink ("test-fflush.txt");
133       return 1;
134     }
135   fclose (f);
136   unlink ("test-fflush.txt");
137   return 0;
138 }