sysfile.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. dosResult:=dosRead(h,addr,len);
  198. if dosResult<0 then begin
  199. dosError2InOut(IoErr);
  200. end else begin
  201. do_read:=dosResult;
  202. end
  203. end;
  204. function do_filepos(handle: longint) : longint;
  205. var dosResult: LongInt;
  206. begin
  207. checkCTRLC;
  208. do_filepos:=-1;
  209. if CheckInList(MOS_fileList,handle)<>nil then begin
  210. { Seeking zero from OFFSET_CURRENT to find out where we are }
  211. dosResult:=dosSeek(handle,0,OFFSET_CURRENT);
  212. if dosResult<0 then begin
  213. dosError2InOut(IoErr);
  214. end else begin
  215. do_filepos:=dosResult;
  216. end;
  217. end;
  218. end;
  219. procedure do_seek(handle, pos: longint);
  220. begin
  221. checkCTRLC;
  222. if CheckInList(MOS_fileList,handle)<>nil then begin
  223. { Seeking from OFFSET_BEGINNING }
  224. if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
  225. dosError2InOut(IoErr);
  226. end;
  227. end;
  228. function do_seekend(handle: longint):longint;
  229. var dosResult: LongInt;
  230. begin
  231. checkCTRLC;
  232. do_seekend:=-1;
  233. if CheckInList(MOS_fileList,handle)<>nil then begin
  234. { Seeking to OFFSET_END }
  235. dosResult:=dosSeek(handle,0,OFFSET_END);
  236. if dosResult<0 then begin
  237. dosError2InOut(IoErr);
  238. end else begin
  239. do_seekend:=dosResult;
  240. end;
  241. end;
  242. end;
  243. function do_filesize(handle : longint) : longint;
  244. var currfilepos: longint;
  245. begin
  246. checkCTRLC;
  247. do_filesize:=-1;
  248. if CheckInList(MOS_fileList,handle)<>nil then begin
  249. currfilepos:=do_filepos(handle);
  250. { We have to do this twice, because seek returns the OLD position }
  251. do_filesize:=do_seekend(handle);
  252. do_filesize:=do_seekend(handle);
  253. do_seek(handle,currfilepos);
  254. end;
  255. end;
  256. { truncate at a given position }
  257. procedure do_truncate(handle, pos: longint);
  258. begin
  259. checkCTRLC;
  260. if CheckInList(MOS_fileList,handle)<>nil then begin
  261. { Seeking from OFFSET_BEGINNING }
  262. if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
  263. dosError2InOut(IoErr);
  264. end;
  265. end;
  266. procedure do_open(var f;p:pchar;flags:longint);
  267. {
  268. filerec and textrec have both handle and mode as the first items so
  269. they could use the same routine for opening/creating.
  270. when (flags and $10) the file will be append
  271. when (flags and $100) the file will be truncate/rewritten
  272. when (flags and $1000) there is no check for close (needed for textfiles)
  273. }
  274. var
  275. handle : LongInt;
  276. openflags: LongInt;
  277. tmpStr : array[0..255] of Char;
  278. begin
  279. tmpStr:=PathConv(strpas(p))+#0;
  280. { close first if opened }
  281. if ((flags and $10000)=0) then begin
  282. case filerec(f).mode of
  283. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  284. fmclosed : ;
  285. else begin
  286. inoutres:=102; {not assigned}
  287. exit;
  288. end;
  289. end;
  290. end;
  291. { reset file handle }
  292. filerec(f).handle:=UnusedHandle;
  293. { convert filemode to filerec modes }
  294. { READ/WRITE on existing file }
  295. { RESET/APPEND }
  296. openflags:=MODE_OLDFILE;
  297. case (flags and 3) of
  298. 0 : filerec(f).mode:=fminput;
  299. 1 : filerec(f).mode:=fmoutput;
  300. 2 : filerec(f).mode:=fminout;
  301. end;
  302. { rewrite (create a new file) }
  303. if (flags and $1000)<>0 then openflags:=MODE_NEWFILE;
  304. { empty name is special }
  305. if p[0]=#0 then begin
  306. case filerec(f).mode of
  307. fminput :
  308. filerec(f).handle:=StdInputHandle;
  309. fmappend,
  310. fmoutput : begin
  311. filerec(f).handle:=StdOutputHandle;
  312. filerec(f).mode:=fmoutput; {fool fmappend}
  313. end;
  314. end;
  315. exit;
  316. end;
  317. handle:=Open(@tmpStr,openflags);
  318. if handle=0 then begin
  319. dosError2InOut(IoErr);
  320. end else begin
  321. AddToList(MOS_fileList,handle);
  322. filerec(f).handle:=handle;
  323. end;
  324. { append mode }
  325. if ((Flags and $100)<>0) and
  326. (FileRec(F).Handle<>UnusedHandle) then begin
  327. do_seekend(filerec(f).handle);
  328. filerec(f).mode:=fmoutput; {fool fmappend}
  329. end;
  330. end;
  331. function do_isdevice(handle: longint): boolean;
  332. begin
  333. if (handle=StdOutputHandle) or (handle=StdInputHandle) or
  334. (handle=StdErrorHandle) then
  335. do_isdevice:=True
  336. else
  337. do_isdevice:=False;
  338. end;