filutil.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1998 by the Free Pascal development team
  5. File utility calls
  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. { private functions }
  14. {******************************************************************************}
  15. { some internal constants }
  16. const
  17. ofRead = $0000; { Open for reading }
  18. ofWrite = $0001; { Open for writing }
  19. ofReadWrite = $0002; { Open for reading/writing }
  20. faFail = $0000; { Fail if file does not exist }
  21. faCreate = $0010; { Create if file does not exist }
  22. faOpen = $0001; { Open if file exists }
  23. faOpenReplace = $0002; { Clear if file exists }
  24. { converts S to a pchar and copies it to the transfer-buffer. }
  25. procedure StringToTB(const S: string);
  26. var P: pchar; Len: integer;
  27. begin
  28. Len := Length(S) + 1;
  29. P := StrPCopy(StrAlloc(Len), S);
  30. SysCopyToDos(longint(P), Len);
  31. StrDispose(P);
  32. end ;
  33. { Native OpenFile function.
  34. if return value <> 0 call failed. }
  35. function OpenFile(const FileName: string; var Handle: longint; Mode, Action: word): longint;
  36. var
  37. Regs: registers;
  38. begin
  39. result := 0;
  40. Handle := 0;
  41. StringToTB(FileName);
  42. if LFNSupport then Regs.Eax:=$716c
  43. else Regs.Eax:=$6c00;
  44. Regs.Edx := Action; { Action if file exists/not exists }
  45. Regs.Ds := tb_segment;
  46. Regs.Esi := tb_offset;
  47. Regs.Ebx := $2000 + (Mode and $ff); { file open mode }
  48. Regs.Ecx := $20; { Attributes }
  49. RealIntr($21, Regs);
  50. if Regs.Flags and CarryFlag <> 0 then result := Regs.Ax
  51. else Handle := Regs.Eax;
  52. end ;
  53. {******************************************************************************}
  54. { Public functions }
  55. {******************************************************************************}
  56. Function FileOpen (Const FileName : string; Mode : Integer) : Longint;
  57. var e: integer;
  58. Begin
  59. e := OpenFile(FileName, result, Mode, faOpen);
  60. if e <> 0 then result := -1;
  61. end ;
  62. Function FileCreate (Const FileName : String) : Longint;
  63. var e: integer;
  64. begin
  65. e := OpenFile(FileName, result, ofReadWrite, faCreate or faOpenReplace);
  66. if e <> 0 then result := -1;
  67. end;
  68. Function FileRead (Handle : Longint; Var Buffer; Count : longint) : Longint;
  69. begin
  70. result := Do_Read(Handle, longint(@Buffer), Count);
  71. end;
  72. Function FileWrite (Handle : Longint; Var Buffer; Count : Longint) : Longint;
  73. begin
  74. result := Do_Write(Handle, longint(@Buffer), Count);
  75. end;
  76. Function FileSeek (Handle, Offset, Origin : Longint) : Longint;
  77. var Regs: registers;
  78. begin
  79. Regs.Eax := $4200;
  80. Regs.Al := Origin;
  81. Regs.Edx := Lo(Offset);
  82. Regs.Ecx := Hi(Offset);
  83. Regs.Ebx := Handle;
  84. RealIntr($21, Regs);
  85. if Regs.Flags and CarryFlag <> 0 then
  86. result := -1
  87. else begin
  88. LongRec(result).Lo := Regs.Eax;
  89. LongRec(result).Hi := Regs.Edx;
  90. end ;
  91. end;
  92. Procedure FileClose (Handle : Longint);
  93. var Regs: registers;
  94. begin
  95. Regs.Eax := $3e00;
  96. Regs.Ebx := Handle;
  97. RealIntr($21, Regs);
  98. end;
  99. Function FileAge (Const FileName : String): Longint;
  100. var Handle: longint;
  101. begin
  102. Handle := FileOpen(FileName, 0);
  103. if Handle <> -1 then begin
  104. result := FileGetDate(Handle);
  105. FileClose(Handle);
  106. end
  107. else result := -1;
  108. end;
  109. Function FileExists (Const FileName : String) : Boolean;
  110. var Handle: longint;
  111. begin
  112. //!! This can be done quicker, need to find out how
  113. Result := (OpenFile(FileName, Handle, ofRead, faOpen) = 0);
  114. if Handle <> 0 then
  115. FileClose(Handle);
  116. end;
  117. Type PSearchrec = ^Searchrec;
  118. Function FindFirst (Const Path : String; Attr : Longint; Var Rslt : TSearchRec) : Longint;
  119. Var Sr : PSearchrec;
  120. begin
  121. //!! Sr := New(PSearchRec);
  122. getmem(sr,sizeof(searchrec));
  123. Rslt.FindHandle := longint(Sr);
  124. DOS.FindFirst(Path, Attr, Sr^);
  125. result := -DosError;
  126. if result = 0 then begin
  127. Rslt.Time := Sr^.Time;
  128. Rslt.Size := Sr^.Size;
  129. Rslt.Attr := Sr^.Attr;
  130. Rslt.ExcludeAttr := 0;
  131. Rslt.Name := Sr^.Name;
  132. end ;
  133. end;
  134. Function FindNext (Var Rslt : TSearchRec) : Longint;
  135. var Sr: PSearchRec;
  136. begin
  137. Sr := PSearchRec(Rslt.FindHandle);
  138. if Sr <> nil then begin
  139. DOS.FindNext(Sr^);
  140. result := -DosError;
  141. if result = 0 then begin
  142. Rslt.Time := Sr^.Time;
  143. Rslt.Size := Sr^.Size;
  144. Rslt.Attr := Sr^.Attr;
  145. Rslt.ExcludeAttr := 0;
  146. Rslt.Name := Sr^.Name;
  147. end ;
  148. end ;
  149. end;
  150. Procedure FindClose (Var F : TSearchrec);
  151. var Sr: PSearchRec;
  152. begin
  153. Sr := PSearchRec(F.FindHandle);
  154. if Sr <> nil then
  155. //!! Dispose(Sr);
  156. freemem(sr,sizeof(tsearchrec));
  157. F.FindHandle := 0;
  158. end;
  159. Function FileGetDate (Handle : Longint) : Longint;
  160. var Regs: registers;
  161. begin
  162. //!! for win95 an alternative function is available.
  163. Regs.Ebx := Handle;
  164. Regs.Eax := $5700;
  165. RealIntr($21, Regs);
  166. if Regs.Flags and CarryFlag <> 0 then result := -Regs.Ax
  167. else begin
  168. LongRec(result).Lo := Regs.Edx;
  169. LongRec(result).Hi := Regs.Eax;
  170. end ;
  171. end;
  172. Function FileSetDate (Handle, Age : Longint) : Longint;
  173. var Regs: registers;
  174. begin
  175. Regs.Ebx := Handle;
  176. Regs.Eax := $5701;
  177. Regs.Ecx := Lo(Age);
  178. Regs.Edx := Hi(Age);
  179. RealIntr($21, Regs);
  180. if Regs.Flags and CarryFlag <> 0 then result := -Regs.Ax
  181. else result := 0;
  182. end;
  183. Function FileGetAttr (Const FileName : String) : Longint;
  184. var Regs: registers;
  185. begin
  186. StringToTB(FileName);
  187. Regs.Edx := tb_offset;
  188. Regs.Ds := tb_segment;
  189. if LFNSupport then
  190. begin
  191. Regs.Ax := $7143;
  192. Regs.Bx := 0;
  193. end
  194. else
  195. Regs.Ax := $4300;
  196. RealIntr($21, Regs);
  197. if Regs.Flags and CarryFlag <> 0 then
  198. result := -1
  199. else
  200. result := Regs.Cx;
  201. end;
  202. Function FileSetAttr (Const Filename : String; Attr: longint) : Longint;
  203. var Regs: registers;
  204. begin
  205. StringToTB(FileName);
  206. Regs.Edx := tb_offset;
  207. Regs.Ds := tb_segment;
  208. if LFNSupport then
  209. begin
  210. Regs.Ax := $7143;
  211. Regs.Bx := 1;
  212. end
  213. else
  214. Regs.Ax := $4301;
  215. Regs.Cx := Attr;
  216. RealIntr($21, Regs);
  217. if Regs.Flags and CarryFlag <> 0 then result := -Regs.Ax
  218. else result := 0;
  219. end;
  220. Function DeleteFile (Const FileName : String) : Boolean;
  221. var Regs: registers;
  222. begin
  223. StringToTB(FileName);
  224. Regs.Edx := tb_offset;
  225. Regs.Ds := tb_offset;
  226. if LFNSupport then
  227. Regs.Eax := $7141
  228. else
  229. Regs.Eax := $4100;
  230. Regs.Esi := 0;
  231. Regs.Ecx := 0;
  232. RealIntr($21, Regs);
  233. result := (Regs.Flags and CarryFlag = 0);
  234. end;
  235. Function RenameFile (Const OldName, NewName : String) : Boolean;
  236. var Regs: registers;
  237. begin
  238. StringToTB(OldName + #0 + NewName);
  239. Regs.Edx := tb_offset;
  240. Regs.Ds := tb_segment;
  241. Regs.Edi := tb_offset + Length(OldName) + 1;
  242. Regs.Es := tb_segment;
  243. if LFNSupport then
  244. Regs.Eax := $7156
  245. else
  246. Regs.Eax := $5600;
  247. Regs.Ecx := $ff;
  248. RealIntr($21, Regs);
  249. result := (Regs.Flags and CarryFlag = 0);
  250. end;
  251. Function FileSearch (Const Name, DirList : String) : String;
  252. begin
  253. result := DOS.FSearch(Name, DirList);
  254. end;
  255. {
  256. $Log$
  257. Revision 1.4 1998-10-29 13:16:19 michael
  258. * Fix for fileseek by gertjan schouten
  259. Revision 1.3 1998/10/15 09:39:13 michael
  260. Changes from Gretjan Schouten
  261. Revision 1.2 1998/10/12 08:02:16 michael
  262. wrong file committed
  263. Revision 1.1 1998/10/11 12:21:01 michael
  264. Added file calls. Implemented for linux only
  265. }