summaryrefslogtreecommitdiff
path: root/userland/libc/stdio/fopen.c
blob: 38ccbf187c5957a9b277160e6d6559754dea1261 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html
FILE *fopen(const char *pathname, const char *mode) {
  int flag = 0;
  for (; *mode; mode++) {
    // r or rb
    // Open file for reading.
    // w or wb
    // Truncate to zero length or create file for writing.
    // a or ab
    // Append; open or create file for writing at
    // end-of-file.
    switch (*mode) {
    case 'r':
      flag |= O_READ;
      break;
    case 'w':
      flag |= O_WRITE | O_TRUNC;
      break;
    case 'a':
      flag |= O_APPEND;
      break;
    }
  }

  int fd = open(pathname, flag, 0);
  if (-1 == fd) {
    return NULL;
  }

  FILE *r = fdopen(fd, mode);
  if (!r) {
    close(fd);
    return NULL;
  }
  r->has_control_over_the_fd = 1;
  return r;
}