sysfile.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. Low level File Routines
  14. All these functions can set InOutRes on errors
  15. ****************************************************************************}
  16. PROCEDURE NW2PASErr (Err : LONGINT);
  17. BEGIN
  18. if Err = 0 then { Else it will go through all the cases }
  19. exit;
  20. case Err of
  21. Sys_ENFILE,
  22. Sys_EMFILE : Inoutres:=4;
  23. Sys_ENOENT : Inoutres:=2;
  24. Sys_EBADF : Inoutres:=6;
  25. Sys_ENOMEM,
  26. Sys_EFAULT : Inoutres:=217;
  27. Sys_EINVAL : Inoutres:=218;
  28. Sys_EPIPE,
  29. Sys_EINTR,
  30. Sys_EIO,
  31. Sys_EAGAIN,
  32. Sys_ENOSPC : Inoutres:=101;
  33. Sys_ENAMETOOLONG,
  34. Sys_ELOOP,
  35. Sys_ENOTDIR : Inoutres:=3;
  36. Sys_EROFS,
  37. Sys_EEXIST,
  38. Sys_EACCES : Inoutres:=5;
  39. Sys_EBUSY : Inoutres:=162;
  40. end;
  41. END;
  42. FUNCTION errno : LONGINT;
  43. BEGIN
  44. errno := __get_errno_ptr^;
  45. END;
  46. PROCEDURE Errno2Inoutres;
  47. BEGIN
  48. NW2PASErr (errno);
  49. END;
  50. PROCEDURE SetFileError (VAR Err : LONGINT);
  51. BEGIN
  52. IF Err >= 0 THEN
  53. InOutRes := 0
  54. ELSE
  55. BEGIN
  56. Err := errno;
  57. NW2PASErr (Err);
  58. Err := 0;
  59. END;
  60. END;
  61. { close a file from the handle value }
  62. procedure do_close(handle : thandle);
  63. VAR res : LONGINT;
  64. begin
  65. res := _close (handle);
  66. IF res <> 0 THEN
  67. SetFileError (res)
  68. ELSE
  69. InOutRes := 0;
  70. end;
  71. procedure do_erase(p : pchar);
  72. VAR res : LONGINT;
  73. begin
  74. res := _unlink (p);
  75. IF Res < 0 THEN
  76. SetFileError (res)
  77. ELSE
  78. InOutRes := 0;
  79. end;
  80. procedure do_rename(p1,p2 : pchar);
  81. VAR res : LONGINT;
  82. begin
  83. res := _rename (p1,p2);
  84. IF Res < 0 THEN
  85. SetFileError (res)
  86. ELSE
  87. InOutRes := 0
  88. end;
  89. function do_write(h:thandle;addr:pointer;len : longint) : longint;
  90. VAR res : LONGINT;
  91. begin
  92. res := _write (h,addr,len);
  93. IF res > 0 THEN
  94. InOutRes := 0
  95. ELSE
  96. SetFileError (res);
  97. do_write := res;
  98. end;
  99. function do_read(h:thandle;addr:pointer;len : longint) : longint;
  100. VAR res : LONGINT;
  101. begin
  102. res := _read (h,addr,len);
  103. IF res > 0 THEN
  104. InOutRes := 0
  105. ELSE
  106. SetFileError (res);
  107. do_read := res;
  108. end;
  109. function do_filepos(handle : thandle) : longint;
  110. VAR res : LONGINT;
  111. begin
  112. InOutRes:=1;
  113. res := _tell (handle);
  114. IF res < 0 THEN
  115. SetFileError (res)
  116. ELSE
  117. InOutRes := 0;
  118. do_filepos := res;
  119. end;
  120. CONST SEEK_SET = 0; // Seek from beginning of file.
  121. SEEK_CUR = 1; // Seek from current position.
  122. SEEK_END = 2; // Seek from end of file.
  123. procedure do_seek(handle:thandle;pos : longint);
  124. VAR res : LONGINT;
  125. begin
  126. res := _lseek (handle,pos, SEEK_SET);
  127. IF res >= 0 THEN
  128. InOutRes := 0
  129. ELSE
  130. SetFileError (res);
  131. end;
  132. function do_seekend(handle:thandle):longint;
  133. VAR res : LONGINT;
  134. begin
  135. res := _lseek (handle,0, SEEK_END);
  136. IF res >= 0 THEN
  137. InOutRes := 0
  138. ELSE
  139. SetFileError (res);
  140. do_seekend := res;
  141. end;
  142. function do_filesize(handle : thandle) : longint;
  143. VAR res : LONGINT;
  144. begin
  145. res := _filelength (handle);
  146. IF res < 0 THEN
  147. BEGIN
  148. SetFileError (Res);
  149. do_filesize := -1;
  150. END ELSE
  151. BEGIN
  152. InOutRes := 0;
  153. do_filesize := res;
  154. END;
  155. end;
  156. { truncate at a given position }
  157. procedure do_truncate (handle:thandle;pos:longint);
  158. VAR res : LONGINT;
  159. begin
  160. res := _chsize (handle,pos);
  161. IF res <> 0 THEN
  162. SetFileError (res)
  163. ELSE
  164. InOutRes := 0;
  165. end;
  166. // mostly stolen from syslinux
  167. procedure do_open(var f;p:pchar;flags:longint);
  168. {
  169. filerec and textrec have both handle and mode as the first items so
  170. they could use the same routine for opening/creating.
  171. when (flags and $10) the file will be append
  172. when (flags and $100) the file will be truncate/rewritten
  173. when (flags and $1000) there is no check for close (needed for textfiles)
  174. }
  175. var
  176. oflags : longint;
  177. Begin
  178. { close first if opened }
  179. if ((flags and $10000)=0) then
  180. begin
  181. case FileRec(f).mode of
  182. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  183. fmclosed : ;
  184. else
  185. begin
  186. inoutres:=102; {not assigned}
  187. exit;
  188. end;
  189. end;
  190. end;
  191. { reset file Handle }
  192. FileRec(f).Handle:=UnusedHandle;
  193. { We do the conversion of filemodes here, concentrated on 1 place }
  194. case (flags and 3) of
  195. 0 : begin
  196. oflags := O_RDONLY;
  197. filerec(f).mode := fminput;
  198. end;
  199. 1 : begin
  200. oflags := O_WRONLY;
  201. filerec(f).mode := fmoutput;
  202. end;
  203. 2 : begin
  204. oflags := O_RDWR;
  205. filerec(f).mode := fminout;
  206. end;
  207. end;
  208. if (flags and $1000)=$1000 then
  209. oflags:=oflags or (O_CREAT or O_TRUNC)
  210. else
  211. if (flags and $100)=$100 then
  212. oflags:=oflags or (O_APPEND);
  213. { empty name is special }
  214. if p[0]=#0 then
  215. begin
  216. case FileRec(f).mode of
  217. fminput :
  218. FileRec(f).Handle:=StdInputHandle;
  219. fminout, { this is set by rewrite }
  220. fmoutput :
  221. FileRec(f).Handle:=StdOutputHandle;
  222. fmappend :
  223. begin
  224. FileRec(f).Handle:=StdOutputHandle;
  225. FileRec(f).mode:=fmoutput; {fool fmappend}
  226. end;
  227. end;
  228. exit;
  229. end;
  230. { real open call }
  231. FileRec(f).Handle := _open(p,oflags,438);
  232. //WriteLn ('_open (',p,') returned ',ErrNo, 'Handle: ',FileRec(f).Handle);
  233. // errno does not seem to be set on succsess ??
  234. IF FileRec(f).Handle < 0 THEN
  235. if (ErrNo=Sys_EROFS) and ((OFlags and O_RDWR)<>0) then
  236. begin // i.e. for cd-rom
  237. Oflags:=Oflags and not(O_RDWR);
  238. FileRec(f).Handle := _open(p,oflags,438);
  239. end;
  240. IF FileRec(f).Handle < 0 THEN
  241. Errno2Inoutres
  242. ELSE
  243. InOutRes := 0;
  244. End;
  245. function do_isdevice(handle:THandle):boolean;
  246. begin
  247. do_isdevice := (_isatty (handle) > 0);
  248. end;
  249. {
  250. $Log$
  251. Revision 1.1 2005-02-06 16:57:18 peter
  252. * threads for go32v2,os,emx,netware
  253. Revision 1.1 2005/02/06 13:06:20 peter
  254. * moved file and dir functions to sysfile/sysdir
  255. * win32 thread in systemunit
  256. }