sysfile.inc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Main OS dependant body of the system unit, loosely modelled
  4. after POSIX. *BSD version (Linux version is near identical)
  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. procedure Do_Close(Handle:thandle);
  12. var
  13. res: __wasi_errno_t;
  14. begin
  15. repeat
  16. res:=__wasi_fd_close(Handle);
  17. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  18. if res=__WASI_ERRNO_SUCCESS then
  19. InOutRes:=0
  20. else
  21. InOutRes:=Errno2InoutRes(res);
  22. end;
  23. procedure Do_Erase(p: pchar; pchangeable: boolean);
  24. begin
  25. DebugWriteLn('Do_Erase');
  26. end;
  27. procedure do_truncate (handle:thandle;fpos:int64);
  28. begin
  29. DebugWriteLn('do_truncate');
  30. end;
  31. procedure Do_Rename(p1,p2:pchar; p1changeable, p2changeable: boolean);
  32. begin
  33. DebugWriteLn('Do_Rename');
  34. end;
  35. function Do_Write(Handle:thandle;Addr:Pointer;Len:Longint):longint;
  36. var
  37. our_iov: __wasi_ciovec_t;
  38. our_nwritten: longint;
  39. res: __wasi_errno_t;
  40. begin
  41. repeat
  42. our_iov.buf := Addr;
  43. our_iov.buf_len := Len;
  44. res:=__wasi_fd_write(Handle, @our_iov, 1, @our_nwritten);
  45. until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
  46. if res=__WASI_ERRNO_SUCCESS then
  47. begin
  48. Do_Write:=our_nwritten;
  49. InOutRes:=0;
  50. end
  51. else
  52. begin
  53. Do_Write:=0;
  54. InOutRes:=Errno2InoutRes(res);
  55. end;
  56. end;
  57. function Do_Read(Handle:thandle;Addr:Pointer;Len:Longint):Longint;
  58. var
  59. our_iov: __wasi_iovec_t;
  60. our_nread: __wasi_size_t;
  61. res: __wasi_errno_t;
  62. begin
  63. repeat
  64. our_iov.buf:=Addr;
  65. our_iov.buf_len:=Len;
  66. __wasi_fd_read(Handle,@our_iov,1,@our_nread);
  67. until (res=__WASI_ERRNO_SUCCESS) or ((res<>__WASI_ERRNO_INTR) and (res<>__WASI_ERRNO_AGAIN));
  68. if res=__WASI_ERRNO_SUCCESS then
  69. begin
  70. Do_Read:=our_nread;
  71. InOutRes:=0;
  72. end
  73. else
  74. begin
  75. Do_Read:=0;
  76. InOutRes:=Errno2InoutRes(res);
  77. end;
  78. end;
  79. function Do_FilePos(Handle: thandle):Int64;
  80. begin
  81. DebugWriteLn('Do_FilePos');
  82. end;
  83. procedure Do_Seek(Handle:thandle;Pos:Int64);
  84. begin
  85. DebugWriteLn('Do_Seek');
  86. end;
  87. function Do_Seekend(Handle:thandle):Int64;
  88. begin
  89. DebugWriteLn('Do_Seekend');
  90. end;
  91. function Do_FileSize(Handle:thandle):Int64;
  92. begin
  93. DebugWriteLn('Do_FileSize');
  94. end;
  95. procedure Do_Open(var f; p: pchar; flags: longint; pchangeable: boolean);
  96. {
  97. FileRec and textrec have both Handle and mode as the first items so
  98. they could use the same routine for opening/creating.
  99. when (flags and $100) the file will be append
  100. when (flags and $1000) the file will be truncate/rewritten
  101. when (flags and $10000) there is no check for close (needed for textfiles)
  102. }
  103. var
  104. oflags : __wasi_oflags_t = 0;
  105. fs_rights_base: __wasi_rights_t = 0;
  106. fdflags: __wasi_fdflags_t = 0;
  107. ourfd: __wasi_fd_t;
  108. res: __wasi_errno_t;
  109. Begin
  110. { close first if opened }
  111. if ((flags and $10000)=0) then
  112. begin
  113. case FileRec(f).mode of
  114. fminput,fmoutput,fminout : Do_Close(FileRec(f).Handle);
  115. fmclosed : ;
  116. else
  117. begin
  118. inoutres:=102; {not assigned}
  119. exit;
  120. end;
  121. end;
  122. end;
  123. { reset file Handle }
  124. FileRec(f).Handle:=UnusedHandle;
  125. { We do the conversion of filemodes here, concentrated on 1 place }
  126. case (flags and 3) of
  127. 0 : begin
  128. fs_rights_base :=__WASI_RIGHTS_FD_READ;
  129. FileRec(f).mode:=fminput;
  130. end;
  131. 1 : begin
  132. fs_rights_base :=__WASI_RIGHTS_FD_WRITE;
  133. FileRec(f).mode:=fmoutput;
  134. end;
  135. 2 : begin
  136. fs_rights_base :=__WASI_RIGHTS_FD_READ or __WASI_RIGHTS_FD_WRITE;
  137. FileRec(f).mode:=fminout;
  138. end;
  139. end;
  140. if (flags and $1000)=$1000 then
  141. oflags:=oflags or (__WASI_OFLAGS_CREAT or __WASI_OFLAGS_TRUNC)
  142. else
  143. if (flags and $100)=$100 then
  144. fdflags:=fdflags or __WASI_FDFLAGS_APPEND;
  145. { empty name is special }
  146. if p[0]=#0 then
  147. begin
  148. case FileRec(f).mode of
  149. fminput :
  150. FileRec(f).Handle:=StdInputHandle;
  151. fminout, { this is set by rewrite }
  152. fmoutput :
  153. FileRec(f).Handle:=StdOutputHandle;
  154. fmappend :
  155. begin
  156. FileRec(f).Handle:=StdOutputHandle;
  157. FileRec(f).mode:=fmoutput; {fool fmappend}
  158. end;
  159. end;
  160. exit;
  161. end;
  162. { real open call }
  163. repeat
  164. res:=__wasi_path_open(3, { not sure about this fd... }
  165. 0,
  166. p,
  167. strlen(p),
  168. oflags,
  169. fs_rights_base,
  170. fs_rights_base,
  171. fdflags,
  172. @ourfd);
  173. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  174. {if (res=__WASI_ERRNO_ROFS) and ((OFlags and O_RDWR)<>0) then
  175. begin
  176. Oflags:=Oflags and not(O_RDWR);
  177. repeat
  178. FileRec(f).Handle:=Fpopen(p,oflags,MODE_OPEN);
  179. until (FileRec(f).Handle<>-1) or (geterrno<>ESysEINTR);
  180. end;}
  181. If res<>__WASI_ERRNO_SUCCESS Then
  182. begin
  183. FileRec(f).mode:=fmclosed;
  184. InOutRes:=Errno2InoutRes(res);
  185. end
  186. else
  187. begin
  188. FileRec(f).Handle:=ourfd;
  189. InOutRes:=0;
  190. end;
  191. end;