2021-11-01 15:46:28 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/ksys.h>
|
2021-04-27 18:33:31 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2021-11-01 15:46:28 +01:00
|
|
|
#include <sys/dirent.h>
|
2021-04-27 18:33:31 +02:00
|
|
|
|
2021-11-01 15:46:28 +01:00
|
|
|
|
|
|
|
static FILE* _set_errno(FILE *out, int err){
|
|
|
|
errno = err;
|
|
|
|
if(out){
|
|
|
|
free(out->name);
|
|
|
|
free(out);
|
|
|
|
}
|
|
|
|
out = NULL;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void _create_file(char *name, FILE *out){
|
|
|
|
if(_ksys_file_create(name)){
|
|
|
|
_set_errno(out, EIO);
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 17:28:07 +02:00
|
|
|
|
2021-04-27 18:33:31 +02:00
|
|
|
FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) {
|
2021-05-23 17:28:07 +02:00
|
|
|
if(!_name || !_mode || !out){
|
2021-11-01 15:46:28 +01:00
|
|
|
return _set_errno(out, EINVAL);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
|
2021-05-23 17:28:07 +02:00
|
|
|
ksys_bdfe_t info;
|
|
|
|
int no_file = _ksys_file_get_info(_name, &info);
|
2021-11-01 15:46:28 +01:00
|
|
|
if(!no_file && info.attributes & IS_FOLDER){
|
|
|
|
return _set_errno(out, EISDIR);
|
|
|
|
}
|
|
|
|
|
2021-05-23 17:28:07 +02:00
|
|
|
out->eof=0;
|
|
|
|
out->error=0;
|
|
|
|
out->position=0;
|
2021-04-27 18:33:31 +02:00
|
|
|
out->name = strdup(_name);
|
2021-05-23 17:28:07 +02:00
|
|
|
|
2021-11-01 15:46:28 +01:00
|
|
|
if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; }
|
|
|
|
if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; }
|
|
|
|
if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; }
|
|
|
|
if (strchr(_mode, '+')) { out->mode |= _FILEMODE_PLUS; }
|
|
|
|
|
|
|
|
if(out->mode & _FILEMODE_A){
|
2021-05-23 17:28:07 +02:00
|
|
|
if(no_file){
|
2021-11-01 15:46:28 +01:00
|
|
|
_create_file(out->name, out);
|
2021-05-23 17:28:07 +02:00
|
|
|
}
|
2021-04-27 18:33:31 +02:00
|
|
|
out->position = info.size;
|
2021-11-01 15:46:28 +01:00
|
|
|
}else if(out->mode & _FILEMODE_W){
|
|
|
|
_create_file(out->name, out);
|
|
|
|
}else if((out->mode & _FILEMODE_R)){
|
2021-05-23 17:28:07 +02:00
|
|
|
if(no_file){
|
2021-11-01 15:46:28 +01:00
|
|
|
_set_errno(out, ENOENT);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
2021-11-01 15:46:28 +01:00
|
|
|
}else{
|
|
|
|
_set_errno(out, EINVAL);
|
2021-04-27 18:33:31 +02:00
|
|
|
}
|
|
|
|
return out;
|
2021-05-23 17:28:07 +02:00
|
|
|
}
|