From 24a1e0e24ae459634f7eb97166b95002043de43c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 15 Jul 2019 23:22:29 +0200 Subject: [PATCH] better os.executable on macos --- vlib/os/os.v | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index 2a49dee6e6..5c0627d200 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -507,8 +507,8 @@ fn on_segfault(f voidptr) { } } -pub fn getexepath() string { - mut result := [4096]byte // [MAX_PATH]byte --> error byte undefined +pub fn executable() string { + mut result := malloc(MAX_PATH) $if linux { count := int(C.readlink('/proc/self/exe', result, MAX_PATH )) if(count < 0) { @@ -523,11 +523,22 @@ pub fn getexepath() string { } $if mac { + buf := malloc(MAX_PATH) + pid := C.getpid() + ret := C.proc_pidpath (pid, buf, MAX_PATH) + if ret <= 0 { + println('executable() failed') + return '' + } + return string(buf) +/* +// This doesn't work with symlinks mut bufsize := MAX_PATH // if buffer is too small this will be updated with size needed if C._NSGetExecutablePath(result, &bufsize) == -1 { panic('Could not get executable path, buffer too small (need: $bufsize).') } - return tos(result, strlen(result)) + return string(result) +*/ } }