* fixed AddDisk() in sysutils and dos units, and fixed memory leaks

related to this functionality, based on analysis/patch by Michael
    Kamburelis (mantis #9985)

git-svn-id: trunk@12814 -
This commit is contained in:
Jonas Maebe 2009-02-27 22:02:24 +00:00
parent 9e7efd7321
commit f7003119d0
5 changed files with 87 additions and 5 deletions

2
.gitattributes vendored
View File

@ -9645,6 +9645,8 @@ tests/webtbs/tw9894a.pp svneol=native#text/plain
tests/webtbs/tw9897.pp svneol=native#text/plain
tests/webtbs/tw9918.pp svneol=native#text/plain
tests/webtbs/tw9919.pp -text
tests/webtbs/tw9985.pp svneol=native#text/plain
tests/webtbs/tw9985a.pp svneol=native#text/plain
tests/webtbs/ub1873.pp svneol=native#text/plain
tests/webtbs/ub1883.pp svneol=native#text/plain
tests/webtbs/uw0555.pp svneol=native#text/plain

View File

@ -388,13 +388,13 @@ var
Function AddDisk(const path:string) : byte;
begin
if not (DriveStr[Drives]=nil) then
FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
FreeMem(DriveStr[Drives]);
GetMem(DriveStr[Drives],length(Path)+1);
StrPCopy(DriveStr[Drives],path);
AddDisk:=Drives;
inc(Drives);
if Drives>26 then
Drives:=4;
AddDisk:=Drives;
end;
@ -424,6 +424,19 @@ Begin
End;
Procedure FreeDriveStr;
var
i: longint;
begin
for i:=low(drivestr) to high(drivestr) do
if assigned(drivestr[i]) then
begin
freemem(drivestr[i]);
drivestr[i]:=nil;
end;
end;
{******************************************************************************
--- Findfirst FindNext ---
******************************************************************************}
@ -895,4 +908,6 @@ End;
--- Initialization ---
******************************************************************************}
Finalization
FreeDriveStr;
End.

View File

@ -705,19 +705,19 @@ Const
'/.'
);
var
Drives : byte;
Drives : byte = 4;
DriveStr : array[4..26] of pchar;
Function AddDisk(const path:string) : Byte;
begin
if not (DriveStr[Drives]=nil) then
FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
FreeMem(DriveStr[Drives]);
GetMem(DriveStr[Drives],length(Path)+1);
StrPCopy(DriveStr[Drives],path);
Result:=Drives;
inc(Drives);
if Drives>26 then
Drives:=4;
Result:=Drives;
end;
@ -746,6 +746,20 @@ Begin
End;
Procedure FreeDriveStr;
var
i: longint;
begin
for i:=low(drivestr) to high(drivestr) do
if assigned(drivestr[i]) then
begin
freemem(drivestr[i]);
drivestr[i]:=nil;
end;
end;
Function GetCurrentDir : String;
begin
GetDir (0,Result);
@ -1155,5 +1169,6 @@ Initialization
InitInternational; { Initialize internationalization settings }
SysConfigDir:='/etc'; { Initialize system config dir }
Finalization
FreeDriveStr;
DoneExceptions;
end.

25
tests/webtbs/tw9985.pp Normal file
View File

@ -0,0 +1,25 @@
{ %opt=-gh }
{$mode objfpc}
uses SysUtils;
var
DiskNum: Byte;
begin
HaltOnNotReleased := true;
Writeln(DiskFree(3), '/', DiskSize(3));
{ Now get disk / by AddDisk. DiskFree and DiskSize below should return
the same (well, assuming that nothing was writeen to disk between
calls...). }
{$ifdef unix}
DiskNum := AddDisk('/');
{$else}
{ dos/windows/os/2 ... Still needs other cases for other OSes }
DiskNum := AddDisk('C:');
{$endif}
Writeln(DiskFree(DiskNum), '/', DiskSize(DiskNum));
if (disksize(3)<>disksize(disknum)) then
halt(1);
end.

25
tests/webtbs/tw9985a.pp Normal file
View File

@ -0,0 +1,25 @@
{ %opt=-gh }
{$mode objfpc}
uses Dos;
var
DiskNum: Byte;
begin
HaltOnNotReleased := true;
Writeln(DiskFree(3), '/', DiskSize(3));
{ Now get disk / by AddDisk. DiskFree and DiskSize below should return
the same (well, assuming that nothing was writeen to disk between
calls...). }
{$ifdef unix}
DiskNum := AddDisk('/');
{$else}
{ dos/windows/os/2 ... Still needs other cases for other OSes }
DiskNum := AddDisk('C:');
{$endif}
Writeln(DiskFree(DiskNum), '/', DiskSize(DiskNum));
if (disksize(3)<>disksize(disknum)) then
halt(1);
end.