summaryrefslogtreecommitdiff
path: root/userland/libc/stdio/fgets.c
blob: 8e21501c74aba6e8f9c7354f1839b982afcc3060 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>

char *fgets(char *s, int n, FILE *stream) {
  for (int i = 0; i < n; i++) {
    char c;
    fread(&c, 1, 1, stream);
    if (stream->has_error)
      return NULL;
    if (stream->is_eof)
      return NULL;
    s[i] = c;
    if (c == '\n')
      break;
  }
  return s;
}