-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
This commit is contained in:
victor 2006-12-24 01:01:00 +00:00
parent 6ec45316ae
commit c4329e6227
4 changed files with 72 additions and 11 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -1,5 +1,56 @@
#include "stdio.h"
#include "string.h"
#include <stdio.h>
#include <string.h>
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;
}

View File

@ -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