1#include <fcntl.h>
2#include <string.h>
3
4int __fmodeflags(const char* mode) {
5    int flags;
6    if (strchr(mode, '+'))
7        flags = O_RDWR;
8    else if (*mode == 'r')
9        flags = O_RDONLY;
10    else
11        flags = O_WRONLY;
12    if (strchr(mode, 'x'))
13        flags |= O_EXCL;
14    if (strchr(mode, 'e'))
15        flags |= O_CLOEXEC;
16    if (*mode != 'r')
17        flags |= O_CREAT;
18    if (*mode == 'w')
19        flags |= O_TRUNC;
20    if (*mode == 'a')
21        flags |= O_APPEND;
22    return flags;
23}
24