sysfile.inc 8.2 KB

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