sysutils.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 1999-2021 by the Free Pascal development team.
  4. Sysutils unit for The WebAssembly System Interface (WASI).
  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. {$inline on}
  12. unit sysutils;
  13. interface
  14. {$MODE objfpc}
  15. {$MODESWITCH out}
  16. { force ansistrings }
  17. {$H+}
  18. {$modeswitch typehelpers}
  19. {$modeswitch advancedrecords}
  20. uses
  21. wasiapi;
  22. {$DEFINE HAS_SLEEP}
  23. {$DEFINE HAS_GETTICKCOUNT64}
  24. { used OS file system APIs use ansistring }
  25. {$define SYSUTILS_HAS_ANSISTR_FILEUTIL_IMPL}
  26. { OS has an ansistring/single byte environment variable API }
  27. {$define SYSUTILS_HAS_ANSISTR_ENVVAR_IMPL}
  28. { Include platform independent interface part }
  29. {$i sysutilh.inc}
  30. implementation
  31. uses
  32. sysconst;
  33. {$DEFINE FPC_FEXPAND_UNC} (* UNC paths are supported *)
  34. {$DEFINE FPC_FEXPAND_DRIVES} (* Full paths begin with drive specification *)
  35. {$DEFINE HAS_LOCALTIMEZONEOFFSET}
  36. {$DEFINE executeprocuni} (* Only 1 byte version of ExecuteProcess is provided by the OS *)
  37. { Include platform independent implementation part }
  38. {$i sysutils.inc}
  39. function GetTickCount64: QWord;
  40. var
  41. NanoSecsPast: __wasi_timestamp_t;
  42. begin
  43. if __wasi_clock_time_get(__WASI_CLOCKID_MONOTONIC,1000000,@NanoSecsPast)=__WASI_ERRNO_SUCCESS then
  44. Result:=NanoSecsPast div 1000000
  45. else
  46. Result:=0;
  47. end;
  48. {****************************************************************************
  49. File Functions
  50. ****************************************************************************}
  51. Function FileOpen (Const FileName : RawByteString; Mode : Integer) : THandle;
  52. Var
  53. SystemFileName: RawByteString;
  54. oflags : __wasi_oflags_t = 0;
  55. fs_rights_base: __wasi_rights_t = 0;
  56. fdflags: __wasi_fdflags_t = 0;
  57. ourfd: __wasi_fd_t;
  58. res: __wasi_errno_t;
  59. pr: RawByteString;
  60. fd: __wasi_fd_t;
  61. Begin
  62. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  63. case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
  64. fmOpenRead:
  65. fs_rights_base :=__WASI_RIGHTS_FD_READ or
  66. __WASI_RIGHTS_FD_FILESTAT_GET or
  67. __WASI_RIGHTS_FD_SEEK or
  68. __WASI_RIGHTS_FD_TELL or
  69. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  70. __WASI_RIGHTS_FD_ADVISE or
  71. __WASI_RIGHTS_POLL_FD_READWRITE;
  72. fmOpenWrite:
  73. fs_rights_base :=__WASI_RIGHTS_FD_WRITE or
  74. __WASI_RIGHTS_FD_FILESTAT_GET or
  75. __WASI_RIGHTS_FD_SEEK or
  76. __WASI_RIGHTS_FD_TELL or
  77. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  78. __WASI_RIGHTS_FD_ADVISE or
  79. __WASI_RIGHTS_POLL_FD_READWRITE or
  80. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  81. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  82. __WASI_RIGHTS_FD_ALLOCATE or
  83. __WASI_RIGHTS_FD_DATASYNC or
  84. __WASI_RIGHTS_FD_SYNC;
  85. fmOpenReadWrite:
  86. fs_rights_base :=__WASI_RIGHTS_FD_READ or
  87. __WASI_RIGHTS_FD_WRITE or
  88. __WASI_RIGHTS_FD_FILESTAT_GET or
  89. __WASI_RIGHTS_FD_SEEK or
  90. __WASI_RIGHTS_FD_TELL or
  91. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  92. __WASI_RIGHTS_FD_ADVISE or
  93. __WASI_RIGHTS_POLL_FD_READWRITE or
  94. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  95. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  96. __WASI_RIGHTS_FD_ALLOCATE or
  97. __WASI_RIGHTS_FD_DATASYNC or
  98. __WASI_RIGHTS_FD_SYNC;
  99. end;
  100. if not ConvertToFdRelativePath(SystemFileName,fd,pr) then
  101. begin
  102. result:=-1;
  103. exit;
  104. end;
  105. repeat
  106. res:=__wasi_path_open(fd,
  107. 0,
  108. PChar(pr),
  109. length(pr),
  110. oflags,
  111. fs_rights_base,
  112. fs_rights_base,
  113. fdflags,
  114. @ourfd);
  115. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  116. If res=__WASI_ERRNO_SUCCESS Then
  117. Result:=ourfd
  118. else
  119. Result:=-1;
  120. end;
  121. Function FileCreate (Const FileName : RawByteString) : THandle;
  122. begin
  123. end;
  124. Function FileCreate (Const FileName : RawByteString; ShareMode:integer; Rights : integer) : THandle;
  125. begin
  126. end;
  127. Function FileCreate (Const FileName : RawByteString; Rights:integer) : THandle;
  128. begin
  129. end;
  130. Function FileRead (Handle : THandle; Out Buffer; Count : longint) : Longint;
  131. begin
  132. end;
  133. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
  134. begin
  135. end;
  136. Function FileSeek (Handle : THandle; FOffset, Origin : Longint) : Longint;
  137. begin
  138. end;
  139. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: {Integer}Longint) : Int64;
  140. begin
  141. end;
  142. Procedure FileClose (Handle : THandle);
  143. var
  144. res: __wasi_errno_t;
  145. begin
  146. repeat
  147. res:=__wasi_fd_close(Handle);
  148. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  149. end;
  150. Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
  151. begin
  152. end;
  153. Function FileAge (Const FileName : RawByteString): Int64;
  154. begin
  155. end;
  156. function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
  157. begin
  158. Result := False;
  159. end;
  160. function FileExists (const FileName: RawByteString; FollowLink : Boolean): boolean;
  161. begin
  162. end;
  163. Function DirectoryExists (Const Directory : RawByteString; FollowLink : Boolean) : Boolean;
  164. begin
  165. end;
  166. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  167. begin
  168. { not yet implemented }
  169. Result := -1;
  170. end;
  171. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  172. begin
  173. { not yet implemented }
  174. Result := -1;
  175. end;
  176. Procedure InternalFindClose(var Handle: THandle);
  177. begin
  178. end;
  179. Function FileGetDate (Handle : THandle) : Int64;
  180. begin
  181. end;
  182. Function FileSetDate (Handle : THandle; Age : Int64) : Longint;
  183. begin
  184. end;
  185. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  186. begin
  187. end;
  188. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  189. begin
  190. end;
  191. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  192. begin
  193. end;
  194. Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
  195. begin
  196. end;
  197. {****************************************************************************
  198. Disk Functions
  199. ****************************************************************************}
  200. function diskfree(drive : byte) : int64;
  201. begin
  202. end;
  203. function disksize(drive : byte) : int64;
  204. begin
  205. end;
  206. {****************************************************************************
  207. Time Functions
  208. ****************************************************************************}
  209. {$I tzenv.inc}
  210. Procedure GetLocalTime(var SystemTime: TSystemTime);
  211. begin
  212. end ;
  213. {****************************************************************************
  214. Misc Functions
  215. ****************************************************************************}
  216. procedure sysBeep;
  217. begin
  218. end;
  219. {****************************************************************************
  220. Locale Functions
  221. ****************************************************************************}
  222. procedure InitAnsi;
  223. begin
  224. end;
  225. Procedure InitInternational;
  226. begin
  227. InitInternationalGeneric;
  228. InitAnsi;
  229. end;
  230. function SysErrorMessage(ErrorCode: Integer): String;
  231. begin
  232. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  233. end;
  234. {****************************************************************************
  235. Os utils
  236. ****************************************************************************}
  237. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  238. var
  239. hp : ppchar;
  240. hs : string;
  241. eqpos : longint;
  242. begin
  243. result:='';
  244. hp:=envp;
  245. if hp<>nil then
  246. while assigned(hp^) do
  247. begin
  248. hs:=strpas(hp^);
  249. eqpos:=pos('=',hs);
  250. if copy(hs,1,eqpos-1)=envvar then
  251. begin
  252. result:=copy(hs,eqpos+1,length(hs)-eqpos);
  253. break;
  254. end;
  255. inc(hp);
  256. end;
  257. end;
  258. Function GetEnvironmentVariableCount : Integer;
  259. var
  260. p: ppchar;
  261. begin
  262. result:=0;
  263. p:=envp; {defined in system}
  264. if p<>nil then
  265. while p^<>nil do
  266. begin
  267. inc(result);
  268. inc(p);
  269. end;
  270. end;
  271. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  272. Var
  273. i : longint;
  274. p : ppchar;
  275. begin
  276. if (Index <= 0) or (envp=nil) then
  277. result:=''
  278. else
  279. begin
  280. p:=envp; {defined in system}
  281. i:=1;
  282. while (i<Index) and (p^<>nil) do
  283. begin
  284. inc(i);
  285. inc(p);
  286. end;
  287. if p^=nil then
  288. result:=''
  289. else
  290. result:=strpas(p^)
  291. end;
  292. end;
  293. function ExecuteProcess(Const Path: RawByteString; Const ComLine: RawByteString;Flags:TExecuteFlags=[]):integer;
  294. begin
  295. end;
  296. function ExecuteProcess (const Path: RawByteString;
  297. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  298. begin
  299. end;
  300. {*************************************************************************
  301. Sleep
  302. *************************************************************************}
  303. procedure Sleep (MilliSeconds: Cardinal);
  304. var
  305. subscription: __wasi_subscription_t;
  306. event: __wasi_event_t;
  307. nevents: __wasi_size_t;
  308. begin
  309. FillChar(subscription,SizeOf(subscription),0);
  310. subscription.u.tag:=__WASI_EVENTTYPE_CLOCK;
  311. subscription.u.u.clock.id:=__WASI_CLOCKID_MONOTONIC;
  312. subscription.u.u.clock.timeout:=MilliSeconds*1000000;
  313. subscription.u.u.clock.precision:=1000000;
  314. subscription.u.u.clock.flags:=0; { timeout value is relative }
  315. __wasi_poll_oneoff(@subscription,@event,1,@nevents);
  316. end;
  317. {****************************************************************************
  318. Initialization code
  319. ****************************************************************************}
  320. Initialization
  321. InitExceptions; { Initialize exceptions. OS independent }
  322. InitInternational; { Initialize internationalization settings }
  323. OnBeep:=@SysBeep;
  324. InitTZ;
  325. Finalization
  326. FreeTerminateProcs;
  327. DoneExceptions;
  328. end.