Subversion Repositories Kolibri OS

Rev

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