From c4329e62275c4610c85d5c335aa14c8f2edd1d67 Mon Sep 17 00:00:00 2001 From: victor Date: Sun, 24 Dec 2006 01:01:00 +0000 Subject: [PATCH] -fixed memory leak in fopen/fclose -added support relative path in fopen -make up of build.bat git-svn-id: svn://kolibrios.org@245 a494cfbc-eb01-0410-851d-a64ba20cac60 --- programs/develop/metcc/trunk/libc/build.bat | 10 ++- .../develop/metcc/trunk/libc/file/fclose.c | 1 + .../develop/metcc/trunk/libc/file/fopen.c | 71 ++++++++++++++++--- .../develop/metcc/trunk/libc/start/start.asm | 1 + 4 files changed, 72 insertions(+), 11 deletions(-) diff --git a/programs/develop/metcc/trunk/libc/build.bat b/programs/develop/metcc/trunk/libc/build.bat index 7a6add95b4..04dc47bc96 100644 --- a/programs/develop/metcc/trunk/libc/build.bat +++ b/programs/develop/metcc/trunk/libc/build.bat @@ -1,4 +1,8 @@ @echo off +echo #################################################### +echo # Melibc builder # +echo # usage: build [clean] # +echo #################################################### rem #### CONFIG SECTION #### set LIBNAME=melibc.a set INCLUDE=include @@ -56,7 +60,9 @@ pause exit 1 :Exit_OK -echo all operations has been done... -echo for cleaning run this script with param " clean" +echo #################################################### +echo # All operations has been done... # +echo # For cleaning run this script with param " clean" # +echo #################################################### pause exit 0 \ No newline at end of file diff --git a/programs/develop/metcc/trunk/libc/file/fclose.c b/programs/develop/metcc/trunk/libc/file/fclose.c index 8373bbf4d9..1715085889 100644 --- a/programs/develop/metcc/trunk/libc/file/fclose.c +++ b/programs/develop/metcc/trunk/libc/file/fclose.c @@ -5,6 +5,7 @@ int 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; } \ No newline at end of file diff --git a/programs/develop/metcc/trunk/libc/file/fopen.c b/programs/develop/metcc/trunk/libc/file/fopen.c index 52b696abdb..9df127fd0e 100644 --- a/programs/develop/metcc/trunk/libc/file/fopen.c +++ b/programs/develop/metcc/trunk/libc/file/fopen.c @@ -1,5 +1,56 @@ -#include "stdio.h" -#include "string.h" +#include +#include + + +extern struct{int argc; char** argv;} __ARGS; + +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++] = '/'; + 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; + } + } + buff[len]= '\0'; + return buff; +} + + FILE* fopen(const char* filename, const char *mode) { FILE* res; @@ -37,20 +88,22 @@ FILE* fopen(const char* filename, const char *mode) res->buffersize=0; res->filesize=0; res->filepos=0; - res->filename=0; res->mode=imode; + res->filename=getfullpath(filename); //check if file exists - res=_msys_read_file(filename, 0, 0, 0, &res->filesize); + res=_msys_read_file(res->filename, 0, 0, 0, &res->filesize); if (res==5) { if ((imode & 3) == FILE_OPEN_READ) { + free(res->filename); free(res); return 0; } - res=_msys_create_file(filename); + res=_msys_create_file(res->filename); if (res!=0) { + free(res->filename); free(res); return 0; } @@ -62,8 +115,9 @@ FILE* fopen(const char* filename, const char *mode) res->buffer=malloc(res->buffersize); if (res->buffer=0) { - free(res); - return 0; + free(res->filename); + free(res); + return 0; } res->filesize=0; }else @@ -75,7 +129,7 @@ FILE* fopen(const char* filename, const char *mode) free(res); return 0; } - res=_msys_read_file(filename,0,res->filesize,res->buffer,0); + res=_msys_read_file(res->filename,0,res->filesize,res->buffer,0); if (res!=0) { free(res->buffer); @@ -84,6 +138,5 @@ FILE* fopen(const char* filename, const char *mode) if (imode & 3==FILE_OPEN_APPEND) res->filepos=res->filesize; } - res->filename=strdup(filename); return res; } diff --git a/programs/develop/metcc/trunk/libc/start/start.asm b/programs/develop/metcc/trunk/libc/start/start.asm index be6adf70ab..eae03a659f 100644 --- a/programs/develop/metcc/trunk/libc/start/start.asm +++ b/programs/develop/metcc/trunk/libc/start/start.asm @@ -3,6 +3,7 @@ section '.text' executable public start extrn mf_init extrn main +public argc as '__ARGS' __DEBUG__ equ 1 __DEBUG_LEVEL__ equ 1