sysutils.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. fs_rights_base: __wasi_rights_t = 0;
  55. ourfd: __wasi_fd_t;
  56. res: __wasi_errno_t;
  57. pr: RawByteString;
  58. fd: __wasi_fd_t;
  59. Begin
  60. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  61. case (Mode and (fmOpenRead or fmOpenWrite or fmOpenReadWrite)) of
  62. fmOpenRead:
  63. fs_rights_base :=__WASI_RIGHTS_FD_READ or
  64. __WASI_RIGHTS_FD_FILESTAT_GET or
  65. __WASI_RIGHTS_FD_SEEK or
  66. __WASI_RIGHTS_FD_TELL or
  67. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  68. __WASI_RIGHTS_FD_ADVISE or
  69. __WASI_RIGHTS_POLL_FD_READWRITE;
  70. fmOpenWrite:
  71. fs_rights_base :=__WASI_RIGHTS_FD_WRITE or
  72. __WASI_RIGHTS_FD_FILESTAT_GET or
  73. __WASI_RIGHTS_FD_SEEK or
  74. __WASI_RIGHTS_FD_TELL or
  75. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  76. __WASI_RIGHTS_FD_ADVISE or
  77. __WASI_RIGHTS_POLL_FD_READWRITE or
  78. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  79. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  80. __WASI_RIGHTS_FD_ALLOCATE or
  81. __WASI_RIGHTS_FD_DATASYNC or
  82. __WASI_RIGHTS_FD_SYNC;
  83. fmOpenReadWrite:
  84. fs_rights_base :=__WASI_RIGHTS_FD_READ or
  85. __WASI_RIGHTS_FD_WRITE or
  86. __WASI_RIGHTS_FD_FILESTAT_GET or
  87. __WASI_RIGHTS_FD_SEEK or
  88. __WASI_RIGHTS_FD_TELL or
  89. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  90. __WASI_RIGHTS_FD_ADVISE or
  91. __WASI_RIGHTS_POLL_FD_READWRITE or
  92. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  93. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  94. __WASI_RIGHTS_FD_ALLOCATE or
  95. __WASI_RIGHTS_FD_DATASYNC or
  96. __WASI_RIGHTS_FD_SYNC;
  97. end;
  98. if not ConvertToFdRelativePath(SystemFileName,fd,pr) then
  99. begin
  100. result:=-1;
  101. exit;
  102. end;
  103. repeat
  104. res:=__wasi_path_open(fd,
  105. 0,
  106. PChar(pr),
  107. length(pr),
  108. 0,
  109. fs_rights_base,
  110. fs_rights_base,
  111. 0,
  112. @ourfd);
  113. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  114. If res=__WASI_ERRNO_SUCCESS Then
  115. Result:=ourfd
  116. else
  117. Result:=-1;
  118. end;
  119. Function FileCreate (Const FileName : RawByteString) : THandle;
  120. Const
  121. fs_rights_base: __wasi_rights_t =
  122. __WASI_RIGHTS_FD_READ or
  123. __WASI_RIGHTS_FD_WRITE or
  124. __WASI_RIGHTS_FD_FILESTAT_GET or
  125. __WASI_RIGHTS_FD_SEEK or
  126. __WASI_RIGHTS_FD_TELL or
  127. __WASI_RIGHTS_FD_FDSTAT_SET_FLAGS or
  128. __WASI_RIGHTS_FD_ADVISE or
  129. __WASI_RIGHTS_POLL_FD_READWRITE or
  130. __WASI_RIGHTS_FD_FILESTAT_SET_SIZE or
  131. __WASI_RIGHTS_FD_FILESTAT_SET_TIMES or
  132. __WASI_RIGHTS_FD_ALLOCATE or
  133. __WASI_RIGHTS_FD_DATASYNC or
  134. __WASI_RIGHTS_FD_SYNC;
  135. Var
  136. SystemFileName: RawByteString;
  137. ourfd: __wasi_fd_t;
  138. res: __wasi_errno_t;
  139. pr: RawByteString;
  140. fd: __wasi_fd_t;
  141. Begin
  142. SystemFileName:=ToSingleByteFileSystemEncodedFileName(FileName);
  143. if not ConvertToFdRelativePath(SystemFileName,fd,pr) then
  144. begin
  145. result:=-1;
  146. exit;
  147. end;
  148. repeat
  149. res:=__wasi_path_open(fd,
  150. 0,
  151. PChar(pr),
  152. length(pr),
  153. __WASI_OFLAGS_CREAT or __WASI_OFLAGS_TRUNC,
  154. fs_rights_base,
  155. fs_rights_base,
  156. 0,
  157. @ourfd);
  158. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  159. If res=__WASI_ERRNO_SUCCESS Then
  160. Result:=ourfd
  161. else
  162. Result:=-1;
  163. end;
  164. Function FileCreate (Const FileName : RawByteString; ShareMode:integer; Rights : integer) : THandle;
  165. begin
  166. FileCreate:=FileCreate(FileName);
  167. end;
  168. Function FileCreate (Const FileName : RawByteString; Rights:integer) : THandle;
  169. begin
  170. FileCreate:=FileCreate(FileName);
  171. end;
  172. Function FileRead (Handle : THandle; Out Buffer; Count : longint) : Longint;
  173. begin
  174. end;
  175. Function FileWrite (Handle : THandle; const Buffer; Count : Longint) : Longint;
  176. begin
  177. end;
  178. Function FileSeek (Handle : THandle; FOffset, Origin : Longint) : Longint;
  179. begin
  180. end;
  181. Function FileSeek (Handle : THandle; FOffset: Int64; Origin: {Integer}Longint) : Int64;
  182. begin
  183. end;
  184. Procedure FileClose (Handle : THandle);
  185. var
  186. res: __wasi_errno_t;
  187. begin
  188. repeat
  189. res:=__wasi_fd_close(Handle);
  190. until (res=__WASI_ERRNO_SUCCESS) or (res<>__WASI_ERRNO_INTR);
  191. end;
  192. Function FileTruncate (Handle: THandle; Size: Int64) : boolean;
  193. begin
  194. end;
  195. Function FileAge (Const FileName : RawByteString): Int64;
  196. begin
  197. end;
  198. function FileGetSymLinkTarget(const FileName: RawByteString; out SymLinkRec: TRawbyteSymLinkRec): Boolean;
  199. begin
  200. Result := False;
  201. end;
  202. function FileExists (const FileName: RawByteString; FollowLink : Boolean): boolean;
  203. begin
  204. end;
  205. Function DirectoryExists (Const Directory : RawByteString; FollowLink : Boolean) : Boolean;
  206. begin
  207. end;
  208. Function InternalFindFirst (Const Path : RawByteString; Attr : Longint; out Rslt : TAbstractSearchRec; var Name: RawByteString) : Longint;
  209. begin
  210. { not yet implemented }
  211. Result := -1;
  212. end;
  213. Function InternalFindNext (var Rslt : TAbstractSearchRec; var Name : RawByteString) : Longint;
  214. begin
  215. { not yet implemented }
  216. Result := -1;
  217. end;
  218. Procedure InternalFindClose(var Handle: THandle);
  219. begin
  220. end;
  221. Function FileGetDate (Handle : THandle) : Int64;
  222. begin
  223. end;
  224. Function FileSetDate (Handle : THandle; Age : Int64) : Longint;
  225. begin
  226. end;
  227. Function FileGetAttr (Const FileName : RawByteString) : Longint;
  228. begin
  229. end;
  230. Function FileSetAttr (Const Filename : RawByteString; Attr: longint) : Longint;
  231. begin
  232. end;
  233. Function DeleteFile (Const FileName : RawByteString) : Boolean;
  234. begin
  235. end;
  236. Function RenameFile (Const OldName, NewName : RawByteString) : Boolean;
  237. begin
  238. end;
  239. {****************************************************************************
  240. Disk Functions
  241. ****************************************************************************}
  242. function diskfree(drive : byte) : int64;
  243. begin
  244. end;
  245. function disksize(drive : byte) : int64;
  246. begin
  247. end;
  248. {****************************************************************************
  249. Time Functions
  250. ****************************************************************************}
  251. {$I tzenv.inc}
  252. Procedure GetLocalTime(var SystemTime: TSystemTime);
  253. begin
  254. end ;
  255. {****************************************************************************
  256. Misc Functions
  257. ****************************************************************************}
  258. procedure sysBeep;
  259. begin
  260. end;
  261. {****************************************************************************
  262. Locale Functions
  263. ****************************************************************************}
  264. procedure InitAnsi;
  265. begin
  266. end;
  267. Procedure InitInternational;
  268. begin
  269. InitInternationalGeneric;
  270. InitAnsi;
  271. end;
  272. function SysErrorMessage(ErrorCode: Integer): String;
  273. begin
  274. Result:=Format(SUnknownErrorCode,[ErrorCode]);
  275. end;
  276. {****************************************************************************
  277. Os utils
  278. ****************************************************************************}
  279. Function GetEnvironmentVariable(Const EnvVar : String) : String;
  280. var
  281. hp : ppchar;
  282. hs : string;
  283. eqpos : longint;
  284. begin
  285. result:='';
  286. hp:=envp;
  287. if hp<>nil then
  288. while assigned(hp^) do
  289. begin
  290. hs:=strpas(hp^);
  291. eqpos:=pos('=',hs);
  292. if copy(hs,1,eqpos-1)=envvar then
  293. begin
  294. result:=copy(hs,eqpos+1,length(hs)-eqpos);
  295. break;
  296. end;
  297. inc(hp);
  298. end;
  299. end;
  300. Function GetEnvironmentVariableCount : Integer;
  301. var
  302. p: ppchar;
  303. begin
  304. result:=0;
  305. p:=envp; {defined in system}
  306. if p<>nil then
  307. while p^<>nil do
  308. begin
  309. inc(result);
  310. inc(p);
  311. end;
  312. end;
  313. Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
  314. Var
  315. i : longint;
  316. p : ppchar;
  317. begin
  318. if (Index <= 0) or (envp=nil) then
  319. result:=''
  320. else
  321. begin
  322. p:=envp; {defined in system}
  323. i:=1;
  324. while (i<Index) and (p^<>nil) do
  325. begin
  326. inc(i);
  327. inc(p);
  328. end;
  329. if p^=nil then
  330. result:=''
  331. else
  332. result:=strpas(p^)
  333. end;
  334. end;
  335. function ExecuteProcess(Const Path: RawByteString; Const ComLine: RawByteString;Flags:TExecuteFlags=[]):integer;
  336. begin
  337. end;
  338. function ExecuteProcess (const Path: RawByteString;
  339. const ComLine: array of RawByteString;Flags:TExecuteFlags=[]): integer;
  340. begin
  341. end;
  342. {*************************************************************************
  343. Sleep
  344. *************************************************************************}
  345. procedure Sleep (MilliSeconds: Cardinal);
  346. var
  347. subscription: __wasi_subscription_t;
  348. event: __wasi_event_t;
  349. nevents: __wasi_size_t;
  350. begin
  351. FillChar(subscription,SizeOf(subscription),0);
  352. subscription.u.tag:=__WASI_EVENTTYPE_CLOCK;
  353. subscription.u.u.clock.id:=__WASI_CLOCKID_MONOTONIC;
  354. subscription.u.u.clock.timeout:=MilliSeconds*1000000;
  355. subscription.u.u.clock.precision:=1000000;
  356. subscription.u.u.clock.flags:=0; { timeout value is relative }
  357. __wasi_poll_oneoff(@subscription,@event,1,@nevents);
  358. end;
  359. {****************************************************************************
  360. Initialization code
  361. ****************************************************************************}
  362. Initialization
  363. InitExceptions; { Initialize exceptions. OS independent }
  364. InitInternational; { Initialize internationalization settings }
  365. OnBeep:=@SysBeep;
  366. InitTZ;
  367. Finalization
  368. FreeTerminateProcs;
  369. DoneExceptions;
  370. end.