sysutils.pp 13 KB

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