From: DeaDDooMER Date: Mon, 24 Feb 2020 19:44:45 +0000 (+0300) Subject: DswProcs: add function Directory.GetPath X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=7147de0ec5fe252c907577218ae17a28ac84daba;p=cpc.git DswProcs: add function Directory.GetPath --- diff --git a/src/generic/Dsw/Mod/Procs.cp b/src/generic/Dsw/Mod/Procs.cp index 261415f..e5a31b3 100644 --- a/src/generic/Dsw/Mod/Procs.cp +++ b/src/generic/Dsw/Mod/Procs.cp @@ -5,6 +5,8 @@ MODULE DswProcs; IMPORT Kernel; TYPE + String* = POINTER TO ARRAY OF CHAR; + Directory* = POINTER TO ABSTRACT RECORD END; Process* = POINTER TO ABSTRACT RECORD END; @@ -13,6 +15,7 @@ MODULE DswProcs; dir-, stdDir-: Directory; PROCEDURE (d: Directory) New* (): Process, NEW, ABSTRACT; + PROCEDURE (d: Directory) GetPath* (IN name: ARRAY OF CHAR): String, NEW, ABSTRACT; PROCEDURE (p: Process) Program* (IN exe: ARRAY OF CHAR), NEW, ABSTRACT; PROCEDURE (p: Process) PutParam* (IN par: ARRAY OF CHAR), NEW, ABSTRACT; diff --git a/src/posix/generic/Dsw/Mod/HostProcs.cp b/src/posix/generic/Dsw/Mod/HostProcs.cp index efe8473..2aa4758 100644 --- a/src/posix/generic/Dsw/Mod/HostProcs.cp +++ b/src/posix/generic/Dsw/Mod/HostProcs.cp @@ -1,8 +1,8 @@ MODULE DswHostProcs; - IMPORT SYSTEM, Log, DswProcs, HostLang, unistd := PosixCunistd, stdlib := PosixCstdlib, + IMPORT SYSTEM, DswProcs, HostLang, unistd := PosixCunistd, stdlib := PosixCstdlib, signal := PosixCsignal, sys_wait := PosixCsys_wait, macro := PosixCmacro, - errno := PosixCerrno; + errno := PosixCerrno, sys_stat := PosixCsys_stat; TYPE Directory = POINTER TO RECORD (DswProcs.Directory) END; @@ -16,13 +16,6 @@ MODULE DswHostProcs; code: unistd.int; END; - PROCEDURE (d: Directory) New* (): Process; - VAR p: Process; - BEGIN - NEW(p); p.argv := NIL; p.pid := -1; p.code := 0; - RETURN p - END New; - PROCEDURE ToHost (IN s: ARRAY OF CHAR): String; VAR ss: String; res: INTEGER; BEGIN @@ -33,6 +26,56 @@ MODULE DswHostProcs; RETURN ss END ToHost; + PROCEDURE IsRegular (mode: sys_stat.mode_t): BOOLEAN; + BEGIN + RETURN BITS(mode) * BITS(sys_stat.S_IFMT) = BITS(sys_stat.S_IFREG) + END IsRegular; + + PROCEDURE (d: Directory) New* (): Process; + VAR p: Process; + BEGIN + NEW(p); p.argv := NIL; p.pid := -1; p.code := 0; + RETURN p + END New; + + PROCEDURE (d: Directory) GetPath (IN name: ARRAY OF CHAR): DswProcs.String; + VAR + ch: CHAR; + v: POINTER [untagged] TO ARRAY [untagged] OF SHORTCHAR; + s, e, ret: POINTER TO ARRAY OF CHAR; + w: String; + cres: sys_stat.int; + i, j, k, len, res: INTEGER; + buf: sys_stat.struct_stat; + found: BOOLEAN; + BEGIN + v := stdlib.getenv("PATH"); + IF v # NIL THEN + NEW(s, LEN(v$) * 3 + 1); + HostLang.HostToString(v$, s, HostLang.pep383x, res); + ASSERT(res = 0, 100); + len := LEN(name$); + NEW(e, LEN(s$) + len + 2); + i := 0; + REPEAT + j := -1; found := FALSE; + REPEAT ch := s[i]; INC(i); INC(j); e[j] := ch UNTIL (ch = ":") OR (ch = 0X); + IF e[0] = "/" THEN + e[j] := "/"; INC(j); + FOR k := 0 TO len DO + e[j + k] := name[k] + END; + e[j + k] := 0X; + w := ToHost(e); + cres := macro.stat(w, buf); + found := (cres = 0) & IsRegular(buf.st_mode) & (unistd.access(w, unistd.X_OK) = 0) + END + UNTIL found OR (ch = 0X); + IF ~found THEN e := NIL END + END; + RETURN e + END GetPath; + PROCEDURE (p: Process) Program* (IN exe: ARRAY OF CHAR); VAR argv: SString; BEGIN