sysfile.inc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. MorphOS File-handling Support Functions
  13. *****************************************************************************}
  14. type
  15. { AmigaOS does not automatically close opened files on exit back to }
  16. { the operating system, therefore as a precuation we close all files }
  17. { manually on exit. }
  18. PFileList = ^TFileList;
  19. TFileList = record { no packed, must be correctly aligned }
  20. handle : LongInt; { Handle to file }
  21. next : PFileList; { Next file in list }
  22. end;
  23. var
  24. MOS_fileList: PFileList; public name 'MOS_FILELIST'; { List pointer to opened files }
  25. { Function to be called at program shutdown, to close all opened files }
  26. procedure CloseList(l: PFileList);
  27. var
  28. tmpNext : PFileList;
  29. tmpHandle : LongInt;
  30. begin
  31. if l=nil then exit;
  32. { First, close all tracked files }
  33. tmpNext:=l^.next;
  34. while tmpNext<>nil do begin
  35. tmpHandle:=tmpNext^.handle;
  36. if (tmpHandle<>StdInputHandle) and (tmpHandle<>StdOutputHandle)
  37. and (tmpHandle<>StdErrorHandle) then begin
  38. dosClose(tmpHandle);
  39. end;
  40. tmpNext:=tmpNext^.next;
  41. end;
  42. { Next, erase the linked list }
  43. while l<>nil do begin
  44. tmpNext:=l;
  45. l:=l^.next;
  46. dispose(tmpNext);
  47. end;
  48. end;
  49. { Function to be called to add a file to the opened file list }
  50. procedure AddToList(var l: PFileList; h: LongInt); alias: 'ADDTOLIST'; [public];
  51. var
  52. p : PFileList;
  53. inList: Boolean;
  54. begin
  55. inList:=False;
  56. if l<>nil then begin
  57. { if there is a valid filelist, search for the value }
  58. { in the list to avoid double additions }
  59. p:=l;
  60. while (p^.next<>nil) and (not inList) do
  61. if p^.next^.handle=h then inList:=True
  62. else p:=p^.next;
  63. p:=nil;
  64. end else begin
  65. { if the list is not yet allocated, allocate it. }
  66. New(l);
  67. l^.next:=nil;
  68. end;
  69. if not inList then begin
  70. New(p);
  71. p^.handle:=h;
  72. p^.next:=l^.next;
  73. l^.next:=p;
  74. end;
  75. end;
  76. { Function to be called to remove a file from the list }
  77. procedure RemoveFromList(var l: PFileList; h: LongInt); alias: 'REMOVEFROMLIST'; [public];
  78. var
  79. p : PFileList;
  80. inList: Boolean;
  81. begin
  82. if l=nil then exit;
  83. inList:=False;
  84. p:=l;
  85. while (p^.next<>nil) and (not inList) do
  86. if p^.next^.handle=h then inList:=True
  87. else p:=p^.next;
  88. if p^.next<>nil then begin
  89. dispose(p^.next);
  90. p^.next:=p^.next^.next;
  91. end;
  92. end;
  93. {****************************************************************************
  94. Low level File Routines
  95. All these functions can set InOutRes on errors
  96. ****************************************************************************}
  97. { close a file from the handle value }
  98. procedure do_close(handle : longint);
  99. begin
  100. if (handle<=0) then exit;
  101. RemoveFromList(MOS_fileList,handle);
  102. { Do _NOT_ check CTRL_C on Close, because it will conflict
  103. with System_Exit! }
  104. if not dosClose(handle) then
  105. dosError2InOut(IoErr);
  106. end;
  107. procedure do_erase(p : pchar);
  108. begin
  109. checkCTRLC;
  110. if not dosDeleteFile(p) then
  111. dosError2InOut(IoErr);
  112. end;
  113. procedure do_rename(p1,p2 : pchar);
  114. begin
  115. checkCTRLC;
  116. if not dosRename(p1,p2) then
  117. dosError2InOut(IoErr);
  118. end;
  119. function do_write(h:longint; addr: pointer; len: longint) : longint;
  120. var dosResult: LongInt;
  121. begin
  122. checkCTRLC;
  123. do_write:=0;
  124. if (len<=0) or (h<=0) then exit;
  125. dosResult:=dosWrite(h,addr,len);
  126. if dosResult<0 then begin
  127. dosError2InOut(IoErr);
  128. end else begin
  129. do_write:=dosResult;
  130. end;
  131. end;
  132. function do_read(h:longint; addr: pointer; len: longint) : longint;
  133. var dosResult: LongInt;
  134. begin
  135. checkCTRLC;
  136. do_read:=0;
  137. if (len<=0) or (h<=0) then exit;
  138. dosResult:=dosRead(h,addr,len);
  139. if dosResult<0 then begin
  140. dosError2InOut(IoErr);
  141. end else begin
  142. do_read:=dosResult;
  143. end
  144. end;
  145. function do_filepos(handle : longint) : longint;
  146. var dosResult: LongInt;
  147. begin
  148. checkCTRLC;
  149. do_filepos:=-1;
  150. if (handle<=0) then exit;
  151. { Seeking zero from OFFSET_CURRENT to find out where we are }
  152. dosResult:=dosSeek(handle,0,OFFSET_CURRENT);
  153. if dosResult<0 then begin
  154. dosError2InOut(IoErr);
  155. end else begin
  156. do_filepos:=dosResult;
  157. end;
  158. end;
  159. procedure do_seek(handle,pos : longint);
  160. begin
  161. checkCTRLC;
  162. if (handle<=0) then exit;
  163. { Seeking from OFFSET_BEGINNING }
  164. if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
  165. dosError2InOut(IoErr);
  166. end;
  167. function do_seekend(handle:longint):longint;
  168. var dosResult: LongInt;
  169. begin
  170. checkCTRLC;
  171. do_seekend:=-1;
  172. if (handle<=0) then exit;
  173. { Seeking to OFFSET_END }
  174. dosResult:=dosSeek(handle,0,OFFSET_END);
  175. if dosResult<0 then begin
  176. dosError2InOut(IoErr);
  177. end else begin
  178. do_seekend:=dosResult;
  179. end
  180. end;
  181. function do_filesize(handle : longint) : longint;
  182. var currfilepos: longint;
  183. begin
  184. checkCTRLC;
  185. do_filesize:=-1;
  186. if (handle<=0) then exit;
  187. currfilepos:=do_filepos(handle);
  188. { We have to do this twice, because seek returns the OLD position }
  189. do_filesize:=do_seekend(handle);
  190. do_filesize:=do_seekend(handle);
  191. do_seek(handle,currfilepos)
  192. end;
  193. { truncate at a given position }
  194. procedure do_truncate (handle,pos:longint);
  195. begin
  196. checkCTRLC;
  197. if (handle<=0) then exit;
  198. { Seeking from OFFSET_BEGINNING }
  199. if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
  200. dosError2InOut(IoErr);
  201. end;
  202. procedure do_open(var f;p:pchar;flags:longint);
  203. {
  204. filerec and textrec have both handle and mode as the first items so
  205. they could use the same routine for opening/creating.
  206. when (flags and $10) the file will be append
  207. when (flags and $100) the file will be truncate/rewritten
  208. when (flags and $1000) there is no check for close (needed for textfiles)
  209. }
  210. var
  211. handle : LongInt;
  212. openflags: LongInt;
  213. tmpStr : array[0..255] of Char;
  214. begin
  215. tmpStr:=PathConv(strpas(p))+#0;
  216. { close first if opened }
  217. if ((flags and $10000)=0) then begin
  218. case filerec(f).mode of
  219. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  220. fmclosed : ;
  221. else begin
  222. inoutres:=102; {not assigned}
  223. exit;
  224. end;
  225. end;
  226. end;
  227. { reset file handle }
  228. filerec(f).handle:=UnusedHandle;
  229. { convert filemode to filerec modes }
  230. { READ/WRITE on existing file }
  231. { RESET/APPEND }
  232. openflags:=MODE_OLDFILE;
  233. case (flags and 3) of
  234. 0 : filerec(f).mode:=fminput;
  235. 1 : filerec(f).mode:=fmoutput;
  236. 2 : filerec(f).mode:=fminout;
  237. end;
  238. { rewrite (create a new file) }
  239. if (flags and $1000)<>0 then openflags:=MODE_NEWFILE;
  240. { empty name is special }
  241. if p[0]=#0 then begin
  242. case filerec(f).mode of
  243. fminput :
  244. filerec(f).handle:=StdInputHandle;
  245. fmappend,
  246. fmoutput : begin
  247. filerec(f).handle:=StdOutputHandle;
  248. filerec(f).mode:=fmoutput; {fool fmappend}
  249. end;
  250. end;
  251. exit;
  252. end;
  253. handle:=Open(@tmpStr,openflags);
  254. if handle=0 then begin
  255. dosError2InOut(IoErr);
  256. end else begin
  257. AddToList(MOS_fileList,handle);
  258. filerec(f).handle:=handle;
  259. end;
  260. { append mode }
  261. if ((Flags and $100)<>0) and
  262. (FileRec(F).Handle<>UnusedHandle) then begin
  263. do_seekend(filerec(f).handle);
  264. filerec(f).mode:=fmoutput; {fool fmappend}
  265. end;
  266. end;
  267. function do_isdevice(handle:longint):boolean;
  268. begin
  269. if (handle=StdOutputHandle) or (handle=StdInputHandle) or
  270. (handle=StdErrorHandle) then
  271. do_isdevice:=True
  272. else
  273. do_isdevice:=False;
  274. end;