summaryrefslogtreecommitdiff
path: root/userland/libc/stdio/fgetc.c
blob: dc7af77b584b9f390144ecfe6f30784960348252 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <assert.h>
#include <stdio.h>

int fgetc(FILE *stream) {
  if (stream->has_buffered_char) {
    stream->has_buffered_char = 0;
    fseek(stream, 1, SEEK_CUR);
    return stream->buffered_char;
  }
  char c;
  if (1 == fread(&c, 1, 1, stream)) {
    return (int)c;
  }
  // FIXME: Should use feof and ferror
  if (stream->has_error) {
    return EOF;
  }
  if (stream->is_eof) {
    return EOF;
  }
  // How did we get here?
  assert(0);
  return EOF;
}