2006-12-24 04:01:00 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-08-24 23:49:07 +04:00
|
|
|
#include <stdlib.h>
|
2006-12-24 04:01:00 +03:00
|
|
|
|
2007-08-24 23:49:07 +04:00
|
|
|
extern char __argv;
|
|
|
|
extern char __path;
|
2006-12-24 04:01:00 +03:00
|
|
|
|
2016-05-19 15:15:22 +03:00
|
|
|
int errno = 0;
|
|
|
|
|
|
|
|
|
2007-10-15 13:42:17 +04:00
|
|
|
const char* getfullpath(const char *path){
|
2006-12-24 04:01:00 +03:00
|
|
|
|
2007-08-24 23:49:07 +04:00
|
|
|
int i,j,relpath_pos,localpath_size;
|
|
|
|
int filename_size;
|
|
|
|
char local_path;
|
|
|
|
char *programpath;
|
|
|
|
char *newpath;
|
|
|
|
|
|
|
|
i=0;
|
|
|
|
local_path=1; //enable local path
|
|
|
|
while((*(path+i)!='\0') || (*(path+i)!=0))
|
|
|
|
{
|
|
|
|
if (*(path+i)=='.')
|
|
|
|
{
|
|
|
|
if (*(path+i+1)=='/')
|
|
|
|
{ //detected relative path
|
|
|
|
relpath_pos=i+2;
|
|
|
|
local_path=0;
|
|
|
|
break;
|
|
|
|
}
|
2006-12-24 04:01:00 +03:00
|
|
|
}
|
2007-08-24 23:49:07 +04:00
|
|
|
if (*(path+i)=='/')
|
|
|
|
{ //disabple local path
|
|
|
|
local_path=0;
|
|
|
|
return(path);
|
2006-12-24 04:01:00 +03:00
|
|
|
}
|
2007-08-24 23:49:07 +04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
filename_size=i;
|
|
|
|
|
|
|
|
programpath=&__path;
|
|
|
|
|
|
|
|
if (local_path==1)
|
|
|
|
{
|
2016-05-19 15:15:22 +03:00
|
|
|
i=FILENAME_MAX;
|
2007-08-24 23:49:07 +04:00
|
|
|
//find local path of program
|
|
|
|
while(*(programpath+i)!='/')
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
localpath_size=i;
|
2016-05-19 15:15:22 +03:00
|
|
|
newpath=malloc(FILENAME_MAX);
|
|
|
|
if(!newpath)
|
|
|
|
{
|
|
|
|
errno = E_NOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-24 23:49:07 +04:00
|
|
|
//copy local path to the new path
|
|
|
|
for(i=0;i<=localpath_size;i++)
|
|
|
|
{
|
|
|
|
*(newpath+i)=*(programpath+i);
|
|
|
|
}
|
|
|
|
//copy filename to the new path
|
|
|
|
for(i=0;i<filename_size;i++)
|
|
|
|
{
|
|
|
|
*(newpath+localpath_size+1+i)=*(path+i);
|
|
|
|
}
|
|
|
|
return(newpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
//if we here than path is a relative
|
2016-05-19 15:15:22 +03:00
|
|
|
i=FILENAME_MAX;
|
2007-08-24 23:49:07 +04:00
|
|
|
//find local path of program
|
|
|
|
while(*(programpath+i)!='/')
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
localpath_size=i;
|
|
|
|
i=0;
|
|
|
|
//find file name size
|
|
|
|
while((*(path+relpath_pos+i)!='\0') || (*(path+relpath_pos+i)!=0))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
filename_size=i;
|
2016-05-19 15:15:22 +03:00
|
|
|
newpath=malloc(FILENAME_MAX);
|
|
|
|
if(!newpath)
|
|
|
|
{
|
|
|
|
errno = E_NOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-24 23:49:07 +04:00
|
|
|
//copy local path to the new path
|
|
|
|
for(i=0;i<=localpath_size;i++)
|
|
|
|
{
|
|
|
|
*(newpath+i)=*(programpath+i);
|
|
|
|
}
|
|
|
|
//copy filename to the new path
|
|
|
|
for(i=0;i<filename_size;i++)
|
|
|
|
{
|
|
|
|
*(newpath+localpath_size+1+i)=*(path+relpath_pos+i);
|
|
|
|
}
|
|
|
|
return(newpath);
|
2006-12-24 04:01:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-07 18:14:53 +04:00
|
|
|
FILE* fopen(const char* filename, const char *mode)
|
|
|
|
{
|
2007-08-24 23:49:07 +04:00
|
|
|
FILE* res;
|
|
|
|
int imode;
|
|
|
|
imode=0;
|
|
|
|
if (*mode=='r')
|
2006-09-07 18:14:53 +04:00
|
|
|
{
|
2007-08-24 23:49:07 +04:00
|
|
|
imode=FILE_OPEN_READ;
|
|
|
|
mode++;
|
|
|
|
}else if (*mode=='w')
|
2006-09-07 18:14:53 +04:00
|
|
|
{
|
2007-08-24 23:49:07 +04:00
|
|
|
imode=FILE_OPEN_WRITE;
|
|
|
|
mode++;
|
|
|
|
}else if (*mode=='a')
|
|
|
|
{
|
|
|
|
imode=FILE_OPEN_APPEND;
|
|
|
|
mode++;
|
2006-09-07 18:14:53 +04:00
|
|
|
}else
|
2007-08-24 23:49:07 +04:00
|
|
|
return 0;
|
2016-05-19 15:15:22 +03:00
|
|
|
if (*mode=='+')
|
|
|
|
{
|
|
|
|
imode|=FILE_OPEN_PLUS;
|
|
|
|
mode++;
|
|
|
|
}
|
2007-08-24 23:49:07 +04:00
|
|
|
if (*mode=='t')
|
|
|
|
{
|
|
|
|
imode|=FILE_OPEN_TEXT;
|
|
|
|
mode++;
|
|
|
|
}else if (*mode=='b')
|
|
|
|
mode++;
|
|
|
|
if (*mode=='+')
|
2006-09-07 18:14:53 +04:00
|
|
|
{
|
2007-08-24 23:49:07 +04:00
|
|
|
imode|=FILE_OPEN_PLUS;
|
|
|
|
mode++;
|
2006-09-07 18:14:53 +04:00
|
|
|
}
|
2007-08-24 23:49:07 +04:00
|
|
|
if (*mode!=0)
|
2016-05-19 15:15:22 +03:00
|
|
|
return NULL;
|
2007-08-24 23:49:07 +04:00
|
|
|
res=malloc(sizeof(FILE));
|
2016-05-19 15:15:22 +03:00
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
res->buffer=malloc(BUFSIZ);
|
|
|
|
res->buffersize=BUFSIZ;
|
|
|
|
res->filesize=0;
|
|
|
|
res->filepos=0;
|
|
|
|
res->mode=imode;
|
|
|
|
res->filename=(char*)getfullpath(filename);
|
|
|
|
}
|
|
|
|
if(!res || !res->buffer || !res->filename)
|
|
|
|
{
|
|
|
|
errno = E_NOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-24 23:49:07 +04:00
|
|
|
|
|
|
|
if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND))
|
|
|
|
{
|
|
|
|
res->filesize=_ksys_get_filesize(res->filename);
|
|
|
|
}
|
|
|
|
return res;
|
2006-09-07 18:14:53 +04:00
|
|
|
}
|