Rev 8687 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8730 | turbocat | 1 | #include "stddef.h" |
8687 | turbocat | 2 | #include "sys/ksys.h" |
3 | #include |
||
4 | #include |
||
5 | #include |
||
6 | #include |
||
7 | |||
8730 | turbocat | 8 | #define CREATE_FILE() if(_ksys_file_create(_name)){ \ |
9 | errno= EIO; \ |
||
10 | free(out); \ |
||
11 | out = NULL; \ |
||
12 | } |
||
13 | |||
8687 | turbocat | 14 | FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) { |
8730 | turbocat | 15 | if(!_name || !_mode || !out){ |
16 | errno = EINVAL; |
||
8687 | turbocat | 17 | return NULL; |
18 | } |
||
19 | |||
8730 | turbocat | 20 | if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; } |
21 | if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; } |
||
22 | if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; } |
||
8687 | turbocat | 23 | |
8730 | turbocat | 24 | ksys_bdfe_t info; |
25 | int no_file = _ksys_file_get_info(_name, &info); |
||
26 | out->eof=0; |
||
27 | out->error=0; |
||
28 | out->position=0; |
||
8687 | turbocat | 29 | out->name = strdup(_name); |
8730 | turbocat | 30 | |
31 | switch (out->mode) { |
||
32 | case _FILEMODE_A : |
||
33 | if(no_file){ |
||
34 | CREATE_FILE(); |
||
35 | } |
||
8687 | turbocat | 36 | out->position = info.size; |
8730 | turbocat | 37 | break; |
38 | case _FILEMODE_W : |
||
39 | CREATE_FILE(); |
||
40 | break; |
||
41 | case _FILEMODE_R : |
||
42 | if(no_file){ |
||
43 | free(out); |
||
44 | out = NULL; |
||
8687 | turbocat | 45 | } |
8730 | turbocat | 46 | break; |
47 | default: |
||
48 | free(out); |
||
49 | out = NULL; |
||
50 | break; |
||
8687 | turbocat | 51 | } |
52 | return out; |
||
8730 | turbocat | 53 | } |