Browse Source

+ implemented SysUtils.GetEnvironmentVariable, GetEnvironmentVariableCount and
GetEnvironmentString for WASI

Nikolay Nikolov 3 years ago
parent
commit
e078408dcf
1 changed files with 48 additions and 1 deletions
  1. 48 1
      rtl/wasi/sysutils.pp

+ 48 - 1
rtl/wasi/sysutils.pp

@@ -253,15 +253,62 @@ end;
 ****************************************************************************}
 
 Function GetEnvironmentVariable(Const EnvVar : String) : String;
-begin
+var
+  hp : ppchar;
+  hs : string;
+  eqpos : longint;
+begin
+  result:='';
+  hp:=envp;
+  if hp<>nil then
+    while assigned(hp^) do
+      begin
+        hs:=strpas(hp^);
+        eqpos:=pos('=',hs);
+        if copy(hs,1,eqpos-1)=envvar then
+          begin
+            result:=copy(hs,eqpos+1,length(hs)-eqpos);
+            break;
+          end;
+        inc(hp);
+      end;
 end;
 
 Function GetEnvironmentVariableCount : Integer;
+var
+  p: ppchar;
 begin
+  result:=0;
+  p:=envp;      {defined in system}
+  if p<>nil then
+    while p^<>nil do
+      begin
+        inc(result);
+        inc(p);
+      end;
 end;
 
 Function GetEnvironmentString(Index : Integer) : {$ifdef FPC_RTL_UNICODE}UnicodeString{$else}AnsiString{$endif};
+Var
+  i : longint;
+  p : ppchar;
 begin
+  if (Index <= 0) or (envp=nil) then
+    result:=''
+  else
+    begin
+      p:=envp;      {defined in system}
+      i:=1;
+      while (i<Index) and (p^<>nil) do
+        begin
+          inc(i);
+          inc(p);
+        end;
+      if p^=nil then
+        result:=''
+      else
+        result:=strpas(p^)
+    end;
 end;