sysfile.inc 9.9 KB

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