disk.inc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. Disk functions from Delphi's sysutils.pas
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. {
  13. The Diskfree and Disksize functions need a file on the specified drive, since this
  14. is required for the statfs system call.
  15. These filenames are set in drivestr[0..26], and have been preset to :
  16. 0 - '.' (default drive - hence current dir is ok.)
  17. 1 - '/fd0/.' (floppy drive 1 - should be adapted to local system )
  18. 2 - '/fd1/.' (floppy drive 2 - should be adapted to local system )
  19. 3 - '/' (C: equivalent of dos is the root partition)
  20. 4..26 (can be set by you're own applications)
  21. ! Use AddDisk() to Add new drives !
  22. They both return -1 when a failure occurs.
  23. }
  24. Const
  25. FixDriveStr : array[0..3] of pchar=(
  26. '.',
  27. '/fd0/.',
  28. '/fd1/.',
  29. '/.'
  30. );
  31. var
  32. Drives : byte;
  33. DriveStr : array[4..26] of pchar;
  34. Procedure AddDisk(const path:string);
  35. begin
  36. if not (DriveStr[Drives]=nil) then
  37. FreeMem(DriveStr[Drives],StrLen(DriveStr[Drives])+1);
  38. GetMem(DriveStr[Drives],length(Path)+1);
  39. StrPCopy(DriveStr[Drives],path);
  40. inc(Drives);
  41. if Drives>26 then
  42. Drives:=4;
  43. end;
  44. {$ifdef INT64}
  45. Function DiskFree(Drive: Byte): int64;
  46. var
  47. fs : statfs;
  48. Begin
  49. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  50. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  51. Diskfree:=int64(fs.bavail)*int64(fs.bsize)
  52. else
  53. Diskfree:=-1;
  54. End;
  55. Function DiskSize(Drive: Byte): int64;
  56. var
  57. fs : statfs;
  58. Begin
  59. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  60. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  61. DiskSize:=int64(fs.blocks)*int64(fs.bsize)
  62. else
  63. DiskSize:=-1;
  64. End;
  65. {$else}
  66. Function DiskFree(Drive: Byte): Longint;
  67. var
  68. fs : statfs;
  69. Begin
  70. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  71. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  72. Diskfree:=fs.bavail*fs.bsize
  73. else
  74. Diskfree:=-1;
  75. End;
  76. Function DiskSize(Drive: Byte): Longint;
  77. var
  78. fs : statfs;
  79. Begin
  80. if ((Drive<4) and (not (fixdrivestr[Drive]=nil)) and fsstat(StrPas(fixdrivestr[drive]),fs)) or
  81. ((not (drivestr[Drive]=nil)) and fsstat(StrPas(drivestr[drive]),fs)) then
  82. DiskSize:=fs.blocks*fs.bsize
  83. else
  84. DiskSize:=-1;
  85. End;
  86. {$endif INT64}
  87. Function GetCurrentDir : String;
  88. begin
  89. GetDir (0,Result);
  90. end;
  91. Function SetCurrentDir (Const NewDir : String) : Boolean;
  92. begin
  93. {$I-}
  94. ChDir(NewDir);
  95. result := (IOResult = 0);
  96. {$I+}
  97. end;
  98. Function CreateDir (Const NewDir : String) : Boolean;
  99. begin
  100. {$I-}
  101. MkDir(NewDir);
  102. result := (IOResult = 0);
  103. {$I+}
  104. end;
  105. Function RemoveDir (Const Dir : String) : Boolean;
  106. begin
  107. {$I-}
  108. RmDir(Dir);
  109. result := (IOResult = 0);
  110. {$I+}
  111. end;
  112. {
  113. $Log$
  114. Revision 1.7 2000-05-15 19:28:41 peter
  115. * int64 support for diskfree,disksize
  116. Revision 1.6 2000/02/09 16:59:31 peter
  117. * truncated log
  118. Revision 1.5 2000/01/07 16:41:40 daniel
  119. * copyright 2000
  120. }