Subversion Repositories Kolibri OS

Rev

Rev 8687 | Go to most recent revision | Blame | 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.  
  8. #define CREATE_FILE()   if(_ksys_file_create(_name)){ \
  9.                             errno= EIO; \
  10.                             free(out); \
  11.                             out = NULL; \
  12.                         }
  13.  
  14. FILE *freopen(const char *restrict _name, const char *restrict _mode, FILE *restrict out) {
  15.     if(!_name || !_mode || !out){
  16.         errno = EINVAL;
  17.         return NULL;
  18.     }
  19.  
  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; }
  23.  
  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;
  29.     out->name = strdup(_name);
  30.    
  31.     switch (out->mode) {
  32.     case _FILEMODE_A :
  33.         if(no_file){
  34.             CREATE_FILE();
  35.         }
  36.         out->position = info.size;
  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;
  45.         }
  46.         break;
  47.     default:
  48.         free(out);
  49.         out = NULL;
  50.         break;
  51.     }
  52.     return out;
  53. }