2006-12-24 02:01:00 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2007-08-24 21:49:07 +02:00
|
|
|
#include <stdlib.h>
|
2006-12-24 02:01:00 +01:00
|
|
|
|
2018-11-01 18:24:22 +01:00
|
|
|
int errno = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
// removed by Seiemargl 26-oct-2018
|
|
|
|
// use get_current_folder() from kos32sys.h instead
|
|
|
|
|
|
|
|
|
2007-08-24 21:49:07 +02:00
|
|
|
extern char __argv;
|
|
|
|
extern char __path;
|
2006-12-24 02:01:00 +01:00
|
|
|
|
2018-11-01 18:24:22 +01:00
|
|
|
// convert relative to program path ./file.txt to absolute
|
2007-10-15 11:42:17 +02:00
|
|
|
const char* getfullpath(const char *path){
|
2018-11-01 18:24:22 +01:00
|
|
|
|
2006-12-24 02:01:00 +01:00
|
|
|
|
2018-03-05 18:53:31 +01:00
|
|
|
int relpath_pos, localpath_size;
|
2007-08-24 21:49:07 +02:00
|
|
|
char *programpath;
|
|
|
|
char *newpath;
|
2018-03-05 18:53:31 +01:00
|
|
|
char *prgname;
|
2007-08-24 21:49:07 +02:00
|
|
|
|
2018-11-01 18:24:22 +01:00
|
|
|
if (path[0] == '/') //
|
2007-08-24 21:49:07 +02:00
|
|
|
{
|
2018-11-01 18:24:22 +01:00
|
|
|
return(strdup(path)); // dup need as free in fclose()
|
2007-08-24 21:49:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 18:53:31 +01:00
|
|
|
relpath_pos = 0;
|
|
|
|
if (path[0] == '.' && path[1] == '/')
|
2007-08-24 21:49:07 +02:00
|
|
|
{
|
2018-03-05 18:53:31 +01:00
|
|
|
//detected relative path, begins with ./
|
|
|
|
relpath_pos=2;
|
2007-08-24 21:49:07 +02:00
|
|
|
}
|
|
|
|
|
2018-03-05 18:53:31 +01:00
|
|
|
programpath=&__path;
|
|
|
|
|
|
|
|
//if we here than path is a relative or local
|
|
|
|
prgname = strrchr(programpath, '/');
|
|
|
|
if (!prgname) return strdup(path);
|
|
|
|
|
|
|
|
localpath_size = prgname - programpath + 1;
|
|
|
|
|
|
|
|
newpath = malloc(FILENAME_MAX);
|
2016-05-19 14:15:22 +02:00
|
|
|
if(!newpath)
|
|
|
|
{
|
|
|
|
errno = E_NOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-24 21:49:07 +02:00
|
|
|
//copy local path to the new path
|
2018-03-05 18:53:31 +01:00
|
|
|
strncpy(newpath, programpath, localpath_size);
|
|
|
|
newpath[localpath_size] = 0;
|
|
|
|
|
|
|
|
//copy filename to the new path
|
|
|
|
strcpy(newpath + localpath_size, path + relpath_pos);
|
|
|
|
|
|
|
|
return(newpath);
|
2006-12-24 02:01:00 +01:00
|
|
|
}
|
2018-11-01 18:24:22 +01:00
|
|
|
*/
|
2006-12-24 02:01:00 +01:00
|
|
|
|
2018-03-05 18:53:31 +01:00
|
|
|
|
2006-09-07 16:14:53 +02:00
|
|
|
FILE* fopen(const char* filename, const char *mode)
|
|
|
|
{
|
2007-08-24 21:49:07 +02:00
|
|
|
FILE* res;
|
2018-03-05 18:53:31 +01:00
|
|
|
int imode, sz = -1;
|
|
|
|
char *fullname;
|
|
|
|
|
2007-08-24 21:49:07 +02:00
|
|
|
imode=0;
|
|
|
|
if (*mode=='r')
|
2006-09-07 16:14:53 +02:00
|
|
|
{
|
2007-08-24 21:49:07 +02:00
|
|
|
imode=FILE_OPEN_READ;
|
|
|
|
mode++;
|
|
|
|
}else if (*mode=='w')
|
2006-09-07 16:14:53 +02:00
|
|
|
{
|
2007-08-24 21:49:07 +02:00
|
|
|
imode=FILE_OPEN_WRITE;
|
|
|
|
mode++;
|
|
|
|
}else if (*mode=='a')
|
|
|
|
{
|
|
|
|
imode=FILE_OPEN_APPEND;
|
|
|
|
mode++;
|
2006-09-07 16:14:53 +02:00
|
|
|
}else
|
2007-08-24 21:49:07 +02:00
|
|
|
return 0;
|
2016-05-19 14:15:22 +02:00
|
|
|
if (*mode=='+')
|
|
|
|
{
|
|
|
|
imode|=FILE_OPEN_PLUS;
|
|
|
|
mode++;
|
|
|
|
}
|
2007-08-24 21:49:07 +02:00
|
|
|
if (*mode=='t')
|
|
|
|
{
|
|
|
|
imode|=FILE_OPEN_TEXT;
|
|
|
|
mode++;
|
|
|
|
}else if (*mode=='b')
|
|
|
|
mode++;
|
|
|
|
if (*mode=='+')
|
2006-09-07 16:14:53 +02:00
|
|
|
{
|
2007-08-24 21:49:07 +02:00
|
|
|
imode|=FILE_OPEN_PLUS;
|
|
|
|
mode++;
|
2006-09-07 16:14:53 +02:00
|
|
|
}
|
2007-08-24 21:49:07 +02:00
|
|
|
if (*mode!=0)
|
2016-05-19 14:15:22 +02:00
|
|
|
return NULL;
|
2018-03-05 18:53:31 +01:00
|
|
|
|
2018-11-01 18:24:22 +01:00
|
|
|
// fullname = (char*)getfullpath(filename);
|
|
|
|
fullname = strdup(filename);
|
2018-03-05 18:53:31 +01:00
|
|
|
if ((imode & 3) == FILE_OPEN_READ && fullname) /* check existense */
|
|
|
|
{
|
|
|
|
sz = _ksys_get_filesize(fullname);
|
|
|
|
if (sz < 0)
|
|
|
|
{
|
|
|
|
free(fullname);
|
|
|
|
errno = sz;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = malloc(sizeof(FILE));
|
2016-05-19 14:15:22 +02:00
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
res->buffer=malloc(BUFSIZ);
|
|
|
|
res->buffersize=BUFSIZ;
|
|
|
|
res->filesize=0;
|
|
|
|
res->filepos=0;
|
|
|
|
res->mode=imode;
|
2018-03-05 18:53:31 +01:00
|
|
|
res->filename=fullname;
|
2018-03-12 21:41:06 +01:00
|
|
|
res->ungetc_buf = EOF;
|
|
|
|
res->buffer_start = -1;
|
|
|
|
res->buffer_end = -1;
|
2016-05-19 14:15:22 +02:00
|
|
|
}
|
|
|
|
if(!res || !res->buffer || !res->filename)
|
|
|
|
{
|
|
|
|
errno = E_NOMEM;
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-08-24 21:49:07 +02:00
|
|
|
|
2018-03-12 21:41:06 +01:00
|
|
|
if ((imode & 3) == FILE_OPEN_READ || (imode & 3) == FILE_OPEN_APPEND)
|
2007-08-24 21:49:07 +02:00
|
|
|
{
|
2018-03-05 18:53:31 +01:00
|
|
|
if (sz > 0) /*already got*/
|
|
|
|
res->filesize = sz;
|
|
|
|
else
|
|
|
|
res->filesize=_ksys_get_filesize(res->filename);
|
2007-08-24 21:49:07 +02:00
|
|
|
}
|
|
|
|
return res;
|
2006-09-07 16:14:53 +02:00
|
|
|
}
|