/programs/develop/metcc/trunk/libc/file/fclose.c |
---|
1,11 → 1,9 |
#include "stdio.h" |
#include "string.h" |
int fclose(FILE* file) |
#include <stdio.h> |
#include <string.h> |
#include <stdlib.h> |
void fclose(FILE* file) |
{ |
int res; |
res=_msys_write_file(file->filename, 0, file->filesize, file->buffer); |
free(file->buffer); |
free(file->filename); |
free(file); |
return res; |
} |
/programs/develop/metcc/trunk/libc/file/feof.c |
---|
1,4 → 1,4 |
#include "stdio.h" |
#include <stdio.h> |
int feof(FILE* file) |
{ |
return file->filepos>=file->filesize; |
/programs/develop/metcc/trunk/libc/file/fflush.c |
---|
1,7 → 1,7 |
#include "stdio.h" |
#include <stdio.h> |
int fflush(FILE* file) |
{ |
if ((file->mode & 3)==FILE_OPEN_READ) |
return 0; |
return _msys_file_write(file->filename, 0, file->filesize, file->buffer) ? EOF : 0; |
return(EOF); |
} |
/programs/develop/metcc/trunk/libc/file/fgetc.c |
---|
1,12 → 1,22 |
#include "stdio.h" |
#include <stdio.h> |
int fgetc(FILE* file) |
{ |
if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) |
return EOF; |
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 |
{ |
return (int)file->buffer[file->filepos++]; |
res=_ksys_readfile(file->filename,file->filepos,1,file->buffer); |
if (res==0) |
{ |
file->filepos++; |
return (int)file->buffer[0]; |
} |
else return(res); |
} |
} |
/programs/develop/metcc/trunk/libc/file/fopen.c |
---|
1,56 → 1,95 |
#include <stdio.h> |
#include <string.h> |
#include <stdlib.h> |
extern char __argv; |
extern char __path; |
extern struct{int argc; char** argv;} __ARGS; |
char* getfullpath(const char *path){ |
char* getfullpath(const char* relpath){ |
byte prev_is_slash=0; |
int len=0, depth=0, i; |
char* buff; |
buff = malloc(strlen(__ARGS.argv[0]) + strlen(relpath)); |
if(*relpath == '/') buff[0] = '\0'; |
else { |
len = strrchr(__ARGS.argv[0], '/') - __ARGS.argv[0] + 1; |
strncpy(buff, __ARGS.argv[0], len); |
prev_is_slash = 1; |
buff[len] = 0; |
for(i=0; buff[i]; i++) |
if(buff[i] == '/' && i < len-1) depth++; |
} |
for (; *relpath; relpath++){ |
switch (*relpath){ |
case '/': |
prev_is_slash = 1; |
buff[len++] = '/'; |
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; |
case '.': |
if(*(relpath+1) == '.' && *(relpath+2) == '/' && prev_is_slash){ |
if(!depth) return 0; |
buff[len-1] = 0; |
len = strrchr(buff, '/') + 1 - buff; |
buff[len] = 0; |
depth--; |
relpath += 2; |
} else { |
depth++; |
buff[len++] = '.'; |
} |
break; |
default: |
if(prev_is_slash){ |
depth++; |
prev_is_slash = 0; |
} |
buff[len++] = *relpath; |
break; |
if (*(path+i)=='/') |
{ //disabple local path |
local_path=0; |
return(path); |
} |
i++; |
} |
buff[len]= '\0'; |
return buff; |
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; |
84,59 → 123,16 |
if (*mode!=0) |
return 0; |
res=malloc(sizeof(FILE)); |
res->buffer=0; |
res->buffersize=0; |
res->buffer=malloc(256); |
res->buffersize=256; |
res->filesize=0; |
res->filepos=0; |
res->mode=imode; |
res->filename=getfullpath(filename); |
//check if file exists |
res=_msys_read_file(res->filename, 0, 0, 0, &res->filesize); |
if (res==5) |
if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND)) |
{ |
if ((imode & 3) == FILE_OPEN_READ) |
{ |
free(res->filename); |
free(res); |
return 0; |
res->filesize=_ksys_get_filesize(res->filename); |
} |
res=_msys_create_file(res->filename); |
if (res!=0) |
{ |
free(res->filename); |
free(res); |
return 0; |
} |
res->filesize=0; |
} |
if (imode & 3==FILE_OPEN_WRITE) |
{ |
res->buffersize=512; |
res->buffer=malloc(res->buffersize); |
if (res->buffer=0) |
{ |
free(res->filename); |
free(res); |
return 0; |
} |
res->filesize=0; |
}else |
{ |
res->buffersize=(res->filesize & (~511))+512; |
res->buffer=malloc(res->buffersize); |
if (res->buffer==0) |
{ |
free(res); |
return 0; |
} |
res=_msys_read_file(res->filename,0,res->filesize,res->buffer,0); |
if (res!=0) |
{ |
free(res->buffer); |
free(res); |
} |
if (imode & 3==FILE_OPEN_APPEND) |
res->filepos=res->filesize; |
} |
return res; |
} |
/programs/develop/metcc/trunk/libc/file/fputc.c |
---|
1,24 → 1,35 |
#include "stdio.h" |
int fputc(int c,FILE* file) |
{ |
void* p; |
if ((file->mode & 3)==FILE_OPEN_READ) |
return EOF; |
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; |
if (file->filepos==file->filesize) |
{ |
file->filesize++; |
if (file->filesize>file->buffersize) |
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) |
{ |
p=realloc(file->buffer,file->filesize+file->filesize<<1); |
if (p==0) |
return EOF; |
file->buffersize=file->filesize+file->filesize<<1; |
file->buffer=p; |
if (file->filepos==0) |
{ //file not craeted |
res=_ksys_rewritefile(file->filename,1,file->buffer); |
if (res!=0) return(res); |
file->filepos++; |
return 0; |
} |
} |
file->buffer[file->filepos]=(char)c; |
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/develop/metcc/trunk/libc/file/fread.c |
---|
1,12 → 1,25 |
#include "stdio.h" |
#include <stdio.h> |
#include <mesys.h> |
int fread(void* buffer,int size,int count,FILE* file) |
{ |
if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) |
return 0; |
count=count*size; |
if (count+file->filepos>file->filesize) |
count=file->filesize-file->filepos; |
memcpy(buffer,file->buffer+file->filepos,count); |
file->filepos+=count; |
return count/size; |
dword res; |
dword fullsize; |
if ((file->mode & 3!=FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) 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/develop/metcc/trunk/libc/file/fwrite.c |
---|
1,23 → 1,57 |
#include "stdio.h" |
int fwrite(const void* buffer,int size,int count,FILE* file) |
#include <stdio.h> |
#include <mesys.h> |
int fwrite(void *buffer,int size,int count,FILE* file) |
{ |
void* p; |
if ((file->mode & 3==FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) |
return 0; |
dword res; |
dword fullsize; |
if ((file->mode & 3==FILE_OPEN_READ) && (file->mode & FILE_OPEN_PLUS==0)) return 0; |
if (file->mode & 3==FILE_OPEN_APPEND) |
file->filepos=file->filesize; |
count=count*size; |
if (file->buffersize<file->filepos+count) |
fullsize=count*size; |
if ((file->filesize)<(file->filepos+fullsize)) file->filesize=file->filepos+fullsize; |
if (file->mode & 3==FILE_OPEN_APPEND) |
{ |
p=realloc(file->buffer,(file->filepos+count)+(file->filepos+count)<<1); |
if (p==0) |
return 0; |
file->buffer=p; |
file->buffersize=(file->filepos+count)+(file->filepos+count)<<1; |
file->filepos==file->filesize; |
res=_ksys_appendtofile(file->filename,file->filepos,fullsize,buffer); |
if (res==0) |
{ |
file->filepos+=fullsize; |
fullsize=fullsize/size; |
return(fullsize); |
} |
if (file->filesize<file->filepos+count) |
file->filesize=file->filepos+count; |
memcpy(file->buffer+file->filepos,buffer,count); |
file->filepos+=count; |
return count; |
else return(0); |
} |
if (file->mode & 3==FILE_OPEN_WRITE) |
{ |
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/develop/metcc/trunk/libc/file/ungetc.c |
---|
1,11 → 1,17 |
#include "stdio.h" |
int ungetc(int c,FILE* file) |
{ |
dword res; |
if (c==EOF) |
return EOF; |
if (file->filepos<=0 || file->filepos>file->filesize) |
return EOF; |
file->filepos--; |
file->buffer[file->filepos]=(char)c; |
return c; |
res=_ksys_readfile(file->filename,file->filepos,1,file->buffer); |
if (res==0) |
{ |
return(c); |
} |
else return(EOF); |
} |