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