/programs/games/nsider/stdio/fclose.c |
---|
0,0 → 1,9 |
#include <stdio.h> |
#include <string.h> |
#include <stdlib.h> |
void fclose(FILE* file) |
{ |
free(file->buffer); |
free(file); |
} |
/programs/games/nsider/stdio/feof.c |
---|
0,0 → 1,5 |
#include <stdio.h> |
int feof(FILE* file) |
{ |
return file->filepos>=file->filesize; |
} |
/programs/games/nsider/stdio/fflush.c |
---|
0,0 → 1,7 |
#include <stdio.h> |
int fflush(FILE* file) |
{ |
if ((file->mode & 3)==FILE_OPEN_READ) |
return 0; |
return(EOF); |
} |
/programs/games/nsider/stdio/fgetc.c |
---|
0,0 → 1,22 |
#include <stdio.h> |
int fgetc(FILE* file) |
{ |
dword res; |
if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) return EOF; |
if (file->filepos>=file->filesize) |
{ |
return EOF; |
} |
else |
{ |
res=_ksys_readfile(file->filename,file->filepos,1,file->buffer); |
if (res==0) |
{ |
file->filepos++; |
return (int)file->buffer[0]; |
} |
else return(res); |
} |
} |
/programs/games/nsider/stdio/fgetpos.c |
---|
0,0 → 1,6 |
#include <stdio.h> |
int fgetpos(FILE* file,fpos_t* pos) |
{ |
*pos=file->filepos; |
return 0; |
} |
/programs/games/nsider/stdio/fopen.c |
---|
0,0 → 1,138 |
#include <stdio.h> |
#include <string.h> |
#include <stdlib.h> |
extern char __argv; |
extern char __path; |
const char* getfullpath(const char *path){ |
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; |
} |
} |
if (*(path+i)=='/') |
{ //disabple local path |
local_path=0; |
return(path); |
} |
i++; |
} |
filename_size=i; |
programpath=&__path; |
if (local_path==1) |
{ |
i=0x400; |
//find local path of program |
while(*(programpath+i)!='/') |
{ |
i--; |
} |
localpath_size=i; |
newpath=malloc(0x400); |
//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 |
i=0x400; |
//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; |
newpath=malloc(0x400); |
//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); |
} |
FILE* fopen(const char* filename, const char *mode) |
{ |
FILE* res; |
int imode; |
imode=0; |
if (*mode=='r') |
{ |
imode=FILE_OPEN_READ; |
mode++; |
}else if (*mode=='w') |
{ |
imode=FILE_OPEN_WRITE; |
mode++; |
}else if (*mode=='a') |
{ |
imode=FILE_OPEN_APPEND; |
mode++; |
}else |
return 0; |
if (*mode=='t') |
{ |
imode|=FILE_OPEN_TEXT; |
mode++; |
}else if (*mode=='b') |
mode++; |
if (*mode=='+') |
{ |
imode|=FILE_OPEN_PLUS; |
mode++; |
} |
if (*mode!=0) |
return 0; |
res=malloc(sizeof(FILE)); |
res->buffer=malloc(256); |
res->buffersize=256; |
res->filesize=0; |
res->filepos=0; |
res->mode=imode; |
res->filename=getfullpath(filename); |
if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND)) |
{ |
res->filesize=_ksys_get_filesize(res->filename); |
} |
return res; |
} |
/programs/games/nsider/stdio/fputc.c |
---|
0,0 → 1,35 |
#include <stdio.h> |
int fputc(int c,FILE* file) |
{ |
dword res; |
if ((file->mode & 3)==FILE_OPEN_READ) return EOF; |
file->buffer[0]=c; |
if ((file->mode & 3)==FILE_OPEN_APPEND) |
{ |
file->filepos=file->filesize; |
file->filesize++; |
res=_ksys_appendtofile(file->filename,file->filepos,1,file->buffer); |
if (res!=0) return(res); |
file->filepos++; |
return(0); |
} |
if ((file->mode & 3)==FILE_OPEN_WRITE) |
{ |
if (file->filepos==0) |
{ //file not craeted |
res=_ksys_rewritefile(file->filename,1,file->buffer); |
if (res!=0) return(res); |
file->filepos++; |
return 0; |
} |
else |
{ //file craeted and need append one byte |
res=_ksys_appendtofile(file->filename,file->filepos,1,file->buffer); |
if (res!=0) return(res); |
file->filepos++; |
return 0; |
} |
} |
} |
/programs/games/nsider/stdio/fread.c |
---|
0,0 → 1,26 |
#include <stdio.h> |
#include <kolibrisys.h> |
int fread(void *buffer,int size,int count,FILE* file) |
{ |
dword res; |
dword fullsize; |
if ((file->mode!=FILE_OPEN_READ) || (file->mode==FILE_OPEN_PLUS)) return 0; |
fullsize=count*size; |
if ((fullsize+file->filepos)>(file->filesize)) |
{ |
fullsize=file->filesize-file->filepos; |
if (fullsize<=0) return(0); |
} |
res=_ksys_readfile(file->filename,file->filepos,fullsize,buffer); |
if (res==0) |
{ |
file->filepos=file->filepos+fullsize; |
fullsize=fullsize/size; |
return(fullsize); |
} |
else return 0; |
} |
/programs/games/nsider/stdio/fseek.c |
---|
0,0 → 1,11 |
#include <stdio.h> |
int fseek(FILE* file,long offset,int origin) |
{ |
if (origin==SEEK_CUR) |
offset+=file->filepos; |
else if (origin==SEEK_END) |
offset+=file->filesize; |
else if (origin!=SEEK_SET) |
return EOF; |
return fsetpos(file,offset); |
} |
/programs/games/nsider/stdio/fsetpos.c |
---|
0,0 → 1,11 |
#include <stdio.h> |
int fsetpos(FILE* file,const fpos_t * pos) |
{ |
if (*pos>=0) |
{ |
file->filepos=*pos; |
return 0; |
} |
else |
return EOF; |
} |
/programs/games/nsider/stdio/ftell.c |
---|
0,0 → 1,5 |
#include <stdio.h> |
long ftell(FILE* file) |
{ |
return file->filepos; |
} |
/programs/games/nsider/stdio/fwrite.c |
---|
0,0 → 1,58 |
#include <stdio.h> |
#include <kolibrisys.h> |
int fwrite(void *buffer,int size,int count,FILE* file) |
{ |
dword res; |
dword fullsize; |
if (file->mode==FILE_OPEN_READ) return 0; |
if (file->mode==FILE_OPEN_APPEND) |
file->filepos=file->filesize; |
fullsize=count*size; |
if ((file->filesize)<(file->filepos+fullsize)) file->filesize=file->filepos+fullsize; |
/* |
if (file->mode==FILE_OPEN_APPEND) |
{ |
file->filepos==file->filesize; |
res=_ksys_appendtofile(file->filename,file->filepos,fullsize,buffer); |
if (res==0) |
{ |
file->filepos+=fullsize; |
fullsize=fullsize/size; |
return(fullsize); |
} |
else return(0); |
} |
*/ |
if ((file->mode==FILE_OPEN_WRITE) || (file->mode==FILE_OPEN_APPEND)) |
{ |
if (file->filepos==0) |
{ //file mot craeted yet |
res=_ksys_rewritefile(file->filename,fullsize,buffer); |
if (res==0) |
{ |
file->filepos+=fullsize; |
fullsize=fullsize/count; |
return(fullsize); |
} |
else return(0); |
} |
else |
{ |
res=_ksys_appendtofile(file->filename,file->filepos,fullsize,buffer); |
if (res==0) |
{ |
file->filepos+=fullsize; |
fullsize=fullsize/count; |
return(fullsize); |
} |
else return(0); |
} |
} |
else return(0); |
} |
/programs/games/nsider/stdio/rewind.c |
---|
0,0 → 1,5 |
#include <stdio.h> |
void rewind(FILE* file) |
{ |
file->filepos=0; |
} |