sysfile.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2016 by Free Pascal development team
  4. Low level file functions for Atari TOS
  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:=gemdos_fclose(handle);
  21. if dosResult < 0 then
  22. Error2InOutRes(dosResult);
  23. end;
  24. procedure do_erase(p : pchar; pchangeable: boolean);
  25. var
  26. oldp: pchar;
  27. dosResult: longint;
  28. begin
  29. oldp:=p;
  30. DoDirSeparators(p,pchangeable);
  31. dosResult:=gemdos_fdelete(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 : pchar;
  40. dosResult: longint;
  41. begin
  42. oldp1:=p1;
  43. oldp2:=p2;
  44. DoDirSeparators(p1,p1changeable);
  45. DoDirSeparators(p2,p2changeable);
  46. dosResult:=gemdos_frename(0,p1,p2);
  47. if dosResult < 0 then
  48. Error2InOutRes(dosResult);
  49. if oldp1<>p1 then
  50. FreeMem(p1);
  51. if oldp2<>p2 then
  52. FreeMem(p2);
  53. end;
  54. function do_write(h: longint; addr: pointer; len: longint) : longint;
  55. var
  56. dosResult: longint;
  57. begin
  58. do_write:=0;
  59. if (len<=0) or (h=-1) then
  60. exit;
  61. dosResult:=gemdos_fwrite(h, len, addr);
  62. if dosResult < 0 then
  63. begin
  64. Error2InOutRes(dosResult);
  65. end
  66. else
  67. do_write:=dosResult;
  68. end;
  69. function do_read(h: longint; addr: pointer; len: longint) : longint;
  70. var
  71. dosResult: longint;
  72. begin
  73. do_read:=0;
  74. if (len<=0) or (h=-1) then exit;
  75. dosResult:=gemdos_fread(h, len, addr);
  76. if dosResult<0 then
  77. begin
  78. Error2InOutRes(dosResult);
  79. end
  80. else
  81. do_read:=dosResult;
  82. end;
  83. function do_filepos(handle: longint) : longint;
  84. var
  85. dosResult: longint;
  86. begin
  87. do_filepos:=-1;
  88. dosResult:=gemdos_fseek(0, handle, SEEK_FROM_CURRENT);
  89. if dosResult < 0 then
  90. begin
  91. Error2InOutRes(dosResult);
  92. end
  93. else
  94. do_filepos:=dosResult;
  95. end;
  96. procedure do_seek(handle, pos: longint);
  97. var
  98. dosResult: longint;
  99. begin
  100. dosResult:=gemdos_fseek(pos, handle, SEEK_FROM_START);
  101. if dosResult < 0 then
  102. Error2InOutRes(dosResult);
  103. end;
  104. function do_seekend(handle: longint):longint;
  105. var
  106. dosResult: longint;
  107. begin
  108. do_seekend:=-1;
  109. dosResult:=gemdos_fseek(0, handle, SEEK_FROM_END);
  110. if dosResult < 0 then
  111. begin
  112. Error2InOutRes(dosResult);
  113. end
  114. else
  115. do_seekend:=dosResult;
  116. end;
  117. function do_filesize(handle : THandle) : longint;
  118. var
  119. currfilepos: longint;
  120. begin
  121. do_filesize:=-1;
  122. currfilepos:=do_filepos(handle);
  123. if currfilepos >= 0 then
  124. begin
  125. do_filesize:=do_seekend(handle);
  126. end;
  127. do_seek(handle,currfilepos);
  128. end;
  129. { truncate at a given position }
  130. procedure do_truncate(handle, pos: longint);
  131. begin
  132. { TODO: }
  133. end;
  134. procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
  135. {
  136. filerec and textrec have both handle and mode as the first items so
  137. they could use the same routine for opening/creating.
  138. when (flags and $100) the file will be append
  139. when (flags and $1000) the file will be truncate/rewritten
  140. when (flags and $10000) there is no check for close (needed for textfiles)
  141. }
  142. var
  143. oldp : pchar;
  144. dosResult: longint;
  145. begin
  146. { close first if opened }
  147. if ((flags and $10000)=0) then
  148. begin
  149. case filerec(f).mode of
  150. fmInput, fmOutput, fmInout:
  151. do_close(filerec(f).handle);
  152. fmClosed: ;
  153. else
  154. begin
  155. InOutRes:=102; {not assigned}
  156. exit;
  157. end;
  158. end;
  159. end;
  160. { reset file handle }
  161. filerec(f).handle:=UnusedHandle;
  162. { convert filemode to filerec modes }
  163. case (flags and 3) of
  164. 0 : filerec(f).mode:=fmInput;
  165. 1 : filerec(f).mode:=fmOutput;
  166. 2 : filerec(f).mode:=fmInout;
  167. end;
  168. { empty name is special }
  169. if p[0]=#0 then begin
  170. case filerec(f).mode of
  171. fminput :
  172. filerec(f).handle:=StdInputHandle;
  173. fmappend,
  174. fmoutput : begin
  175. filerec(f).handle:=StdOutputHandle;
  176. filerec(f).mode:=fmOutput; {fool fmappend}
  177. end;
  178. end;
  179. exit;
  180. end;
  181. oldp:=p;
  182. DoDirSeparators(p);
  183. { rewrite (create a new file) }
  184. if (flags and $1000)<>0 then
  185. dosResult:=gemdos_fcreate(p,0)
  186. else
  187. dosResult:=gemdos_fopen(p,flags and 3);
  188. if oldp<>p then
  189. freemem(p);
  190. if dosResult < 0 then
  191. begin
  192. Error2InOutRes(dosResult);
  193. filerec(f).mode:=fmClosed;
  194. exit;
  195. end
  196. else
  197. filerec(f).handle:=word(dosResult);
  198. { append mode }
  199. if ((Flags and $100)<>0) and
  200. (FileRec(F).Handle<>UnusedHandle) then begin
  201. do_seekend(filerec(f).handle);
  202. filerec(f).mode:=fmOutput; {fool fmappend}
  203. end;
  204. end;
  205. function do_isdevice(handle: thandle): boolean;
  206. begin
  207. if (handle=StdOutputHandle) or (handle=StdInputHandle) or
  208. (handle=StdErrorHandle) then
  209. do_isdevice:=True
  210. else
  211. do_isdevice:=False;
  212. end;