update nearly all FSF copyright year lists to include 2010
[gnulib.git] / tests / test-freadable.c
1 /* Test of freadable() function.
2    Copyright (C) 2007-2010 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 Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include "freadable.h"
22
23 #include <stdio.h>
24
25 #include "macros.h"
26
27 /* None of the files accessed by this test are large, so disable the
28    fseek link warning if we are not using the gnulib fseek module.  */
29 #if !GNULIB_FSEEK
30 # undef fseek
31 #endif
32
33 #define TESTFILE "t-freadable.tmp"
34
35 int
36 main ()
37 {
38   FILE *fp;
39
40   /* Create a file with some contents.  */
41   fp = fopen (TESTFILE, "w");
42   if (fp == NULL)
43     goto skip;
44   ASSERT (!freadable (fp));
45   if (fwrite ("foobarsh", 1, 8, fp) < 8)
46     goto skip;
47   ASSERT (!freadable (fp));
48   if (fclose (fp))
49     goto skip;
50
51   /* Open it in read-only mode.  */
52   fp = fopen (TESTFILE, "r");
53   if (fp == NULL)
54     goto skip;
55   ASSERT (freadable (fp));
56   if (fgetc (fp) != 'f')
57     goto skip;
58   ASSERT (freadable (fp));
59   if (fseek (fp, 2, SEEK_CUR))
60     goto skip;
61   ASSERT (freadable (fp));
62   if (fgetc (fp) != 'b')
63     goto skip;
64   ASSERT (freadable (fp));
65   fflush (fp);
66   ASSERT (freadable (fp));
67   if (fgetc (fp) != 'a')
68     goto skip;
69   ASSERT (freadable (fp));
70   if (fseek (fp, 0, SEEK_END))
71     goto skip;
72   ASSERT (freadable (fp));
73   if (fclose (fp))
74     goto skip;
75
76   /* Open it in read-write mode.  */
77   fp = fopen (TESTFILE, "r+");
78   if (fp == NULL)
79     goto skip;
80   ASSERT (freadable (fp));
81   if (fgetc (fp) != 'f')
82     goto skip;
83   ASSERT (freadable (fp));
84   if (fseek (fp, 2, SEEK_CUR))
85     goto skip;
86   ASSERT (freadable (fp));
87   if (fgetc (fp) != 'b')
88     goto skip;
89   ASSERT (freadable (fp));
90   fflush (fp);
91   ASSERT (freadable (fp));
92   if (fgetc (fp) != 'a')
93     goto skip;
94   ASSERT (freadable (fp));
95   if (fputc ('z', fp) != 'z')
96     goto skip;
97   ASSERT (freadable (fp));
98   if (fseek (fp, 0, SEEK_END))
99     goto skip;
100   ASSERT (freadable (fp));
101   if (fclose (fp))
102     goto skip;
103
104   /* Open it in append mode.  */
105   fp = fopen (TESTFILE, "a");
106   if (fp == NULL)
107     goto skip;
108   ASSERT (!freadable (fp));
109   if (fwrite ("bla", 1, 3, fp) < 3)
110     goto skip;
111   ASSERT (!freadable (fp));
112   if (fclose (fp))
113     goto skip;
114
115   return 0;
116
117  skip:
118   fprintf (stderr, "Skipping test: file operations failed.\n");
119   return 77;
120 }