sysfile.inc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2001 by Free Pascal development team
  4. Low leve file functions
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {****************************************************************************
  12. Low Level File Routines
  13. ****************************************************************************}
  14. procedure do_close(h:thandle);
  15. begin
  16. { Only three standard handles under real OS/2 }
  17. if h>2 then
  18. begin
  19. InOutRes:=DosClose(h);
  20. end;
  21. {$ifdef IODEBUG}
  22. writeln('do_close: handle=', H, ', InOutRes=', InOutRes);
  23. {$endif}
  24. end;
  25. procedure do_erase(p:Pchar);
  26. begin
  27. DoDirSeparators(p);
  28. inoutres:=DosDelete(p);
  29. end;
  30. procedure do_rename(p1,p2:Pchar);
  31. begin
  32. DoDirSeparators(p1);
  33. DoDirSeparators(p2);
  34. inoutres:=DosMove(p1, p2);
  35. end;
  36. function do_read(h:thandle;addr:pointer;len:longint):longint;
  37. Var
  38. T: cardinal;
  39. begin
  40. {$ifdef IODEBUG}
  41. write('do_read: handle=', h, ', addr=', ptrint(addr), ', length=', len);
  42. {$endif}
  43. InOutRes:=DosRead(H, Addr, Len, T);
  44. do_read:= longint (T);
  45. {$ifdef IODEBUG}
  46. writeln(', actual_len=', t, ', InOutRes=', InOutRes);
  47. {$endif}
  48. end;
  49. function do_write(h:thandle;addr:pointer;len:longint) : longint;
  50. Var
  51. T: cardinal;
  52. begin
  53. {$ifdef IODEBUG}
  54. write('do_write: handle=', h, ', addr=', ptrint(addr), ', length=', len);
  55. {$endif}
  56. InOutRes:=DosWrite(H, Addr, Len, T);
  57. do_write:= longint (T);
  58. {$ifdef IODEBUG}
  59. writeln(', actual_len=', t, ', InOutRes=', InOutRes);
  60. {$endif}
  61. end;
  62. function Do_FilePos (Handle: THandle): int64;
  63. var
  64. PosActual: int64;
  65. begin
  66. InOutRes := Sys_DosSetFilePtrL (Handle, 0, 1, PosActual);
  67. Do_FilePos := PosActual;
  68. {$ifdef IODEBUG}
  69. writeln('do_filepos: handle=', Handle, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
  70. {$endif}
  71. end;
  72. procedure Do_Seek (Handle: THandle; Pos: int64);
  73. var
  74. PosActual: int64;
  75. begin
  76. InOutRes:=Sys_DosSetFilePtrL(Handle, Pos, 0 {ZeroBased}, PosActual);
  77. {$ifdef IODEBUG}
  78. writeln('do_seek: handle=', Handle, ', pos=', pos, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
  79. {$endif}
  80. end;
  81. function Do_SeekEnd (Handle: THandle): int64;
  82. var
  83. PosActual: int64;
  84. begin
  85. InOutRes := Sys_DosSetFilePtrL (Handle, 0, 2 {EndBased}, PosActual);
  86. Do_SeekEnd := PosActual;
  87. {$ifdef IODEBUG}
  88. writeln('do_seekend: handle=', Handle, ', actual_pos=', PosActual, ', InOutRes=', InOutRes);
  89. {$endif}
  90. end;
  91. function Do_FileSize (Handle: THandle): int64;
  92. var
  93. AktFilePos: int64;
  94. begin
  95. AktFilePos := Do_FilePos (Handle);
  96. Do_FileSize := Do_SeekEnd (Handle);
  97. Do_Seek (Handle, AktFilePos);
  98. end;
  99. procedure Do_Truncate (Handle: THandle; Pos: int64);
  100. begin
  101. InOutRes := Sys_DosSetFileSizeL (Handle, Pos);
  102. Do_SeekEnd (Handle);
  103. end;
  104. const
  105. FileHandleCount: cardinal = 20;
  106. function Increase_File_Handle_Count: boolean;
  107. var Err: word;
  108. L1: longint;
  109. L2: cardinal;
  110. begin
  111. L1 := 10;
  112. if DosSetRelMaxFH (L1, L2) <> 0 then
  113. Increase_File_Handle_Count := false
  114. else
  115. if L2 > FileHandleCount then
  116. begin
  117. FileHandleCount := L2;
  118. Increase_File_Handle_Count := true;
  119. end
  120. else
  121. Increase_File_Handle_Count := false;
  122. end;
  123. procedure do_open(var f;p:pchar;flags:longint);
  124. {
  125. filerec and textrec have both handle and mode as the first items so
  126. they could use the same routine for opening/creating.
  127. when (flags and $100) the file will be append
  128. when (flags and $1000) the file will be truncate/rewritten
  129. when (flags and $10000) there is no check for close (needed for textfiles)
  130. }
  131. var
  132. Action, Attrib, OpenFlags, FM: Cardinal;
  133. begin
  134. // convert unix slashes to normal slashes
  135. DoDirSeparators(p);
  136. // close first if opened
  137. if ((flags and $10000)=0) then
  138. begin
  139. case filerec(f).mode of
  140. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  141. fmclosed:;
  142. else
  143. begin
  144. inoutres:=102; {not assigned}
  145. exit;
  146. end;
  147. end;
  148. end;
  149. // reset file handle
  150. filerec(f).handle := UnusedHandle;
  151. Attrib:=0;
  152. OpenFlags:=0;
  153. // convert filesharing
  154. FM := Flags and $FF and not (8);
  155. (* DenyNone if sharing not specified. *)
  156. if FM and 112 = 0 then
  157. FM := FM or 64;
  158. // convert filemode to filerec modes and access mode
  159. case (FM and 3) of
  160. 0: filerec(f).mode:=fminput;
  161. 1: filerec(f).mode:=fmoutput;
  162. 2: filerec(f).mode:=fminout;
  163. end;
  164. if (flags and $1000)<>0 then
  165. OpenFlags:=OpenFlags or 2 {doOverwrite} or 16 {doCreate} // Create/overwrite
  166. else
  167. OpenFlags:=OpenFlags or 1 {doOpen}; // Open existing
  168. // Handle Std I/O
  169. if p[0]=#0 then
  170. begin
  171. case FileRec(f).mode of
  172. fminput :
  173. FileRec(f).Handle:=StdInputHandle;
  174. fminout, // this is set by rewrite
  175. fmoutput :
  176. FileRec(f).Handle:=StdOutputHandle;
  177. fmappend :
  178. begin
  179. FileRec(f).Handle:=StdOutputHandle;
  180. FileRec(f).mode:=fmoutput; // fool fmappend
  181. end;
  182. end;
  183. exit;
  184. end;
  185. Attrib:=32 {faArchive};
  186. InOutRes:=Sys_DosOpenL(p, FileRec(F).Handle, Action, 0, Attrib, OpenFlags, FM, nil);
  187. // If too many open files try to set more file handles and open again
  188. if (InOutRes = 4) then
  189. if Increase_File_Handle_Count then
  190. InOutRes:=Sys_DosOpenL(p, FileRec(F).Handle, Action, 0, Attrib, OpenFlags, FM, nil);
  191. If InOutRes<>0 then FileRec(F).Handle:=UnusedHandle;
  192. // If Handle created -> make some things
  193. if (FileRec(F).Handle <> UnusedHandle) then
  194. begin
  195. // Move to end of file for Append command
  196. if ((Flags and $100) <> 0) then
  197. begin
  198. do_seekend(FileRec(F).Handle);
  199. FileRec(F).Mode := fmOutput;
  200. end;
  201. end;
  202. {$ifdef IODEBUG}
  203. writeln('do_open,', filerec(f).handle, ',', filerec(f).name, ',', filerec(f).mode, ', InOutRes=', InOutRes);
  204. {$endif}
  205. end;
  206. function do_isdevice (Handle: THandle): boolean;
  207. var
  208. HT, Attr: cardinal;
  209. begin
  210. do_isdevice:=false;
  211. If DosQueryHType(Handle, HT, Attr)<>0 then exit;
  212. if ht=1 then do_isdevice:=true;
  213. end;
  214. {$ASMMODE ATT}