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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
#include <stdarg.h>
#include <errno.h>
#include "ds2_fcntl.h"
#include "fs_api.h"
#include "ds2_malloc.h"
/*typedef struct {
FILE_STRUCT* stream;
int flags;
} _fd_t;
_fd_t* _fd_list = NULL;
int _fd_count = 0;*/
int open(const char* path, int oflag, ... ) {
if(oflag & _O_UNSUPPORTED) {
errno = EINVAL;
return -1;
}
// TODO - Deal more correctly with certain flags.
FILE_STRUCT* tempFile = fat_fopen(path, "rb");
if(oflag & O_CREAT) {
if(tempFile == NULL)
tempFile = fat_fopen(path, "wb");
if(tempFile == NULL)
return -1;
} else if(tempFile == NULL) {
return -1;
} else if(oflag & O_TRUNC) {
tempFile = fat_fopen(path, "wb");
if(tempFile == NULL)
return -1;
}
fat_fclose(tempFile);
char tempMode[16];
if((oflag & 0x3) == O_RDONLY) {
sprintf(tempMode, "rb");
} else if((oflag & 0x3) == O_WRONLY) {
if(oflag & O_APPEND)
sprintf(tempMode, "ab");
else
sprintf(tempMode, "wb");
} else if((oflag & 0x3) == O_RDWR) {
if(oflag & O_APPEND)
sprintf(tempMode, "ab+");
else
sprintf(tempMode, "rb+");
}
tempFile = fat_fopen(path, tempMode);
if(tempFile == NULL)
return -1;
return tempFile -> fd;
}
int fcntl(int fildes, int cmd, ...) {
/*if((fildes < 0) || (fildes >= _fd_count) || (_fd_list[fildes].stream == NULL)) {
errno = EINVAL;
return -1;
}
if((cmd <= 0) || (cmd > _F_LAST)) {
errno = EINVAL;
return -1;
}
va_list ap;
int arg;
void* flock;
switch(cmd) {
case F_SETFD:
case F_SETOWN:
va_start(ap, cmd);
arg = va_arg(ap, int);
va_end(ap);
break;
case F_GETLK:
case F_SETLK:
va_start(ap, cmd);
flock = va_arg(ap, void*);
va_end(ap);
break;
}
switch(cmd) {
case F_DUPFD: // Duplicate file descriptors not supported.
errno = EINVAL;
return -1;
case F_GETFD:
return (_fd_list[fildes].flags & _F_FILE_DESC);
case F_SETFD:
arg &= _F_FILE_DESC;
_fd_list[fildes].flags &= ~_F_FILE_DESC;
_fd_list[fildes].flags |= arg;
return 0;
case F_GETFL:
return (_fd_list[fildes].flags & (O_ACCMODE | _O_FILE_STATUS));
case F_SETFL:
arg &= (O_ACCMODE | _O_FILE_STATUS);
if(arg & _O_UNSUPPORTED) {
errno = EINVAL;
return -1;
}
if((arg & O_ACCMODE) != (_fd_list[fildes].flags & O_ACCMODE)) {
errno = EINVAL;
return -1;
}
_fd_list[fildes].flags &= ~(O_ACCMODE | _O_FILE_STATUS);
_fd_list[fildes].flags |= arg;
return 0;
case F_GETOWN:
case F_SETOWN:
errno = -1;
return -1;
case F_GETLK:
case F_SETLK:
case F_SETLKW:
return -1;
}
errno = EINVAL;*/
return -1;
}
|