Subversion Repositories Kolibri OS

Rev

Rev 6433 | 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. int errno = 0;
  9.  
  10. const char* getfullpath(const char *path){
  11.  
  12.         int  relpath_pos, localpath_size;
  13.         char *programpath;
  14.         char *newpath;
  15.         char *prgname;
  16.  
  17.         if (path[0] == '/') /* root */
  18.         {
  19.             return(strdup(path)); /* dup need as free in fclose() */
  20.         }
  21.  
  22.         relpath_pos = 0;
  23.         if (path[0] == '.' && path[1] == '/')
  24.         {
  25.             //detected relative path, begins with ./
  26.             relpath_pos=2;
  27.         }
  28.  
  29.         programpath=&__path;
  30.  
  31.         //if we here than path is a relative or local
  32.         prgname = strrchr(programpath, '/');
  33.         if (!prgname) return strdup(path);
  34.  
  35.         localpath_size = prgname - programpath + 1;
  36.  
  37.         newpath = malloc(FILENAME_MAX);
  38.         if(!newpath)
  39.         {
  40.             errno = E_NOMEM;
  41.             return NULL;
  42.         }
  43.         //copy local path to the new path
  44.         strncpy(newpath, programpath, localpath_size);
  45.         newpath[localpath_size] = 0;
  46.  
  47.         //copy filename to the new path
  48.         strcpy(newpath + localpath_size, path + relpath_pos);
  49.  
  50.         return(newpath);
  51. }
  52.  
  53.  
  54.  
  55. FILE* fopen(const char* filename, const char *mode)
  56. {
  57.         FILE* res;
  58.         int imode, sz = -1;
  59.                 char *fullname;
  60.  
  61.         imode=0;
  62.         if (*mode=='r')
  63.         {
  64.                 imode=FILE_OPEN_READ;
  65.                 mode++;
  66.         }else if (*mode=='w')
  67.         {
  68.                 imode=FILE_OPEN_WRITE;
  69.                 mode++;
  70.         }else if (*mode=='a')
  71.         {
  72.                 imode=FILE_OPEN_APPEND;
  73.                 mode++;
  74.         }else
  75.                 return 0;
  76.         if (*mode=='+')
  77.         {
  78.                 imode|=FILE_OPEN_PLUS;
  79.                 mode++;
  80.         }
  81.         if (*mode=='t')
  82.         {
  83.                 imode|=FILE_OPEN_TEXT;
  84.                 mode++;
  85.         }else if (*mode=='b')
  86.                 mode++;
  87.         if (*mode=='+')
  88.         {
  89.                 imode|=FILE_OPEN_PLUS;
  90.                 mode++;
  91.         }
  92.         if (*mode!=0)
  93.                 return NULL;
  94.                
  95.                 fullname = (char*)getfullpath(filename);
  96.                 if ((imode & 3) == FILE_OPEN_READ && fullname)  /* check existense */
  97.                 {
  98.                         sz = _ksys_get_filesize(fullname);
  99.                         if (sz < 0)
  100.                         {
  101.                                 free(fullname);
  102.                                 errno = sz;
  103.                                 return NULL;
  104.                         }
  105.                 }
  106.                        
  107.         res = malloc(sizeof(FILE));
  108.         if (res)
  109.         {
  110.             res->buffer=malloc(BUFSIZ);
  111.             res->buffersize=BUFSIZ;
  112.             res->filesize=0;
  113.             res->filepos=0;
  114.             res->mode=imode;
  115.             res->filename=fullname;
  116.         }
  117.         if(!res || !res->buffer || !res->filename)
  118.         {
  119.             errno = E_NOMEM;
  120.             return NULL;
  121.         }
  122.  
  123.         if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND))
  124.         {
  125.                 if (sz > 0) /*already got*/
  126.                         res->filesize = sz;
  127.                 else
  128.                         res->filesize=_ksys_get_filesize(res->filename);
  129.         }
  130.         return res;
  131. }
  132.