Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. extern char __argv;
  6. extern char __path;
  7.  
  8. const char* getfullpath(const char *path){
  9.  
  10.         int i,j,relpath_pos,localpath_size;
  11.         int filename_size;
  12.         char local_path;
  13.         char *programpath;
  14.         char *newpath;
  15.  
  16.         i=0;
  17.         local_path=1; //enable local path
  18.         while((*(path+i)!='\0') || (*(path+i)!=0))
  19.         {
  20.                 if (*(path+i)=='.')
  21.                 {
  22.                         if (*(path+i+1)=='/')
  23.                         {       //detected relative path
  24.                                 relpath_pos=i+2;
  25.                                 local_path=0;
  26.                                 break;
  27.                         }
  28.                 }
  29.                 if (*(path+i)=='/')
  30.                 {       //disabple local path
  31.                         local_path=0;
  32.                         return(path);
  33.                 }
  34.                 i++;
  35.         }
  36.         filename_size=i;
  37.  
  38.         programpath=&__path;
  39.  
  40.         if (local_path==1)
  41.         {
  42.                 i=0x400;
  43.                 //find local path of program
  44.                 while(*(programpath+i)!='/')
  45.                 {
  46.                         i--;
  47.                 }
  48.                 localpath_size=i;
  49.                 newpath=malloc(0x400);
  50.                 //copy local path to the new path
  51.                 for(i=0;i<=localpath_size;i++)
  52.                 {
  53.                         *(newpath+i)=*(programpath+i);
  54.                 }
  55.                 //copy filename to the new path
  56.                 for(i=0;i<filename_size;i++)
  57.                 {
  58.                         *(newpath+localpath_size+1+i)=*(path+i);
  59.                 }
  60.                 return(newpath);
  61.         }
  62.  
  63.        //if we here than path is a relative
  64.        i=0x400;
  65.        //find local path of program
  66.        while(*(programpath+i)!='/')
  67.        {
  68.                 i--;
  69.        }
  70.        localpath_size=i;
  71.        i=0;
  72.        //find file name size
  73.        while((*(path+relpath_pos+i)!='\0') || (*(path+relpath_pos+i)!=0))
  74.        {
  75.                 i++;
  76.        }
  77.        filename_size=i;
  78.        newpath=malloc(0x400);
  79.         //copy local path to the new path
  80.        for(i=0;i<=localpath_size;i++)
  81.        {
  82.                 *(newpath+i)=*(programpath+i);
  83.        }
  84.        //copy filename to the new path
  85.        for(i=0;i<filename_size;i++)
  86.        {
  87.                 *(newpath+localpath_size+1+i)=*(path+relpath_pos+i);
  88.        }
  89.        return(newpath);
  90. }
  91.  
  92.  
  93. FILE* fopen(const char* filename, const char *mode)
  94. {
  95.         FILE* res;
  96.         int imode;
  97.         imode=0;
  98.         if (*mode=='r')
  99.         {
  100.                 imode=FILE_OPEN_READ;
  101.                 mode++;
  102.         }else if (*mode=='w')
  103.         {
  104.                 imode=FILE_OPEN_WRITE;
  105.                 mode++;
  106.         }else if (*mode=='a')
  107.         {
  108.                 imode=FILE_OPEN_APPEND;
  109.                 mode++;
  110.         }else
  111.                 return 0;
  112.         if (*mode=='t')
  113.         {
  114.                 imode|=FILE_OPEN_TEXT;
  115.                 mode++;
  116.         }else if (*mode=='b')
  117.                 mode++;
  118.         if (*mode=='+')
  119.         {
  120.                 imode|=FILE_OPEN_PLUS;
  121.                 mode++;
  122.         }
  123.         if (*mode!=0)
  124.                 return 0;
  125.         res=malloc(sizeof(FILE));
  126.         res->buffer=malloc(256);
  127.         res->buffersize=256;
  128.         res->filesize=0;
  129.         res->filepos=0;
  130.         res->mode=imode;
  131.         res->filename=getfullpath(filename);
  132.  
  133.         if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND))
  134.         {
  135.                 res->filesize=_ksys_get_filesize(res->filename);
  136.         }
  137.         return res;
  138. }
  139.