sysfile.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2023 by Free Pascal development team
  4. Low level file functions for Human 68k (Sharp X68000)
  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. All these functions can set InOutRes on errors
  14. ****************************************************************************}
  15. { close a file from the handle value }
  16. procedure do_close(handle : longint);
  17. var
  18. dosResult: longint;
  19. begin
  20. dosResult:=h68kdos_close(handle);
  21. if dosResult < 0 then
  22. Error2InOutRes(dosResult);
  23. end;
  24. procedure do_erase(p : pchar; pchangeable: boolean);
  25. var
  26. oldp: PAnsiChar;
  27. dosResult: longint;
  28. begin
  29. oldp:=p;
  30. DoDirSeparators(p,pchangeable);
  31. dosResult:=h68kdos_delete(p);
  32. if dosResult <0 then
  33. Error2InOutRes(dosResult);
  34. if oldp<>p then
  35. FreeMem(p);
  36. end;
  37. procedure do_rename(p1,p2 : pchar; p1changeable, p2changeable: boolean);
  38. var
  39. oldp1, oldp2 : PAnsiChar;
  40. dosResult: longint;
  41. begin
  42. oldp1:=p1;
  43. oldp2:=p2;
  44. DoDirSeparators(p1,p1changeable);
  45. DoDirSeparators(p2,p2changeable);
  46. if hi(human68k_vernum) <= 2 then
  47. dosResult:=h68kdos_rename_v2(p1,p2)
  48. else
  49. dosResult:=h68kdos_rename_v3(p1,p2);
  50. if dosResult < 0 then
  51. Error2InOutRes(dosResult);
  52. if oldp1<>p1 then
  53. FreeMem(p1);
  54. if oldp2<>p2 then
  55. FreeMem(p2);
  56. end;
  57. function do_write(h: longint; addr: pointer; len: longint) : longint;
  58. var
  59. dosResult: longint;
  60. begin
  61. do_write:=0;
  62. if (len<=0) or (h=-1) then
  63. exit;
  64. dosResult:=h68kdos_write(h, addr, len);
  65. if dosResult < 0 then
  66. begin
  67. Error2InOutRes(dosResult);
  68. end
  69. else
  70. do_write:=dosResult;
  71. end;
  72. function do_read(h: longint; addr: pointer; len: longint) : longint;
  73. var
  74. dosResult: longint;
  75. begin
  76. do_read:=0;
  77. if (len<=0) or (h=-1) then exit;
  78. dosResult:=h68kdos_read(h, addr, len);
  79. if dosResult<0 then
  80. begin
  81. Error2InOutRes(dosResult);
  82. end
  83. else
  84. do_read:=dosResult;
  85. end;
  86. function do_filepos(handle: longint) : longint;
  87. var
  88. dosResult: longint;
  89. begin
  90. do_filepos:=-1;
  91. dosResult:=h68kdos_seek(handle, 0, SEEK_FROM_CURRENT);
  92. if dosResult < 0 then
  93. begin
  94. Error2InOutRes(dosResult);
  95. end
  96. else
  97. do_filepos:=dosResult;
  98. end;
  99. procedure do_seek(handle, pos: longint);
  100. var
  101. dosResult: longint;
  102. begin
  103. dosResult:=h68kdos_seek(handle, pos, SEEK_FROM_START);
  104. if dosResult < 0 then
  105. Error2InOutRes(dosResult);
  106. end;
  107. function do_seekend(handle: longint):longint;
  108. var
  109. dosResult: longint;
  110. begin
  111. do_seekend:=-1;
  112. dosResult:=h68kdos_seek(handle, 0, SEEK_FROM_END);
  113. if dosResult < 0 then
  114. begin
  115. Error2InOutRes(dosResult);
  116. end
  117. else
  118. do_seekend:=dosResult;
  119. end;
  120. function do_filesize(handle : THandle) : longint;
  121. var
  122. currfilepos: longint;
  123. begin
  124. do_filesize:=-1;
  125. currfilepos:=do_filepos(handle);
  126. if currfilepos >= 0 then
  127. begin
  128. do_filesize:=do_seekend(handle);
  129. end;
  130. do_seek(handle,currfilepos);
  131. end;
  132. { truncate at a given position }
  133. procedure do_truncate(handle, pos: longint);
  134. begin
  135. end;
  136. procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
  137. {
  138. filerec and textrec have both handle and mode as the first items so
  139. they could use the same routine for opening/creating.
  140. when (flags and $100) the file will be append
  141. when (flags and $1000) the file will be truncate/rewritten
  142. when (flags and $10000) there is no check for close (needed for textfiles)
  143. }
  144. var
  145. oldp : PAnsiChar;
  146. dosResult: longint;
  147. begin
  148. { close first if opened }
  149. if ((flags and $10000)=0) then
  150. begin
  151. case filerec(f).mode of
  152. fmInput, fmOutput, fmInout:
  153. do_close(filerec(f).handle);
  154. fmClosed: ;
  155. else
  156. begin
  157. InOutRes:=102; {not assigned}
  158. exit;
  159. end;
  160. end;
  161. end;
  162. { reset file handle }
  163. filerec(f).handle:=UnusedHandle;
  164. { convert filemode to filerec modes }
  165. case (flags and 3) of
  166. 0 : filerec(f).mode:=fmInput;
  167. 1 : filerec(f).mode:=fmOutput;
  168. 2 : filerec(f).mode:=fmInout;
  169. end;
  170. { empty name is special }
  171. if p[0]=#0 then begin
  172. case filerec(f).mode of
  173. fminput :
  174. filerec(f).handle:=StdInputHandle;
  175. fmappend,
  176. fmoutput : begin
  177. filerec(f).handle:=StdOutputHandle;
  178. filerec(f).mode:=fmOutput; {fool fmappend}
  179. end;
  180. end;
  181. exit;
  182. end;
  183. oldp:=p;
  184. DoDirSeparators(p);
  185. { rewrite (create a new file) }
  186. if (flags and $1000)<>0 then
  187. dosResult:=h68kdos_create(p,0)
  188. else
  189. dosResult:=h68kdos_open(p,flags and 3);
  190. if oldp<>p then
  191. freemem(p);
  192. if dosResult < 0 then
  193. begin
  194. Error2InOutRes(dosResult);
  195. filerec(f).mode:=fmClosed;
  196. exit;
  197. end
  198. else
  199. filerec(f).handle:=word(dosResult);
  200. { append mode }
  201. if ((Flags and $100)<>0) and
  202. (FileRec(F).Handle<>UnusedHandle) then begin
  203. do_seekend(filerec(f).handle);
  204. filerec(f).mode:=fmOutput; {fool fmappend}
  205. end;
  206. end;
  207. function do_isdevice(handle: thandle): boolean;
  208. begin
  209. { FIX ME! }
  210. { Prefer to return true here as a default answer, as it is less harmful
  211. than false. This basically determines if the file handle is a "device",
  212. for example the console. Returning true here causes a flush before a
  213. read on the file handle which is preferred for consoleio, and a few
  214. other minor behavioral changes, for example shorter stacktraces.
  215. Returning false will cause weird behavior and unprinted lines when
  216. read() and write() is mixed during consoleio. }
  217. do_isdevice:=true;
  218. end;