1 /* Test of freadable() function.
2 Copyright (C) 2007-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 Bruno Haible <bruno@clisp.org>, 2007. */
21 /* None of the files accessed by this test are large, so disable the
22 fseek link warning if we are not using the gnulib fseek module. */
23 #define _GL_NO_LARGE_FILES
24 #include "freadable.h"
30 #define TESTFILE "t-freadable.tmp"
37 /* Create a file with some contents. */
38 fp
= fopen (TESTFILE
, "w");
41 ASSERT (!freadable (fp
));
42 if (fwrite ("foobarsh", 1, 8, fp
) < 8)
44 ASSERT (!freadable (fp
));
48 /* Open it in read-only mode. */
49 fp
= fopen (TESTFILE
, "r");
52 ASSERT (freadable (fp
));
53 if (fgetc (fp
) != 'f')
55 ASSERT (freadable (fp
));
56 if (fseek (fp
, 2, SEEK_CUR
))
58 ASSERT (freadable (fp
));
59 if (fgetc (fp
) != 'b')
61 ASSERT (freadable (fp
));
63 ASSERT (freadable (fp
));
64 if (fgetc (fp
) != 'a')
66 ASSERT (freadable (fp
));
67 if (fseek (fp
, 0, SEEK_END
))
69 ASSERT (freadable (fp
));
73 /* Open it in read-write mode. */
74 fp
= fopen (TESTFILE
, "r+");
77 ASSERT (freadable (fp
));
78 if (fgetc (fp
) != 'f')
80 ASSERT (freadable (fp
));
81 if (fseek (fp
, 2, SEEK_CUR
))
83 ASSERT (freadable (fp
));
84 if (fgetc (fp
) != 'b')
86 ASSERT (freadable (fp
));
88 ASSERT (freadable (fp
));
89 if (fgetc (fp
) != 'a')
91 ASSERT (freadable (fp
));
92 if (fputc ('z', fp
) != 'z')
94 ASSERT (freadable (fp
));
95 if (fseek (fp
, 0, SEEK_END
))
97 ASSERT (freadable (fp
));
101 /* Open it in append mode. */
102 fp
= fopen (TESTFILE
, "a");
105 ASSERT (!freadable (fp
));
106 if (fwrite ("bla", 1, 3, fp
) < 3)
108 ASSERT (!freadable (fp
));
112 return test_exit_status
;
115 if (test_exit_status
!= EXIT_SUCCESS
)
116 return test_exit_status
;
117 fprintf (stderr
, "Skipping test: file operations failed.\n");