Rev 8793 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8793 | Rev 9230 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #include "stddef.h" |
1 | #include |
2 | #include "sys/ksys.h" |
2 | #include |
3 | #include |
3 | #include |
4 | #include |
4 | #include |
5 | #include |
5 | #include |
6 | #include |
6 | #include |
- | 7 | #include |
|
Line -... | Line 8... | ||
- | 8 | ||
7 | 9 | ||
- | 10 | static FILE* _set_errno(FILE *out, int err){ |
|
- | 11 | errno = err; |
|
8 | #define CREATE_FILE() if(_ksys_file_create(_name)){ \ |
12 | if(out){ |
9 | errno= EIO; \ |
13 | free(out->name); |
- | 14 | free(out); |
|
- | 15 | } |
|
- | 16 | out = NULL; |
|
- | 17 | return out; |
|
- | 18 | } |
|
- | 19 | ||
- | 20 | static void _create_file(char *name, FILE *out){ |
|
10 | free(out); \ |
21 | if(_ksys_file_create(name)){ |
- | 22 | _set_errno(out, EIO); |
|
11 | out = NULL; \ |
23 | } |
Line 12... | Line 24... | ||
12 | } |
24 | } |
13 | 25 | ||
14 | FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) { |
26 | FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) { |
15 | if(!_name || !_mode || !out){ |
- | |
16 | errno = EINVAL; |
27 | if(!_name || !_mode || !out){ |
Line 17... | Line -... | ||
17 | return NULL; |
- | |
18 | } |
- | |
19 | - | ||
20 | if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; } |
- | |
21 | if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; } |
28 | return _set_errno(out, EINVAL); |
22 | if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; } |
29 | } |
- | 30 | ||
- | 31 | ksys_bdfe_t info; |
|
- | 32 | int no_file = _ksys_file_get_info(_name, &info); |
|
- | 33 | if(!no_file && info.attributes & IS_FOLDER){ |
|
23 | 34 | return _set_errno(out, EISDIR); |
|
24 | ksys_bdfe_t info; |
35 | } |
25 | int no_file = _ksys_file_get_info(_name, &info); |
36 | |
26 | out->eof=0; |
37 | out->eof=0; |
Line -... | Line 38... | ||
- | 38 | out->error=0; |
|
- | 39 | out->position=0; |
|
27 | out->error=0; |
40 | out->name = strdup(_name); |
- | 41 | ||
- | 42 | if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; } |
|
28 | out->position=0; |
43 | if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; } |
29 | out->name = strdup(_name); |
44 | if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; } |
30 | 45 | if (strchr(_mode, '+')) { out->mode |= _FILEMODE_PLUS; } |
|
31 | switch (out->mode) { |
46 | |
32 | case _FILEMODE_A : |
47 | if(out->mode & _FILEMODE_A){ |
33 | if(no_file){ |
- | |
34 | CREATE_FILE(); |
48 | if(no_file){ |
35 | } |
49 | _create_file(out->name, out); |
36 | out->position = info.size; |
- | |
37 | break; |
50 | } |
38 | case _FILEMODE_W : |
51 | out->position = info.size; |
39 | CREATE_FILE(); |
52 | }else if(out->mode & _FILEMODE_W){ |
40 | break; |
- | |
41 | case _FILEMODE_R : |
53 | _create_file(out->name, out); |
42 | if(no_file){ |
- | |
43 | free(out); |
54 | }else if((out->mode & _FILEMODE_R)){ |
44 | out = NULL; |
55 | if(no_file){ |
45 | } |
- | |
46 | break; |
- | |
47 | default: |
56 | _set_errno(out, ENOENT); |
48 | free(out); |
57 | } |
49 | out = NULL; |
58 | }else{ |
50 | break; |
59 | _set_errno(out, EINVAL); |