]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/sanitizer_common/TestCases/Posix/feof_fileno_ferror.cc
Vendor import of compiler-rt trunk r351319 (just before the release_80
[FreeBSD/FreeBSD.git] / test / sanitizer_common / TestCases / Posix / feof_fileno_ferror.cc
1 // RUN: %clangxx -g %s -o %t && %run %t
2
3 #include <assert.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 int main(int argc, char **argv) {
8   FILE *fp = fopen(argv[0], "r");
9   assert(fp);
10
11   // file should be good upon opening
12   assert(!feof(fp) && !ferror(fp));
13
14   // read until EOF
15   char buf[BUFSIZ];
16   while (fread(buf, 1, sizeof buf, fp) != 0) {}
17   assert(feof(fp));
18
19   // clear EOF
20   clearerr(fp);
21   assert(!feof(fp) && !ferror(fp));
22
23   // get file descriptor
24   int fd = fileno(fp);
25   assert(fd != -1);
26
27   // break the file by closing underlying descriptor
28   assert(close(fd) != -1);
29
30   // verify that an error is signalled
31   assert(fread(buf, 1, sizeof buf, fp) == 0);
32   assert(ferror(fp));
33
34   // clear error
35   clearerr(fp);
36   assert(!feof(fp) && !ferror(fp));
37
38   // fclose() will return EBADF because of closed fd
39   assert(fclose(fp) == -1);
40   return 0;
41 }