Subversion Repositories Kolibri OS

Rev

Rev 8793 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <stddef.h>
  2. #include <sys/ksys.h>
  3. #include <errno.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/dirent.h>
  8.  
  9.  
  10. static FILE* _set_errno(FILE *out, int err){
  11.     errno = err;
  12.     if(out){
  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){
  21.     if(_ksys_file_create(name)){
  22.        _set_errno(out, EIO);
  23.     }
  24. }
  25.  
  26. FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) {
  27.     if(!_name || !_mode || !out){
  28.         return _set_errno(out, EINVAL);
  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){
  34.         return _set_errno(out, EISDIR);
  35.     }
  36.    
  37.     out->eof=0;
  38.     out->error=0;
  39.     out->position=0;
  40.     out->name = strdup(_name);
  41.    
  42.     if (strchr(_mode, 'r')) { out->mode = _FILEMODE_R; }
  43.     if (strchr(_mode, 'w')) { out->mode = _FILEMODE_W; }
  44.     if (strchr(_mode, 'a')) { out->mode = _FILEMODE_A; }
  45.     if (strchr(_mode, '+')) { out->mode |= _FILEMODE_PLUS; }
  46.  
  47.     if(out->mode & _FILEMODE_A){
  48.         if(no_file){
  49.             _create_file(out->name, out);
  50.         }
  51.         out->position = info.size;
  52.     }else if(out->mode & _FILEMODE_W){
  53.         _create_file(out->name, out);
  54.     }else if((out->mode & _FILEMODE_R)){
  55.         if(no_file){
  56.             _set_errno(out, ENOENT);
  57.         }
  58.     }else{
  59.         _set_errno(out, EINVAL);
  60.     }
  61.     return out;
  62. }