reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
// RUN: %clangxx -g %s -o %t && %run %t

#include <assert.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv) {
  FILE *fp = fopen(argv[0], "r");
  assert(fp);

  // file should be good upon opening
  assert(!feof(fp) && !ferror(fp));

  // read until EOF
  char buf[BUFSIZ];
  while (fread(buf, 1, sizeof buf, fp) != 0) {}
  assert(feof(fp));

  // clear EOF
  clearerr(fp);
  assert(!feof(fp) && !ferror(fp));

  // get file descriptor
  int fd = fileno(fp);
  assert(fd != -1);

  // break the file by closing underlying descriptor
  assert(close(fd) != -1);

  // verify that an error is signalled
  assert(fread(buf, 1, sizeof buf, fp) == 0);
  assert(ferror(fp));

  // clear error
  clearerr(fp);
  assert(!feof(fp) && !ferror(fp));

  // fclose() will return EBADF because of closed fd
  assert(fclose(fp) == -1);
  return 0;
}