forked from odin-lang/Odin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
os/os2: add get_executable_path and get_executable_directory
- Loading branch information
Showing
10 changed files
with
233 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package os2 | ||
|
||
import "base:runtime" | ||
|
||
import "core:sys/darwin" | ||
import "core:sys/posix" | ||
|
||
_get_executable_path :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) { | ||
size: u32 | ||
|
||
ret := darwin._NSGetExecutablePath(nil, &size) | ||
assert(ret == -1) | ||
assert(size > 0) | ||
|
||
TEMP_ALLOCATOR_GUARD() | ||
|
||
buf := make([]byte, size, temp_allocator()) or_return | ||
assert(u32(len(buf)) == size) | ||
|
||
ret = darwin._NSGetExecutablePath(raw_data(buf), &size) | ||
assert(ret == 0) | ||
|
||
real := posix.realpath(cstring(raw_data(buf))) | ||
if real == nil { | ||
err = _get_platform_error() | ||
return | ||
} | ||
defer posix.free(real) | ||
|
||
return clone_string(string(real), allocator) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package os2 | ||
|
||
import "base:runtime" | ||
|
||
import "core:sys/freebsd" | ||
import "core:sys/posix" | ||
|
||
_get_executable_path :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) { | ||
req := []freebsd.MIB_Identifier{.CTL_KERN, .KERN_PROC, .KERN_PROC_PATHNAME, freebsd.MIB_Identifier(-1)} | ||
|
||
size: uint | ||
if ret := freebsd.sysctl(req, nil, &size, nil, 0); ret != .NONE { | ||
err = _get_platform_error(posix.Errno(ret)) | ||
return | ||
} | ||
assert(size > 0) | ||
|
||
buf := make([]byte, size, allocator) or_return | ||
defer if err != nil { delete(buf, allocator) } | ||
|
||
assert(uint(len(buf)) == size) | ||
|
||
if ret := freebsd.sysctl(req, raw_data(buf), &size, nil, 0); ret != .NONE { | ||
err = _get_platform_error(posix.Errno(ret)) | ||
return | ||
} | ||
|
||
return string(buf[:size]), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package os2 | ||
|
||
import "base:runtime" | ||
|
||
import "core:sys/posix" | ||
|
||
_get_executable_path :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) { | ||
TEMP_ALLOCATOR_GUARD() | ||
|
||
buf := make([dynamic]byte, 1024, temp_allocator()) or_return | ||
for { | ||
n := posix.readlink("/proc/curproc/exe", raw_data(buf), len(buf)) | ||
if n < 0 { | ||
err = _get_platform_error() | ||
return | ||
} | ||
|
||
if n < len(buf) { | ||
return clone_string(string(buf[:n]), allocator) | ||
} | ||
|
||
resize(&buf, len(buf)*2) or_return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package os2 | ||
|
||
import "base:runtime" | ||
|
||
import "core:strings" | ||
import "core:sys/posix" | ||
|
||
_get_executable_path :: proc(allocator: runtime.Allocator) -> (path: string, err: Error) { | ||
// OpenBSD does not have an API for this, we do our best below. | ||
|
||
if len(runtime.args__) <= 0 { | ||
err = .Invalid_Path | ||
return | ||
} | ||
|
||
real :: proc(path: cstring, allocator: runtime.Allocator) -> (out: string, err: Error) { | ||
real := posix.realpath(path) | ||
if real == nil { | ||
err = _get_platform_error() | ||
return | ||
} | ||
defer posix.free(real) | ||
return clone_string(string(real), allocator) | ||
} | ||
|
||
arg := runtime.args__[0] | ||
sarg := string(arg) | ||
|
||
if len(sarg) == 0 { | ||
err = .Invalid_Path | ||
return | ||
} | ||
|
||
if sarg[0] == '.' || sarg[0] == '/' { | ||
return real(arg, allocator) | ||
} | ||
|
||
TEMP_ALLOCATOR_GUARD() | ||
|
||
buf := strings.builder_make(temp_allocator()) | ||
|
||
paths := get_env("PATH", temp_allocator()) | ||
for dir in strings.split_iterator(&paths, ":") { | ||
strings.builder_reset(&buf) | ||
strings.write_string(&buf, dir) | ||
strings.write_string(&buf, "/") | ||
strings.write_string(&buf, sarg) | ||
|
||
cpath := strings.to_cstring(&buf) | ||
if posix.access(cpath, {.X_OK}) == .OK { | ||
return real(cpath, allocator) | ||
} | ||
} | ||
|
||
err = .Invalid_Path | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package darwin | ||
|
||
foreign import system "system:System.framework" | ||
|
||
foreign system { | ||
_NSGetExecutablePath :: proc(buf: [^]byte, bufsize: ^u32) -> i32 --- | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package tests_core_os_os2 | ||
|
||
import os "core:os/os2" | ||
import "core:log" | ||
import "core:path/filepath" | ||
import "core:testing" | ||
import "core:strings" | ||
|
||
@(test) | ||
test_executable :: proc(t: ^testing.T) { | ||
path, err := os.get_executable_path(context.allocator) | ||
defer delete(path) | ||
|
||
log.infof("executable path: %q", path) | ||
|
||
// NOTE: some sanity checks that should always be the case, at least in the CI. | ||
|
||
testing.expect_value(t, err, nil) | ||
testing.expect(t, len(path) > 0) | ||
testing.expect(t, filepath.is_abs(path)) | ||
testing.expectf(t, strings.contains(path, filepath.base(os.args[0])), "expected the executable path to contain the base of os.args[0] which is %q", filepath.base(os.args[0])) | ||
} |