sysfile.inc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 ASYS_FPC_FILEDEBUG}
  13. {*****************************************************************************
  14. 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. ASYS_fileList: PFileList; public name 'ASYS_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 ASYS_FPC_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 ASYS_FPC_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. begin
  119. inList:=nil;
  120. if l=nil then begin
  121. CheckInList:=inList;
  122. exit;
  123. end;
  124. p:=l;
  125. while (p^.next<>nil) and (inList=nil) do
  126. if p^.next^.handle=h then inList:=p^.next
  127. else p:=p^.next;
  128. {$IFDEF ASYS_FPC_FILEDEBUG}
  129. if inList=nil then
  130. RawDoFmt('FPC_FILE_DEBUG: Warning! Check for not existing filehandle: $%lx !'+#10,@h,pointer(1),nil);
  131. {$ENDIF}
  132. CheckInList:=inList;
  133. end;
  134. {****************************************************************************
  135. Low level File Routines
  136. All these functions can set InOutRes on errors
  137. ****************************************************************************}
  138. { close a file from the handle value }
  139. procedure do_close(handle : longint);
  140. begin
  141. if RemoveFromList(ASYS_fileList,handle) then begin
  142. { Do _NOT_ check CTRL_C on Close, because it will conflict
  143. with System_Exit! }
  144. if not dosClose(handle) then
  145. dosError2InOut(IoErr);
  146. end;
  147. end;
  148. procedure do_erase(p : pchar; pchangeable: boolean);
  149. var
  150. tmpStr: array[0..255] of Char;
  151. begin
  152. tmpStr:=PathConv(strpas(p))+#0;
  153. checkCTRLC;
  154. if not dosDeleteFile(@tmpStr) then
  155. dosError2InOut(IoErr);
  156. end;
  157. procedure do_rename(p1,p2 : pchar; p1changeable, p2changeable: boolean);
  158. { quite stack-effective code, huh? :) damn path conversions... (KB) }
  159. var
  160. tmpStr1: array[0..255] of Char;
  161. tmpStr2: array[0..255] of Char;
  162. begin
  163. tmpStr1:=PathConv(strpas(p1))+#0;
  164. tmpStr2:=PathConv(strpas(p2))+#0;
  165. checkCTRLC;
  166. if not (dosRename(@tmpStr1,@tmpStr2) <> 0) then
  167. dosError2InOut(IoErr);
  168. end;
  169. function do_write(h: longint; addr: pointer; len: longint) : longint;
  170. var dosResult: LongInt;
  171. begin
  172. checkCTRLC;
  173. do_write:=0;
  174. if (len<=0) or (h=0) or (h=-1) then exit;
  175. {$IFDEF ASYS_FPC_FILEDEBUG}
  176. if not ((h=StdOutputHandle) or (h=StdInputHandle) or
  177. (h=StdErrorHandle)) then CheckInList(ASYS_fileList,h);
  178. {$ENDIF}
  179. dosResult:=dosWrite(h,addr,len);
  180. if dosResult<0 then begin
  181. dosError2InOut(IoErr);
  182. end else begin
  183. do_write:=dosResult;
  184. end;
  185. end;
  186. function do_read(h: longint; addr: pointer; len: longint) : longint;
  187. var dosResult: LongInt;
  188. begin
  189. checkCTRLC;
  190. do_read:=0;
  191. if (len<=0) or (h=0) or (h=-1) then exit;
  192. {$IFDEF ASYS_FPC_FILEDEBUG}
  193. if not ((h=StdOutputHandle) or (h=StdInputHandle) or
  194. (h=StdErrorHandle)) then CheckInList(ASYS_fileList,h);
  195. {$ENDIF}
  196. dosResult:=dosRead(h,addr,len);
  197. if dosResult<0 then begin
  198. dosError2InOut(IoErr);
  199. end else begin
  200. do_read:=dosResult;
  201. end
  202. end;
  203. function do_filepos(handle: longint) : longint;
  204. var dosResult: LongInt;
  205. begin
  206. checkCTRLC;
  207. do_filepos:=-1;
  208. if CheckInList(ASYS_fileList,handle)<>nil then begin
  209. { Seeking zero from OFFSET_CURRENT to find out where we are }
  210. dosResult:=dosSeek(handle,0,OFFSET_CURRENT);
  211. if dosResult<0 then begin
  212. dosError2InOut(IoErr);
  213. end else begin
  214. do_filepos:=dosResult;
  215. end;
  216. end;
  217. end;
  218. procedure do_seek(handle, pos: longint);
  219. begin
  220. checkCTRLC;
  221. if CheckInList(ASYS_fileList,handle)<>nil then begin
  222. { Seeking from OFFSET_BEGINNING }
  223. if dosSeek(handle,pos,OFFSET_BEGINNING)<0 then
  224. dosError2InOut(IoErr);
  225. end;
  226. end;
  227. function do_seekend(handle: longint):longint;
  228. var dosResult: LongInt;
  229. begin
  230. checkCTRLC;
  231. do_seekend:=-1;
  232. if CheckInList(ASYS_fileList,handle)<>nil then begin
  233. { Seeking to OFFSET_END }
  234. dosResult:=dosSeek(handle,0,OFFSET_END);
  235. if dosResult<0 then begin
  236. dosError2InOut(IoErr);
  237. end else begin
  238. do_seekend:=dosSeek(handle,0,OFFSET_CURRENT);
  239. end;
  240. end;
  241. end;
  242. function do_filesize(handle : longint) : longint;
  243. var currfilepos: longint;
  244. begin
  245. checkCTRLC;
  246. do_filesize:=-1;
  247. if CheckInList(ASYS_fileList,handle)<>nil then begin
  248. currfilepos:=do_filepos(handle);
  249. do_filesize:=do_seekend(handle);
  250. do_seek(handle,currfilepos);
  251. end;
  252. end;
  253. { truncate at a given position }
  254. procedure do_truncate(handle, pos: longint);
  255. begin
  256. checkCTRLC;
  257. if CheckInList(ASYS_fileList,handle)<>nil then begin
  258. { Seeking from OFFSET_BEGINNING }
  259. if SetFileSize(handle,pos,OFFSET_BEGINNING)<0 then
  260. dosError2InOut(IoErr);
  261. end;
  262. end;
  263. procedure do_open(var f;p:pchar;flags:longint; pchangeable: boolean);
  264. {
  265. filerec and textrec have both handle and mode as the first items so
  266. they could use the same routine for opening/creating.
  267. when (flags and $10) the file will be append
  268. when (flags and $100) the file will be truncate/rewritten
  269. when (flags and $1000) there is no check for close (needed for textfiles)
  270. }
  271. var
  272. handle : LongInt;
  273. openflags: LongInt;
  274. tmpStr : array[0..255] of Char;
  275. begin
  276. tmpStr:=PathConv(strpas(p))+#0;
  277. { close first if opened }
  278. if ((flags and $10000)=0) then begin
  279. case filerec(f).mode of
  280. fminput,fmoutput,fminout : Do_Close(filerec(f).handle);
  281. fmclosed : ;
  282. else begin
  283. inoutres:=102; {not assigned}
  284. exit;
  285. end;
  286. end;
  287. end;
  288. { reset file handle }
  289. filerec(f).handle:=UnusedHandle;
  290. { convert filemode to filerec modes }
  291. { READ/WRITE on existing file }
  292. { RESET/APPEND }
  293. openflags:=MODE_OLDFILE;
  294. case (flags and 3) of
  295. 0 : filerec(f).mode:=fminput;
  296. 1 : filerec(f).mode:=fmoutput;
  297. 2 : filerec(f).mode:=fminout;
  298. end;
  299. { rewrite (create a new file) }
  300. if (flags and $1000)<>0 then openflags:=MODE_NEWFILE;
  301. { empty name is special }
  302. if p[0]=#0 then begin
  303. case filerec(f).mode of
  304. fminput :
  305. filerec(f).handle:=StdInputHandle;
  306. fmappend,
  307. fmoutput : begin
  308. filerec(f).handle:=StdOutputHandle;
  309. filerec(f).mode:=fmoutput; {fool fmappend}
  310. end;
  311. end;
  312. exit;
  313. end;
  314. handle:=Open(@tmpStr,openflags);
  315. if handle=0 then begin
  316. dosError2InOut(IoErr);
  317. end else begin
  318. AddToList(ASYS_fileList,handle);
  319. filerec(f).handle:=handle;
  320. end;
  321. { append mode }
  322. if ((Flags and $100)<>0) and
  323. (FileRec(F).Handle<>UnusedHandle) then begin
  324. do_seekend(filerec(f).handle);
  325. filerec(f).mode:=fmoutput; {fool fmappend}
  326. end;
  327. end;
  328. function do_isdevice(handle: longint): boolean;
  329. begin
  330. if (handle=StdOutputHandle) or (handle=StdInputHandle) or
  331. (handle=StdErrorHandle) then
  332. do_isdevice:=True
  333. else
  334. do_isdevice:=False;
  335. end;